Initial Import
[profile/ivi/json-glib.git] / json-glib / tests / path.c
1 #include <string.h>
2 #include <glib.h>
3 #include <json-glib/json-glib.h>
4
5 static const char *test_json =
6 "{ \"store\": {"
7 "    \"book\": [ "
8 "      { \"category\": \"reference\","
9 "        \"author\": \"Nigel Rees\","
10 "        \"title\": \"Sayings of the Century\","
11 "        \"price\": \"8.95\""
12 "      },"
13 "      { \"category\": \"fiction\","
14 "        \"author\": \"Evelyn Waugh\","
15 "        \"title\": \"Sword of Honour\","
16 "        \"price\": \"12.99\""
17 "      },"
18 "      { \"category\": \"fiction\","
19 "        \"author\": \"Herman Melville\","
20 "        \"title\": \"Moby Dick\","
21 "        \"isbn\": \"0-553-21311-3\","
22 "        \"price\": \"8.99\""
23 "      },"
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\""
29 "      }"
30 "    ],"
31 "    \"bicycle\": {"
32 "      \"color\": \"red\","
33 "      \"price\": \"19.95\""
34 "    }"
35 "  }"
36 "}";
37
38 static const struct {
39   const char *exp;
40   const char *res;
41 } test_expressions[] = {
42   {
43     "$.store.book[0].title",
44     "[\"Sayings of the Century\"]"
45   },
46   {
47     "$['store']['book'][0]['title']",
48     "[\"Sayings of the Century\"]"
49   },
50   {
51     "$.store.book[*].author",
52     "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
53   },
54   {
55     "$..author",
56     "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
57   },
58   {
59     "$.store.*",
60     NULL
61   },
62   {
63     "$.store..price",
64     "[\"8.95\",\"12.99\",\"8.99\",\"22.99\",\"19.95\"]"
65   },
66   {
67     "$..book[2]",
68     "[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":\"8.99\"}]"
69   },
70   {
71     "$..book[-1:]",
72     "[{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":\"22.99\"}]"
73   },
74   {
75     "$..book[0,1]",
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\"}]"
77   },
78   {
79     "$..book[:2]",
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\"}]"
81   },
82 };
83
84 static void
85 test_expression (void)
86 {
87   JsonPath *path = json_path_new ();
88   int i;
89
90   for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
91     {
92       const char *expr = test_expressions[i].exp;
93       GError *error = NULL;
94
95       g_assert (json_path_compile (path, expr, &error));
96       g_assert_no_error (error);
97     }
98
99   g_object_unref (path);
100 }
101
102 static void
103 test_match (void)
104 {
105   JsonParser *parser = json_parser_new ();
106   JsonGenerator *gen = json_generator_new ();
107   JsonPath *path = json_path_new ();
108   JsonNode *root;
109   int i;
110
111   json_parser_load_from_data (parser, test_json, -1, NULL);
112   root = json_parser_get_root (parser);
113
114   for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
115     {
116       const char *expr = test_expressions[i].exp;
117       const char *res  = test_expressions[i].res;
118       JsonNode *matches;
119       char *str;
120
121       if (res == NULL || *res == '\0')
122         continue;
123
124       g_assert (json_path_compile (path, expr, NULL));
125
126       matches = json_path_match (path, root);
127       g_assert (JSON_NODE_HOLDS_ARRAY (matches));
128
129       json_generator_set_root (gen, matches);
130       str = json_generator_to_data (gen, NULL);
131
132       if (g_test_verbose ())
133         {
134           g_print ("* expr[%02d]: '%s' =>\n"
135                    "- result:   %s\n"
136                    "- expected: %s\n",
137                    i, expr, str, res);
138         }
139
140       g_assert_cmpstr (str, ==, res);
141
142       g_free (str);
143       json_node_free (matches);
144     }
145
146   g_object_unref (parser);
147   g_object_unref (path);
148   g_object_unref (gen);
149 }
150
151 int
152 main (int   argc,
153       char *argv[])
154 {
155   g_type_init ();
156   g_test_init (&argc, &argv, NULL);
157   g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=");
158
159   g_test_add_func ("/path/expressions", test_expression);
160   g_test_add_func ("/path/match", test_match);
161
162   return g_test_run ();
163 }