[json-glib] tests: Move the invalid JSON unit to its own file



commit 6ebe68eb539a119f99205593b0246399923daef7
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Thu Oct 25 17:10:39 2012 +0100

    tests: Move the invalid JSON unit to its own file
    
    Instead of putting it inside the parser unit test.

 json-glib/tests/Makefile.am |    1 +
 json-glib/tests/invalid.c   |   93 +++++++++++++++++++++++++++++++++++++++++++
 json-glib/tests/parser.c    |   52 ------------------------
 3 files changed, 94 insertions(+), 52 deletions(-)
---
diff --git a/json-glib/tests/Makefile.am b/json-glib/tests/Makefile.am
index ac39d76..9815b95 100644
--- a/json-glib/tests/Makefile.am
+++ b/json-glib/tests/Makefile.am
@@ -26,6 +26,7 @@ TEST_PROGS += \
 	builder			\
 	generator		\
 	gvariant		\
+	invalid			\
 	node			\
 	object			\
 	parser			\
diff --git a/json-glib/tests/invalid.c b/json-glib/tests/invalid.c
new file mode 100644
index 0000000..db9f69b
--- /dev/null
+++ b/json-glib/tests/invalid.c
@@ -0,0 +1,93 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <glib.h>
+
+#include <json-glib/json-glib.h>
+
+static void
+test_invalid_bareword (gconstpointer user_data)
+{
+  const char *json = user_data;
+  GError *error = NULL;
+  JsonParser *parser;
+  gboolean res;
+
+  parser = json_parser_new ();
+  g_assert (JSON_IS_PARSER (parser));
+
+  if (g_test_verbose ())
+    g_print ("invalid data: '%s'...", json);
+
+  res = json_parser_load_from_data (parser, json, -1, &error);
+
+  g_assert (!res);
+  g_assert_error (error, JSON_PARSER_ERROR, JSON_PARSER_ERROR_INVALID_BAREWORD);
+
+  if (g_test_verbose ())
+    g_print ("expected error: %s\n", error->message);
+
+  g_clear_error (&error);
+
+  g_object_unref (parser);
+}
+
+static const struct
+{
+  const char *path;
+  const char *json;
+  gpointer func;
+} test_invalid[] = {
+  /* bareword */
+  { "invalid-bareword-1", "rainbows", test_invalid_bareword },
+  { "invalid-bareword-2", "[ unicorns ]", test_invalid_bareword },
+  { "invalid-bareword-3", "{ \"foo\" : ponies }", test_invalid_bareword },
+  { "invalid-bareword-4", "[ 3, 2, 1, lift_off ]", test_invalid_bareword },
+};
+
+static guint n_test_invalid = G_N_ELEMENTS (test_invalid);
+
+#if 0
+static const struct
+{
+  const gchar *str;
+  JsonParserError code;
+} test_invalid[] = {
+  { "test", JSON_PARSER_ERROR_INVALID_BAREWORD },
+  { "[ foo, ]", JSON_PARSER_ERROR_INVALID_BAREWORD },
+  { "[ true, ]", JSON_PARSER_ERROR_TRAILING_COMMA },
+  { "{ \"foo\" : true \"bar\" : false }", JSON_PARSER_ERROR_MISSING_COMMA },
+  { "[ true, [ false, ] ]", JSON_PARSER_ERROR_TRAILING_COMMA },
+  { "{ \"foo\" : { \"bar\" : false, } }", JSON_PARSER_ERROR_TRAILING_COMMA },
+  { "[ { }, { }, { }, ]", JSON_PARSER_ERROR_TRAILING_COMMA },
+  { "{ \"foo\" false }", JSON_PARSER_ERROR_MISSING_COLON }
+};
+#endif
+
+int
+main (int   argc,
+      char *argv[])
+{
+  int i;
+
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  for (i = 0; i < n_test_invalid; i++)
+    {
+      char *test_path = g_strconcat ("/invalid/json/", test_invalid[i].path, NULL);
+
+      g_test_add_data_func_full (test_path,
+                                 (gpointer) test_invalid[i].json,
+                                 test_invalid[i].func,
+                                 NULL);
+
+      g_free (test_path);
+    }
+
+  return g_test_run ();
+}
diff --git a/json-glib/tests/parser.c b/json-glib/tests/parser.c
index d3f2404..2a1ee58 100644
--- a/json-glib/tests/parser.c
+++ b/json-glib/tests/parser.c
@@ -128,21 +128,6 @@ static const struct
   { "{ \"test\" : \"foo \\u00e8\" }", "test", "foo Ã" }
 };
 
-static const struct
-{
-  const gchar *str;
-  JsonParserError code;
-} test_invalid[] = {
-  { "test", JSON_PARSER_ERROR_INVALID_BAREWORD },
-  { "[ foo, ]", JSON_PARSER_ERROR_INVALID_BAREWORD },
-  { "[ true, ]", JSON_PARSER_ERROR_TRAILING_COMMA },
-  { "{ \"foo\" : true \"bar\" : false }", JSON_PARSER_ERROR_MISSING_COMMA },
-  { "[ true, [ false, ] ]", JSON_PARSER_ERROR_TRAILING_COMMA },
-  { "{ \"foo\" : { \"bar\" : false, } }", JSON_PARSER_ERROR_TRAILING_COMMA },
-  { "[ { }, { }, { }, ]", JSON_PARSER_ERROR_TRAILING_COMMA },
-  { "{ \"foo\" false }", JSON_PARSER_ERROR_MISSING_COLON }
-};
-
 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);
@@ -150,7 +135,6 @@ 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)
@@ -650,41 +634,6 @@ test_unicode_escape (void)
 }
 
 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 (error, JSON_PARSER_ERROR, test_invalid[i].code);
-
-      if (g_test_verbose ())
-        g_print ("Error: %s\n", error->message);
-
-      g_clear_error (&error);
-    }
-
-  g_object_unref (parser);
-}
-
-static void
 test_stream_sync (void)
 {
   JsonParser *parser;
@@ -777,7 +726,6 @@ 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);
   g_test_add_func ("/parser/stream-sync", test_stream_sync);
   g_test_add_func ("/parser/stream-async", test_stream_async);
 



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