Skip to content

Commit 2e94f78

Browse files
committed
cleanup, last chapter
1 parent 2543d68 commit 2e94f78

File tree

7 files changed

+91
-22
lines changed

7 files changed

+91
-22
lines changed

05_howler/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_usage():
3737
"""usage"""
3838

3939
for flag in ['-h', '--help']:
40-
rv, out = getstatusoutput('{} {}'.format(prg, flag))
40+
rv, out = getstatusoutput(f'{prg} {flag}')
4141
assert rv == 0
4242
assert re.match("usage", out, re.IGNORECASE)
4343

06_wc/test.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_usage():
2424
"""usage"""
2525

2626
for flag in ['-h', '--help']:
27-
rv, out = getstatusoutput('{} {}'.format(prg, flag))
27+
rv, out = getstatusoutput(f'{prg} {flag}')
2828
assert rv == 0
2929
assert re.match("usage", out, re.IGNORECASE)
3030

@@ -42,7 +42,7 @@ def test_bad_file():
4242
"""bad_file"""
4343

4444
bad = random_string()
45-
rv, out = getstatusoutput('{} {}'.format(prg, bad))
45+
rv, out = getstatusoutput(f'{prg} {bad}')
4646
assert rv != 0
4747
assert re.search(f"No such file or directory: '{bad}'", out)
4848

@@ -51,8 +51,7 @@ def test_bad_file():
5151
def test_empty():
5252
"""Test on empty"""
5353

54-
rv, out = getstatusoutput('{} ./empty.txt'.format(prg))
55-
54+
rv, out = getstatusoutput(f'{prg} ./empty.txt')
5655
assert rv == 0
5756
assert out.rstrip() == ' 0 0 0 ./empty.txt'
5857

@@ -61,8 +60,7 @@ def test_empty():
6160
def test_one():
6261
"""Test on one"""
6362

64-
rv, out = getstatusoutput('{} ./one.txt'.format(prg))
65-
63+
rv, out = getstatusoutput(f'{prg} ./one.txt')
6664
assert rv == 0
6765
assert out.rstrip() == ' 1 1 2 ./one.txt'
6866

@@ -71,8 +69,7 @@ def test_one():
7169
def test_two():
7270
"""Test on two"""
7371

74-
rv, out = getstatusoutput('{} ./two.txt'.format(prg))
75-
72+
rv, out = getstatusoutput(f'{prg} ./two.txt')
7673
assert rv == 0
7774
assert out.rstrip() == ' 2 2 4 ./two.txt'
7875

@@ -81,8 +78,7 @@ def test_two():
8178
def test_fox():
8279
"""Test on fox"""
8380

84-
rv, out = getstatusoutput('{} {}'.format(prg, fox))
85-
81+
rv, out = getstatusoutput(f'{prg} {fox}')
8682
assert rv == 0
8783
assert out.rstrip() == ' 1 9 45 ../inputs/fox.txt'
8884

@@ -91,8 +87,7 @@ def test_fox():
9187
def test_more():
9288
"""Test on more than one file"""
9389

94-
rv, out = getstatusoutput('{} {} {}'.format(prg, fox, sonnet))
95-
90+
rv, out = getstatusoutput(f'{prg} {fox} {sonnet}')
9691
expected = (' 1 9 45 ../inputs/fox.txt\n'
9792
' 17 118 661 ../inputs/sonnet-29.txt\n'
9893
' 18 127 706 total')
@@ -104,7 +99,6 @@ def test_more():
10499
def test_stdin():
105100
"""Test on stdin"""
106101

107-
rv, out = getstatusoutput('{} < {}'.format(prg, fox))
108-
102+
rv, out = getstatusoutput(f'{prg} < {fox}')
109103
assert rv == 0
110104
assert out.rstrip() == ' 1 9 45 <stdin>'

08_apples_and_bananas/test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_usage():
2121
"""usage"""
2222

2323
for flag in ['-h', '--help']:
24-
rv, out = getstatusoutput('{} {}'.format(prg, flag))
24+
rv, out = getstatusoutput(f'{prg} {flag}')
2525
assert rv == 0
2626
assert re.match("usage", out, re.IGNORECASE)
2727

