8
8
import string
9
9
from subprocess import getstatusoutput
10
10
11
- prg = ' ./bottles.py'
11
+ PRG = " ./bottles.py"
12
12
13
13
14
14
# --------------------------------------------------
15
15
def test_exists ():
16
16
"""exists"""
17
17
18
- assert os .path .isfile (prg )
18
+ assert os .path .isfile (PRG )
19
19
20
20
21
21
# --------------------------------------------------
22
22
def test_usage ():
23
23
"""usage"""
24
24
25
- for flag in ['-h' , ' --help' ]:
26
- rv , out = getstatusoutput (f' { prg } { flag } ' )
25
+ for flag in ["-h" , " --help" ]:
26
+ rv , out = getstatusoutput (f"python { PRG } { flag } " )
27
27
assert rv == 0
28
28
assert re .match ("usage" , out , re .IGNORECASE )
29
29
@@ -33,17 +33,17 @@ def test_bad_int():
33
33
"""Bad integer value"""
34
34
35
35
bad = random .randint (- 10 , 0 )
36
- rv , out = getstatusoutput (f' { prg } -n { bad } ' )
36
+ rv , out = getstatusoutput (f"python { PRG } -n { bad } " )
37
37
assert rv != 0
38
- assert re .search (f'--num "{ bad } " must be greater than 0' , out )
38
+ assert re .search (f'--num: "{ bad } " must be greater than 0' , out )
39
39
40
40
41
41
# --------------------------------------------------
42
42
def test_float ():
43
43
"""float value"""
44
44
45
45
bad = round (random .random () * 10 , 2 )
46
- rv , out = getstatusoutput (f' { prg } --num { bad } ' )
46
+ rv , out = getstatusoutput (f"python { PRG } --num { bad } " )
47
47
assert rv != 0
48
48
assert re .search (f"invalid int value: '{ bad } '" , out )
49
49
@@ -53,7 +53,7 @@ def test_str():
53
53
"""str value"""
54
54
55
55
bad = random_string ()
56
- rv , out = getstatusoutput (f' { prg } -n { bad } ' )
56
+ rv , out = getstatusoutput (f"python { PRG } -n { bad } " )
57
57
assert rv != 0
58
58
assert re .search (f"invalid int value: '{ bad } '" , out )
59
59
@@ -62,12 +62,14 @@ def test_str():
62
62
def test_one ():
63
63
"""One bottle of beer"""
64
64
65
- expected = ('1 bottle of beer on the wall,\n '
66
- '1 bottle of beer,\n '
67
- 'Take one down, pass it around,\n '
68
- 'No more bottles of beer on the wall!' )
65
+ expected = (
66
+ "1 bottle of beer on the wall,\n "
67
+ "1 bottle of beer,\n "
68
+ "Take one down, pass it around,\n "
69
+ "No more bottles of beer on the wall!"
70
+ )
69
71
70
- rv , out = getstatusoutput (f' { prg } --num 1' )
72
+ rv , out = getstatusoutput (f"python { PRG } --num 1" )
71
73
assert rv == 0
72
74
assert out == expected
73
75
@@ -76,16 +78,18 @@ def test_one():
76
78
def test_two ():
77
79
"""Two bottles of beer"""
78
80
79
- expected = ('2 bottles of beer on the wall,\n '
80
- '2 bottles of beer,\n '
81
- 'Take one down, pass it around,\n '
82
- '1 bottle of beer on the wall!\n \n '
83
- '1 bottle of beer on the wall,\n '
84
- '1 bottle of beer,\n '
85
- 'Take one down, pass it around,\n '
86
- 'No more bottles of beer on the wall!' )
87
-
88
- rv , out = getstatusoutput (f'{ prg } -n 2' )
81
+ expected = (
82
+ "2 bottles of beer on the wall,\n "
83
+ "2 bottles of beer,\n "
84
+ "Take one down, pass it around,\n "
85
+ "1 bottle of beer on the wall!\n \n "
86
+ "1 bottle of beer on the wall,\n "
87
+ "1 bottle of beer,\n "
88
+ "Take one down, pass it around,\n "
89
+ "No more bottles of beer on the wall!"
90
+ )
91
+
92
+ rv , out = getstatusoutput (f"python { PRG } -n 2" )
89
93
assert rv == 0
90
94
assert out == expected
91
95
@@ -94,21 +98,19 @@ def test_two():
94
98
def test_random ():
95
99
"""Random number"""
96
100
97
- sums = dict (
98
- map (lambda x : x .split ('\t ' ),
99
- open ('sums.txt' ).read ().splitlines ()))
101
+ sums = dict (map (lambda x : x .split ("\t " ), open ("sums.txt" ).read ().splitlines ()))
100
102
101
103
for n in random .choices (list (sums .keys ()), k = 10 ):
102
- flag = '-n' if random .choice ([0 , 1 ]) == 1 else ' --num'
103
- rv , out = getstatusoutput (f' { prg } { flag } { n } ' )
104
- out += ' \n ' # because the last newline is removed
104
+ flag = "-n" if random .choice ([0 , 1 ]) == 1 else " --num"
105
+ rv , out = getstatusoutput (f"python { PRG } { flag } { n } " )
106
+ out += " \n " # because the last newline is removed
105
107
assert rv == 0
106
- assert hashlib .md5 (out .encode (' utf-8' )).hexdigest () == sums [n ]
108
+ assert hashlib .md5 (out .encode (" utf-8" )).hexdigest () == sums [n ]
107
109
108
110
109
111
# --------------------------------------------------
110
112
def random_string ():
111
113
"""generate a random string"""
112
114
113
115
k = random .randint (5 , 10 )
114
- return '' .join (random .choices (string .ascii_letters + string .digits , k = k ))
116
+ return "" .join (random .choices (string .ascii_letters + string .digits , k = k ))
0 commit comments