-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_aligned_print.py
72 lines (56 loc) · 1.51 KB
/
test_aligned_print.py
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
67
68
69
70
71
72
import aligned_print
import io
def test_basic():
output = io.StringIO()
print_function = lambda msg: output.write(f'{msg}\n')
with aligned_print.printer(print_function) as print_:
print_('a', 'a')
print_('bb', 'b')
print_('ccc', 'c')
print_('dddd', 'd')
print_('eeeee', 'e')
expected = """
a => a
bb => b
ccc => c
dddd => d
eeeee => e
"""
assert(output.getvalue().strip('\n') == expected.strip('\n'))
def test_attribute_printing():
output = io.StringIO()
print_function = lambda msg: output.write(f'{msg}\n')
class Foo(object):
def __init__(self):
self.name = 'foo'
self.id = 10
self.age = 29
with aligned_print.printer(print_function) as print_:
foo = Foo()
list(map(lambda pair: print_(pair[0], pair[1]), foo.__dict__.items()))
expected = """
name => foo
id => 10
age => 29
"""
assert(output.getvalue().strip('\n') == expected.strip('\n'))
def test_empty_with_statement():
with aligned_print.printer() as print_:
pass
def test_separator():
output = io.StringIO()
print_function = lambda msg: output.write(f'{msg}\n')
with aligned_print.printer(print_function, "|") as print_:
print_('a', 'a')
print_('bb', 'b')
print_('ccc', 'c')
print_('dddd', 'd')
print_('eeeee', 'e')
expected = """
a |a
bb |b
ccc |c
dddd |d
eeeee|e
"""
assert(output.getvalue().strip('\n') == expected.strip('\n'))