[json-glib] tests/path: Allow testing for valid queries



commit 72909d28f48365f249e87ddb0ebea8ad0f0adac1
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed May 15 21:39:46 2013 +0100

    tests/path: Allow testing for valid queries
    
    This will allow adding negative tests to JsonPath.
    
    The tests will only be caught when compiling the query; an invalid query
    won't return a match, so there's no point to check matches.

 json-glib/tests/path.c |   64 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 50 insertions(+), 14 deletions(-)
---
diff --git a/json-glib/tests/path.c b/json-glib/tests/path.c
index 2b7c1f2..d3aa74b 100644
--- a/json-glib/tests/path.c
+++ b/json-glib/tests/path.c
@@ -39,66 +39,82 @@ static const struct {
   const char *desc;
   const char *expr;
   const char *res;
+
+  guint is_valid : 1;
+
+  JsonPathError error_code;
 } test_expressions[] = {
   {
     "Title of the first book in the store, using objct notation.",
     "$.store.book[0].title",
-    "[\"Sayings of the Century\"]"
+    "[\"Sayings of the Century\"]",
+    TRUE,
   },
   {
     "Title of the first book in the store, using array notation.",
     "$['store']['book'][0]['title']",
-    "[\"Sayings of the Century\"]"
+    "[\"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\"]"
+    "[\"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\"]"
+    "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]",
+    TRUE,
   },
   {
     "Everything inside the store.",
     "$.store.*",
-    NULL
+    NULL,
+    TRUE,
   },
   {
     "All the prices in the store.",
     "$.store..price",
-    "[\"8.95\",\"12.99\",\"8.99\",\"22.99\",\"19.95\"]"
+    "[\"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\"}]"
+    "[{\"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\"}]"
+    "[{\"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\"}]"
+    "[{\"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\"}]"
+    "[{\"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\"}]"
+    "[{\"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\"]"
+    "[\"red\",\"19.95\"]",
+    TRUE,
   },
 };
 
@@ -111,10 +127,27 @@ test_expression (void)
   for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
     {
       const char *expr = test_expressions[i].expr;
+      const char *desc = test_expressions[i].desc;
       GError *error = NULL;
 
-      g_assert (json_path_compile (path, expr, &error));
-      g_assert_no_error (error);
+      if (g_test_verbose ())
+        {
+          g_print ("* expr[%02d]: %s ('%s')\n",
+                   i, desc, expr);
+        }
+
+      if (test_expressions[i].is_valid)
+        {
+          g_assert (json_path_compile (path, expr, &error));
+          g_assert_no_error (error);
+        }
+      else
+        {
+          JsonPathError code = test_expressions[i].error_code;
+
+          g_assert (!json_path_compile (path, expr, &error));
+          g_assert_error (error, JSON_PATH_ERROR, code);
+        }
     }
 
   g_object_unref (path);
@@ -143,6 +176,9 @@ test_match (void)
       if (res == NULL || *res == '\0')
         continue;
 
+      if (!test_expressions[i].is_valid)
+        continue;
+
       g_assert (json_path_compile (path, expr, NULL));
 
       matches = json_path_match (path, root);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]