Created a TestCase subclass to use for tests, too.
import render.text
def main():
- grid = Grid.from_string("""
- 3...2...7
- ...3.1...
- ..6...4..
- 89.....54
- 4...5...8
- 61.....32
- ..8...2..
- ...5.9...
- 1...6...9
- """)
-
- grid.add_constraint(Diagonal(grid, direction='down', offset=0))
- grid.add_constraint(Diagonal(grid, direction='up', offset=0))
-
- r = render.text.AsciiArtGridRenderer()
- print r.render_grid(grid)
-
- grid.solve()
-
- print r.render_grid(grid)
+ print "I don't do anything any more!"
+from unittest import TestCase
+
+from pseudoku.grid import Grid
+
+class SudokuTestCase(TestCase):
+ """TestCase subclass that provides some convenience methods for testing
+ sudoku puzzles.
+ """
+
+ def assertSolvable(self, grid, msg=None):
+ """Asserts that the given puzzle, when run through Grid.solve(),
+ produces a filled grid.
+
+ grid may be either a Grid object or a string.
+ """
+
+ if type(grid) == str:
+ grid = Grid.from_string(grid)
+
+ grid.solve()
+ self.assertTrue(grid.filled, msg)
--- /dev/null
+from pseudoku.grid import Grid
+from pseudoku.grid.constraints import Diagonal
+
+from pseudoku.tests import SudokuTestCase
+
+class DiagonalGridTestCase(SudokuTestCase):
+ """Tests that grids with extra non-standard constraints are solved."""
+
+
+ def test_main_diagonals(self):
+ grid = Grid.from_string("""
+ 3...2...7 ...3.1... ..6...4..
+ 89.....54 4...5...8 61.....32
+ ..8...2.. ...5.9... 1...6...9
+ """)
+ grid.add_constraint(Diagonal(grid, direction='down', offset=0))
+ grid.add_constraint(Diagonal(grid, direction='up', offset=0))
+ self.assertSolvable(grid, "Main diagonals (Color Sudoku, page 8)")
-from unittest import TestCase
-
from pseudoku.grid import Grid
-class SimpleGridTestCase(TestCase):
+from pseudoku.tests import SudokuTestCase
+
+class SimpleGridTestCase(SudokuTestCase):
"""Tests the solving integrity of some puzzles that can be solved using
only elimination and uniqueness.
"""
- def _test_single_puzzle(self, puzzle, msg=None):
- """Solves a single puzzle and verifies that the result is, at least,
- not incorrect.
- """
-
- grid = Grid.from_string(puzzle)
- grid.solve()
- self.assertTrue(grid.filled, msg)
-
def test_simple(self):
- self._test_single_puzzle("""
+ self.assertSolvable("""
...69.... 9.5..876. ..4..1.2.
6...5...3 38.....49 7...3...2
.7.9..3.. .231..4.8 ....83...