From a79333333f7e2169dc7e2658908dcd5578cd5b36 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 | 14 ++++++++++++-- core/libjsonnet_test_locale.cpp | 24 ++++++++++++++++++++++++ tests.sh | 2 ++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 core/libjsonnet_test_locale.cpp diff --git a/.gitignore b/.gitignore index 1c6d65dc5..85de6ff73 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ libjsonnet++.so.* libjsonnet.js libjsonnet_test_file libjsonnet_test_snippet +libjsonnet_test_locale **/core.* **/vgcore **/vgcore.* diff --git a/Makefile b/Makefile index 474655ceb..11e3d12d0 100644 --- a/Makefile +++ b/Makefile @@ -84,6 +84,7 @@ LIBS = \ ALL = \ libjsonnet_test_snippet \ libjsonnet_test_file \ + libjsonnet_test_locale \ libjsonnet.js \ doc/js/libjsonnet.js \ $(BINS) \ @@ -135,7 +136,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: @@ -148,7 +149,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 @@ -211,6 +213,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 faa8b2f74..3c02c29c3 100755 --- a/tests.sh +++ b/tests.sh @@ -10,6 +10,8 @@ if [ -z "$DISABLE_LIB_TESTS" ]; then LD_LIBRARY_PATH=. ./libjsonnet_test_snippet "${TEST_SNIPPET}" || FAIL=TRUE echo -n '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