FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tiny_python_projects/11_bottles_of_beer/test.py at master · mcmarto/tiny_python_projects · GitHub
mcmarto
/
tiny_python_projects
Public
forked from
kyclark/tiny_python_projects
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tiny_python_projects
/
11_bottles_of_beer
/
test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
114 lines (82 loc) · 2.98 KB
Breadcrumbs
tiny_python_projects
/
11_bottles_of_beer
/
test.py
Copy path
File metadata and controls
executable file
·
114 lines (82 loc) · 2.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python3
"""tests for bottles.py"""
import
hashlib
import
os
import
random
import
re
import
string
from
subprocess
import
getstatusoutput
prg
=
'./bottles.py'
# --------------------------------------------------
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
test_bad_int
():
"""Bad integer value"""
bad
=
random
.
randint
(
-
10
,
1
)
rv
,
out
=
getstatusoutput
(
f'
{
prg
}
-n
{
bad
}
'
)
assert
rv
!=
0
assert
re
.
search
(
f'--num "
{
bad
}
" must be greater than 0'
,
out
)
# --------------------------------------------------
def
test_float
():
"""float value"""
bad
=
round
(
random
.
random
()
*
10
,
2
)
rv
,
out
=
getstatusoutput
(
f'
{
prg
}
--num
{
bad
}
'
)
assert
rv
!=
0
assert
re
.
search
(
f"invalid int value: '
{
bad
}
'"
,
out
)
# --------------------------------------------------
def
test_str
():
"""str value"""
bad
=
random_string
()
rv
,
out
=
getstatusoutput
(
f'
{
prg
}
-n
{
bad
}
'
)
assert
rv
!=
0
assert
re
.
search
(
f"invalid int value: '
{
bad
}
'"
,
out
)
# --------------------------------------------------
def
test_one
():
"""One bottle of beer"""
expected
=
(
'1 bottle of beer on the wall,
\n
'
'1 bottle of beer,
\n
'
'Take one down, pass it around,
\n
'
'No more bottles of beer on the wall!'
)
rv
,
out
=
getstatusoutput
(
f'
{
prg
}
--num 1'
)
assert
rv
==
0
assert
out
==
expected
# --------------------------------------------------
def
test_two
():
"""Two bottles of beer"""
expected
=
(
'2 bottles of beer on the wall,
\n
'
'2 bottles of beer,
\n
'
'Take one down, pass it around,
\n
'
'1 bottle of beer on the wall!
\n
\n
'
'1 bottle of beer on the wall,
\n
'
'1 bottle of beer,
\n
'
'Take one down, pass it around,
\n
'
'No more bottles of beer on the wall!'
)
rv
,
out
=
getstatusoutput
(
f'
{
prg
}
-n 2'
)
assert
rv
==
0
assert
out
==
expected
# --------------------------------------------------
def
test_random
():
"""Random number"""
sums
=
dict
(
map
(
lambda
x
:
x
.
split
(
'
\t
'
),
open
(
'sums.txt'
).
read
().
splitlines
()))
for
n
in
random
.
choices
(
list
(
sums
.
keys
()),
k
=
10
):
flag
=
'-n'
if
random
.
choice
([
0
,
1
])
==
1
else
'--num'
rv
,
out
=
getstatusoutput
(
f'
{
prg
}
{
flag
}
{
n
}
'
)
out
+=
'
\n
'
# because the last newline is removed
assert
rv
==
0
assert
hashlib
.
md5
(
out
.
encode
(
'utf-8'
)).
hexdigest
()
==
sums
[
n
]
# --------------------------------------------------
def
random_string
():
"""generate a random string"""
k
=
random
.
randint
(
5
,
10
)
return
''
.
join
(
random
.
choices
(
string
.
ascii_letters
+
string
.
digits
,
k
=
k
))
Back
|
FazBrowse Home
|
New Git URL