27
27
Json::Value val = JsonWrapper::StringToJson(good_json);
28
28
EXPECT_EQ(val, Json::Value(Json::objectValue));
29
29
std::string bad_json = "{";
30
EXPECT_THROW(JsonWrapper::StringToJson(bad_json), Squeal);
30
EXPECT_THROW(JsonWrapper::StringToJson(bad_json), MAUS::Exception);
33
33
TEST(JsonWrapperTest, JsonToString) {
45
45
EXPECT_EQ(Json::Value(static_cast<int>(i+2)),
46
46
JsonWrapper::GetItem(good_val, i, JsonWrapper::anyValue));
47
47
EXPECT_THROW( // bad type
48
JsonWrapper::GetItem(good_val, i, JsonWrapper::stringValue), Squeal);
48
JsonWrapper::GetItem(good_val, i, JsonWrapper::stringValue), MAUS::Exception);
50
50
Json::Value empty_array = JsonWrapper::StringToJson("[]");
51
51
EXPECT_THROW( // empty value
52
JsonWrapper::GetItem(empty_array, 0, JsonWrapper::anyValue), Squeal);
52
JsonWrapper::GetItem(empty_array, 0, JsonWrapper::anyValue), MAUS::Exception);
53
53
EXPECT_THROW( // out of range item
54
JsonWrapper::GetItem(good_val, 4, JsonWrapper::anyValue), Squeal);
54
JsonWrapper::GetItem(good_val, 4, JsonWrapper::anyValue), MAUS::Exception);
70
70
for (unsigned int j = 0; j < 5; ++j) {
73
JsonWrapper::GetProperty(good_val, gets[i], types[j]), Squeal)
73
JsonWrapper::GetProperty(good_val, gets[i], types[j]), MAUS::Exception)
74
74
<< i << " " << j << " " << gets[i];
76
76
EXPECT_EQ(Json::Value(2.5),
87
87
JsonWrapper::GetProperty(good_val, gets[5], types[5]));
88
88
Json::Value emptyProp = JsonWrapper::StringToJson("{}");
89
89
EXPECT_THROW( // non-existent property
90
JsonWrapper::GetProperty(emptyProp, "a", JsonWrapper::anyValue), Squeal);
90
JsonWrapper::GetProperty(emptyProp, "a", JsonWrapper::anyValue), MAUS::Exception);
93
93
TEST(JsonWrapperTest, TypeConversionTest) {
103
103
EXPECT_EQ(JsonWrapper::JsonTypeToValueType(wr_tp[i]), js_tp[i]);
104
104
EXPECT_EQ(JsonWrapper::ValueTypeToJsonType(js_tp[i]), wr_tp[i]);
106
EXPECT_THROW(JsonWrapper::JsonTypeToValueType(JsonWrapper::anyValue), Squeal);
106
EXPECT_THROW(JsonWrapper::JsonTypeToValueType(JsonWrapper::anyValue), MAUS::Exception);
109
109
TEST(JsonWrapperTest, SimilarTypeTest) {
160
160
EXPECT_TRUE(JsonWrapper::AlmostEqual(arr_1, arr_1, 1e-9));
161
161
EXPECT_FALSE(JsonWrapper::AlmostEqual(arr_1, arr_2, 1e-9));
163
Json::Value arr_3, arr_4, arr_5;
165
arr_4.append(uint_1);
166
arr_5.append(real_1);
167
EXPECT_TRUE(JsonWrapper::AlmostEqual(arr_3, arr_4, 1e-9, true));
168
EXPECT_FALSE(JsonWrapper::AlmostEqual(arr_3, arr_4, 1e-9, false));
169
EXPECT_FALSE(JsonWrapper::AlmostEqual(arr_3, arr_5, 1e-9, true));
163
171
// more object tests below
164
172
Json::Value obj_1(Json::objectValue), obj_2(Json::objectValue);
165
173
obj_1["1"] = int_1;
166
174
obj_2["1"] = int_2;
167
175
EXPECT_TRUE(JsonWrapper::AlmostEqual(obj_1, obj_1, 1e-9));
168
176
EXPECT_FALSE(JsonWrapper::AlmostEqual(obj_1, obj_2, 1e-9));
178
Json::Value obj_3(Json::objectValue), obj_4(Json::objectValue);
181
EXPECT_TRUE(JsonWrapper::AlmostEqual(obj_3, obj_4, 1e-9, true));
182
EXPECT_FALSE(JsonWrapper::AlmostEqual(obj_3, obj_4, 1e-9, false));
171
185
TEST(JsonWrapperTest, ArrayEqualTest) {
232
246
EXPECT_EQ(object_merged["array"].size(), size_t(2));
233
247
EXPECT_EQ(object_merged["string"], Json::Value("string_value"));
234
248
// throw because common non-array properties
235
EXPECT_THROW(JsonWrapper::ObjectMerge(object_1, object_1), Squeal);
249
EXPECT_THROW(JsonWrapper::ObjectMerge(object_1, object_1), MAUS::Exception);
236
250
// throw because non-objects
237
EXPECT_THROW(JsonWrapper::ObjectMerge(int_1, object_1), Squeal);
238
EXPECT_THROW(JsonWrapper::ObjectMerge(object_1, int_1), Squeal);
251
EXPECT_THROW(JsonWrapper::ObjectMerge(int_1, object_1), MAUS::Exception);
252
EXPECT_THROW(JsonWrapper::ObjectMerge(object_1, int_1), MAUS::Exception);
241
255
TEST(JsonWrapperTest, ArrayMergeTest) {
252
266
EXPECT_EQ(array_merged[2], Json::Value("bob"));
253
267
EXPECT_EQ(array_merged[3], Json::Value(3));
254
268
// non-array in merge
255
EXPECT_THROW(JsonWrapper::ArrayMerge(int_1, array_1), Squeal);
256
EXPECT_THROW(JsonWrapper::ArrayMerge(array_1, int_1), Squeal);
269
EXPECT_THROW(JsonWrapper::ArrayMerge(int_1, array_1), MAUS::Exception);
270
EXPECT_THROW(JsonWrapper::ArrayMerge(array_1, int_1), MAUS::Exception);
259
273
TEST(JsonWrapperTest, JsonValueTypeToStringTest) {
270
284
EXPECT_EQ(JsonWrapper::Path::GetPath(test), "#test/append");
271
285
JsonWrapper::Path::AppendPath(test, 1);
272
286
EXPECT_EQ(JsonWrapper::Path::GetPath(test), "#test/append/1");
273
EXPECT_THROW(JsonWrapper::Path::AppendPath(test, "append/"), Squeal);
287
EXPECT_THROW(JsonWrapper::Path::AppendPath(test, "append/"), MAUS::Exception);
274
288
Json::Value test_2;
275
289
JsonWrapper::Path::AppendPath(test_2, "append");
276
290
EXPECT_EQ(JsonWrapper::Path::GetPath(test_2), "#append");
300
314
Json::Value("test_2"));
301
315
// not an object or array
302
316
Json::Value not_branch(1);
303
EXPECT_THROW(JsonWrapper::Path::DereferencePath(not_branch, "#1"), Squeal);
317
EXPECT_THROW(JsonWrapper::Path::DereferencePath(not_branch, "#1"), MAUS::Exception);
304
318
// branch does not exist
305
EXPECT_THROW(JsonWrapper::Path::DereferencePath(test, "#no_branch"), Squeal);
319
EXPECT_THROW(JsonWrapper::Path::DereferencePath(test, "#no_branch"), MAUS::Exception);
306
320
// array element does not exist
307
EXPECT_THROW(JsonWrapper::Path::DereferencePath(test, "#object_0/2"), Squeal);
321
EXPECT_THROW(JsonWrapper::Path::DereferencePath(test, "#object_0/2"), MAUS::Exception);
310
324
TEST(JsonWrapperTest, SetPathRecursiveTest) {