Skip to content

Commit d202962

Browse files
test: Add tests for pforma
1 parent 36bce5c commit d202962

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

test/test_typecheck/test_name_resolve.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_top_scope_attr(self):
4545
m.assert_called_once()
4646
v2 = nr.run()
4747
self.assertIs(v2, v)
48+
self.assertIs(v2, nr.top_scope)
4849
m.assert_called_once() # Still only once
4950

5051

test/test_util/test_pformat.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import enum
2+
3+
from test.common import CommonTestCase
4+
from util.pformat import pformat
5+
6+
7+
class MyEnum(enum.Enum):
8+
FOO = 'foo'
9+
BAR = 'bar'
10+
11+
12+
def dedent(s: str, keep_empty_ends=False):
13+
res = s.splitlines()
14+
if not keep_empty_ends:
15+
if res[0].lstrip() == '':
16+
del res[0]
17+
if res[-1].lstrip() == '':
18+
del res[-1]
19+
by = min(next(i for (i, c) in enumerate(ln) if not c.isspace())
20+
for ln in res if ln and not ln.isspace())
21+
return dedent_by(s, by, keep_empty_ends=True)
22+
23+
24+
def dedent_by(s: str, by: int, keep_empty_ends=False):
25+
res = s.splitlines()
26+
if not keep_empty_ends:
27+
if res[0].lstrip() == '':
28+
del res[0]
29+
if res[-1].lstrip() == '':
30+
del res[-1]
31+
for i, ln in enumerate(res):
32+
start = next((j for j, c in enumerate(ln) if not c.isspace() or j > by), len(ln))
33+
res[i] = ln[start:]
34+
return '\n'.join(res)
35+
36+
37+
class TestPFormat(CommonTestCase):
38+
def test_fmt_enum(self):
39+
self.assertEqual('MyEnum.FOO', pformat(MyEnum.FOO))
40+
41+
def test_fmt_short(self):
42+
self.assertEqual('[MyEnum.FOO, 6]', pformat([MyEnum.FOO, 6]))
43+
44+
def test_fmt_tuple(self):
45+
self.assertEqual('(MyEnum.FOO,)', pformat((MyEnum.FOO,)))
46+
self.assertEqual('(MyEnum.FOO, 6)', pformat((MyEnum.FOO, 6)))
47+
self.assertEqual(dedent('''
48+
(
49+
MyEnum.FOO,
50+
(4, 5)
51+
)'''), pformat((MyEnum.FOO, (4, 5))))
52+
self.assertEqual(dedent('''
53+
(
54+
(
55+
(
56+
2,
57+
3,
58+
(
59+
4,
60+
)
61+
)
62+
)
63+
)'''), pformat(((2, 3, (4,)),)))

0 commit comments

Comments
 (0)