[json-glib] tests: Improve coverage



commit 4fe6c5977d95c37c693d4f0b1d2dd90ae564b8b3
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Fri Oct 26 16:40:59 2012 +0100

    tests: Improve coverage

 json-glib/tests/array.c     |    4 ++
 json-glib/tests/builder.c   |    9 +++++-
 json-glib/tests/generator.c |   69 ++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 80 insertions(+), 2 deletions(-)
---
diff --git a/json-glib/tests/array.c b/json-glib/tests/array.c
index eab67d6..f0aa195 100644
--- a/json-glib/tests/array.c
+++ b/json-glib/tests/array.c
@@ -60,6 +60,10 @@ test_add_element (void)
   json_array_add_object_element (array, json_object_new ());
   g_assert (json_array_get_object_element (array, 7) != NULL);
 
+  json_array_add_object_element (array, NULL);
+  g_assert (json_array_get_object_element (array, 8) == NULL);
+  g_assert (json_array_get_null_element (array, 8));
+
   json_array_unref (array);
 }
 
diff --git a/json-glib/tests/builder.c b/json-glib/tests/builder.c
index 9bf2bd8..b93ab7f 100644
--- a/json-glib/tests/builder.c
+++ b/json-glib/tests/builder.c
@@ -6,7 +6,7 @@
 
 #include <json-glib/json-glib.h>
 
-static const gchar *complex_object = "{\"depth1\":[1,{\"depth2\":[3,[null],\"after array\"],\"value2\":true}],\"object1\":{}}";
+static const gchar *complex_object = "{\"depth1\":[1,{\"depth2\":[3,[null,false],\"after array\"],\"value2\":true}],\"object1\":{},\"value3\":null,\"value4\":42}";
 
 static const gchar *empty_object = "{\"a\":{}}";
 
@@ -36,6 +36,7 @@ test_builder_complex (void)
 
   json_builder_begin_array (builder);
   json_builder_add_null_value (builder);
+  json_builder_add_boolean_value (builder, FALSE);
   json_builder_end_array (builder);
 
   json_builder_add_string_value (builder, "after array");
@@ -51,6 +52,12 @@ test_builder_complex (void)
   json_builder_begin_object (builder);
   json_builder_end_object (builder);
 
+  json_builder_set_member_name (builder, "value3");
+  json_builder_add_null_value (builder);
+
+  json_builder_set_member_name (builder, "value4");
+  json_builder_add_int_value (builder, 42);
+
   json_builder_end_object (builder);
 
   node = json_builder_get_root (builder);
diff --git a/json-glib/tests/generator.c b/json-glib/tests/generator.c
index 5cc8b92..eb4cb78 100644
--- a/json-glib/tests/generator.c
+++ b/json-glib/tests/generator.c
@@ -35,6 +35,27 @@ static const gchar *nested_object =
   "}"
 "}";
 
+static const char *pretty_examples[] = {
+ "[\n]",
+
+ "{\n}",
+
+ "[\n"
+ "\ttrue,\n"
+ "\tfalse,\n"
+ "\tnull,\n"
+ "\t\"hello\"\n"
+ "]",
+
+ "{\n"
+ "\t\"foo\" : 42,\n"
+ "\t\"bar\" : true,\n"
+ "\t\"baz\" : null\n"
+ "}",
+};
+
+static const int n_pretty_examples = G_N_ELEMENTS (pretty_examples);
+
 static const struct {
   const gchar *lang;
   const gchar *sep;
@@ -58,13 +79,17 @@ test_empty_array (void)
   json_node_take_array (root, json_array_new ());
 
   json_generator_set_root (gen, root);
-  g_object_set (gen, "pretty", FALSE, NULL);
+  g_object_set (gen, "pretty", FALSE, "indent", 0, "indent-char", ' ', NULL);
 
   data = json_generator_to_data (gen, &len);
 
   g_assert_cmpint (len, ==, strlen (empty_array));
   g_assert_cmpstr (data, ==, empty_array);
 
+  g_assert (json_generator_get_pretty (gen) == FALSE);
+  g_assert_cmpint (json_generator_get_indent (gen), ==, 0);
+  g_assert_cmpint (json_generator_get_indent_char (gen), ==, ' ');
+
   g_free (data);
   json_node_free (root);
   g_object_unref (gen);
@@ -311,6 +336,47 @@ test_decimal_separator (void)
   json_node_free (node);
 }
 
+static void
+test_pretty (void)
+{
+  JsonParser *parser = json_parser_new ();
+  JsonGenerator *generator = json_generator_new ();
+  int i;
+
+  json_generator_set_pretty (generator, TRUE);
+  json_generator_set_indent (generator, 1);
+  json_generator_set_indent_char (generator, '\t');
+
+  for (i = 0; i < n_pretty_examples; i++)
+    {
+      JsonNode *root;
+      char *data;
+      gsize len;
+
+      g_assert (json_parser_load_from_data (parser, pretty_examples[i], -1, NULL));
+
+      root = json_parser_get_root (parser);
+      g_assert (root != NULL);
+
+      json_generator_set_root (generator, root);
+
+      data = json_generator_to_data (generator, &len);
+
+      if (g_test_verbose ())
+        g_print ("** checking pretty printing:\n%s\n** expected:\n%s\n",
+                 data,
+                 pretty_examples[i]);
+
+      g_assert_cmpint (len, ==, strlen (pretty_examples[i]));
+      g_assert_cmpstr (data, ==, pretty_examples[i]);
+
+      g_free (data);
+    }
+
+  g_object_unref (generator);
+  g_object_unref (parser);
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -325,6 +391,7 @@ main (int   argc,
   g_test_add_func ("/generator/simple-object", test_simple_object);
   g_test_add_func ("/generator/nested-object", test_nested_object);
   g_test_add_func ("/generator/decimal-separator", test_decimal_separator);
+  g_test_add_func ("/generator/pretty", test_pretty);
 
   return g_test_run ();
 }



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