[json-glib] path: Fix get all object members with wildcard



commit a9bc17dc04b425888b40b5ecef6baefcf369ec84
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Fri Jan 11 17:47:13 2013 +0000

    path: Fix get all object members with wildcard
    
    Similar to the fix that went in commit path e348b1fa, we need to fix
    getting all the members of an object by using the wildcard notation.

 json-glib/json-path.c  |    2 +-
 json-glib/tests/path.c |   28 +++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 6 deletions(-)
---
diff --git a/json-glib/json-path.c b/json-glib/json-path.c
index fc8d52c..4fb61bc 100644
--- a/json-glib/json-path.c
+++ b/json-glib/json-path.c
@@ -853,7 +853,7 @@ walk_path_node (GList      *path,
               else
                 {
                   JSON_NOTE (PATH, "glob match member '%s'", (char *) l->data);
-                  json_array_add_element (results, json_node_copy (root));
+                  json_array_add_element (results, json_node_copy (member));
                 }
             }
           g_list_free (members);
diff --git a/json-glib/tests/path.c b/json-glib/tests/path.c
index 712b65d..2b7c1f2 100644
--- a/json-glib/tests/path.c
+++ b/json-glib/tests/path.c
@@ -36,53 +36,70 @@ static const char *test_json =
 "}";
 
 static const struct {
-  const char *exp;
+  const char *desc;
+  const char *expr;
   const char *res;
 } test_expressions[] = {
   {
+    "Title of the first book in the store, using objct notation.",
     "$.store.book[0].title",
     "[\"Sayings of the Century\"]"
   },
   {
+    "Title of the first book in the store, using array notation.",
     "$['store']['book'][0]['title']",
     "[\"Sayings of the Century\"]"
   },
   {
+    "All the authors from the every book.",
     "$.store.book[*].author",
     "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
   },
   {
+    "All the authors.",
     "$..author",
     "[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
   },
   {
+    "Everything inside the store.",
     "$.store.*",
     NULL
   },
   {
+    "All the prices in the store.",
     "$.store..price",
     "[\"8.95\",\"12.99\",\"8.99\",\"22.99\",\"19.95\"]"
   },
   {
+    "The third book.",
     "$..book[2]",
     "[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":\"8.99\"}]"
   },
   {
+    "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\"}]"
   },
   {
+    "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\"}]"
   },
   {
+    "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\"}]"
   },
   {
+    "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\"}]"
   },
+  {
+    "All the members of the bicycle object.",
+    "$.store.bicycle.*",
+    "[\"red\",\"19.95\"]"
+  },
 };
 
 static void
@@ -93,7 +110,7 @@ test_expression (void)
 
   for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
     {
-      const char *expr = test_expressions[i].exp;
+      const char *expr = test_expressions[i].expr;
       GError *error = NULL;
 
       g_assert (json_path_compile (path, expr, &error));
@@ -117,7 +134,8 @@ test_match (void)
 
   for (i = 0; i < G_N_ELEMENTS (test_expressions); i++)
     {
-      const char *expr = test_expressions[i].exp;
+      const char *desc = test_expressions[i].desc;
+      const char *expr = test_expressions[i].expr;
       const char *res  = test_expressions[i].res;
       JsonNode *matches;
       char *str;
@@ -135,10 +153,10 @@ test_match (void)
 
       if (g_test_verbose ())
         {
-          g_print ("* expr[%02d]: '%s' =>\n"
+          g_print ("* expr[%02d]: %s ('%s') =>\n"
                    "- result:   %s\n"
                    "- expected: %s\n",
-                   i, expr, str, res);
+                   i, desc, expr, str, res);
         }
 
       g_assert_cmpstr (str, ==, res);



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