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
·113 lines (85 loc) · 2.93 KB
/
test.py
File metadata and controls
executable file
·113 lines (85 loc) · 2.93 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
#!/usr/bin/env python3
"""tests for mad_lib.py"""
import re
import os
import random
import string
from subprocess import getstatusoutput
prg = './mad.py'
no_blanks = 'inputs/no_blanks.txt'
fox = 'inputs/fox.txt'
hlp = 'inputs/help.txt'
verona = 'inputs/romeo_juliet.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():
"""Test bad input 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_no_blanks():
"""Test no blanks"""
rv, out = getstatusoutput(f'{prg} {no_blanks}')
assert rv != 0
assert out == f'"{no_blanks}" has no placeholders.'
# --------------------------------------------------
def test_fox():
"""test fox"""
args = f'{fox} -i surly car under bicycle'
rv, out = getstatusoutput(f'{prg} {args}')
assert rv == 0
assert out.strip() == 'The quick surly car jumps under the lazy bicycle.'
# --------------------------------------------------
def test_help():
"""test help"""
expected = """
Hey! I need tacos!
Oi! Not just salsa!
Hola! You know I need queso!
Arriba!
""".strip()
args = f'{hlp} -i Hey tacos Oi salsa Hola queso Arriba'
rv, out = getstatusoutput(f'{prg} {args}')
assert rv == 0
assert out.strip() == expected.strip()
# --------------------------------------------------
def test_verona():
"""test verona"""
expected = """
Two cars, both alike in dignity,
In fair Detroit, where we lay our scene,
From ancient oil break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross'd pistons take their life;
Whose misadventur'd piteous overthrows
Doth with their stick shift bury their parents' strife.
The fearful passage of their furious love,
And the continuance of their parents' rage,
Which, but their children's end, nought could accelerate,
Is now the 42 hours' traffic of our stage;
The which if you with patient foot attend,
What here shall hammer, our toil shall strive to mend.
""".strip()
args = (f'{verona} --inputs cars Detroit oil pistons '
'"stick shift" furious accelerate 42 foot hammer')
rv, out = getstatusoutput(f'{prg} {args}')
assert rv == 0
assert out.strip() == expected.strip()
# --------------------------------------------------
def random_string():
"""generate a random string"""
k = random.randint(5, 10)
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))