Skip to content

make exceptions repr tests Python 3.7-friendly by eval-repr round-trip #1117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions tests/snippets/exceptions.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
8 changes: 4 additions & 4 deletions tests/snippets/json_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,))
Expand All @@ -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"}'))
Expand Down Expand Up @@ -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