10 #include <json-glib/json-glib.h>
12 static const gchar *test_empty_string = "";
13 static const gchar *test_empty_array_string = "[ ]";
14 static const gchar *test_empty_object_string = "{ }";
17 verify_int_value (JsonNode *node)
19 g_assert_cmpint (42, ==, json_node_get_int (node));
23 verify_boolean_value (JsonNode *node)
25 g_assert_cmpint (TRUE, ==, json_node_get_boolean (node));
29 verify_string_value (JsonNode *node)
31 g_assert_cmpstr ("string", ==, json_node_get_string (node));
35 verify_double_value (JsonNode *node)
37 g_assert_cmpfloat (10.2e3, ==, json_node_get_double (node));
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 }
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 }
65 static const gchar *test_nested_arrays[] = {
67 "[ [ ], [ true, [ true ] ] ]",
68 "[ [ false, true, 42 ], [ true, false, 3.14 ], \"test\" ]",
70 "[ false, { \"test\" : 42 } ]",
71 "[ { \"test\" : 42 }, null ]",
72 "[ true, { \"test\" : 42 }, null ]",
73 "[ { \"channel\" : \"/meta/connect\" } ]"
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 }
90 static const gchar *test_nested_objects[] = {
91 "{ \"array\" : [ false, \"foo\" ], \"object\" : { \"foo\" : true } }",
93 "\"type\" : \"ClutterGroup\", "
97 "\"type\" : \"ClutterRectangle\", "
99 "{ \"type\" : \"ClutterText\", \"text\" : \"hello there\" }"
103 "\"type\" : \"ClutterGroup\", "
106 "{ \"type\" : \"ClutterText\", \"text\" : \"hello\" }"
113 static const struct {
116 } test_assignments[] = {
117 { "var foo = [ false, false, true ]", "foo" },
118 { "var bar = [ true, 42 ];", "bar" },
119 { "var baz = { \"foo\" : false }", "baz" }
128 { "{ \"test\" : \"foo \\u00e8\" }", "test", "foo รจ" }
134 JsonParserError code;
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 }
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);
159 GError *error = NULL;
161 parser = json_parser_new ();
162 g_assert (JSON_IS_PARSER (parser));
164 if (g_test_verbose ())
165 g_print ("checking json_parser_load_from_data with empty string...\n");
167 if (!json_parser_load_from_data (parser, test_empty_string, -1, &error))
169 if (g_test_verbose ())
170 g_print ("Error: %s\n", error->message);
171 g_error_free (error);
172 g_object_unref (parser);
177 if (g_test_verbose ())
178 g_print ("checking json_parser_get_root...\n");
180 g_assert (NULL == json_parser_get_root (parser));
183 g_object_unref (parser);
187 test_base_value (void)
192 parser = json_parser_new ();
193 g_assert (JSON_IS_PARSER (parser));
195 if (g_test_verbose ())
196 g_print ("checking json_parser_load_from_data with base-values...\n");
198 for (i = 0; i < n_test_base_values; i++)
200 GError *error = NULL;
202 if (!json_parser_load_from_data (parser, test_base_values[i].str, -1, &error))
204 if (g_test_verbose ())
205 g_print ("Error: %s\n", error->message);
207 g_error_free (error);
208 g_object_unref (parser);
215 g_assert (NULL != json_parser_get_root (parser));
217 root = json_parser_get_root (parser);
218 g_assert (root != NULL);
219 g_assert (json_node_get_parent (root) == NULL);
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);
228 if (test_base_values[i].verify_value)
229 test_base_values[i].verify_value (root);
233 g_object_unref (parser);
237 test_empty_array (void)
240 GError *error = NULL;
242 parser = json_parser_new ();
243 g_assert (JSON_IS_PARSER (parser));
245 if (g_test_verbose ())
246 g_print ("checking json_parser_load_from_data with empty array...\n");
248 if (!json_parser_load_from_data (parser, test_empty_array_string, -1, &error))
250 if (g_test_verbose ())
251 g_print ("Error: %s\n", error->message);
252 g_error_free (error);
253 g_object_unref (parser);
261 g_assert (NULL != json_parser_get_root (parser));
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);
269 array = json_node_get_array (root);
270 g_assert (array != NULL);
272 if (g_test_verbose ())
273 g_print ("checking array is empty...\n");
274 g_assert_cmpint (json_array_get_length (array), ==, 0);
277 g_object_unref (parser);
281 test_simple_array (void)
286 parser = json_parser_new ();
287 g_assert (JSON_IS_PARSER (parser));
289 if (g_test_verbose ())
290 g_print ("checking json_parser_load_from_data with simple arrays...\n");
292 for (i = 0; i < n_test_simple_arrays; i++)
294 GError *error = NULL;
296 if (g_test_verbose ())
297 g_print ("Parsing: '%s'\n", test_simple_arrays[i].str);
299 if (!json_parser_load_from_data (parser, test_simple_arrays[i].str, -1, &error))
301 if (g_test_verbose ())
302 g_print ("Error: %s\n", error->message);
304 g_error_free (error);
305 g_object_unref (parser);
310 JsonNode *root, *node;
313 g_assert (NULL != json_parser_get_root (parser));
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);
321 array = json_node_get_array (root);
322 g_assert (array != NULL);
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);
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);
341 g_object_unref (parser);
345 test_nested_array (void)
350 parser = json_parser_new ();
351 g_assert (JSON_IS_PARSER (parser));
353 if (g_test_verbose ())
354 g_print ("checking json_parser_load_from_data with nested arrays...\n");
356 for (i = 0; i < n_test_nested_arrays; i++)
358 GError *error = NULL;
360 if (!json_parser_load_from_data (parser, test_nested_arrays[i], -1, &error))
362 if (g_test_verbose ())
363 g_print ("Error: %s\n", error->message);
365 g_error_free (error);
366 g_object_unref (parser);
374 g_assert (NULL != json_parser_get_root (parser));
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);
382 array = json_node_get_array (root);
383 g_assert (array != NULL);
385 if (g_test_verbose ())
386 g_print ("checking array is not empty...\n");
387 g_assert_cmpint (json_array_get_length (array), >, 0);
391 g_object_unref (parser);
395 test_empty_object (void)
398 GError *error = NULL;
400 parser = json_parser_new ();
401 g_assert (JSON_IS_PARSER (parser));
403 if (g_test_verbose ())
404 g_print ("checking json_parser_load_from_data with empty object...\n");
406 if (!json_parser_load_from_data (parser, test_empty_object_string, -1, &error))
408 if (g_test_verbose ())
409 g_print ("Error: %s\n", error->message);
410 g_error_free (error);
411 g_object_unref (parser);
419 g_assert (NULL != json_parser_get_root (parser));
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);
428 object = json_node_get_object (root);
429 g_assert (object != NULL);
431 if (g_test_verbose ())
432 g_print ("checking object is empty...\n");
433 g_assert_cmpint (json_object_get_size (object), ==, 0);
436 g_object_unref (parser);
440 test_simple_object (void)
445 parser = json_parser_new ();
446 g_assert (JSON_IS_PARSER (parser));
448 if (g_test_verbose ())
449 g_print ("checking json_parser_load_from_data with simple objects...\n");
451 for (i = 0; i < n_test_simple_objects; i++)
453 GError *error = NULL;
455 if (!json_parser_load_from_data (parser, test_simple_objects[i].str, -1, &error))
457 if (g_test_verbose ())
458 g_print ("Error: %s\n", error->message);
460 g_error_free (error);
461 g_object_unref (parser);
466 JsonNode *root, *node;
469 g_assert (NULL != json_parser_get_root (parser));
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);
477 object = json_node_get_object (root);
478 g_assert (object != NULL);
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);
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);
497 g_object_unref (parser);
501 test_nested_object (void)
506 parser = json_parser_new ();
507 g_assert (JSON_IS_PARSER (parser));
509 if (g_test_verbose ())
510 g_print ("checking json_parser_load_from_data with nested objects...\n");
512 for (i = 0; i < n_test_nested_objects; i++)
514 GError *error = NULL;
516 if (!json_parser_load_from_data (parser, test_nested_objects[i], -1, &error))
518 if (g_test_verbose ())
519 g_print ("Error: %s\n", error->message);
521 g_error_free (error);
522 g_object_unref (parser);
530 g_assert (NULL != json_parser_get_root (parser));
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);
538 object = json_node_get_object (root);
539 g_assert (object != NULL);
541 if (g_test_verbose ())
542 g_print ("checking object is not empty...\n");
543 g_assert_cmpint (json_object_get_size (object), >, 0);
547 g_object_unref (parser);
551 test_assignment (void)
556 parser = json_parser_new ();
557 g_assert (JSON_IS_PARSER (parser));
559 if (g_test_verbose ())
560 g_print ("checking json_parser_load_from_data with assignments...\n");
562 for (i = 0; i < n_test_assignments; i++)
564 GError *error = NULL;
566 if (!json_parser_load_from_data (parser, test_assignments[i].str, -1, &error))
568 if (g_test_verbose ())
569 g_print ("Error: %s\n", error->message);
571 g_error_free (error);
572 g_object_unref (parser);
579 if (g_test_verbose ())
580 g_print ("checking assignment...\n");
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));
589 g_object_unref (parser);
593 test_unicode_escape (void)
598 parser = json_parser_new ();
599 g_assert (JSON_IS_PARSER (parser));
601 if (g_test_verbose ())
602 g_print ("checking json_parser_load_from_data with unicode escape...\n");
604 for (i = 0; i < n_test_unicode; i++)
606 GError *error = NULL;
608 if (!json_parser_load_from_data (parser, test_unicode[i].str, -1, &error))
610 if (g_test_verbose ())
611 g_print ("Error: %s\n", error->message);
613 g_error_free (error);
614 g_object_unref (parser);
619 JsonNode *root, *node;
622 g_assert (NULL != json_parser_get_root (parser));
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);
629 object = json_node_get_object (root);
630 g_assert (object != NULL);
632 if (g_test_verbose ())
633 g_print ("checking object is not empty...\n");
634 g_assert_cmpint (json_object_get_size (object), >, 0);
636 node = json_object_get_member (object, test_unicode[i].member);
637 g_assert_cmpint (JSON_NODE_TYPE (node), ==, JSON_NODE_VALUE);
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);
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));
649 g_object_unref (parser);
653 test_invalid_json (void)
656 GError *error = NULL;
659 parser = json_parser_new ();
660 g_assert (JSON_IS_PARSER (parser));
662 if (g_test_verbose ())
663 g_print ("checking json_parser_load_from_data with invalid data...\n");
665 for (i = 0; i < n_test_invalid; i++)
669 if (g_test_verbose ())
670 g_print ("Parsing: '%s'\n", test_invalid[i].str);
672 res = json_parser_load_from_data (parser, test_invalid[i].str, -1,
676 g_assert_error (error, JSON_PARSER_ERROR, test_invalid[i].code);
678 if (g_test_verbose ())
679 g_print ("Error: %s\n", error->message);
681 g_clear_error (&error);
684 g_object_unref (parser);
688 test_stream_sync (void)
692 GFileInputStream *stream;
693 GError *error = NULL;
696 parser = json_parser_new ();
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);
703 json_parser_load_from_stream (parser, G_INPUT_STREAM (stream), NULL, &error);
704 g_assert (error == NULL);
706 root = json_parser_get_root (parser);
707 g_assert (root != NULL);
708 g_assert (JSON_NODE_HOLDS_ARRAY (root));
710 g_object_unref (stream);
711 g_object_unref (file);
712 g_object_unref (parser);
716 on_load_complete (GObject *gobject,
717 GAsyncResult *result,
720 JsonParser *parser = JSON_PARSER (gobject);
721 GMainLoop *main_loop = user_data;
722 GError *error = NULL;
726 res = json_parser_load_from_stream_finish (parser, result, &error);
728 g_assert (error == NULL);
730 root = json_parser_get_root (parser);
731 g_assert (root != NULL);
732 g_assert (JSON_NODE_HOLDS_ARRAY (root));
734 g_main_loop_quit (main_loop);
738 test_stream_async (void)
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);
746 g_assert (error == NULL);
747 g_assert (stream != NULL);
749 main_loop = g_main_loop_new (NULL, FALSE);
751 json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream), NULL,
755 g_main_loop_run (main_loop);
757 g_main_loop_unref (main_loop);
758 g_object_unref (stream);
759 g_object_unref (file);
760 g_object_unref (parser);
768 g_test_init (&argc, &argv, NULL);
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);
784 return g_test_run ();