Skip to content

Commit

Permalink
implement: unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
TerminalFi committed May 9, 2020
1 parent 10e8065 commit e012467
Showing 1 changed file with 69 additions and 29 deletions.
98 changes: 69 additions & 29 deletions hjson/tests/test_decode.py
Original file line number Diff line number Diff line change
@@ -1,23 1,26 @@
from __future__ import absolute_import

import decimal
from unittest import TestCase

import hjson as json
from hjson.compat import StringIO
from hjson import OrderedDict
from hjson.compat import StringIO


class TestDecode(TestCase):
if not hasattr(TestCase, 'assertIs'):
if not hasattr(TestCase, "assertIs"):

def assertIs(self, a, b):
self.assertTrue(a is b, '%r is %r' % (a, b))
self.assertTrue(a is b, "%r is %r" % (a, b))

def test_decimal(self):
rval = json.loads('1.1', parse_float=decimal.Decimal)
rval = json.loads("1.1", parse_float=decimal.Decimal)
self.assertTrue(isinstance(rval, decimal.Decimal))
self.assertEqual(rval, decimal.Decimal('1.1'))
self.assertEqual(rval, decimal.Decimal("1.1"))

def test_float(self):
rval = json.loads('1', parse_int=float)
rval = json.loads("1", parse_int=float)
self.assertTrue(isinstance(rval, float))
self.assertEqual(rval, 1.0)

Expand All @@ -26,32 29,38 @@ def test_decoder_optimizations(self):
# the whitespace regex, so this test is designed to try and
# exercise the uncommon cases. The array cases are already covered.
rval = json.loads('{ "key" : "value" , "k":"v" }')
self.assertEqual(rval, {"key":"value", "k":"v"})
self.assertEqual(rval, {"key": "value", "k": "v"})

def test_empty_objects(self):
s = '{}'
s = "{}"
self.assertEqual(json.loads(s), eval(s))
s = '[]'
s = "[]"
self.assertEqual(json.loads(s), eval(s))
s = '""'
self.assertEqual(json.loads(s), eval(s))

def test_object_pairs_hook(self):
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
("qrt", 5), ("pad", 6), ("hoy", 7)]
p = [
("xkd", 1),
("kcw", 2),
("art", 3),
("hxm", 4),
("qrt", 5),
("pad", 6),
("hoy", 7),
]
self.assertEqual(json.loads(s), eval(s))
self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p)
self.assertEqual(json.load(StringIO(s),
object_pairs_hook=lambda x: x), p)
self.assertEqual(json.load(StringIO(s), object_pairs_hook=lambda x: x), p)
od = json.loads(s, object_pairs_hook=OrderedDict)
self.assertEqual(od, OrderedDict(p))
self.assertEqual(type(od), OrderedDict)
# the object_pairs_hook takes priority over the object_hook
self.assertEqual(json.loads(s,
object_pairs_hook=OrderedDict,
object_hook=lambda x: None),
OrderedDict(p))
self.assertEqual(
json.loads(s, object_pairs_hook=OrderedDict, object_hook=lambda x: None),
OrderedDict(p),
)

def check_keys_reuse(self, source, loads):
rval = loads(source)
Expand All @@ -60,7 69,7 @@ def check_keys_reuse(self, source, loads):
self.assertIs(b, d)

def test_keys_reuse_str(self):
s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'.encode('utf8')
s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'.encode("utf8")
self.check_keys_reuse(s, json.loads)

def test_keys_reuse_unicode(self):
Expand All @@ -73,27 82,58 @@ def test_empty_strings(self):
self.assertEqual(json.loads('[""]'), [""])
self.assertEqual(json.loads(u'[""]'), [u""])

def test_multiline_string(self):
s1 = """
hello: '''
'''
"""
s2 = """
hello: '''
'''
"""
s3 = """
hello: ''''''
"""
s4 = """
hello: ''
"""
s5 = """
hello: ""
"""
self.assertEqual(json.loads(s1), {"hello": ""})
self.assertEqual(json.loads(s2), {"hello": ""})
self.assertEqual(json.loads(s3), {"hello": ""})
self.assertEqual(json.loads(s4), {"hello": ""})
self.assertEqual(json.loads(s5), {"hello": ""})

def test_raw_decode(self):
cls = json.decoder.HjsonDecoder
self.assertEqual(
({'a': {}}, 9),
cls().raw_decode("{\"a\": {}}"))
self.assertEqual(({"a": {}}, 9), cls().raw_decode('{"a": {}}'))
# http://code.google.com/p/simplejson/issues/detail?id=85
self.assertEqual(
({'a': {}}, 9),
cls(object_pairs_hook=dict).raw_decode("{\"a\": {}}"))
({"a": {}}, 9), cls(object_pairs_hook=dict).raw_decode('{"a": {}}')
)
# https://github.com/simplejson/simplejson/pull/38
self.assertEqual(
({'a': {}}, 11),
cls().raw_decode(" \n{\"a\": {}}"))
self.assertEqual(({"a": {}}, 11), cls().raw_decode(' \n{"a": {}}'))

def test_bounds_checking(self):
# https://github.com/simplejson/simplejson/issues/98
j = json.decoder.HjsonDecoder()
for i in [4, 5, 6, -1, -2, -3, -4, -5, -6]:
self.assertRaises(ValueError, j.scan_once, '1234', i)
self.assertRaises(ValueError, j.raw_decode, '1234', i)
x, y = sorted(['128931233', '472389423'], key=id)
self.assertRaises(ValueError, j.scan_once, "1234", i)
self.assertRaises(ValueError, j.raw_decode, "1234", i)
x, y = sorted(["128931233", "472389423"], key=id)
diff = id(x) - id(y)
self.assertRaises(ValueError, j.scan_once, y, diff)
self.assertRaises(ValueError, j.raw_decode, y, i)

0 comments on commit e012467

Please sign in to comment.