def test_objects(): o = object convert_with_assertion(o) assert object is json.loads('{"__type__": "__builtin__.object"}', cls=JSONObjectDecoder) assert object is json.loads('{"__type__": "builtins.object"}', cls=JSONObjectDecoder)
def test_simple_objects(): j1 = JO(1, 2, 3) j2 = JO(1.1, 2.2, 3.3) j3 = JO('a', "bcd", None) j4 = JO('1.1', "3", "0") j5 = JO(-2, "preempted", float) convert_with_assertion(j1) convert_with_assertion(j2) convert_with_assertion(j3) convert_with_assertion(j4) convert_with_assertion(j5)
def test_strings(): c = 'a' s = 'abc' i = '3' f = '3.14' convert_with_assertion(c) convert_with_assertion(s) convert_with_assertion(i) convert_with_assertion(f)
def test_nested_objects(): l1 = [JO(1, 2, 3), JO('1.1', "3", "0")] t1 = tuple(l1[:]) d1 = {0: l1[:], 1: tuple(t1)} convert_with_assertion(l1) convert_with_assertion(t1) convert_with_assertion(d1)
def test_complex_objects(): j1 = JO({0: 'a', 1: int, 2: -5}, dict(enumerate(list("abc"))), (1, 2, 3)) j2 = JO([1, 2, { 3: 'a' }, (4, 5, set([6, 7]))], { 0: [1, 2], 1: (3, 4), 'abc': [{ 'x': 1, 'y': 2 }] }, j1) j3 = JO({ 0: j1, 1: { 0: j1 } }, { 0: list, 1: { 0: float } }, { 0: None, 1: { 0: None } }) j4 = JO({ 0: JO(1, 2, 3), 1: { 0: JO(1, 2, 3) } }, { 0: JO(-2, "preempted", float), 1: { 0: JO(-2, "preempted", float) } }, JO(-2, "preempted", float)) convert_with_assertion(j1) convert_with_assertion(j2) convert_with_assertion(j3) convert_with_assertion(j4)
def test_numbers(): arr = np.array([1., 2., 3.]) convert_with_assertion(arr)
def test_class_type(): convert_with_assertion(Custom) convert_with_assertion(Custom2)
def test_nested_type(): convert_with_assertion([int, type(None)]) convert_with_assertion([{'a': float, 'b': (complex, set([set, dict]))}])
def test_types(): for _type in [int, float, str, type, complex, list, dict, set, tuple, type(None)]: convert_with_assertion(_type)
def test_sets(): s1 = (1, 2, 3) s2 = set("abc") convert_with_assertion(s1) convert_with_assertion(s2)
def test_lists(): l1 = [1, 2, 3] l2 = list("abc") convert_with_assertion(l1) convert_with_assertion(l2)
def test_numbers(): i = 1 f = 1.1 convert_with_assertion(i) convert_with_assertion(f)
def test_nested(): n1 = [1, 2, {3: 'a'}, (4, 5, set([6, 7])), None] n2 = {0: [1, 2], 1: (3, 4), 'abc': [{'x': 1, 'y': 2}]} convert_with_assertion(n1) convert_with_assertion(n2)
def test_dicts(): d1 = {0: 'a', 1: 'b', 2: 'c'} d2 = dict(enumerate(list("abc"))) convert_with_assertion(d1) convert_with_assertion(d2)