| 1 |
#include <iostream> |
| 2 |
#include <sstream> |
| 3 |
#include <locale> |
| 4 |
#include <cassert> |
| 5 |
#include <libjsonnet++.h> |
| 6 |
#include "gtest/gtest.h" |
| 7 |
|
| 8 |
// Regression test for the follwing issue: https://github.com/google/jsonnet/issues/722 |
| 9 |
|
| 10 |
// Define a custom numpunct facet, so that we don't rely on any particular locale |
| 11 |
// being available on the system where the test is built and run. |
| 12 |
class Punctuator : public std::numpunct<char> { |
| 13 |
protected: |
| 14 |
virtual char do_decimal_point() const override { return '!'; } |
| 15 |
virtual std::string do_grouping() const override { return "\1\2"; } |
| 16 |
virtual char do_thousands_sep() const override { return '\''; } |
| 17 |
}; |
| 18 |
|
| 19 |
class TestWithModifiedGlobalLocale : public testing::Test { |
| 20 |
protected: |
| 21 |
TestWithModifiedGlobalLocale(): |
| 22 |
glocale_(std::locale::classic(), new Punctuator()) {} |
| 23 |
|
| 24 |
void SetUp() override { |
| 25 |
this->previous_global_locale_ = std::locale::global(this->glocale_); |
| 26 |
} |
| 27 |
void TearDown() override { |
| 28 |
std::locale::global(this->previous_global_locale_); |
| 29 |
} |
| 30 |
private: |
| 31 |
std::locale glocale_; |
| 32 |
std::locale previous_global_locale_; |
| 33 |
}; |
| 34 |
|
| 35 |
TEST_F(TestWithModifiedGlobalLocale, Test) { |
| 36 |
std::string templatedJSONString { "20000.5" }; |
| 37 |
|
| 38 |
// Self-test to make sure the custom global locale is actually being applied. |
| 39 |
std::ostringstream ss; |
| 40 |
ss << 20000.5; |
| 41 |
EXPECT_EQ(ss.str(), "20'00'0!5"); |
| 42 |
|
| 43 |
jsonnet::Jsonnet jsonnet {}; |
| 44 |
ASSERT_TRUE(jsonnet.init()); |
| 45 |
|
| 46 |
std::string expanded; |
| 47 |
ASSERT_TRUE(jsonnet.evaluateSnippet("", templatedJSONString, &expanded)) |
| 48 |
<< "Error parsing Jsonnet: " << jsonnet.lastError(); |
| 49 |
std::string expected = "20000.5\n"; |
| 50 |
EXPECT_EQ(expected, expanded); |
| 51 |
} |