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
·107 lines (75 loc) · 2.65 KB
/
test.py
File metadata and controls
executable file
·107 lines (75 loc) · 2.65 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
#!/usr/bin/env python3
"""tests for wc.py"""
import os
import random
import re
import string
from subprocess import getstatusoutput
prg = './wc.py'
empty = './inputs/empty.txt'
one_line = './inputs/one.txt'
two_lines = './inputs/two.txt'
fox = '../inputs/fox.txt'
sonnet = '../inputs/sonnet-29.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 re.match("usage", out, re.IGNORECASE)
# --------------------------------------------------
def random_string():
"""generate a random string"""
k = random.randint(5, 10)
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))
# --------------------------------------------------
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_empty():
"""Test on empty"""
rv, out = getstatusoutput(f'{prg} {empty}')
assert rv == 0
assert out.rstrip() == ' 0 0 0 ./inputs/empty.txt'
# --------------------------------------------------
def test_one():
"""Test on one"""
rv, out = getstatusoutput(f'{prg} {one_line}')
assert rv == 0
assert out.rstrip() == ' 1 1 2 ./inputs/one.txt'
# --------------------------------------------------
def test_two():
"""Test on two"""
rv, out = getstatusoutput(f'{prg} {two_lines}')
assert rv == 0
assert out.rstrip() == ' 2 2 4 ./inputs/two.txt'
# --------------------------------------------------
def test_fox():
"""Test on fox"""
rv, out = getstatusoutput(f'{prg} {fox}')
assert rv == 0
assert out.rstrip() == ' 1 9 45 ../inputs/fox.txt'
# --------------------------------------------------
def test_more():
"""Test on more than one file"""
rv, out = getstatusoutput(f'{prg} {fox} {sonnet}')
expected = (' 1 9 45 ../inputs/fox.txt\n'
' 17 118 661 ../inputs/sonnet-29.txt\n'
' 18 127 706 total')
assert rv == 0
assert out.rstrip() == expected
# --------------------------------------------------
def test_stdin():
"""Test on stdin"""
rv, out = getstatusoutput(f'{prg} < {fox}')
assert rv == 0
assert out.rstrip() == ' 1 9 45 <stdin>'