git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 2/2] json-writer: t0019: add Python unit test

From
Ggit@jeffhostetler.com <git@jeffhostetler.com>
Date
Jun 5, 2018, 16:33 UTC
Message-ID
<20180605163358.119080-3-git@jeffhostetler.com>
In-Reply-To
<20180605163358.119080-1-git@jeffhostetler.com>
From: Jeff Hostetler <jeffhost@microsoft.com>
Test json-writer output using Python.
Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
---
 t/t0019-json-writer.sh  | 38 ++++++++++++++++++++++++++++++++++++++
 t/t0019/parse_json_1.py | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 t/t0019/parse_json_1.py
diff --git a/t/t0019-json-writer.sh b/t/t0019-json-writer.sh
index c9c2e23..951cd89 100755
--- a/t/t0019-json-writer.sh
+++ b/t/t0019-json-writer.sh
@@ -233,4 +233,42 @@ test_expect_success 'inline array with no members' '
 	test_cmp expect actual
 '
 
+# As a sanity check, ask Python to parse our generated JSON.  Let Python
+# recursively dump the resulting dictionary in sorted order.  Confirm that
+# that matches our expectations.
+test_expect_success PYTHON 'parse JSON using Python' '
+	cat >expect <<-\EOF &&
+	a abc
+	b 42
+	sub1 dict
+	sub1.c 3.14
+	sub1.d True
+	sub1.sub2 list
+	sub1.sub2[0] False
+	sub1.sub2[1] dict
+	sub1.sub2[1].g 0
+	sub1.sub2[1].h 1
+	sub1.sub2[2] None
+	EOF
+	test-json-writer >output.json \
+		@object \
+			@object-string a abc \
+			@object-int b 42 \
+			@object-object "sub1" \
+				@object-double c 2 3.140 \
+				@object-true d \
+				@object-array "sub2" \
+					@array-false \
+					@array-object \
+						@object-int g 0 \
+						@object-int h 1 \
+					@end \
+					@array-null \
+				@end \
+			@end \
+		@end &&
+	python "$TEST_DIRECTORY"/t0019/parse_json_1.py <output.json >actual &&
+	test_cmp expect actual
+'
+
 test_done
diff --git a/t/t0019/parse_json_1.py b/t/t0019/parse_json_1.py
new file mode 100644
index 0000000..9d928a3
--- /dev/null
+++ b/t/t0019/parse_json_1.py
@@ -0,0 +1,35 @@
+import os
+import sys
+import json
+
+def dump_item(label_input, v):
+    if type(v) is dict:
+        print("%s dict" % (label_input))
+        dump_dict(label_input, v)
+    elif type(v) is list:
+        print("%s list" % (label_input))
+        dump_list(label_input, v)
+    else:
+        print("%s %s" % (label_input, v))
+
+def dump_list(label_input, list_input):
+    ix = 0
+    for v in list_input:
+        label = ("%s[%d]" % (label_input, ix))
+        dump_item(label, v)
+        ix += 1
+    return
+              
+def dump_dict(label_input, dict_input):
+    for k in sorted(dict_input.iterkeys()):
+        v = dict_input[k]
+        if (len(label_input) > 0):
+            label = ("%s.%s" % (label_input, k))
+        else:
+            label = k
+        dump_item(label, v)
+    return
+
+for line in sys.stdin:
+    data = json.loads(line)
+    dump_dict("", data)
-- 
2.9.3
Previous: git@jeffhostetler.comNext: Todd Zullinger
Message 3 of 13 in “json-writer V7”
  1. 0/2 json-writer V7git@jeffhostetler.com, Jun 5, 2018
  2. 1/2 json_writer: new routines to create data in JSON formatgit@jeffhostetler.com, Jun 5, 2018
  3. 2/2 json-writer: t0019: add Python unit testgit@jeffhostetler.com, Jun 5, 2018
  4. Todd ZullingerJun 6, 2018
  5. Jeff KingJun 6, 2018
  6. Jeff HostetlerJun 6, 2018
  7. Ramsay JonesJun 7, 2018
  8. Todd ZullingerJun 7, 2018
  9. Jeff KingJun 7, 2018
  10. Jeff KingJun 7, 2018
  11. Jeff KingJun 7, 2018
  12. Ramsay JonesJun 7, 2018
  13. Jeff HostetlerJun 6, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.