summaryrefslogtreecommitdiff
path: root/json-glib/tests/path.c
blob: 67d2a8710a9a59c5c57623ede9b590e369f9b2d5 (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
#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 *exp;
  const char *res;
} test_expressions[] = {
  {
    "$.store.book[0].title",
    "[\"Sayings of the Century\"]"
  },
  {
    "$['store']['book'][0]['title']",
    "[\"Sayings of the Century\"]"
  },
  {
    "$.store.book[*].author",
    "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
  },
  {
    "$..author",
    "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
  },
  {
    "$.store.*",
    NULL
  },
  {
    "$.store..price",
    "[\"8.95\",\"12.99\",\"8.99\",\"22.99\",\"19.95\"]"
  },
  {
    "$..book[2]",
    "[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":\"8.99\"}]"
  },
  {
    "$..book[-1:]",
    "[{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":\"22.99\"}]"
  },
  {
    "$..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\"}]"
  },
  {
    "$..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\"}]"
  },
};

static void
test_expression (void)
{
  JsonPath *path = json_path_new ();
  int i;

  for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
    {
      const char *expr = test_expressions[i].exp;
      GError *error = NULL;

      g_assert (json_path_compile (path, expr, &error));
      g_assert_no_error (error);
    }

  g_object_unref (path);
}

static void
test_match (void)
{
  JsonParser *parser = json_parser_new ();
  JsonGenerator *gen = json_generator_new ();
  JsonPath *path = json_path_new ();
  JsonNode *root;
  int i;

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

  for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
    {
      const char *expr = test_expressions[i].exp;
      const char *res  = test_expressions[i].res;
      JsonNode *matches;
      char *str;

      if (res == NULL || *res == '\0')
        continue;

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

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

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

      if (g_test_verbose ())
        {
          g_print ("* expr[%02d]: '%s' =>\n"
                   "- result:   %s\n"
                   "- expected: %s\n",
                   i, 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[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);
  g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=");

  g_test_add_func ("/path/expressions", test_expression);
  g_test_add_func ("/path/match", test_match);

  return g_test_run ();
}