2014-04-17 13:26:30 +02:00
|
|
|
""" Tests for the minesweeper exercise
|
|
|
|
|
|
|
|
|
|
Implementation note:
|
|
|
|
|
The board function must validate its input and raise a
|
|
|
|
|
ValueError with a meaningfull error message if the
|
|
|
|
|
input turns out to be malformed.
|
|
|
|
|
"""
|
|
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
import unittest
|
|
|
|
|
|
2014-06-11 15:06:22 +02:00
|
|
|
from minesweeper import board
|
|
|
|
|
|
|
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
class MinesweeperTest(unittest.TestCase):
|
|
|
|
|
def test_board1(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+------+",
|
|
|
|
|
"| * * |",
|
|
|
|
|
"| * |",
|
|
|
|
|
"| * |",
|
|
|
|
|
"| * *|",
|
|
|
|
|
"| * * |",
|
|
|
|
|
"| |",
|
|
|
|
|
"+------+"]
|
|
|
|
|
out = ["+------+",
|
|
|
|
|
"|1*22*1|",
|
|
|
|
|
"|12*322|",
|
|
|
|
|
"| 123*2|",
|
|
|
|
|
"|112*4*|",
|
|
|
|
|
"|1*22*2|",
|
|
|
|
|
"|111111|",
|
|
|
|
|
"+------+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board2(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-----+",
|
2015-11-08 09:07:52 +01:00
|
|
|
"| * * |",
|
2014-10-27 10:28:48 +01:00
|
|
|
"| |",
|
|
|
|
|
"| * |",
|
|
|
|
|
"| * *|",
|
|
|
|
|
"| * * |",
|
|
|
|
|
"+-----+"]
|
|
|
|
|
out = ["+-----+",
|
|
|
|
|
"|1*2*1|",
|
|
|
|
|
"|11322|",
|
|
|
|
|
"| 12*2|",
|
|
|
|
|
"|12*4*|",
|
|
|
|
|
"|1*3*2|",
|
|
|
|
|
"+-----+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board3(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-----+",
|
|
|
|
|
"| * * |",
|
|
|
|
|
"+-----+"]
|
|
|
|
|
out = ["+-----+",
|
|
|
|
|
"|1*2*1|",
|
|
|
|
|
"+-----+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board4(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-+",
|
|
|
|
|
"|*|",
|
|
|
|
|
"| |",
|
|
|
|
|
"|*|",
|
|
|
|
|
"| |",
|
|
|
|
|
"| |",
|
|
|
|
|
"+-+"]
|
|
|
|
|
out = ["+-+",
|
|
|
|
|
"|*|",
|
|
|
|
|
"|2|",
|
|
|
|
|
"|*|",
|
|
|
|
|
"|1|",
|
|
|
|
|
"| |",
|
|
|
|
|
"+-+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board5(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-+",
|
|
|
|
|
"|*|",
|
|
|
|
|
"+-+"]
|
|
|
|
|
out = ["+-+",
|
|
|
|
|
"|*|",
|
|
|
|
|
"+-+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board6(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+--+",
|
|
|
|
|
"|**|",
|
|
|
|
|
"|**|",
|
|
|
|
|
"+--+"]
|
|
|
|
|
out = ["+--+",
|
|
|
|
|
"|**|",
|
|
|
|
|
"|**|",
|
|
|
|
|
"+--+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board7(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+--+",
|
|
|
|
|
"|**|",
|
|
|
|
|
"|**|",
|
|
|
|
|
"+--+"]
|
|
|
|
|
out = ["+--+",
|
|
|
|
|
"|**|",
|
|
|
|
|
"|**|",
|
|
|
|
|
"+--+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board8(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+---+",
|
|
|
|
|
"|***|",
|
|
|
|
|
"|* *|",
|
|
|
|
|
"|***|",
|
|
|
|
|
"+---+"]
|
|
|
|
|
out = ["+---+",
|
|
|
|
|
"|***|",
|
|
|
|
|
"|*8*|",
|
|
|
|
|
"|***|",
|
|
|
|
|
"+---+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_board9(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-----+",
|
|
|
|
|
"| |",
|
|
|
|
|
"| * |",
|
|
|
|
|
"| |",
|
|
|
|
|
"| |",
|
|
|
|
|
"| * |",
|
|
|
|
|
"+-----+"]
|
|
|
|
|
out = ["+-----+",
|
|
|
|
|
"| 111|",
|
|
|
|
|
"| 1*1|",
|
|
|
|
|
"| 111|",
|
|
|
|
|
"|111 |",
|
|
|
|
|
"|1*1 |",
|
|
|
|
|
"+-----+"]
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(board(inp), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_different_len(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-+",
|
|
|
|
|
"| |",
|
|
|
|
|
"|* |",
|
|
|
|
|
"| |",
|
|
|
|
|
"+-+"]
|
2017-10-21 03:17:15 -05:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
|
board(inp)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_faulty_border(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-----+",
|
|
|
|
|
"* * |",
|
|
|
|
|
"+-- --+"]
|
2017-10-21 03:17:15 -05:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
|
board(inp)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_invalid_char(self):
|
2014-10-27 10:28:48 +01:00
|
|
|
inp = ["+-----+",
|
2015-11-08 09:07:52 +01:00
|
|
|
"|X * |",
|
2014-10-27 10:28:48 +01:00
|
|
|
"+-----+"]
|
2017-10-21 03:17:15 -05:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
|
board(inp)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2014-10-27 10:28:48 +01:00
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|