forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_membership.py
More file actions
66 lines (49 loc) · 1.28 KB
/
operator_membership.py
File metadata and controls
66 lines (49 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
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
assert "a" in {"a": 0, "b": 0}
assert "c" not in {"a": 0, "b": 0}
assert 1 in {1: 5, 7: 12}
assert 5 not in {9: 10, 50: 100}
assert True in {True: 5}
assert False not in {True: 5}
# test iter
assert 3 in iter([1, 2, 3])
assert 3 not in iter([1, 2])
# test sequence
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)