From 12d9bb79f951f6ee12ac434781e2c80186833b6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Barzowski?= Date: Sun, 3 Nov 2019 14:21:20 +0100 Subject: [PATCH] Add regression test for locale affecting output See https://github.com/google/jsonnet/issues/722 --- .gitignore | 1 + Makefile | 13 +++++++++++-- core/libjsonnet_test_locale.cpp | 24 ++++++++++++++++++++++++ tests.sh | 2 ++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 core/libjsonnet_test_locale.cpp diff --git a/.gitignore b/.gitignore index b61f1e500..8d4b5d20e 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ libjsonnet++.so libjsonnet++.so.* libjsonnet_test_file libjsonnet_test_snippet +libjsonnet_test_locale **/core.* **/vgcore **/vgcore.* diff --git a/Makefile b/Makefile index 9cda4dcd8..6002d4d2f 100644 --- a/Makefile +++ b/Makefile @@ -133,7 +133,7 @@ install: bins libs all: $(ALL) -test: jsonnet jsonnetfmt libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file +test: jsonnet jsonnetfmt libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file libjsonnet_test_locale ./tests.sh reformat: @@ -146,7 +146,8 @@ MAKEDEPEND_SRCS = \ cmd/jsonnet.cpp \ cmd/jsonnetfmt.cpp \ core/libjsonnet_test_snippet.c \ - core/libjsonnet_test_file.c + core/libjsonnet_test_file.c \ + core/libjsonnet_test_locale.cpp depend: core/std.jsonnet.h rm -f Makefile.depend @@ -196,6 +197,14 @@ LIBJSONNET_TEST_FILE_SRCS = \ libjsonnet_test_file: $(LIBJSONNET_TEST_FILE_SRCS) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -ljsonnet -o $@ +LIBJSONNET_TEST_LOCALE_SRCS = \ + core/libjsonnet_test_locale.cpp \ + libjsonnet++.so \ + include/libjsonnet++.h + +libjsonnet_test_locale: $(LIBJSONNET_TEST_LOCALE_SRCS) + $(CXX) $(CXXFLAGS) $(LDFLAGS) $< -L. -ljsonnet++ -o $@ + # Encode standard library for embedding in C core/%.jsonnet.h: stdlib/%.jsonnet (($(OD) -v -Anone -t u1 $< \ diff --git a/core/libjsonnet_test_locale.cpp b/core/libjsonnet_test_locale.cpp new file mode 100644 index 000000000..4a50869d2 --- /dev/null +++ b/core/libjsonnet_test_locale.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +// Regression test for the follwing issue: https://github.com/google/jsonnet/issues/722 + +int main() { + std::string templatedJSONString { "2000" }; + std::locale glocale("en_US.UTF-8"); + std::locale::global(glocale); + + jsonnet::Jsonnet jsonnet {}; + jsonnet.init(); + + std::string expanded; + if (!jsonnet.evaluateSnippet("", templatedJSONString, &expanded)) { + std::cerr << "Error parsing Jsonnet: "+jsonnet.lastError(); + exit(1); + } + std::string expected = "2000\n"; + assert(expected == expanded); + return 0; +} diff --git a/tests.sh b/tests.sh index 235e9ef39..ac561ec16 100755 --- a/tests.sh +++ b/tests.sh @@ -11,6 +11,8 @@ if [ -z "$DISABLE_LIB_TESTS" ]; then LD_LIBRARY_PATH=. ./libjsonnet_test_snippet "${TEST_SNIPPET}" || FAIL=TRUE printf 'libjsonnet_test_file: ' LD_LIBRARY_PATH=. ./libjsonnet_test_file "test_suite/object.jsonnet" || FAIL=TRUE + echo -n 'libjsonnet_test_locale: ' + LD_LIBRARY_PATH=. ./libjsonnet_test_locale || FAIL=TRUE fi examples/check.sh || FAIL=TRUE examples/terraform/check.sh || FAIL=TRUE