-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_wordcount.py
64 lines (44 loc) · 1.74 KB
/
test_wordcount.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
import os.path
import tempfile
import wordcount_lib
def _make_testfile(filename, data):
"Make a temp file containing the given data; return full path to file."
tempdir = tempfile.mkdtemp(prefix='wordcounttest_')
testfile = os.path.join(tempdir, filename)
with open(testfile, 'wt') as fp:
fp.write(data)
return testfile
def test_consume_1():
# do a basic test of the consume function.
testfile = _make_testfile('sometext.txt', 'a b cc\nddd')
chars, words, lines = wordcount_lib.consume(testfile)
assert chars == 10
assert words == 4
assert lines == 2
def test_consume_2():
# do another basic test of the consume function.
testfile = _make_testfile('sometext.txt', 'a\nb\ncc\nddd\ne')
chars, words, lines = wordcount_lib.consume(testfile)
assert chars == 12 # includes whitespace in char count
assert words == 5
assert lines == 5
def test_consume_3():
# check something tricky: whitespace at beginning & end of line
testfile = _make_testfile('sometext.txt', ' a b c ')
chars, words, lines = wordcount_lib.consume(testfile)
assert chars == 7 # includes whitespace in char count
assert words == 3
assert lines == 1
def test_consume_4():
# check something tricky: whitespace at beginning & end of line
testfile = _make_testfile('sometext.txt', ' a b c d ee ')
chars, words, lines = wordcount_lib.consume(testfile)
assert chars == 12 # includes whitespace in char count
assert words == 5
assert lines == 1
def test_daaaangerous():
try:
wordcount_lib.daaaangerous()
assert False
except ZeroDivisionError as e:
assert True, 'Should thrown exception'