changeset 84:9de5cf4fdeab draft

Add JSON test
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 25 May 2019 23:58:11 +0200
parents d8cc0de4e1ca
children 7c5b59a0ff66
files pcombinators/state.py pcombinators/tests/json.py pcombinators/tests/test_json.py
diffstat 3 files changed, 44 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pcombinators/state.py	Sat May 25 23:49:33 2019 +0200
+++ b/pcombinators/state.py	Sat May 25 23:58:11 2019 +0200
@@ -111,6 +111,8 @@
         if self._fobj:
             self._fobj.close()
 
+    # For efficiency reasons, only check for collectable buffer space when
+    # the buffer is longer than this limit.
     COLLECT_LOWER_LIMIT = 1024
 
     def _maybe_collect(self):
@@ -121,7 +123,9 @@
             self._buf = self._buf[self._index:]
             self._total_offset += self._index
             self._index = 0
-        elif self._holds[0]-self._total_offset > 0: # Find oldest hold and update buffer to hold everything from the oldest hold onwards.
+            return
+        elif self._holds[0]-self._total_offset > 0:
+            # Find oldest hold and update buffer to hold everything from the oldest hold onwards.
             to_clean = self._holds[0]-self._total_offset
             self._buf = self._buf[to_clean:]
             self._total_offset += to_clean
--- a/pcombinators/tests/json.py	Sat May 25 23:49:33 2019 +0200
+++ b/pcombinators/tests/json.py	Sat May 25 23:58:11 2019 +0200
@@ -61,7 +61,9 @@
 Dict = dct >> dict
 
 def parse_json(json):
-    return Value().parse(st.ParseState(ut.remove_unused_whitespace(json)))
+    if type(json) is str:
+        json = st.ParseState(ut.remove_unused_whitespace(json))
+    return Value().parse(json)
 
 def json_result(json):
     r, st = parse_json(json)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcombinators/tests/test_json.py	Sat May 25 23:58:11 2019 +0200
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Created on Sat May 25 23:37:36 2019
+
+@author: lbo
+"""
+
+import io
+import unittest
+
+import pcombinators.state as st
+import pcombinators.tests.json as js
+
+class JSONTest(unittest.TestCase):
+
+    def test_atoms(self):
+        self.assertEqual(1.0, js.json_result('1'))
+        self.assertEqual('ab cd', js.json_result('"ab cd"'))
+
+    def test_flat_structs(self):
+        self.assertEqual([1.0, 2.0, 3.0], js.json_result('[1,   2,3]'))
+        self.assertEqual({'a': 'c', 'b': 3.0}, js.json_result('{"a": "c", "b": 3.0}'))
+
+    def test_nested_structs(self):
+        self.assertEqual([{"a": [1, 2]}, 3], js.json_result('[{"a": [1,2]}, 3]'))
+        self.assertEqual({"a": {"b": {"c": [1,2]}}}, js.json_result('{"a": {"b": {"c": [1,2]}}}'))
+        
+    def test_stream_parse(self):
+        have = '{"id":1,"name":"Foo","price":123,"tags":["Bar","Eek"],"stock":{"warehouse":300,"retail":20}}'
+        want = {"id":1,"name":"Foo","price":123,"tags":["Bar","Eek"],"stock":{"warehouse":300,"retail":20}}
+        self.assertEqual(want, js.json_result(st.ParseFileState(io.StringIO(have))))
+
+if __name__ == '__main__':
+    st.ParseFileState.COLLECT_LOWER_LIMIT = 0
+    unittest.main()
\ No newline at end of file