summaryrefslogtreecommitdiff
path: root/json-glib/tests/path.c
blob: d674da3e20600ca551bd12962efd46a158b0a6bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <string.h>
#include <glib.h>
#include <json-glib/json-glib.h>

static const char *test_json =
"{ \"store\": {"
"    \"book\": [ "
"      { \"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\""
"      },"
"      { \"category\": \"fiction\","
"        \"author\": \"Herman Melville\","
"        \"title\": \"Moby Dick\","
"        \"isbn\": \"0-553-21311-3\","
"        \"price\": \"8.99\""
"      },"
"      { \"category\": \"fiction\","
"        \"author\": \"J. R. R. Tolkien\","
"        \"title\": \"The Lord of the Rings\","
"        \"isbn\": \"0-395-19395-8\","
"        \"price\": \"22.99\""
"      }"
"    ],"
"    \"bicycle\": {"
"      \"color\": \"red\","
"      \"price\": \"19.95\""
"    }"
"  }"
"}";

static const struct {
  const char *desc;
  const char *expr;
  const char *res;

  guint is_valid : 1;

  JsonPathError error_code;
} test_expressions[] = {
  {
    "INVALID: invalid first character",
    "/",
    NULL,
    FALSE,
    JSON_PATH_ERROR_INVALID_QUERY,
  },
  {
    "INVALID: Invalid character following root",
    "$ponies",
    NULL,
    FALSE,
    JSON_PATH_ERROR_INVALID_QUERY,
  },
  {
    "INVALID: missing member name or wildcard after dot",
    "$.store.",
    NULL,
    FALSE,
    JSON_PATH_ERROR_INVALID_QUERY,
  },
  {
    "INVALID: Malformed slice (missing step)",
    "$.store.book[0:1:]",
    NULL,
    FALSE,
    JSON_PATH_ERROR_INVALID_QUERY,
  },
  {
    "INVALID: Malformed set",
    "$.store.book[0,1~2]",
    NULL,
    FALSE,
    JSON_PATH_ERROR_INVALID_QUERY,
  },
  {
    "INVALID: Malformed array notation",
    "${'store'}",
    NULL,
    FALSE,
    JSON_PATH_ERROR_INVALID_QUERY,
  },
  {
    "INVALID: Malformed slice (invalid separator)",
    "$.store.book[0~2]",
    NULL,
    FALSE,
    JSON_PATH_ERROR_INVALID_QUERY,
  },
  {
    "Title of the first book in the store, using objct notation.",
    "$.store.book[0].title",
    "[\"Sayings of the Century\"]",
    TRUE,
  },
  {
    "Title of the first book in the store, using array notation.",
    "$['store']['book'][0]['title']",
    "[\"Sayings of the Century\"]",
    TRUE,
  },
  {
    "All the authors from the every book.",
    "$.store.book[*].author",
    "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]",
    TRUE,
  },
  {
    "All the authors.",
    "$..author",
    "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]",
    TRUE,
  },
  {
    "Everything inside the store.",
    "$.store.*",
    NULL,
    TRUE,
  },
  {
    "All the prices in the store.",
    "$.store..price",
    "[\"8.95\",\"12.99\",\"8.99\",\"22.99\",\"19.95\"]",
    TRUE,
  },
  {
    "The third book.",
    "$..book[2]",
    "[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":\"8.99\"}]",
    TRUE,
  },
  {
    "The last book.",
    "$..book[-1:]",
    "[{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":\"22.99\"}]",
    TRUE,
  },
  {
    "The first two books.",
    "$..book[0,1]",
    "[{\"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\"}]",
    TRUE,
  },
  {
    "The first two books, using a slice.",
    "$..book[:2]",
    "[{\"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\"}]",
    TRUE,
  },
  {
    "All the books.",
    "$['store']['book'][*]",
    "[{\"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\"},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":\"8.99\"},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":\"22.99\"}]",
    TRUE,
  },
  {
    "All the members of the bicycle object.",
    "$.store.bicycle.*",
    "[\"red\",\"19.95\"]",
    TRUE,
  },
  {
    "The root node.",
    "$",
    "[{\"store\":{\"book\":[{\"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\"},"
                           "{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":\"8.99\"},"
                           "{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":\"22.99\"}],"
                 "\"bicycle\":{\"color\":\"red\",\"price\":\"19.95\"}}}]",
    TRUE,
  }
};

