Files
RustPython/tests/snippets/membership.py
Joey Hain 47e95486f0 Reuse and improve ergonomics of assert_raises utility
- Move assert_raises to testutils
- Add optional message argument, with reasonable default
- Reverse order of expr and exception type for readability
- Lambda argument no longer takes parameter
- Convert applicable snippets to use assert_raises
2019-02-16 09:32:42 -08:00

66 lines
1.3 KiB
Python

from testutils import assert_raises
# test lists
assert 3 in [1, 2, 3]
assert 3 not in [1, 2]
assert not (3 in [1, 2])
assert not (3 not in [1, 2, 3])
# test strings
assert "foo" in "foobar"
assert "whatever" not in "foobar"
# test bytes
# TODO: uncomment this when bytes are implemented
# assert b"foo" in b"foobar"
# assert b"whatever" not in b"foobar"
assert b"1" < b"2"
assert b"1" <= b"2"
assert b"5" <= b"5"
assert b"4" > b"2"
assert not b"1" >= b"2"
assert b"10" >= b"10"
assert_raises(TypeError, lambda: bytes() > 2)
# test tuple
assert 1 in (1, 2)
assert 3 not in (1, 2)
# test set
assert 1 in set([1, 2])
assert 3 not in set([1, 2])
# test dicts
# TODO: test dicts when keys other than strings will be allowed
assert "a" in {"a": 0, "b": 0}
assert "c" not in {"a": 0, "b": 0}
# test iter
assert 3 in iter([1, 2, 3])
assert 3 not in iter([1, 2])
# test sequence
# TODO: uncomment this when ranges are usable
# assert 1 in range(0, 2)
# assert 3 not in range(0, 2)
# test __contains__ in user objects
class MyNotContainingClass():
pass
assert_raises(TypeError, lambda: 1 in MyNotContainingClass())
class MyContainingClass():
def __init__(self, value):
self.value = value
def __contains__(self, something):
return something == self.value
assert 2 in MyContainingClass(2)
assert 1 not in MyContainingClass(2)