[json-glib] tests: Add negative tests



commit c22c1151e418af9506547804d4bc9fddb60bc0f1
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Mon Mar 1 17:42:41 2010 +0000

    tests: Add negative tests
    
    Verify that invalid JSON will trigger a parser error.

 json-glib/tests/parser-test.c |   53 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 53 insertions(+), 0 deletions(-)
---
diff --git a/json-glib/tests/parser-test.c b/json-glib/tests/parser-test.c
index d722f67..2f72d85 100644
--- a/json-glib/tests/parser-test.c
+++ b/json-glib/tests/parser-test.c
@@ -108,6 +108,18 @@ static const struct
   { "{ \"test\" : \"foo \\u00e8\" }", "test", "foo è" }
 };
 
+static const struct
+{
+  const gchar *str;
+} test_invalid[] = {
+  { "test" },
+  { "[ foo, ]" },
+  { "[ true, ]" },
+  { "{ \"foo\" : true \"bar\" : false }" },
+  { "[ true, [ false, ] ]" },
+  { "{ \"foo\" : { \"bar\" : false, } }" }
+};
+
 static guint n_test_base_values    = G_N_ELEMENTS (test_base_values);
 static guint n_test_simple_arrays  = G_N_ELEMENTS (test_simple_arrays);
 static guint n_test_nested_arrays  = G_N_ELEMENTS (test_nested_arrays);
@@ -115,6 +127,7 @@ static guint n_test_simple_objects = G_N_ELEMENTS (test_simple_objects);
 static guint n_test_nested_objects = G_N_ELEMENTS (test_nested_objects);
 static guint n_test_assignments    = G_N_ELEMENTS (test_assignments);
 static guint n_test_unicode        = G_N_ELEMENTS (test_unicode);
+static guint n_test_invalid        = G_N_ELEMENTS (test_invalid);
 
 static void
 test_empty (void)
@@ -257,6 +270,9 @@ test_simple_array (void)
     {
       GError *error = NULL;
 
+      if (g_test_verbose ())
+        g_print ("Parsing: '%s'\n", test_simple_arrays[i].str);
+
       if (!json_parser_load_from_data (parser, test_simple_arrays[i].str, -1, &error))
         {
           if (g_test_verbose ())
@@ -610,6 +626,42 @@ test_unicode_escape (void)
   g_object_unref (parser);
 }
 
+static void
+test_invalid_json (void)
+{
+  JsonParser *parser;
+  GError *error = NULL;
+  gint i;
+
+  parser = json_parser_new ();
+  g_assert (JSON_IS_PARSER (parser));
+
+  if (g_test_verbose ())
+    g_print ("checking json_parser_load_from_data with invalid data...\n");
+
+  for (i = 0; i < n_test_invalid; i++)
+    {
+      gboolean res;
+
+      if (g_test_verbose ())
+        g_print ("Parsing: '%s'\n", test_invalid[i].str);
+
+      res = json_parser_load_from_data (parser, test_invalid[i].str, -1,
+                                        &error);
+
+      g_assert (!res);
+      g_assert (error != NULL);
+      g_assert (error->domain == JSON_PARSER_ERROR);
+
+      if (g_test_verbose ())
+        g_print ("Error: %s\n", error->message);
+
+      g_clear_error (&error);
+    }
+
+  g_object_unref (parser);
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -627,6 +679,7 @@ main (int   argc,
   g_test_add_func ("/parser/nested-object", test_nested_object);
   g_test_add_func ("/parser/assignment", test_assignment);
   g_test_add_func ("/parser/unicode-escape", test_unicode_escape);
+  g_test_add_func ("/parser/invalid-json", test_invalid_json);
 
   return g_test_run ();
 }



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