@@ -30,7 +30,7 @@ def test_usage():
3030
def test_bad_vowel():
3131
"""Should fail on a bad vowel"""
3232

33-
rv, out = getstatusoutput('{} -v x foo'.format(prg))
33+
rv, out = getstatusoutput(f'{prg} -v x foo')
3434
assert rv != 0
3535
assert re.match("usage", out, re.IGNORECASE)
3636

@@ -39,37 +39,37 @@ def test_bad_vowel():
3939
def test_command_line():
4040
""" foo -> faa """
4141

42-
out = getoutput('{} foo'.format(prg))
42+
out = getoutput(f'{prg} foo')
4343
assert out.strip() == 'faa'
4444

4545

4646
# --------------------------------------------------
4747
def test_command_line_with_vowel():
4848
""" foo -> fii """
4949

50-
out = getoutput('{} -v i foo'.format(prg))
50+
out = getoutput(f'{prg} -v i foo')
5151
assert out.strip() == 'fii'
5252

5353

5454
# --------------------------------------------------
5555
def test_command_line_with_vowel_preserve_case():
5656
""" foo -> fii """
5757

58-
out = getoutput('{} "APPLES AND BANANAS" --vowel i'.format(prg))
58+
out = getoutput(f'{prg} "APPLES AND BANANAS" --vowel i')
5959
assert out.strip() == 'IPPLIS IND BININIS'
6060

6161

6262
# --------------------------------------------------
6363
def test_file():
6464
""" fox.txt """
6565

66-
out = getoutput('{} {}'.format(prg, fox))
66+
out = getoutput(f'{prg} {fox}')
6767
assert out.strip() == 'Tha qaack brawn fax jamps avar tha lazy dag.'
6868

6969

7070
# --------------------------------------------------
7171
def test_file_with_vowel():
7272
""" fox.txt """
7373

74-
out = getoutput('{} --vowel o {}'.format(prg, fox))
74+
out = getoutput(f'{prg} --vowel o {fox}')
7575
assert out.strip() == 'Tho qoock brown fox jomps ovor tho lozy dog.'

22_itictactoe/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: test
2+
3+
test:
4+
pytest -xv test.py
5+
6+
unit:
7+
pytest -xv unit.py

22_itictactoe/unit.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from tictactoe import format_board, find_winner
2+
import random
3+
4+
5+
# --------------------------------------------------
6+
def test_board_no_state():
7+
"""makes default board"""
8+
9+
board = """
10+
-------------
11+
| 1 | 2 | 3 |
12+
-------------
13+
| 4 | 5 | 6 |
14+
-------------
15+
| 7 | 8 | 9 |
16+
-------------
17+
""".strip()
18+
19+
assert format_board('.' * 9) == board
20+
21+
22+
# --------------------------------------------------
23+
def test_board_with_state():
24+
"""makes board"""
25+
26+
board = """
27+
-------------
28+
| 1 | 2 | 3 |
29+
-------------
30+
| O | X | X |
31+
-------------
32+
| 7 | 8 | 9 |
33+
-------------
34+
""".strip()
35+
36+
assert format_board('...OXX...') == board
37+
38+
39+
# --------------------------------------------------
40+
def test_winning():
41+
"""test winning states"""
42+
43+
wins = [('PPP......'), ('...PPP...'), ('......PPP'), ('P..P..P..'),
44+
('.P..P..P.'), ('..P..P..P'), ('P...P...P'), ('..P.P.P..')]
45+
46+
for player in 'XO':
47+
other_player = 'O' if player == 'X' else 'X'
48+
49+
for state in wins:
50+
state = state.replace('P', player)
51+
dots = [i for i in range(len(state)) if state[i] == '.']
52+
mut = random.sample(dots, k=2)
53+
test_state = ''.join([
54+
other_player if i in mut else state[i]
55+
for i in range(len(state))
56+
])
57+
assert find_winner(test_state) == player
58+
59+
60+
# --------------------------------------------------
61+
def test_losing():
62+
"""test losing states"""
63+
64+
losing_state = list('XXOO.....')
65+
66+
for i in range(10):
67+
random.shuffle(losing_state)
68+
assert find_winner(''.join(losing_state)) == None

0 commit comments

Comments
 (0)