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
·123 lines (88 loc) · 3.26 KB
/
test.py
File metadata and controls
executable file
·123 lines (88 loc) · 3.26 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
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""tests for telephone.py"""
from subprocess import getstatusoutput, getoutput
import os
import random
import re
import string
prg = "./telephone.py"
fox = '../inputs/fox.txt'
now = '../inputs/now.txt'
# --------------------------------------------------
def test_exists():
"""exists"""
assert os.path.isfile(prg)
# --------------------------------------------------
def test_usage():
"""usage"""
for flag in ['', '-h', '--help']:
out = getoutput(f'{prg} {flag}')
assert re.match('usage', out, re.IGNORECASE)
# --------------------------------------------------
def test_bad_seed_str():
"""bad seed str value"""
bad = random_string()
rv, out = getstatusoutput(f'{prg} -s {bad} {fox}')
assert rv > 0
assert re.search(f"invalid int value: '{bad}'", out)
# --------------------------------------------------
def test_bad_mutation_str():
"""bad mutation str value"""
bad = random_string()
rv, out = getstatusoutput(f'{prg} -m {bad} {fox}')
assert rv > 0
assert re.search(f"invalid float value: '{bad}'", out)
# --------------------------------------------------
def test_bad_mutation():
"""bad mutation values"""
for val in ['-1.0', '10.0']:
rv, out = getstatusoutput(f'{prg} -m {val} {fox}')
assert rv > 0
assert re.search(f'--mutations "{val}" must be between 0 and 1', out)
# --------------------------------------------------
def test_for_echo():
"""test"""
txt = open(now).read().rstrip()
rv, out = getstatusoutput(f'{prg} -m 0 "{txt}"')
assert rv == 0
assert out.rstrip() == f'You said: "{txt}"\nI heard : "{txt}"'
# --------------------------------------------------
def test_now_cmd_s1():
"""test"""
txt = open(now).read().rstrip()
rv, out = getstatusoutput(f'{prg} -s 1 "{txt}"')
assert rv == 0
expected = """
Now is Ege time [dr all good me- to come to the jid of the party.
""".strip()
assert out.rstrip() == f'You said: "{txt}"\nI heard : "{expected}"'
# --------------------------------------------------
def test_now_cmd_s2_m4():
"""test"""
txt = open(now).read().rstrip()
rv, out = getstatusoutput(f'{prg} -s 2 -m .4 "{txt}"')
assert rv == 0
expected = """
No$ i% khefMiIe sor@all$glo<BmenYts cAAeltaTtheSaid[HYnthe Aalty.
""".strip()
assert out.rstrip() == f'You said: "{txt}"\nI heard : "{expected}"'
# --------------------------------------------------
def test_fox_file_s1():
"""test"""
rv, out = getstatusoutput(f'{prg} --seed 1 {fox}')
assert rv == 0
txt = open(fox).read().rstrip()
expected = "The duic: brown hox jumps over the lkzy dog."
assert out.rstrip() == f'You said: "{txt}"\nI heard : "{expected}"'
# --------------------------------------------------
def test_fox_file_s2_m6():
"""test"""
rv, out = getstatusoutput(f'{prg} --seed 2 --mutations .6 {fox}')
assert rv == 0
txt = open(fox).read().rstrip()
expected = "ZoA@qric` HwdTB Alx$jumIslolXs th^Yl?dy<YoA."
assert out.rstrip() == f'You said: "{txt}"\nI heard : "{expected}"'
# --------------------------------------------------
def random_string():
"""generate a random filename"""
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=5))