diff --git a/tests/snippets/exceptions.py b/tests/snippets/exceptions.py index cb178fc066a..fed8d320931 100644 --- a/tests/snippets/exceptions.py +++ b/tests/snippets/exceptions.py @@ -1,29 +1,37 @@ +def exceptions_eq(e1, e2): + return type(e1) is type(e2) and e1.args == e2.args + +def round_trip_repr(e): + return exceptions_eq(e, eval(repr(e))) + # KeyError empty_exc = KeyError() assert str(empty_exc) == '' -assert repr(empty_exc) == 'KeyError()' +assert round_trip_repr(empty_exc) assert len(empty_exc.args) == 0 assert type(empty_exc.args) == tuple exc = KeyError('message') assert str(exc) == "'message'" -assert repr(exc) == "KeyError('message',)" +assert round_trip_repr(exc) exc = KeyError('message', 'another message') assert str(exc) == "('message', 'another message')" -assert repr(exc) == "KeyError('message', 'another message')" +assert round_trip_repr(exc) assert exc.args[0] == 'message' assert exc.args[1] == 'another message' class A: def __repr__(self): - return 'repr' + return 'A()' def __str__(self): return 'str' + def __eq__(self, other): + return type(other) is A exc = KeyError(A()) -assert str(exc) == 'repr' -assert repr(exc) == 'KeyError(repr,)' +assert str(exc) == 'A()' +assert round_trip_repr(exc) # ImportError / ModuleNotFoundError exc = ImportError() diff --git a/tests/snippets/json_snippet.py b/tests/snippets/json_snippet.py index a8c0ecc19e0..15866ab86ee 100644 --- a/tests/snippets/json_snippet.py +++ b/tests/snippets/json_snippet.py @@ -5,7 +5,7 @@ def round_trip_test(obj): # serde_json and Python's json module produce slightly differently spaced # output; direct string comparison can't pass on both so we use this as a # proxy - assert obj == json.loads(json.dumps(obj)) + return obj == json.loads(json.dumps(obj)) assert '"string"' == json.dumps("string") assert "1" == json.dumps(1) @@ -17,7 +17,7 @@ def round_trip_test(obj): assert '[]' == json.dumps([]) assert '[1]' == json.dumps([1]) assert '[[1]]' == json.dumps([[1]]) -round_trip_test([1, "string", 1.0, True]) +assert round_trip_test([1, "string", 1.0, True]) assert '[]' == json.dumps(()) assert '[1]' == json.dumps((1,)) @@ -26,7 +26,7 @@ def round_trip_test(obj): assert [1, "string", 1.0, True] == json.loads(json.dumps((1, "string", 1.0, True))) assert '{}' == json.dumps({}) -round_trip_test({'a': 'b'}) +assert round_trip_test({'a': 'b'}) # should reject non-str keys in jsons assert_raises(json.JSONDecodeError, lambda: json.loads('{3: "abc"}')) @@ -68,6 +68,6 @@ class Dict(dict): pass # big ints should not crash VM # TODO: test for correct output when actual serialization implemented and doesn’t throw try: - json.dumps(7*500) + json.dumps(7**500) except: pass