forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·167 lines (120 loc) · 4.33 KB
/
test.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
"""tests for password.py"""
import os
import random
import re
import string
from subprocess import getstatusoutput
prg = './password.py'
words = '../inputs/words.txt'
# --------------------------------------------------
def test_exists():
"""exists"""
assert os.path.isfile(prg)
assert os.path.isfile(words)
# --------------------------------------------------
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():
"""Dies on 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():
"""Dies on bad num"""
bad = random_string()
flag = '-n' if random.choice([0, 1]) else '--num'
rv, out = getstatusoutput(f'{prg} {flag} {bad} {words}')
assert rv != 0
assert re.search(f"invalid int value: '{bad}'", out)
# --------------------------------------------------
def test_bad_num_words():
"""Dies on bad num"""
bad = random_string()
flag = '-w' if random.choice([0, 1]) else '--num_words'
rv, out = getstatusoutput(f'{prg} {flag} {bad} {words}')
assert rv != 0
assert re.search(f"invalid int value: '{bad}'", out)
# --------------------------------------------------
def test_bad_min_word_len():
"""Dies on bad min_word_len"""
bad = random_string()
flag = '-m' if random.choice([0, 1]) else '--min_word_len'
rv, out = getstatusoutput(f'{prg} {flag} {bad} {words}')
assert rv != 0
assert re.search(f"invalid int value: '{bad}'", out)
# --------------------------------------------------
def test_bad_max_word_len():
"""Dies on bad max_word_len"""
bad = random_string()
flag = '-m' if random.choice([0, 1]) else '--max_word_len'
rv, out = getstatusoutput(f'{prg} {flag} {bad} {words}')
assert rv != 0
assert re.search(f"invalid int value: '{bad}'", out)
# --------------------------------------------------
def test_bad_seed():
"""Dies on bad seed"""
bad = random_string()
flag = '-s' if random.choice([0, 1]) else '--seed'
rv, out = getstatusoutput(f'{prg} {flag} {bad} {words}')
assert rv != 0
assert re.search(f"invalid int value: '{bad}'", out)
# --------------------------------------------------
def test_defaults():
"""Test"""
rv, out = getstatusoutput(f'{prg} -s 1 {words}')
assert rv == 0
assert out.strip() == '\n'.join([
'DuniteBoonLociDefat', 'WegaTitmalUnplatSatire', 'IdeanClipsVitiArriet'
])
# --------------------------------------------------
def test_num():
"""Test"""
rv, out = getstatusoutput(f'{prg} -s 1 -n 1 {words}')
assert rv == 0
assert out.strip() == 'DuniteBoonLociDefat'
# --------------------------------------------------
def test_num_words():
"""Test"""
rv, out = getstatusoutput(f'{prg} -s 1 -w 2 {words}')
assert rv == 0
assert out.strip() == '\n'.join(['DuniteBoon', 'LociDefat', 'WegaTitmal'])
# --------------------------------------------------
def test_min_word_len():
"""Test"""
rv, out = getstatusoutput(f'{prg} -s 1 -m 5 {words}')
assert rv == 0
assert out.strip() == '\n'.join([
'CarneyRapperWabenoUndine', 'BabaiFarerBugleOnlepy',
'UnbittMinnyNatalSkanda'
])
# --------------------------------------------------
def test_max_word_len():
"""Test"""
rv, out = getstatusoutput(f'{prg} -s 1 -x 10 {words}')
assert rv == 0
assert out.strip() == '\n'.join([
'DicemanYardwandBoeberaKismetic', 'CubiculumTilsitSnowcapSuer',
'ProhasteHaddockChristmasyTenonitis'
])
# --------------------------------------------------
def test_l33t():
"""Test"""
rv, out = getstatusoutput(f'{prg} -s 1 -l {words}')
assert rv == 0
assert out.strip() == '\n'.join([
'DUn1Teb0onloCiDef4T/', 'Weg4TiTm@LuNPl4T54+1r3_',
'iD3@Ncl1P5v1+14rrie+/'
])
# --------------------------------------------------
def random_string():
"""generate a random string"""
k = random.randint(5, 10)
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))