2 #include <json-glib/json-glib.h>
5 #if !GLIB_CHECK_VERSION (2, 31, 0)
6 #include <glib/gtestutils.h>
15 JsonNode *node = json_node_new (JSON_NODE_NULL);
16 JsonNode *copy = json_node_copy (node);
18 g_assert_cmpint (json_node_get_node_type (node), ==, json_node_get_node_type (copy));
19 g_assert_cmpint (json_node_get_value_type (node), ==, json_node_get_value_type (copy));
20 g_assert_cmpstr (json_node_type_name (node), ==, json_node_type_name (copy));
22 json_node_free (copy);
23 json_node_free (node);
27 test_copy_value (void)
29 JsonNode *node = json_node_new (JSON_NODE_VALUE);
32 json_node_set_string (node, "hello");
34 copy = json_node_copy (node);
35 g_assert_cmpint (json_node_get_node_type (node), ==, json_node_get_node_type (copy));
36 g_assert_cmpstr (json_node_type_name (node), ==, json_node_type_name (copy));
37 g_assert_cmpstr (json_node_get_string (node), ==, json_node_get_string (copy));
39 json_node_free (copy);
40 json_node_free (node);
44 test_copy_object (void)
46 JsonObject *obj = json_object_new ();
47 JsonNode *node = json_node_new (JSON_NODE_OBJECT);
48 JsonNode *value = json_node_new (JSON_NODE_VALUE);
51 json_node_set_int (value, 42);
52 json_object_set_member (obj, "answer", value);
54 json_node_take_object (node, obj);
56 copy = json_node_copy (node);
58 g_assert_cmpint (json_node_get_node_type (node), ==, json_node_get_node_type (copy));
59 g_assert (json_node_get_object (node) == json_node_get_object (copy));
61 json_node_free (copy);
62 json_node_free (node);
68 JsonNode *node = json_node_new (JSON_NODE_NULL);
70 g_assert (JSON_NODE_HOLDS_NULL (node));
71 g_assert_cmpint (json_node_get_value_type (node), ==, G_TYPE_INVALID);
72 g_assert_cmpstr (json_node_type_name (node), ==, "NULL");
74 json_node_free (node);
80 JsonNode *node = json_node_new (JSON_NODE_VALUE);
81 GValue value = { 0, };
82 GValue check = { 0, };
84 g_assert_cmpint (JSON_NODE_TYPE (node), ==, JSON_NODE_VALUE);
86 g_value_init (&value, G_TYPE_INT64);
87 g_value_set_int64 (&value, 42);
89 g_assert_cmpint (G_VALUE_TYPE (&value), ==, G_TYPE_INT64);
90 g_assert_cmpint (g_value_get_int64 (&value), ==, 42);
92 json_node_set_value (node, &value);
93 json_node_get_value (node, &check);
95 g_assert_cmpint (G_VALUE_TYPE (&value), ==, G_VALUE_TYPE (&check));
96 g_assert_cmpint (g_value_get_int64 (&value), ==, g_value_get_int64 (&check));
97 g_assert_cmpint (G_VALUE_TYPE (&check), ==, G_TYPE_INT64);
98 g_assert_cmpint (g_value_get_int64 (&check), ==, 42);
100 g_value_unset (&value);
101 g_value_unset (&check);
102 json_node_free (node);
110 g_test_init (&argc, &argv, NULL);
112 g_test_add_func ("/nodes/null-node", test_null);
113 g_test_add_func ("/nodes/copy-null", test_copy_null);
114 g_test_add_func ("/nodes/copy-value", test_copy_value);
115 g_test_add_func ("/nodes/copy-object", test_copy_object);
116 g_test_add_func ("/nodes/value", test_value);
118 return g_test_run ();