static void
path_expressions_valid (gconstpointer data)
{
  const int index_ = GPOINTER_TO_INT (data);
  const char *expr = test_expressions[index_].expr;
  const char *desc = test_expressions[index_].desc;

  JsonPath *path = json_path_new ();
  GError *error = NULL;

  if (g_test_verbose ())
    g_print ("* %s ('%s')\n", desc, expr);

  g_assert_true (json_path_compile (path, expr, &error));
  g_assert_no_error (error);

  g_object_unref (path);
}

static void
path_expressions_invalid (gconstpointer data)
{
  const int index_ = GPOINTER_TO_INT (data);
  const char *expr = test_expressions[index_].expr;
  const char *desc = test_expressions[index_].desc;
  const JsonPathError code = test_expressions[index_].error_code;

  JsonPath *path = json_path_new ();
  GError *error = NULL;

  if (g_test_verbose ())
    g_print ("* %s ('%s')\n", desc, expr);


  g_assert_false (json_path_compile (path, expr, &error));
  g_assert_error (error, JSON_PATH_ERROR, code);

  g_object_unref (path);
  g_clear_error (&error);
}

static void
path_match (gconstpointer data)
{
  const int index_ = GPOINTER_TO_INT (data);
  const char *desc = test_expressions[index_].desc;
  const char *expr = test_expressions[index_].expr;
  const char *res  = test_expressions[index_].res;

  JsonParser *parser = json_parser_new ();
  JsonGenerator *gen = json_generator_new ();
  JsonPath *path = json_path_new ();
  JsonNode *root;
  JsonNode *matches;
  char *str;

  json_parser_load_from_data (parser, test_json, -1, NULL);
  root = json_parser_get_root (parser);

  g_assert_true (json_path_compile (path, expr, NULL));

  matches = json_path_match (path, root);
  g_assert_true (JSON_NODE_HOLDS_ARRAY (matches));

  json_generator_set_root (gen, matches);
  str = json_generator_to_data (gen, NULL);

  if (g_test_verbose ())
    {
      g_print ("* %s ('%s') =>\n"
               "- result:   %s\n"
               "- expected: %s\n",
               desc, expr,
               str,
               res);
    }

  g_assert_cmpstr (str, ==, res);

  g_free (str);
  json_node_free (matches);

  g_object_unref (parser);
  g_object_unref (path);
  g_object_unref (gen);
}

int
main (int   argc,
      char *argv[])
{
  int i, j;

  g_test_init (&argc, &argv, NULL);
  g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=");

  for (i = 0, j = 1; i < G_N_ELEMENTS (test_expressions); i++)
    {
      char *path;

      if (!test_expressions[i].is_valid)
        continue;

      path = g_strdup_printf ("/path/expressions/valid/%d", j++);

      g_test_add_data_func (path, GINT_TO_POINTER (i), path_expressions_valid);

      g_free (path);
    }

  for (i = 0, j = 1; i < G_N_ELEMENTS (test_expressions); i++)
    {
      char *path;

      if (test_expressions[i].is_valid)
        continue;

      path = g_strdup_printf ("/path/expressions/invalid/%d", j++);

      g_test_add_data_func (path, GINT_TO_POINTER (i), path_expressions_invalid);

      g_free (path);
    }

  for (i = 0, j = 1; i < G_N_ELEMENTS (test_expressions); i++)
    {
      char *path;

      if (!test_expressions[i].is_valid || test_expressions[i].res == NULL)
        continue;

      path = g_strdup_printf ("/path/match/%d", j++);

      g_test_add_data_func (path, GINT_TO_POINTER (i), path_match);

      g_free (path);
    }

  return g_test_run ();
}