Skip to content

Instantly share code, notes, and snippets.

@kunalb
Last active May 2, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunalb/51cf93418c72cc55a16f3804948f3583 to your computer and use it in GitHub Desktop.
Save kunalb/51cf93418c72cc55a16f3804948f3583 to your computer and use it in GitHub Desktop.
#!/bin/env/python3
import unittest
from ttt.core import Grid, Pt, Sym, InvalidSetException
class TestGrid(unittest.TestCase):
def test_set_get(self):
grid = Grid()
grid.set(Pt['A'], Sym.X)
for pt in Pt:
if pt == Pt.A:
self.assertEqual(grid.get(pt), Sym.X)
else:
self.assertEqual(grid.get(pt), None)
def test_get_empty(self):
grid = Grid()
for pt in Pt:
self.assertEqual(grid.get(pt), None)
def test_set_repeated(self):
grid = Grid()
grid.set(Pt['A'], Sym.X)
with self.assertRaises(InvalidSetException):
grid.set(Pt['A'], Sym.O)
def test_no_victory(self):
grid = Grid()
self.assertEqual(grid.winner(), None)
def test_victories(self):
lines = [
{Pt.A, Pt.E, Pt.I},
{Pt.A, Pt.B, Pt.C},
{Pt.G, Pt.H, Pt.I},
{Pt.C, Pt.F, Pt.I},
]
for line in lines:
grid = Grid()
for pt in line:
self.assertEqual(grid.winner(), None)
grid.set(pt, Sym.X)
self.assertEqual(
grid.winner(),
(Sym.X, line))
if __name__ == "__main__":
unittest.main()
#!/bin/env/python3
import io
import re
import unittest
from contextlib import redirect_stdout
from ttt.core import Grid, Pt, Sym, InvalidSetException
from ttt.draw import draw
TEST_BOARD="""
A | B | C
---+---+---
D | E | F
---+---+---
G | H | I
"""
TEST_BOARD_VICTORIES=[
((Pt.D, Pt.E, Pt.F), """
A | B | C
---+---+---
X - X - X
---+---+---
G | H | I
"""),
((Pt.B, Pt.E, Pt.H), """
A | X | C
---+-|-+---
D | X | F
---+-|-+---
G | X | I
"""),
((Pt.A, Pt.E, Pt.I), """
X | B | C
---\---+---
D | X | F
---+---\---
G | H | X
"""),
((Pt.C, Pt.E, Pt.G), """
A | B | X
---+---/---
D | X | F
---/---+---
X | H | I
"""),
]
class TestDraw(unittest.TestCase):
def test_draw_empty(self):
output = io.StringIO()
with redirect_stdout(output):
draw(Grid())
self.assertEqual(TEST_BOARD, output.getvalue())
def test_draw_partial(self):
grid = Grid()
grid.set(Pt.E, Sym.X)
grid.set(Pt.A, Sym.O)
output = io.StringIO()
with redirect_stdout(output):
draw(grid)
self.assertEqual(
TEST_BOARD.replace("E", "X").replace("A", "O"),
output.getvalue())
def test_draw_filled(self):
grid = Grid()
for pt in Pt:
sym = Sym.X
if pt in {Pt.C, Pt.D, Pt.I}:
sym = Sym.O
grid.set(pt, sym)
output = io.StringIO()
with redirect_stdout(output):
draw(grid)
self.assertEqual(
re.sub(r'[A-I]', 'X',
re.sub(r'[CDI]', 'O', TEST_BOARD)),
output.getvalue())
def test_draw_victory(self):
for moves, board in TEST_BOARD_VICTORIES:
grid = Grid()
output = io.StringIO()
for pt in moves:
grid.set(pt, Sym.X)
with redirect_stdout(output):
draw(grid)
self.assertEqual(board, output.getvalue())
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment