Skip to content

Commit 9ee4e2c

Browse files
Fix locale test to avoid relying on test system locales
1 parent 00870e8 commit 9ee4e2c

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

core/libjsonnet_test_locale.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
#include <iostream>
2+
#include <sstream>
23
#include <locale>
34
#include <cassert>
45
#include <libjsonnet++.h>
56

67
// Regression test for the follwing issue: https://github.com/google/jsonnet/issues/722
78

9+
// Define a custom numpunct facet, so that we don't rely on any particular locale
10+
// being available on the system where the test is built and run.
11+
class Punctuator : public std::numpunct<char> {
12+
protected:
13+
virtual char do_decimal_point() const override { return '!'; }
14+
virtual std::string do_grouping() const override { return "\1\2"; }
15+
virtual char do_thousands_sep() const override { return '\''; }
16+
};
17+
818
int main() {
9-
std::string templatedJSONString { "2000" };
10-
std::locale glocale("en_US.UTF-8");
19+
std::string templatedJSONString { "20000.5" };
20+
Punctuator punctuator;
21+
std::locale glocale(std::locale::classic(), &punctuator);
1122
std::locale::global(glocale);
1223

24+
// Self-test to make sure the custom global locale is actually being applied.
25+
std::ostringstream ss;
26+
ss << 20000.5;
27+
assert(ss.str() == "20'00'0!5");
28+
1329
jsonnet::Jsonnet jsonnet {};
1430
jsonnet.init();
1531

@@ -18,7 +34,7 @@ int main() {
1834
std::cerr << "Error parsing Jsonnet: "+jsonnet.lastError();
1935
exit(1);
2036
}
21-
std::string expected = "2000\n";
37+
std::string expected = "20000.5\n";
2238
assert(expected == expanded);
2339
return 0;
2440
}

0 commit comments

Comments
 (0)