def test_not_in_board(self): """ If a word is not available in the board, False is returned. """ board = Board(rows=[ ['A', 'C', 'B'], ]) self.assertFalse(board.is_available_route(word=['A', 'B', 'C']))
def test_route(self): """ If there is a route between each letter, True is returned. """ board = Board(rows=[ ['A', 'B', 'C'], ]) self.assertTrue(board.is_available_route(word=['A', 'B', 'C']))
def test_repeated_tile_contents(self): """ A route which uses the different tiles is valid, even if the contents of some of those tiles are the same. """ board = Board(rows=[ ['A', 'A', 'A'], ]) self.assertTrue(board.is_available_route(word=['A', 'A', 'A']))
def test_repeated_tile(self): """ A route which uses the same tile multiple times is not valid, so if this is the only available route, False is returned. """ board = Board(rows=[ ['A', 'B'], ]) self.assertFalse(board.is_available_route(word=['A', 'B', 'A']))
def test_example_board(self): """ Given a known board and language, expected words are found. """ rows = [ ["Qu", "A", "A"], ["A", "L", "G"], ["R", "G", "I"], ] file, path = mkstemp() with io.open(path, mode='w') as file: file.write( dedent(u"""\ AQuA Qua ILA GALGA LA ALA CHRISTMAS MOTHER """)) language = Language(dictionary_path=path) boggle = Boggle(board=Board(rows=rows), valid_words=language.words) expected = set(['GALGA', 'ILA', 'AQuA', 'ALA', 'QuA']) self.assertEqual(boggle.list_words(), expected)
def test_no_route_not_listed(self): """ Words without an available route are not listed as valid results. """ board = Board(rows=[ ['A', 'B', 'C'], ]) valid_words = set(['ACB']) self.assertEqual( Boggle(board=board, valid_words=valid_words).list_words(), set([]), )
def test_list_words(self): """ A set of words with available routes is returned. """ board = Board(rows=[ ['A', 'B', 'C'], ['D', 'E', 'F'], ]) valid_words = set(['ABC', 'DEF', 'GHI']) self.assertEqual( Boggle(board=board, valid_words=valid_words).list_words(), set(['ABC', 'DEF']), )
def main(): boards = [ { 'board': large_board, 'file': 'large_board' }, { 'board': small_board, 'file': 'small_board' }, ] for board in boards: language = Language(dictionary_path="/usr/share/dict/words", data_path="english_words.json") boggle = Boggle(board=Board(rows=board['board']), valid_words=language.words) found_words = boggle.list_words() if not os.path.exists(board['file']): with io.open(board['file'], 'wb') as word_file: for word in found_words: word_file.write(word + '\n') return with io.open(board['file'], 'rb') as word_file: expected_words = set(word.strip() for word in word_file) if len(expected_words) != len(found_words): for word in found_words: if word not in expected_words: print(word + ': found, not expected') for word in expected_words: if word not in found_words: print(word + ': expected, not found')