def test_set_json(): a = {True, 5.5, 5, None, 'hello'} s = JsonSerializer() r = s.serialize(a) # sets are unordered, and, it turns out, not deterministic # TODO bit of a bodge r = json.loads(r) r = sorted([str(x) for x in r]) assert r == ['5', '5.5', 'None', 'True', 'hello']
def test_from_import(): from emw_serializer import JsonSerializer, Serializer js = JsonSerializer() s = Serializer()
from emw_serializer import JsonSerializer class Gakk: def __init__(self): self.a = 'a' self.b = 5 serializer = JsonSerializer() thing_to_serialize = Gakk() json = serializer.serialize(thing_to_serialize) print(json)
def test_string_json(): a = 'hello' s = JsonSerializer() r = s.serialize(a) assert r == '"hello"'
def test_int_json(): a = 5 s = JsonSerializer() r = s.serialize(a) assert r == '5'
def test_bool_json(): a = True s = JsonSerializer() r = s.serialize(a) assert r == 'true'
def test_float_json(): a = 5.5 s = JsonSerializer() r = s.serialize(a) assert r == '5.5'
def test_method_json(): a = Method() s = JsonSerializer() r = s.serialize(a) assert r == '{"a": 5}'
def test_class_properties_json(): a = ClassProperties() s = JsonSerializer() r = s.serialize(a) assert r == '{"a": true, "b": 5.5, "c": 5, "d": null, "e": "hello", "f": "", "g": "Good"}'
def test_nested_json(): a = Nested() s = JsonSerializer() r = s.serialize(a) assert r == '{"a": 5, "b": {"a": true, "b": 5.5, "c": 5, "d": null, "e": "hello", "f": "", "g": "Good"}, "c": [1, "hello"]}'
def test_property_function_json(): a = PropertyFunction() s = JsonSerializer() r = s.serialize(a) assert r == '{"nope": "nope"}'
def test_property_decorator_json(): a = PropertyDecorator() s = JsonSerializer() r = s.serialize(a) assert r == '{"nope": "nope"}'
def test_inheritance_method_json(): a = InheritanceMethod() s = JsonSerializer() r = s.serialize(a) assert r == '{"a": 5}'
def test_inheritance_object_properties_json(): a = InheritanceObjectProperties() s = JsonSerializer() r = s.serialize(a) assert r == '{"a": true, "b": 5.5, "c": 5, "d": null, "e": "hello", "f": "", "g": "Good"}'
def test_list_json(): a = [True, 5.5, 5, None, 'hello'] s = JsonSerializer() r = s.serialize(a) assert r == '[true, 5.5, 5, null, "hello"]'