diff options
| author | Emmanuele Bassi <ebassi@gnome.org> | 2011-06-19 12:08:03 +0100 | 
|---|---|---|
| committer | Emmanuele Bassi <ebassi@gnome.org> | 2011-06-19 12:08:03 +0100 | 
| commit | 78e896c64e8cbccf1b55291521f11a3f5093080a (patch) | |
| tree | d685216ec40f95a82808f5ec2d52322148d07bed /json-glib/tests/path.c | |
| parent | cef1f16cec529eeb03338f995a8b3d32b262df8b (diff) | |
| download | json-glib-78e896c64e8cbccf1b55291521f11a3f5093080a.tar.gz | |
build: Rename test binaries
It's pointless to add the '-test' suffix to files under the tests/ directory.
Diffstat (limited to 'json-glib/tests/path.c')
| -rw-r--r-- | json-glib/tests/path.c | 143 | 
1 files changed, 143 insertions, 0 deletions
| diff --git a/json-glib/tests/path.c b/json-glib/tests/path.c new file mode 100644 index 0000000..3a5e41c --- /dev/null +++ b/json-glib/tests/path.c @@ -0,0 +1,143 @@ +#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 char *test_expressions[] = { +  "$.store.book[0].title", +  "$['store']['book'][0]['title']", +  "$.store.book[*].author", +  "$..author", +  "$.store.*", +  "$.store..price", +  "$..book[2]", +  "$..book[-1:]", +  "$..book[0,1]", +  "$..book[:2]", +}; + +static const char *test_results[] = { +  "[\"Sayings of the Century\"]", +  "[\"Sayings of the Century\"]", +  "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", +  "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", +  NULL, +  "[\"8.95\",\"12.99\",\"8.99\",\"22.99\",\"19.95\"]", +  "[{\"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\"}]", +  "[{\"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\":\"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]; +      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]; +      const char *res = test_results[i]; +      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 (); +} | 
