Wireless Daemon for Linux
 help / color / mirror / Atom feed
* [PATCH v2 3/9] unit: add primitive json test
@ 2022-01-06 19:50 James Prestwood
  0 siblings, 0 replies; 2+ messages in thread
From: James Prestwood @ 2022-01-06 19:50 UTC (permalink / raw)
  To: iwd 

[-- Attachment #1: Type: text/plain, Size: 4739 bytes --]

---
 unit/test-json.c | 111 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 95 insertions(+), 16 deletions(-)

diff --git a/unit/test-json.c b/unit/test-json.c
index 854b92dd..fe8f756f 100644
--- a/unit/test-json.c
+++ b/unit/test-json.c
@@ -84,8 +84,6 @@ static void test_json_unsupported_types(const void *data)
 	 * Valid JSON objects but currently unsupported types
 	 */
 	char arrays[] = "{\"test\":[1, 2, 3, 4]}";
-	char integers[] = "{\"test\": 10 }";
-	char bools[] = "{\"test\": true}";
 
 	struct json_iter iter;
 	struct json_contents *c = json_contents_new(arrays, strlen(arrays));
@@ -95,20 +93,6 @@ static void test_json_unsupported_types(const void *data)
 				JSON_MANDATORY("test", JSON_ARRAY, NULL),
 				JSON_UNDEFINED));
 	json_contents_free(c);
-
-	c = json_contents_new(integers, strlen(integers));
-	json_iter_init(&iter, c);
-	assert(!json_iter_parse(&iter,
-				JSON_MANDATORY("test", JSON_PRIMITIVE, NULL),
-				JSON_UNDEFINED));
-	json_contents_free(c);
-
-	c = json_contents_new(bools, strlen(bools));
-	json_iter_init(&iter, c);
-	assert(!json_iter_parse(&iter,
-				JSON_MANDATORY("test", JSON_PRIMITIVE, NULL),
-				JSON_UNDEFINED));
-	json_contents_free(c);
 }
 
 /*
@@ -248,6 +232,100 @@ static void test_json_larger_object(const void *data)
 	json_contents_free(c);
 }
 
+static void check_primitives(struct json_iter *i, struct json_iter *ui,
+				struct json_iter *t, struct json_iter *f,
+				struct json_iter *null, struct json_iter *obj)
+{
+
+	int i_val;
+	unsigned int ui_val;
+	bool b_val;
+
+	assert(json_iter_is_valid(i));
+	assert(!json_iter_get_uint(i, NULL));
+	assert(!json_iter_get_boolean(i, NULL));
+	assert(!json_iter_get_null(i));
+	assert(json_iter_get_int(i, &i_val));
+	assert(i_val == -10);
+
+	assert(json_iter_is_valid(ui));
+	assert(!json_iter_get_boolean(ui, NULL));
+	assert(!json_iter_get_null(ui));
+	assert(json_iter_get_int(ui, &i_val));
+	assert(json_iter_get_uint(ui, &ui_val));
+	assert(i_val == 10 && ui_val == 10);
+
+
+	assert(json_iter_is_valid(t));
+	assert(!json_iter_get_null(t));
+	assert(!json_iter_get_int(t, NULL));
+	assert(!json_iter_get_uint(t, NULL));
+	assert(json_iter_get_boolean(t, &b_val));
+	assert(b_val == true);
+
+	assert(json_iter_is_valid(f));
+	assert(!json_iter_get_null(f));
+	assert(!json_iter_get_int(f, NULL));
+	assert(!json_iter_get_uint(f, NULL));
+	assert(json_iter_get_boolean(f, &b_val));
+	assert(b_val == false);
+
+	assert(json_iter_is_valid(null));
+	assert(!json_iter_get_int(null, NULL));
+	assert(!json_iter_get_uint(null, NULL));
+	assert(!json_iter_get_boolean(null, NULL));
+	assert(json_iter_get_null(null));
+
+	if (obj) {
+		assert(json_iter_is_valid(obj));
+		assert(json_iter_parse(obj,
+			JSON_MANDATORY("null_val", JSON_PRIMITIVE, null),
+			JSON_MANDATORY("false_val", JSON_PRIMITIVE, f),
+			JSON_MANDATORY("true_val", JSON_PRIMITIVE, t),
+			JSON_MANDATORY("int_val", JSON_PRIMITIVE, i),
+			JSON_MANDATORY("uint_val", JSON_PRIMITIVE, ui),
+			JSON_UNDEFINED));
+
+		check_primitives(i, ui, t, f, null, NULL);
+	}
+}
+
+static void test_json_primitives(const void *data)
+{
+	char json[] = "{\"int_val\": -10,"
+			"\"uint_val\": 10,"
+			"\"true_val\": true,"
+			"\"false_val\": false,"
+			"\"null_val\": null,"
+			"\"obj_val\":{"
+				"\"int_val\": -10,"
+				"\"uint_val\": 10,"
+				"\"true_val\": true,"
+				"\"false_val\": false,"
+				"\"null_val\": null}}";
+	struct json_contents *c = json_contents_new(json, strlen(json));
+	struct json_iter outer, inner, null, f, t, i, ui;
+	struct json_iter not_found;
+
+	json_iter_init(&outer, c);
+	assert(json_iter_parse(&outer,
+			JSON_MANDATORY("obj_val", JSON_OBJECT, &inner),
+			JSON_MANDATORY("null_val", JSON_PRIMITIVE, &null),
+			JSON_MANDATORY("false_val", JSON_PRIMITIVE, &f),
+			JSON_MANDATORY("true_val", JSON_PRIMITIVE, &t),
+			JSON_MANDATORY("int_val", JSON_PRIMITIVE, &i),
+			JSON_MANDATORY("uint_val", JSON_PRIMITIVE, &ui),
+			JSON_OPTIONAL("not_found", JSON_PRIMITIVE, &not_found),
+			JSON_UNDEFINED));
+
+	assert(!json_iter_is_valid(&not_found));
+
+	check_primitives(&i, &ui, &t, &f, &null, &inner);
+
+	json_contents_free(c);
+}
+
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -259,6 +337,7 @@ int main(int argc, char *argv[])
 	l_test_add("json empty objects", test_json_empty_objects, NULL);
 	l_test_add("json parse out of order", test_json_out_of_order, NULL);
 	l_test_add("json larger object", test_json_larger_object, NULL);
+	l_test_add("json test primitives", test_json_primitives, NULL);
 
 	return l_test_run();
 }
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread
* Re: [PATCH v2 3/9] unit: add primitive json test
@ 2022-01-06 20:08 Denis Kenzior
  0 siblings, 0 replies; 2+ messages in thread
From: Denis Kenzior @ 2022-01-06 20:08 UTC (permalink / raw)
  To: iwd 

[-- Attachment #1: Type: text/plain, Size: 299 bytes --]

Hi James,

On 1/6/22 13:50, James Prestwood wrote:
> ---
>   unit/test-json.c | 111 ++++++++++++++++++++++++++++++++++++++++-------
>   1 file changed, 95 insertions(+), 16 deletions(-)
> 

I removed some double empty lines in this commit.  Patches 1-3 applied, thanks.

Regards,
-Denis

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-06 20:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-06 19:50 [PATCH v2 3/9] unit: add primitive json test James Prestwood
  -- strict thread matches above, loose matches on Subject: below --
2022-01-06 20:08 Denis Kenzior

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox