def test_check_word_lengths(): """Ensure that we verify that word lengths add up to the grid tiles.""" # Should not raise an error. _check_word_lengths(Grid.from_string("aaaa"), [1, 3]) with pytest.raises(SystemExit): _check_word_lengths(Grid.from_string("aaaa"), [1, 2])
def test_grid_factory_constructor_fails_on_non_perfect_square(): """Ensure that we don't accept an input string of a non perfect square.""" try: Grid.from_string("abc") except ValueError as e: assert str(e) == \ "Length of input string (3) was not a perfect square: abc" else: assert False, "Failed to raise ValueError."
def test_grid_delete_word(): """Ensure that we delete a word safely; see the note on `Grid.delete_word`. """ grid = Grid.from_string("aaabbbccc") new_grid = grid.delete_word([(1, 0), (0, 0)]) assert new_grid._rows == [["", "a", "a"], ["", "b", "b"], ["c", "c", "c"]]
def test_grid_to_bool(example_grid): """Ensure that a grid is converted to bool depending on whether it is empty. """ assert bool(example_grid) grid = Grid.from_string("a") del grid[0, 0] assert not bool(grid)
def test_grid_delete_tile(): """Ensure that deleting a tile causes it to disappear and the above tiles to fall down. """ input_string = "aaabbbccc" grid = Grid.from_string(input_string) del grid[2, 0] expected_grid = [["", "a", "a"], ["a", "b", "b"], ["b", "c", "c"]] assert grid._rows == expected_grid
from wordacademysolver.grid import Grid from wordacademysolver.prefixtree import PrefixTree from wordacademysolver.solver import _indices_to_word, _words_of_length, Solver WORD_LIST = {"hello", "got", "python", "pyth", "foo", "bar", "baz"} """A sample list of words.""" GRID = Grid.from_string("gpt" "noy" "oht") """A sample grid.""" def test_indices_to_word(): """Ensure that we can convert a set of indices through a grid into its corresponding word. """ word = "python" indices = [(0, 1), (1, 2), (2, 2), (2, 1), (2, 0), (1, 0)] assert _indices_to_word(GRID, indices) == word def test_words_of_length(): """Ensure that we can find words of a given length.""" tree = PrefixTree(WORD_LIST) # The result should be "got" in two different ways. expected = [[(0, 0), (1, 1), (0, 2)], [(0, 0), (1, 1), (2, 2)]]
def test_grid_factory_constructor_fails_on_empty_grid(): """Ensure that we don't accept empty grids.""" with pytest.raises(ValueError): Grid.from_string("")
def test_grid_factory_constructor(example_grid_array, example_grid_string): """Ensure that the factory constructor creates the expected grid.""" grid = Grid.from_string(example_grid_string) assert grid._rows == example_grid_array