-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
142 lines (108 loc) · 3.14 KB
/
tests.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import random
import sys
from contextlib import contextmanager
from io import StringIO
from textwrap import dedent
from nose.tools import assert_equal, with_setup
from flashcard import load_cards, main
from card_stats import CardStats
# Testing utilities.
test_data = dedent('''\
# comment
你好 (ni3hao3) - hello
永 (yong3) - eternity
''')
test_input = '\nY\n\nN\n'
test_output = dedent('''\
Found 2 cards
Press Enter to flip card, Q to quit.
========================================
你好
----------------------------------------
ni3hao3
hello
Were you correct? (Y/N)
========================================
永
----------------------------------------
yong3
eternity
Were you correct? (Y/N)
''')
test_stats = CardStats({
(('你好',), ('ni3hao3', 'hello')): [False, True, False, True, True],
(('ni3hao3', 'hello'), ('你好',)): [False, False, True],
})
def fix_whitespace(s):
return '\n'.join(line.rstrip() for line in s.splitlines())
@contextmanager
def assert_output(expected):
old_stdout = sys.stdout
sys.stdout = fake_stdout = StringIO()
yield
sys.stdout = old_stdout
fake_stdout.seek(0)
actual = fake_stdout.read()
assert_equal(fix_whitespace(actual), fix_whitespace(expected))
@contextmanager
def fake_input(input_data):
old_stdin = sys.stdin
sys.stdin = fake_stdin = StringIO(input_data)
fake_stdin.seek(0)
yield
sys.stdin = old_stdin
def setup_fake_cards():
with open('fake_cards', 'w') as f:
f.write(test_data)
def setup_fake_cards_stats():
with open('fake_cards.stats', 'wb') as f:
f.write(test_stats.save())
def teardown():
for filename in ['fake_cards', 'fake_cards.stats']:
if os.path.exists(filename):
os.remove(filename)
# Test functions.
def test_load_cards():
assert_equal(load_cards(''), [])
assert_equal(
load_cards(test_data),
[
('你好', 'ni3hao3', 'hello'),
('永', 'yong3', 'eternity'),
]
)
def test_load_cards_error():
with assert_output(''):
assert_equal(load_cards('bad line'), [])
def test_save_load_stats():
data = test_stats.save()
stats = CardStats.load(data)
assert_equal(test_stats, stats)
def test_main_error():
with assert_output('Please specify a deck of flash cards.'):
main([])
@with_setup(setup_fake_cards, teardown)
def test_main():
random.seed(0);
with fake_input(test_input):
with assert_output(test_output):
main(['fake_cards'])
@with_setup(setup_fake_cards, teardown)
@with_setup(setup_fake_cards_stats, teardown)
def test_main_load_empty_stats_file():
random.seed(0);
with fake_input(test_input):
with assert_output(test_output):
main(['fake_cards'])
@with_setup(setup_fake_cards, teardown)
def test_main_quit():
random.seed(0);
with fake_input('q\n'):
with assert_output(dedent('''\
Found 2 cards
Press Enter to flip card, Q to quit.
========================================
你好
''')):
main(['fake_cards'])