8 #define ASSERT_OP(lhs, rhs, op, m) \
10 if(!((lhs) op (rhs))) { \
11 std::cerr << std::boolalpha; \
12 std::cerr << __FILE__ << '[' << __LINE__ << "]: ERROR: " << (m) << std::endl; \
13 std::cerr << "\ttest: " << #lhs << ' ' << #op << ' ' << #rhs << std::endl; \
14 std::cerr << "\tresult: " << (lhs) << ' ' << #op << ' ' << (rhs) << std::endl; \
18 #define ASSERT_EQ(lhs, rhs, m) ASSERT_OP(lhs, rhs, ==, m)
19 #define ASSERT_NE(lhs, rhs, m) ASSERT_OP(lhs, rhs, !=, m)
20 #define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m)
21 #define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m)
24 jansson::Value e1(jansson::Value::load_file("test.json"));
25 jansson::Value e2(e1);
27 jansson::Value e4(jansson::Value::load_string("{\"foo\": true, \"bar\": \"test\"}"));
29 ASSERT_TRUE(e1.is_object(), "e1 is not an object");
30 ASSERT_TRUE(e2.is_object(), "e2 is not an object");
31 ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value");
32 ASSERT_TRUE(e4.is_object(), "e4 is not an object");
34 ASSERT_EQ(e1.size(), 1, "e1 has too many properties");
35 ASSERT_EQ(e2.size(), 1, "e2 has too many properties");
36 ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements");
38 ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object");
39 ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
40 ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
42 ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value");
44 jansson::Iterator i(e1.get("web-app"));
45 ASSERT_EQ(i.key(), "taglib", "first iterator result has incorrect key");
47 ASSERT_EQ(i.key(), "servlet", "first iterator result has incorrect key");
49 ASSERT_EQ(i.key(), "servlet-mapping", "first iterator result has incorrect key");
51 ASSERT_FALSE(i.valid(), "iterator has more values than expected");
53 e3 = jansson::Value::from(12.34);
54 ASSERT_TRUE(e3.is_number(), "e3 is not a number after assignment");
55 ASSERT_EQ(e3.as_real(), 12.34, "e3 has incorrect value after assignment");
57 e3 = jansson::Value::from(true);
58 ASSERT_TRUE(e3.is_boolean(), "e3 is not a boolean after assignment");
59 ASSERT_EQ(e3.as_boolean(), true, "e3 has incorrect value after assignment");
61 e3 = jansson::Value::from("foobar");
62 ASSERT_TRUE(e3.is_string(), "e3 is not a string after assignment");
63 ASSERT_EQ(e3.as_string(), "foobar", "e3 has incorrect value after assignment");
65 e3.set(0, jansson::Value::from("foobar"));
66 ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
67 ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of elements after assignment");
68 ASSERT_EQ(e3[0].as_string(), "foobar", "e3[0] has incorrect value after assignment");
70 e3.set(1, jansson::Value::from("foobar"));
71 ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
72 ASSERT_EQ(e3.size(), 2, "e3 has incorrect number of elements after assignment");
73 ASSERT_EQ(e3[1].as_string(), "foobar", "e3[0] has incorrect value after assignment");
75 e3.set(0, jansson::Value::from("barfoo"));
76 ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
77 ASSERT_EQ(e3.size(), 2, "e3 has incorrect number of elements after assignment");
78 ASSERT_EQ(e3[0].as_string(), "barfoo", "e3[0] has incorrect value after assignment");
80 e3.set(100, jansson::Value::null());
81 ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
82 ASSERT_EQ(e3.size(), 2, "e3 has incorrect number of elements after assignment");