Initial Import
[profile/ivi/json-glib.git] / json-glib / tests / parser.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include <glib.h>
9
10 #include <json-glib/json-glib.h>
11
12 static const gchar *test_empty_string = "";
13 static const gchar *test_empty_array_string = "[ ]";
14 static const gchar *test_empty_object_string = "{ }";
15
16 static void
17 verify_int_value (JsonNode *node)
18 {
19   g_assert_cmpint (42, ==, json_node_get_int (node));
20 }
21
22 static void
23 verify_boolean_value (JsonNode *node)
24 {
25   g_assert_cmpint (TRUE, ==, json_node_get_boolean (node));
26 }
27
28 static void
29 verify_string_value (JsonNode *node)
30 {
31   g_assert_cmpstr ("string", ==, json_node_get_string (node));
32 }
33
34 static void
35 verify_double_value (JsonNode *node)
36 {
37   g_assert_cmpfloat (10.2e3, ==, json_node_get_double (node));
38 }
39
40 static const struct {
41   const gchar *str;
42   JsonNodeType type;
43   GType gtype;
44   void (* verify_value) (JsonNode *node);
45 } test_base_values[] = {
46   { "null",       JSON_NODE_NULL,  G_TYPE_INVALID, NULL, },
47   { "42",         JSON_NODE_VALUE, G_TYPE_INT64,   verify_int_value },
48   { "true",       JSON_NODE_VALUE, G_TYPE_BOOLEAN, verify_boolean_value },
49   { "\"string\"", JSON_NODE_VALUE, G_TYPE_STRING,  verify_string_value },
50   { "10.2e3",     JSON_NODE_VALUE, G_TYPE_DOUBLE,  verify_double_value }
51 };
52
53 static const struct {
54   const gchar *str;
55   gint len;
56   gint element;
57   JsonNodeType type;
58   GType gtype;
59 } test_simple_arrays[] = {
60   { "[ true ]",                 1, 0, JSON_NODE_VALUE, G_TYPE_BOOLEAN },
61   { "[ true, false, null ]",    3, 2, JSON_NODE_NULL,  G_TYPE_INVALID },
62   { "[ 1, 2, 3.14, \"test\" ]", 4, 3, JSON_NODE_VALUE, G_TYPE_STRING  }
63 };
64
65 static const gchar *test_nested_arrays[] = {
66   "[ 42, [ ], null ]",
67   "[ [ ], [ true, [ true ] ] ]",
68   "[ [ false, true, 42 ], [ true, false, 3.14 ], \"test\" ]",
69   "[ true, { } ]",
70   "[ false, { \"test\" : 42 } ]",
71   "[ { \"test\" : 42 }, null ]",
72   "[ true, { \"test\" : 42 }, null ]",
73   "[ { \"channel\" : \"/meta/connect\" } ]"
74 };
75
76 static const struct {
77   const gchar *str;
78   gint size;
79   const gchar *member;
80   JsonNodeType type;
81   GType gtype;
82 } test_simple_objects[] = {
83   { "{ \"test\" : 42 }", 1, "test", JSON_NODE_VALUE, G_TYPE_INT64 },
84   { "{ \"name\" : \"\", \"state\" : 1 }", 2, "name", JSON_NODE_VALUE, G_TYPE_STRING },
85   { "{ \"foo\" : \"bar\", \"baz\" : null }", 2, "baz", JSON_NODE_NULL, G_TYPE_INVALID },
86   { "{ \"channel\" : \"/meta/connect\" }", 1, "channel", JSON_NODE_VALUE, G_TYPE_STRING },
87   { "{ \"halign\":0.5, \"valign\":0.5 }", 2, "valign", JSON_NODE_VALUE, G_TYPE_DOUBLE }
88 };
89
90 static const gchar *test_nested_objects[] = {
91   "{ \"array\" : [ false, \"foo\" ], \"object\" : { \"foo\" : true } }",
92   "{ "
93     "\"type\" : \"ClutterGroup\", "
94     "\"width\" : 1, "
95     "\"children\" : [ "
96       "{ "
97         "\"type\" : \"ClutterRectangle\", "
98         "\"children\" : [ "
99           "{ \"type\" : \"ClutterText\", \"text\" : \"hello there\" }"
100         "] "
101       "}, "
102       "{ "
103         "\"type\" : \"ClutterGroup\", "
104         "\"width\" : 1, "
105         "\"children\" : [ "
106           "{ \"type\" : \"ClutterText\", \"text\" : \"hello\" }"
107         "] "
108       "} "
109     "] "
110   "}"
111 };
112
113 static const struct {
114   const gchar *str;
115   const gchar *var;
116 } test_assignments[] = {
117   { "var foo = [ false, false, true ]", "foo" },
118   { "var bar = [ true, 42 ];", "bar" },
119   { "var baz = { \"foo\" : false }", "baz" }
120 };
121
122 static const struct
123 {
124   const gchar *str;
125   const gchar *member;
126   const gchar *match;
127 } test_unicode[] = {
128   { "{ \"test\" : \"foo \\u00e8\" }", "test", "foo รจ" }
129 };
130
131 static const struct
132 {
133   const gchar *str;
134   JsonParserError code;
135 } test_invalid[] = {
136   { "test", JSON_PARSER_ERROR_INVALID_BAREWORD },
137   { "[ foo, ]", JSON_PARSER_ERROR_INVALID_BAREWORD },
138   { "[ true, ]", JSON_PARSER_ERROR_TRAILING_COMMA },
139   { "{ \"foo\" : true \"bar\" : false }", JSON_PARSER_ERROR_MISSING_COMMA },
140   { "[ true, [ false, ] ]", JSON_PARSER_ERROR_TRAILING_COMMA },
141   { "{ \"foo\" : { \"bar\" : false, } }", JSON_PARSER_ERROR_TRAILING_COMMA },
142   { "[ { }, { }, { }, ]", JSON_PARSER_ERROR_TRAILING_COMMA },
143   { "{ \"foo\" false }", JSON_PARSER_ERROR_MISSING_COLON }
144 };
145
146 static guint n_test_base_values    = G_N_ELEMENTS (test_base_values);
147 static guint n_test_simple_arrays  = G_N_ELEMENTS (test_simple_arrays);
148 static guint n_test_nested_arrays  = G_N_ELEMENTS (test_nested_arrays);
149 static guint n_test_simple_objects = G_N_ELEMENTS (test_simple_objects);
150 static guint n_test_nested_objects = G_N_ELEMENTS (test_nested_objects);
151 static guint n_test_assignments    = G_N_ELEMENTS (test_assignments);
152 static guint n_test_unicode        = G_N_ELEMENTS (test_unicode);
153 static guint n_test_invalid        = G_N_ELEMENTS (test_invalid);
154
155 static void
156 test_empty (void)
157 {
158   JsonParser *parser;
159   GError *error = NULL;
160
161   parser = json_parser_new ();
162   g_assert (JSON_IS_PARSER (parser));
163
164   if (g_test_verbose ())
165     g_print ("checking json_parser_load_from_data with empty string...\n");
166
167   if (!json_parser_load_from_data (parser, test_empty_string, -1, &error))
168     {
169       if (g_test_verbose ())
170         g_print ("Error: %s\n", error->message);
171       g_error_free (error);
172       g_object_unref (parser);
173       exit (1);
174     }
175   else
176     {
177       if (g_test_verbose ())
178         g_print ("checking json_parser_get_root...\n");
179
180       g_assert (NULL == json_parser_get_root (parser));
181     }
182
183   g_object_unref (parser);
184 }
185
186 static void
187 test_base_value (void)
188 {
189   gint i;
190   JsonParser *parser;
191
192   parser = json_parser_new ();
193   g_assert (JSON_IS_PARSER (parser));
194
195   if (g_test_verbose ())
196     g_print ("checking json_parser_load_from_data with base-values...\n");
197
198   for (i = 0; i < n_test_base_values; i++)
199     {
200       GError *error = NULL;
201
202       if (!json_parser_load_from_data (parser, test_base_values[i].str, -1, &error))
203         {
204           if (g_test_verbose ())
205             g_print ("Error: %s\n", error->message);
206
207           g_error_free (error);
208           g_object_unref (parser);
209           exit (1);
210         }
211       else
212         {
213           JsonNode *root;
214
215           g_assert (NULL != json_parser_get_root (parser));
216
217           root = json_parser_get_root (parser);
218           g_assert (root != NULL);
219           g_assert (json_node_get_parent (root) == NULL);
220
221           if (g_test_verbose ())
222             g_print ("checking root node is of the desired type %s...\n",
223                      test_base_values[i].gtype == G_TYPE_INVALID ? "<null>"
224                                                                  : g_type_name (test_base_values[i].gtype));
225           g_assert_cmpint (JSON_NODE_TYPE (root), ==, test_base_values[i].type);
226           g_assert_cmpint (json_node_get_value_type (root), ==, test_base_values[i].gtype);
227
228           if (test_base_values[i].verify_value)
229             test_base_values[i].verify_value (root);
230        }
231     }
232
233   g_object_unref (parser);
234 }
235
236 static void
237 test_empty_array (void)
238 {
239   JsonParser *parser;
240   GError *error = NULL;
241
242   parser = json_parser_new ();
243   g_assert (JSON_IS_PARSER (parser));
244
245   if (g_test_verbose ())
246     g_print ("checking json_parser_load_from_data with empty array...\n");
247
248   if (!json_parser_load_from_data (parser, test_empty_array_string, -1, &error))
249     {
250       if (g_test_verbose ())
251         g_print ("Error: %s\n", error->message);
252       g_error_free (error);
253       g_object_unref (parser);
254       exit (1);
255     }
256   else
257     {
258       JsonNode *root;
259       JsonArray *array;
260
261       g_assert (NULL != json_parser_get_root (parser));
262
263       if (g_test_verbose ())
264         g_print ("checking root node is an array...\n");
265       root = json_parser_get_root (parser);
266       g_assert_cmpint (JSON_NODE_TYPE (root), ==, JSON_NODE_ARRAY);
267       g_assert (json_node_get_parent (root) == NULL);
268
269       array = json_node_get_array (root);
270       g_assert (array != NULL);
271
272       if (g_test_verbose ())
273         g_print ("checking array is empty...\n");
274       g_assert_cmpint (json_array_get_length (array), ==, 0);
275     }
276
277   g_object_unref (parser);
278 }
279
280 static void
281 test_simple_array (void)
282 {
283   gint i;
284   JsonParser *parser;
285
286   parser = json_parser_new ();
287   g_assert (JSON_IS_PARSER (parser));
288
289   if (g_test_verbose ())
290     g_print ("checking json_parser_load_from_data with simple arrays...\n");
291
292   for (i = 0; i < n_test_simple_arrays; i++)
293     {
294       GError *error = NULL;
295
296       if (g_test_verbose ())
297         g_print ("Parsing: '%s'\n", test_simple_arrays[i].str);
298
299       if (!json_parser_load_from_data (parser, test_simple_arrays[i].str, -1, &error))
300         {
301           if (g_test_verbose ())
302             g_print ("Error: %s\n", error->message);
303
304           g_error_free (error);
305           g_object_unref (parser);
306           exit (1);
307         }
308       else
309         {
310           JsonNode *root, *node;
311           JsonArray *array;
312
313           g_assert (NULL != json_parser_get_root (parser));
314
315           if (g_test_verbose ())
316             g_print ("checking root node is an array...\n");
317           root = json_parser_get_root (parser);
318           g_assert_cmpint (JSON_NODE_TYPE (root), ==, JSON_NODE_ARRAY);
319           g_assert (json_node_get_parent (root) == NULL);
320
321           array = json_node_get_array (root);
322           g_assert (array != NULL);
323
324           if (g_test_verbose ())
325             g_print ("checking array is of the desired length (%d)...\n",
326                      test_simple_arrays[i].len);
327           g_assert_cmpint (json_array_get_length (array), ==, test_simple_arrays[i].len);
328
329           if (g_test_verbose ())
330             g_print ("checking element %d is of the desired type %s...\n",
331                      test_simple_arrays[i].element,
332                      g_type_name (test_simple_arrays[i].gtype));
333           node = json_array_get_element (array, test_simple_arrays[i].element);
334           g_assert (node != NULL);
335           g_assert (json_node_get_parent (node) == root);
336           g_assert_cmpint (JSON_NODE_TYPE (node), ==, test_simple_arrays[i].type);
337           g_assert_cmpint (json_node_get_value_type (node), ==, test_simple_arrays[i].gtype);
338         }
339     }
340
341   g_object_unref (parser);
342 }
343
344 static void
345 test_nested_array (void)
346 {
347   gint i;
348   JsonParser *parser;
349
350   parser = json_parser_new ();
351   g_assert (JSON_IS_PARSER (parser));
352
353   if (g_test_verbose ())
354     g_print ("checking json_parser_load_from_data with nested arrays...\n");
355
356   for (i = 0; i < n_test_nested_arrays; i++)
357     {
358       GError *error = NULL;
359
360       if (!json_parser_load_from_data (parser, test_nested_arrays[i], -1, &error))
361         {
362           if (g_test_verbose ())
363             g_print ("Error: %s\n", error->message);
364
365           g_error_free (error);
366           g_object_unref (parser);
367           exit (1);
368         }
369       else
370         {
371           JsonNode *root;
372           JsonArray *array;
373
374           g_assert (NULL != json_parser_get_root (parser));
375
376           if (g_test_verbose ())
377             g_print ("checking root node is an array...\n");
378           root = json_parser_get_root (parser);
379           g_assert_cmpint (JSON_NODE_TYPE (root), ==, JSON_NODE_ARRAY);
380           g_assert (json_node_get_parent (root) == NULL);
381
382           array = json_node_get_array (root);
383           g_assert (array != NULL);
384
385           if (g_test_verbose ())
386             g_print ("checking array is not empty...\n");
387           g_assert_cmpint (json_array_get_length (array), >, 0);
388         }
389     }
390
391   g_object_unref (parser);
392 }
393
394 static void
395 test_empty_object (void)
396 {
397   JsonParser *parser;
398   GError *error = NULL;
399
400   parser = json_parser_new ();
401   g_assert (JSON_IS_PARSER (parser));
402
403   if (g_test_verbose ())
404     g_print ("checking json_parser_load_from_data with empty object...\n");
405
406   if (!json_parser_load_from_data (parser, test_empty_object_string, -1, &error))
407     {
408       if (g_test_verbose ())
409         g_print ("Error: %s\n", error->message);
410       g_error_free (error);
411       g_object_unref (parser);
412       exit (1);
413     }
414   else
415     {
416       JsonNode *root;
417       JsonObject *object;
418
419       g_assert (NULL != json_parser_get_root (parser));
420
421       if (g_test_verbose ())
422         g_print ("checking root node is an object...\n");
423       root = json_parser_get_root (parser);
424       g_assert (json_node_get_parent (root) == NULL);
425       g_assert_cmpint (JSON_NODE_TYPE (root), ==, JSON_NODE_OBJECT);
426       g_assert (json_node_get_parent (root) == NULL);
427
428       object = json_node_get_object (root);
429       g_assert (object != NULL);
430
431       if (g_test_verbose ())
432         g_print ("checking object is empty...\n");
433       g_assert_cmpint (json_object_get_size (object), ==, 0);
434     }
435
436   g_object_unref (parser);
437 }
438
439 static void
440 test_simple_object (void)
441 {
442   gint i;
443   JsonParser *parser;
444
445   parser = json_parser_new ();
446   g_assert (JSON_IS_PARSER (parser));
447
448   if (g_test_verbose ())
449     g_print ("checking json_parser_load_from_data with simple objects...\n");
450
451   for (i = 0; i < n_test_simple_objects; i++)
452     {
453       GError *error = NULL;
454
455       if (!json_parser_load_from_data (parser, test_simple_objects[i].str, -1, &error))
456         {
457           if (g_test_verbose ())
458             g_print ("Error: %s\n", error->message);
459
460           g_error_free (error);
461           g_object_unref (parser);
462           exit (1);
463         }
464       else
465         {
466           JsonNode *root, *node;
467           JsonObject *object;
468
469           g_assert (NULL != json_parser_get_root (parser));
470
471           if (g_test_verbose ())
472             g_print ("checking root node is an object...\n");
473           root = json_parser_get_root (parser);
474           g_assert_cmpint (JSON_NODE_TYPE (root), ==, JSON_NODE_OBJECT);
475           g_assert (json_node_get_parent (root) == NULL);
476
477           object = json_node_get_object (root);
478           g_assert (object != NULL);
479
480           if (g_test_verbose ())
481             g_print ("checking object is of the desired size (%d)...\n",
482                      test_simple_objects[i].size);
483           g_assert_cmpint (json_object_get_size (object), ==, test_simple_objects[i].size);
484
485           if (g_test_verbose ())
486             g_print ("checking member '%s' is of the desired type %s...\n",
487                      test_simple_objects[i].member,
488                      g_type_name (test_simple_objects[i].gtype));
489           node = json_object_get_member (object, test_simple_objects[i].member);
490           g_assert (node != NULL);
491           g_assert (json_node_get_parent (node) == root);
492           g_assert_cmpint (JSON_NODE_TYPE (node), ==, test_simple_objects[i].type);
493           g_assert_cmpint (json_node_get_value_type (node), ==, test_simple_objects[i].gtype);
494         }
495     }
496
497   g_object_unref (parser);
498 }
499
500 static void
501 test_nested_object (void)
502 {
503   gint i;
504   JsonParser *parser;
505
506   parser = json_parser_new ();
507   g_assert (JSON_IS_PARSER (parser));
508
509   if (g_test_verbose ())
510     g_print ("checking json_parser_load_from_data with nested objects...\n");
511
512   for (i = 0; i < n_test_nested_objects; i++)
513     {
514       GError *error = NULL;
515
516       if (!json_parser_load_from_data (parser, test_nested_objects[i], -1, &error))
517         {
518           if (g_test_verbose ())
519             g_print ("Error: %s\n", error->message);
520
521           g_error_free (error);
522           g_object_unref (parser);
523           exit (1);
524         }
525       else
526         {
527           JsonNode *root;
528           JsonObject *object;
529
530           g_assert (NULL != json_parser_get_root (parser));
531
532           if (g_test_verbose ())
533             g_print ("checking root node is an object...\n");
534           root = json_parser_get_root (parser);
535           g_assert_cmpint (JSON_NODE_TYPE (root), ==, JSON_NODE_OBJECT);
536           g_assert (json_node_get_parent (root) == NULL);
537
538           object = json_node_get_object (root);
539           g_assert (object != NULL);
540
541           if (g_test_verbose ())
542             g_print ("checking object is not empty...\n");
543           g_assert_cmpint (json_object_get_size (object), >, 0);
544         }
545     }
546
547   g_object_unref (parser);
548 }
549
550 static void
551 test_assignment (void)
552 {
553   gint i;
554   JsonParser *parser;
555
556   parser = json_parser_new ();
557   g_assert (JSON_IS_PARSER (parser));
558
559   if (g_test_verbose ())
560     g_print ("checking json_parser_load_from_data with assignments...\n");
561
562   for (i = 0; i < n_test_assignments; i++)
563     {
564       GError *error = NULL;
565
566       if (!json_parser_load_from_data (parser, test_assignments[i].str, -1, &error))
567         {
568           if (g_test_verbose ())
569             g_print ("Error: %s\n", error->message);
570
571           g_error_free (error);
572           g_object_unref (parser);
573           exit (1);
574         }
575       else
576         {
577           gchar *var;
578
579           if (g_test_verbose ())
580             g_print ("checking assignment...\n");
581
582           g_assert (json_parser_has_assignment (parser, &var) == TRUE);
583           g_assert (var != NULL);
584           g_assert_cmpstr (var, ==, test_assignments[i].var);
585           g_assert (NULL != json_parser_get_root (parser));
586         }
587     }
588
589   g_object_unref (parser);
590 }
591
592 static void
593 test_unicode_escape (void)
594 {
595   gint i;
596   JsonParser *parser;
597
598   parser = json_parser_new ();
599   g_assert (JSON_IS_PARSER (parser));
600
601   if (g_test_verbose ())
602     g_print ("checking json_parser_load_from_data with unicode escape...\n");
603
604   for (i = 0; i < n_test_unicode; i++)
605     {
606       GError *error = NULL;
607
608       if (!json_parser_load_from_data (parser, test_unicode[i].str, -1, &error))
609         {
610           if (g_test_verbose ())
611             g_print ("Error: %s\n", error->message);
612
613           g_error_free (error);
614           g_object_unref (parser);
615           exit (1);
616         }
617       else
618         {
619           JsonNode *root, *node;
620           JsonObject *object;
621
622           g_assert (NULL != json_parser_get_root (parser));
623
624           if (g_test_verbose ())
625             g_print ("checking root node is an object...\n");
626           root = json_parser_get_root (parser);
627           g_assert_cmpint (JSON_NODE_TYPE (root), ==, JSON_NODE_OBJECT);
628
629           object = json_node_get_object (root);
630           g_assert (object != NULL);
631
632           if (g_test_verbose ())
633             g_print ("checking object is not empty...\n");
634           g_assert_cmpint (json_object_get_size (object), >, 0);
635
636           node = json_object_get_member (object, test_unicode[i].member);
637           g_assert_cmpint (JSON_NODE_TYPE (node), ==, JSON_NODE_VALUE);
638
639           if (g_test_verbose ())
640             g_print ("checking simple string equality...\n");
641           g_assert_cmpstr (json_node_get_string (node), ==, test_unicode[i].match);
642
643           if (g_test_verbose ())
644             g_print ("checking for valid UTF-8...\n");
645           g_assert (g_utf8_validate (json_node_get_string (node), -1, NULL));
646         }
647     }
648
649   g_object_unref (parser);
650 }
651
652 static void
653 test_invalid_json (void)
654 {
655   JsonParser *parser;
656   GError *error = NULL;
657   gint i;
658
659   parser = json_parser_new ();
660   g_assert (JSON_IS_PARSER (parser));
661
662   if (g_test_verbose ())
663     g_print ("checking json_parser_load_from_data with invalid data...\n");
664
665   for (i = 0; i < n_test_invalid; i++)
666     {
667       gboolean res;
668
669       if (g_test_verbose ())
670         g_print ("Parsing: '%s'\n", test_invalid[i].str);
671
672       res = json_parser_load_from_data (parser, test_invalid[i].str, -1,
673                                         &error);
674
675       g_assert (!res);
676       g_assert_error (error, JSON_PARSER_ERROR, test_invalid[i].code);
677
678       if (g_test_verbose ())
679         g_print ("Error: %s\n", error->message);
680
681       g_clear_error (&error);
682     }
683
684   g_object_unref (parser);
685 }
686
687 static void
688 test_stream_sync (void)
689 {
690   JsonParser *parser;
691   GFile *file;
692   GFileInputStream *stream;
693   GError *error = NULL;
694   JsonNode *root;
695
696   parser = json_parser_new ();
697
698   file = g_file_new_for_path (TESTS_DATA_DIR "/stream-load.json");
699   stream = g_file_read (file, NULL, &error);
700   g_assert (error == NULL);
701   g_assert (stream != NULL);
702
703   json_parser_load_from_stream (parser, G_INPUT_STREAM (stream), NULL, &error);
704   g_assert (error == NULL);
705
706   root = json_parser_get_root (parser);
707   g_assert (root != NULL);
708   g_assert (JSON_NODE_HOLDS_ARRAY (root));
709
710   g_object_unref (stream);
711   g_object_unref (file);
712   g_object_unref (parser);
713 }
714
715 static void
716 on_load_complete (GObject      *gobject,
717                   GAsyncResult *result,
718                   gpointer      user_data)
719 {
720   JsonParser *parser = JSON_PARSER (gobject);
721   GMainLoop *main_loop = user_data;
722   GError *error = NULL;
723   JsonNode *root;
724   gboolean res;
725
726   res = json_parser_load_from_stream_finish (parser, result, &error);
727   g_assert (res);
728   g_assert (error == NULL);
729
730   root = json_parser_get_root (parser);
731   g_assert (root != NULL);
732   g_assert (JSON_NODE_HOLDS_ARRAY (root));
733
734   g_main_loop_quit (main_loop);
735 }
736
737 static void
738 test_stream_async (void)
739 {
740   GMainLoop *main_loop;
741   GError *error = NULL;
742   JsonParser *parser = json_parser_new ();
743   GFile *file = g_file_new_for_path (TESTS_DATA_DIR "/stream-load.json");
744   GFileInputStream *stream = g_file_read (file, NULL, &error);
745
746   g_assert (error == NULL);
747   g_assert (stream != NULL);
748
749   main_loop = g_main_loop_new (NULL, FALSE);
750
751   json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream), NULL,
752                                       on_load_complete,
753                                       main_loop);
754
755   g_main_loop_run (main_loop);
756
757   g_main_loop_unref (main_loop);
758   g_object_unref (stream);
759   g_object_unref (file);
760   g_object_unref (parser);
761 }
762
763 int
764 main (int   argc,
765       char *argv[])
766 {
767   g_type_init ();
768   g_test_init (&argc, &argv, NULL);
769
770   g_test_add_func ("/parser/empty-string", test_empty);
771   g_test_add_func ("/parser/base-value", test_base_value);
772   g_test_add_func ("/parser/empty-array", test_empty_array);
773   g_test_add_func ("/parser/simple-array", test_simple_array);
774   g_test_add_func ("/parser/nested-array", test_nested_array);
775   g_test_add_func ("/parser/empty-object", test_empty_object);
776   g_test_add_func ("/parser/simple-object", test_simple_object);
777   g_test_add_func ("/parser/nested-object", test_nested_object);
778   g_test_add_func ("/parser/assignment", test_assignment);
779   g_test_add_func ("/parser/unicode-escape", test_unicode_escape);
780   g_test_add_func ("/parser/invalid-json", test_invalid_json);
781   g_test_add_func ("/parser/stream-sync", test_stream_sync);
782   g_test_add_func ("/parser/stream-async", test_stream_async);
783
784   return g_test_run ();
785 }