25 #include "gtest/gtest.h" 31 std::unique_ptr<LuaParameterDictionary> MakeNonReferenceCounted(
34 code, common::make_unique<DummyFileResolver>());
37 class LuaParameterDictionaryTest :
public ::testing::Test {
39 void ReferenceAllKeysAsIntegers(LuaParameterDictionary* dict) {
40 for (
const string& key : dict->GetKeys()) {
46 TEST_F(LuaParameterDictionaryTest, GetInt) {
48 ASSERT_EQ(dict->GetInt(
"blah"), 100);
51 TEST_F(LuaParameterDictionaryTest, GetString) {
53 ASSERT_EQ(dict->GetString(
"blah"),
"is_a_string");
56 TEST_F(LuaParameterDictionaryTest, GetDouble) {
58 ASSERT_DOUBLE_EQ(dict->GetDouble(
"blah"), 3.1415);
61 TEST_F(LuaParameterDictionaryTest, GetBoolTrue) {
63 ASSERT_TRUE(dict->GetBool(
"blah"));
66 TEST_F(LuaParameterDictionaryTest, GetBoolFalse) {
68 ASSERT_FALSE(dict->GetBool(
"blah"));
71 TEST_F(LuaParameterDictionaryTest, GetDictionary) {
73 MakeDictionary(
"return { blah = { blue = 100, red = 200 }, fasel = 10 }");
75 std::unique_ptr<LuaParameterDictionary> sub_dict(dict->GetDictionary(
"blah"));
76 std::vector<string> keys = sub_dict->GetKeys();
77 ASSERT_EQ(keys.size(), 2);
78 std::sort(keys.begin(), keys.end());
79 ASSERT_EQ(keys[0],
"blue");
80 ASSERT_EQ(keys[1],
"red");
81 ASSERT_TRUE(sub_dict->HasKey(
"blue"));
82 ASSERT_TRUE(sub_dict->HasKey(
"red"));
83 ASSERT_EQ(sub_dict->GetInt(
"blue"), 100);
84 ASSERT_EQ(sub_dict->GetInt(
"red"), 200);
86 ASSERT_EQ(dict->GetString(
"fasel"),
"10");
89 TEST_F(LuaParameterDictionaryTest, GetKeys) {
92 std::vector<string> keys = dict->GetKeys();
93 ASSERT_EQ(keys.size(), 2);
94 std::sort(keys.begin(), keys.end());
95 ASSERT_EQ(keys[0],
"blah");
96 ASSERT_EQ(keys[1],
"blah1");
98 ReferenceAllKeysAsIntegers(dict.get());
101 TEST_F(LuaParameterDictionaryTest, NonExistingKey) {
103 ReferenceAllKeysAsIntegers(dict.get());
104 ASSERT_DEATH(dict->GetInt(
"blah_fasel"),
"Key.* not in dictionary.");
107 TEST_F(LuaParameterDictionaryTest, UintNegative) {
109 ASSERT_DEATH(dict->GetNonNegativeInt(
"blah"),
".*-100 is negative.");
110 ReferenceAllKeysAsIntegers(dict.get());
113 TEST_F(LuaParameterDictionaryTest, ToString) {
115 ceta = { yolo = "hurray" }, 125 const string golden = R
"({ 129 blubber = 123.000000, 137 EXPECT_EQ(golden, dict->ToString()); 141 EXPECT_EQ(dict1->GetBool(
"alpha"),
true);
142 EXPECT_EQ(dict1->GetBool(
"alpha1"),
false);
143 EXPECT_EQ(dict1->GetInt(
"blubber"), 123);
144 EXPECT_EQ(dict1->GetString(
"blub"),
"hello");
145 EXPECT_EQ(dict1->GetDictionary(
"ceta")->GetString(
"yolo"),
"hurray");
146 EXPECT_NEAR(dict1->GetDouble(
"fasel"), 1234.456786, 1e-3);
147 EXPECT_TRUE(std::isinf(-dict1->GetDouble(
"fasel1")));
148 EXPECT_TRUE(std::isinf(dict1->GetDouble(
"fasel2")));
150 EXPECT_EQ(dict->GetBool(
"alpha"),
true);
151 EXPECT_EQ(dict->GetBool(
"alpha1"),
false);
152 EXPECT_EQ(dict->GetInt(
"blubber"), 123);
153 EXPECT_EQ(dict->GetString(
"blub"),
"hello");
154 EXPECT_EQ(dict->GetDictionary(
"ceta")->GetString(
"yolo"),
"hurray");
155 EXPECT_NEAR(dict->GetDouble(
"fasel"), 1234.456786, 1e-3);
156 EXPECT_TRUE(std::isinf(-dict->GetDouble(
"fasel1")));
157 EXPECT_TRUE(std::isinf(dict->GetDouble(
"fasel2")));
160 TEST_F(LuaParameterDictionaryTest, ToStringForArrays) {
161 auto dict = MakeNonReferenceCounted(
167 const string golden = R
"({ 173 EXPECT_EQ(golden, dict->ToString()); 176 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsStrings) { 178 EXPECT_EQ(0, dict->GetKeys().size());
179 const std::vector<string> values = dict->GetArrayValuesAsStrings();
180 EXPECT_EQ(3, values.size());
181 EXPECT_EQ(
"a", values[0]);
182 EXPECT_EQ(
"b", values[1]);
183 EXPECT_EQ(
"c", values[2]);
186 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsDoubles) {
188 EXPECT_EQ(0, dict->GetKeys().size());
189 const std::vector<double> values = dict->GetArrayValuesAsDoubles();
190 EXPECT_EQ(3, values.size());
191 EXPECT_NEAR(1., values[0], 1e-7);
192 EXPECT_NEAR(2., values[1], 1e-7);
193 EXPECT_NEAR(3., values[2], 1e-7);
196 TEST_F(LuaParameterDictionaryTest, GetArrayValuesAsDictionaries) {
198 EXPECT_EQ(0, dict->GetKeys().size());
199 const std::vector<std::unique_ptr<LuaParameterDictionary>> values =
200 dict->GetArrayValuesAsDictionaries();
201 EXPECT_EQ(2, values.size());
202 EXPECT_EQ(1., values[0]->GetInt(
"a"));
203 EXPECT_EQ(3., values[1]->GetInt(
"b"));
206 TEST_F(LuaParameterDictionaryTest, TestChooseTrue) {
208 EXPECT_EQ(1, dict->GetInt(
"a"));
211 TEST_F(LuaParameterDictionaryTest, TestChoseFalse) {
213 EXPECT_EQ(0, dict->GetInt(
"a"));
216 TEST_F(LuaParameterDictionaryTest, TestChooseInvalidArgument) {
217 EXPECT_DEATH(
MakeDictionary(
"return { a = choose('truish', 1, 0) }"),
218 "condition is not a boolean value.");
std::unique_ptr< LuaParameterDictionary > MakeDictionary(const string &code)
static std::unique_ptr< LuaParameterDictionary > NonReferenceCounted(const string &code, std::unique_ptr< FileResolver > file_resolver)