forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·116 lines (86 loc) · 2.92 KB
/
test.py
File metadata and controls
executable file
·116 lines (86 loc) · 2.92 KB
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
#!/usr/bin/env python3
"""tests for days.py"""
import os
import random
import re
import string
from subprocess import getstatusoutput
prg = './head.py'
sonnet = './inputs/sonnet-29.txt'
bustle = './inputs/the-bustle.txt'
gettysburg = './inputs/gettysburg.txt'
# --------------------------------------------------
def test_exists():
"""exists"""
assert os.path.isfile(prg)
# --------------------------------------------------
def test_usage():
"""usage"""
for flag in ['-h', '--help']:
rv, out = getstatusoutput(f'{prg} {flag}')
assert rv == 0
assert out.lower().startswith('usage')
# --------------------------------------------------
def test_bad_file():
"""Bad file"""
bad = random_string()
rv, out = getstatusoutput(f'{prg} {bad}')
assert rv != 0
assert re.search(f"No such file or directory: '{bad}'", out)
# --------------------------------------------------
def test_bad_num():
"""Bad num"""
for bad in random.sample(range(-10, 1), 3):
rv, out = getstatusoutput(f'{prg} -n {bad} {sonnet}')
assert rv != 0
assert re.search(f'--num "{bad}" must be greater than 0', out)
# --------------------------------------------------
def test_default():
"""Default --num"""
rv, out = getstatusoutput(f'{prg} {sonnet}')
assert rv == 0
assert len(out.splitlines()) == 10
expected = """
Sonnet 29
William Shakespeare
When, in disgrace with fortune and men’s eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself and curse my fate,
Wishing me like to one more rich in hope,
Featured like him, like him with friends possessed,
Desiring this man’s art and that man’s scope,
""".strip()
assert out.strip() == expected
# --------------------------------------------------
def test_num_1():
"""--num 1"""
rv, out = getstatusoutput(f'{prg} --num 1 {gettysburg}')
assert rv == 0
assert len(out.splitlines()) == 1
assert out.strip(
) == 'Four score and seven years ago our fathers brought forth on this'
# --------------------------------------------------
def test_n_2():
"""-n 2"""
rv, out = getstatusoutput(f'{prg} -n 2 {sonnet}')
assert rv == 0
assert len(out.splitlines()) == 2
expected = 'Sonnet 29\nWilliam Shakespeare'
assert out.strip() == expected
# --------------------------------------------------
def test_num_3():
"""--num 2"""
rv, out = getstatusoutput(f'{prg} --num 3 {bustle}')
assert rv == 0
assert len(out.splitlines()) == 3
expected = '\n'.join([
'The bustle in a house', 'The morning after death',
'Is solemnest of industries'
])
assert out.strip() == expected
# --------------------------------------------------
def random_string():
"""generate a random string"""
k = random.randint(5, 10)
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))