summaryrefslogtreecommitdiff
path: root/json-glib/tests/array-test.c
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@linux.intel.com>2009-05-17 19:44:41 +0100
committerEmmanuele Bassi <ebassi@linux.intel.com>2009-05-17 19:44:41 +0100
commit930fdf4c4dff1f5310a946c2a9f5b6860f7c8ba8 (patch)
treeea8fbd8f41fce7bbb41069817630d5af90ed349d /json-glib/tests/array-test.c
parent3057a1722e27a13b39ddec4754fb6abda1aea199 (diff)
downloadjson-glib-930fdf4c4dff1f5310a946c2a9f5b6860f7c8ba8.tar.gz
Add JsonArray iteration function
Similarly to commit 3057a172 for JsonObject, the newly added json_array_foreach_element() iterates over a JSON array data type.
Diffstat (limited to 'json-glib/tests/array-test.c')
-rw-r--r--json-glib/tests/array-test.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/json-glib/tests/array-test.c b/json-glib/tests/array-test.c
index f091ecd..3d3bf20 100644
--- a/json-glib/tests/array-test.c
+++ b/json-glib/tests/array-test.c
@@ -47,6 +47,53 @@ test_remove_element (void)
json_array_unref (array);
}
+typedef struct _TestForeachFixture
+{
+ gint n_elements;
+} TestForeachFixture;
+
+static const struct {
+ JsonNodeType element_type;
+ GType element_gtype;
+} type_verify[] = {
+ { JSON_NODE_VALUE, G_TYPE_INT },
+ { JSON_NODE_VALUE, G_TYPE_BOOLEAN },
+ { JSON_NODE_VALUE, G_TYPE_STRING },
+ { JSON_NODE_NULL, G_TYPE_INVALID }
+};
+
+static void
+verify_foreach (JsonArray *array,
+ guint index_,
+ JsonNode *element_node,
+ gpointer user_data)
+{
+ TestForeachFixture *fixture = user_data;
+
+ g_assert (json_node_get_node_type (element_node) == type_verify[index_].element_type);
+ g_assert (json_node_get_value_type (element_node) == type_verify[index_].element_gtype);
+
+ fixture->n_elements += 1;
+}
+
+static void
+test_foreach_element (void)
+{
+ JsonArray *array = json_array_new ();
+ TestForeachFixture fixture = { 0, };
+
+ json_array_add_int_element (array, 42);
+ json_array_add_boolean_element (array, TRUE);
+ json_array_add_string_element (array, "hello");
+ json_array_add_null_element (array);
+
+ json_array_foreach_element (array, verify_foreach, &fixture);
+
+ g_assert_cmpint (fixture.n_elements, ==, json_array_get_length (array));
+
+ json_array_unref (array);
+}
+
int
main (int argc,
char *argv[])
@@ -57,6 +104,7 @@ main (int argc,
g_test_add_func ("/array/empty-array", test_empty_array);
g_test_add_func ("/array/add-element", test_add_element);
g_test_add_func ("/array/remove-element", test_remove_element);
+ g_test_add_func ("/array/foreach-element", test_foreach_element);
return g_test_run ();
}