3 #include <json-glib/json-glib.h>
5 static const char *test_json =
8 " { \"category\": \"reference\","
9 " \"author\": \"Nigel Rees\","
10 " \"title\": \"Sayings of the Century\","
11 " \"price\": \"8.95\""
13 " { \"category\": \"fiction\","
14 " \"author\": \"Evelyn Waugh\","
15 " \"title\": \"Sword of Honour\","
16 " \"price\": \"12.99\""
18 " { \"category\": \"fiction\","
19 " \"author\": \"Herman Melville\","
20 " \"title\": \"Moby Dick\","
21 " \"isbn\": \"0-553-21311-3\","
22 " \"price\": \"8.99\""
24 " { \"category\": \"fiction\","
25 " \"author\": \"J. R. R. Tolkien\","
26 " \"title\": \"The Lord of the Rings\","
27 " \"isbn\": \"0-395-19395-8\","
28 " \"price\": \"22.99\""
32 " \"color\": \"red\","
33 " \"price\": \"19.95\""
41 } test_expressions[] = {
43 "$.store.book[0].title",
44 "[\"Sayings of the Century\"]"
47 "$['store']['book'][0]['title']",
48 "[\"Sayings of the Century\"]"
51 "$.store.book[*].author",
52 "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
56 "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
64 "[\"8.95\",\"12.99\",\"8.99\",\"22.99\",\"19.95\"]"
68 "[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":\"8.99\"}]"
72 "[{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":\"22.99\"}]"
76 "[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":\"8.95\"},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":\"12.99\"}]"
80 "[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":\"8.95\"},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":\"12.99\"}]"
85 test_expression (void)
87 JsonPath *path = json_path_new ();
90 for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
92 const char *expr = test_expressions[i].exp;
95 g_assert (json_path_compile (path, expr, &error));
96 g_assert_no_error (error);
99 g_object_unref (path);
105 JsonParser *parser = json_parser_new ();
106 JsonGenerator *gen = json_generator_new ();
107 JsonPath *path = json_path_new ();
111 json_parser_load_from_data (parser, test_json, -1, NULL);
112 root = json_parser_get_root (parser);
114 for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
116 const char *expr = test_expressions[i].exp;
117 const char *res = test_expressions[i].res;
121 if (res == NULL || *res == '\0')
124 g_assert (json_path_compile (path, expr, NULL));
126 matches = json_path_match (path, root);
127 g_assert (JSON_NODE_HOLDS_ARRAY (matches));
129 json_generator_set_root (gen, matches);
130 str = json_generator_to_data (gen, NULL);
132 if (g_test_verbose ())
134 g_print ("* expr[%02d]: '%s' =>\n"
140 g_assert_cmpstr (str, ==, res);
143 json_node_free (matches);
146 g_object_unref (parser);
147 g_object_unref (path);
148 g_object_unref (gen);
156 g_test_init (&argc, &argv, NULL);
157 g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=");
159 g_test_add_func ("/path/expressions", test_expression);
160 g_test_add_func ("/path/match", test_match);
162 return g_test_run ();