Skip to content

Commit 6bab12b

Browse files
committed
chore(test): bltins arg pass,2 level comprehension,f_back,globals locals
1 parent ee6f9ac commit 6bab12b

6 files changed

Lines changed: 166 additions & 0 deletions

File tree

tests/asserts/bltins.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""Assert-based tests for builtin call/argument passing.
2+
3+
Rules: only builtin functions are tested; no user-defined functions;
4+
do not use argument unpacking (`*` or `**`).
5+
"""
6+
7+
# dict and dict.fromkeys
8+
def test_init():
9+
assert dict() == {}
10+
#assert dict([('a', 1), ('b', 2)]) == {'a': 1, 'b': 2}
11+
assert dict(a=1, b=2) == {'a': 1, 'b': 2}
12+
assert dict.fromkeys([1, 2]) == {1: None, 2: None}
13+
assert dict.fromkeys([1, 2], 0) == {1: 0, 2: 0}
14+
15+
# list
16+
assert list() == []
17+
assert list((1, 2, 3)) == [1, 2, 3]
18+
test_init()
19+
20+
# numeric builtins: max, min
21+
def test_min_max():
22+
assert max(1, 2, 3) == 3
23+
assert max([1, 5, 3]) == 5
24+
assert min([1, 0, -1]) == -1
25+
# use builtin `abs` as a key function
26+
assert max([-1, -2, 0], key=abs) == -2
27+
28+
# simple sanity checks combining builtin calls
29+
s = set(dict.fromkeys((3, 1, 2)).keys())
30+
assert s == {1, 2, 3}
31+
test_min_max()
32+
33+
# Grouped tests using `def` (each function is called so asserts execute)
34+
def test_numeric_builtins():
35+
# # pow with two and three args
36+
# assert pow(2, 3) == 8
37+
# assert pow(2, 3, 5) == 3
38+
39+
# # divmod and round
40+
# assert divmod(7, 3) == (2, 1)
41+
# assert round(2.5) in (2, 3)
42+
43+
# abs and built-in conversions
44+
assert abs(-5) == 5
45+
assert int('10') + 5 == 15
46+
47+
48+
def test_sequence_and_sorting():
49+
# conversions and sorted results
50+
assert list((4, 3, 2)) == [4, 3, 2]
51+
assert tuple([1, 2]) == (1, 2)
52+
assert set([1, 2, 2]) == {1, 2}
53+
assert sorted([3, 1, 2]) == [1, 2, 3]
54+
55+
# reversed returns an iterator; collect via list()
56+
assert list(reversed([1, 2, 3])) == [3, 2, 1]
57+
58+
59+
def test_dictionary_methods_and_lookup():
60+
# dict creation and lookups
61+
x = dict.fromkeys(['x', 'y'], 0)
62+
assert x == {'x': 0, 'y': 0}
63+
x2 = dict(a=1, b=2)
64+
assert x2.get('a') == 1
65+
x2.pop('a') == 1
66+
# ensure pop removed key
67+
assert 'a' not in x2
68+
69+
70+
71+
def test_boolean_any_all_sum():
72+
assert any([0, '', None, 1]) is True
73+
assert all([1, True, 'nonempty']) is True
74+
assert sum([1, 2, 3]) == 6
75+
76+
77+
def test_string_and_bytes():
78+
assert str(123) == '123'
79+
b = bytes([65, 66, 67])
80+
assert b == b'ABC'
81+
82+
83+
# Call grouped tests so their asserts run
84+
test_numeric_builtins()
85+
test_sequence_and_sorting()
86+
test_dictionary_methods_and_lookup()
87+
test_boolean_any_all_sum()
88+
test_string_and_bytes()
89+

tests/asserts/comprehension.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ def foo():
77

88
for i in range(10):
99
assert l[i] == i
10+
11+
assert [i for j in [[1, 2]] for i in j] == [1, 2]
12+
1013
print("ok")

tests/asserts/dict.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
import xfail
3+
def test_dictionary_methods_and_lookup():
4+
x2 = dict(a=1, b=2)
5+
assert x2.pop('a') == 1
6+
# ensure pop removed key
7+
assert 'a' not in x2
8+
9+
test_dictionary_methods_and_lookup()
10+
11+
cnt = 0
12+
def key_error():
13+
ori = dict(a=1, b=2)
14+
x = ori.copy()
15+
x.pop('a')
16+
cnt += 1
17+
assert len(ori) == 2, "dict.copy wrong"
18+
19+
assert 1000 == x.pop('a', 1000)
20+
cnt += 1
21+
22+
x.pop('a')
23+
cnt += 1
24+
25+
xfail.xfail(key_error, KeyError)
26+
assert cnt == 2
27+
28+
29+
def test_dict_update():
30+
d = {}
31+
d.update(dict(a=1))
32+
assert d == {'a': 1}
33+
34+
d.update(b=2)
35+
assert d == {'a': 1, 'b': 2}
36+
37+
d.clear()
38+
d.update([('a', 2)], b=1)
39+
assert d == {'a': 2, 'b': 1}
40+
test_dict_update()
41+

tests/asserts/localsglobals.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
assert id(locals()) == id(globals())
3+
4+
def f():
5+
a = 123
6+
assert id(locals()) != id(globals())
7+
assert locals()['a'] == 123
8+
assert 'a' in locals()
9+
assert 'a' not in globals()
10+
11+
f()
12+

tests/asserts/oserr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
e=OSError()
3+
assert not hasattr(e, "characters_written")
4+
5+
e.characters_written = 42
6+
assert e.characters_written == 42
7+
8+
assert e.filename is None

tests/asserts/traceback.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def f():
3+
raise ValueError
4+
5+
g = None
6+
try: f()
7+
except ValueError as e: g = e
8+
9+
tb = g.__traceback__
10+
nb = tb.tb_next.tb_frame.f_back
11+
assert nb is tb.tb_frame, nb
12+
13+

0 commit comments

Comments
 (0)