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
|
2019-05-29 16:54:26 -07:00
|
|
|
ValueError with a *meaningful* error message if the
|
2014-04-17 13:26:30 +02:00
|
|
|
input turns out to be malformed.
|
|
|
|
|
"""
|
|
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
import unittest
|
|
|
|
|
|
2019-05-29 16:54:26 -07:00
|
|
|
from minesweeper import annotate
|
2014-06-11 15:06:22 +02:00
|
|
|
|
|
|
|
|
|
2018-02-19 23:20:47 +08:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
|
2017-11-13 05:36:02 -08:00
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
class MinesweeperTest(unittest.TestCase):
|
2017-11-13 05:36:02 -08:00
|
|
|
|
|
|
|
|
def test_no_rows(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
self.assertEqual(annotate([]), [])
|
2017-11-13 05:36:02 -08:00
|
|
|
|
|
|
|
|
def test_no_columns(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
self.assertEqual(annotate([""]), [""])
|
2017-11-13 05:36:02 -08:00
|
|
|
|
|
|
|
|
def test_no_mines(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = [" ", " ", " "]
|
|
|
|
|
out = [" ", " ", " "]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
|
|
|
|
|
|
|
|
|
def test_annotate_with_only_mines(self):
|
|
|
|
|
minefield = ["***", "***", "***"]
|
|
|
|
|
out = ["***", "***", "***"]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-11-13 05:36:02 -08:00
|
|
|
def test_mine_surrounded_by_spaces(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = [" ", " * ", " "]
|
|
|
|
|
out = ["111", "1*1", "111"]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-11-13 05:36:02 -08:00
|
|
|
def test_space_surrounded_by_mines(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = ["***", "* *", "***"]
|
|
|
|
|
out = ["***", "*8*", "***"]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-11-13 05:36:02 -08:00
|
|
|
def test_horizontal_line(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = [" * * "]
|
2017-11-13 05:36:02 -08:00
|
|
|
out = ["1*2*1"]
|
2019-05-29 16:54:26 -07:00
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-11-13 05:36:02 -08:00
|
|
|
def test_horizontal_line_mines_at_edges(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = ["* *"]
|
2017-11-13 05:36:02 -08:00
|
|
|
out = ["*1 1*"]
|
2019-05-29 16:54:26 -07:00
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-11-13 05:36:02 -08:00
|
|
|
def test_vertical_line(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = [" ", "*", " ", "*", " "]
|
|
|
|
|
out = ["1", "*", "2", "*", "1"]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-11-13 05:36:02 -08:00
|
|
|
def test_vertical_line_mines_at_edges(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = ["*", " ", " ", " ", "*"]
|
|
|
|
|
out = ["*", "1", " ", "1", "*"]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-11-13 05:36:02 -08:00
|
|
|
def test_cross(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = [" * ", " * ", "*****", " * ", " * "]
|
|
|
|
|
out = [" 2*2 ", "25*52", "*****", "25*52", " 2*2 "]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
|
|
|
|
|
|
|
|
|
def test_large_annotate(self):
|
|
|
|
|
minefield = [" * * ", " * ", " * ",
|
|
|
|
|
" * *", " * * ", " "]
|
|
|
|
|
out = ["1*22*1", "12*322", " 123*2", "112*4*", "1*22*2", "111111"]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
2017-11-13 05:36:02 -08:00
|
|
|
|
|
|
|
|
# Additional test for this track
|
2019-05-29 16:54:26 -07:00
|
|
|
def test_annotate9(self):
|
|
|
|
|
minefield = [" ", " * ", " ", " ", " * "]
|
|
|
|
|
out = [" 111", " 1*1", " 111", "111 ", "1*1 "]
|
|
|
|
|
self.assertEqual(annotate(minefield), out)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_different_len(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = [" ", "* ", " "]
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-05-29 16:54:26 -07:00
|
|
|
annotate(minefield)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
|
|
|
|
def test_invalid_char(self):
|
2019-05-29 16:54:26 -07:00
|
|
|
minefield = ["X * "]
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-05-29 16:54:26 -07:00
|
|
|
annotate(minefield)
|
2014-03-28 22:19:26 -03:00
|
|
|
|
2017-12-12 18:11:43 +00:00
|
|
|
# Utility functions
|
|
|
|
|
def setUp(self):
|
|
|
|
|
try:
|
2018-01-18 10:47:11 -06:00
|
|
|
self.assertRaisesRegex
|
2017-12-12 18:11:43 +00:00
|
|
|
except AttributeError:
|
2018-01-18 10:47:11 -06:00
|
|
|
self.assertRaisesRegex = self.assertRaisesRegexp
|
2017-12-12 18:11:43 +00:00
|
|
|
|
|
|
|
|
def assertRaisesWithMessage(self, exception):
|
|
|
|
|
return self.assertRaisesRegex(exception, r".+")
|
|
|
|
|
|
2014-10-27 10:28:48 +01:00
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|