7
7
import string
8
8
from subprocess import getstatusoutput , getoutput
9
9
10
- prg = './twelve_days.py'
11
- day_one = '\n ' .join ([
12
- 'On the first day of Christmas,' , 'My true love gave to me,' ,
13
- 'A partridge in a pear tree.'
14
- ])
15
-
16
- day_two = '\n ' .join ([
17
- 'On the second day of Christmas,' ,
18
- 'My true love gave to me,' ,
19
- 'Two turtle doves,' ,
20
- 'And a partridge in a pear tree.'
21
- ])
10
+ PRG = "./twelve_days.py"
11
+ DAY_ONE = "\n " .join (
12
+ [
13
+ "On the first day of Christmas," ,
14
+ "My true love gave to me," ,
15
+ "A partridge in a pear tree." ,
16
+ ]
17
+ )
18
+
19
+ DAY_TWO = "\n " .join (
20
+ [
21
+ "On the second day of Christmas," ,
22
+ "My true love gave to me," ,
23
+ "Two turtle doves," ,
24
+ "And a partridge in a pear tree." ,
25
+ ]
26
+ )
22
27
23
28
24
29
# --------------------------------------------------
25
30
def test_exists ():
26
31
"""exists"""
27
32
28
- assert os .path .isfile (prg )
33
+ assert os .path .isfile (PRG )
29
34
30
35
31
36
# --------------------------------------------------
32
37
def test_usage ():
33
38
"""usage"""
34
39
35
- for flag in ['-h' , ' --help' ]:
36
- rv , out = getstatusoutput (f' { prg } { flag } ' )
40
+ for flag in ["-h" , " --help" ]:
41
+ rv , out = getstatusoutput (f"python { PRG } { flag } " )
37
42
assert rv == 0
38
43
assert re .match ("usage" , out , re .IGNORECASE )
39
44
@@ -43,7 +48,7 @@ def test_bad_num():
43
48
"""test bad_num"""
44
49
45
50
for n in [random .choice (r ) for r in (range (- 10 , - 1 ), range (13 , 20 ))]:
46
- rv , out = getstatusoutput (f' { prg } -n { n } ' )
51
+ rv , out = getstatusoutput (f"python { PRG } -n { n } " )
47
52
assert rv != 0
48
53
assert re .search (f'--num "{ n } " must be between 1 and 12' , out )
49
54
@@ -52,43 +57,43 @@ def test_bad_num():
52
57
def test_one ():
53
58
"""test one"""
54
59
55
- out = getoutput (f' { prg } -n 1' )
56
- assert out .rstrip () == day_one
60
+ out = getoutput (f"python { PRG } -n 1" )
61
+ assert out .rstrip () == DAY_ONE
57
62
58
63
59
64
# --------------------------------------------------
60
65
def test_two ():
61
66
"""test two"""
62
67
63
- out = getoutput (f' { prg } --num 2' )
64
- assert out == ' \n \n ' .join ([day_one , day_two ])
68
+ out = getoutput (f"python { PRG } --num 2" )
69
+ assert out == " \n \n " .join ([DAY_ONE , DAY_TWO ])
65
70
66
71
67
72
# --------------------------------------------------
68
73
def test_all_stdout ():
69
74
"""test"""
70
75
71
- out = getoutput (f' { prg } ' ).splitlines ()
76
+ out = getoutput (f"python { PRG } " ).splitlines ()
72
77
assert len (out ) == 113
73
- assert out [0 ] == ' On the first day of Christmas,'
74
- assert out [- 1 ] == ' And a partridge in a pear tree.'
78
+ assert out [0 ] == " On the first day of Christmas,"
79
+ assert out [- 1 ] == " And a partridge in a pear tree."
75
80
76
81
77
82
# --------------------------------------------------
78
83
def test_all ():
79
84
"""Test 1-12"""
80
85
81
- test_out = ' ./test-out'
86
+ test_out = " ./test-out"
82
87
assert os .path .isdir (test_out )
83
88
84
89
for n in range (1 , 13 ):
85
90
print (n )
86
91
# Normal run (STDOUT)
87
- expected_file = os .path .join (test_out , f' { n } .out' )
92
+ expected_file = os .path .join (test_out , f" { n } .out" )
88
93
assert os .path .isfile (expected_file )
89
94
expected = open (expected_file ).read ().rstrip ()
90
95
91
- cmd = f' { prg } -n { n } '
96
+ cmd = f"python { PRG } -n { n } "
92
97
out = getoutput (cmd ).rstrip ()
93
98
assert out == expected
94
99
@@ -98,11 +103,11 @@ def test_all():
98
103
os .remove (out_file )
99
104
100
105
try :
101
- out = getoutput (cmd + f' -o { out_file } ' ).rstrip ()
102
- assert out == ''
106
+ out = getoutput (cmd + f" -o { out_file } " ).rstrip ()
107
+ assert out == ""
103
108
assert os .path .isfile (out_file )
104
109
output = open (out_file ).read ().rstrip ()
105
- assert len (output .split (' \n ' )) == len (expected .split (' \n ' ))
110
+ assert len (output .split (" \n " )) == len (expected .split (" \n " ))
106
111
assert output .rstrip () == expected .rstrip ()
107
112
finally :
108
113
if os .path .isfile (out_file ):
@@ -114,4 +119,4 @@ def random_string():
114
119
"""generate a random string"""
115
120
116
121
k = random .randint (5 , 10 )
117
- return '' .join (random .choices (string .ascii_letters + string .digits , k = k ))
122
+ return "" .join (random .choices (string .ascii_letters + string .digits , k = k ))
0 commit comments