Refactor 4x4 board test to use pytest fixtures

- Update missing symoblic link to boggler src files to access wordlists
This commit is contained in:
2023-05-24 17:37:51 -04:00
parent d81f5e28cc
commit 7500720992
2 changed files with 154 additions and 109 deletions
+1 -1
View File
@@ -1 +1 @@
../src/boggler
../boggler
+61 -16
View File
@@ -5,15 +5,20 @@ from os import path
import pytest
from boggler.boggler_utils import BoggleBoard, build_full_boggle_tree
wordlists_dir = path.abspath("./boggler/wordlists/dwyl/")
letters = [
WORDLISTS_DIR = path.abspath("./boggler/wordlists/dwyl/")
@pytest.fixture
def board_4x4():
return [
['u','n','r','e'],
['qu','n','i','l'],
['i','s','h','a'],
['s','e','l','b']
]
expected_words_max_4 = [
@pytest.fixture
def expected_words_max_4():
return [
"u", "un", "unn", "uns", "uni", "unie", "uni", "unie", "un", "unn", "uni",
"unie", "n", "ni", "nis", "nisi", "nil", "nile", "nu", "nun", "nuns", "nr", "r",
"rin", "rie", "riel", "risqu", "rise", "riss", "ria", "rial", "rial", "rin",
@@ -46,7 +51,9 @@ expected_words_max_4 = [
"bals", "bale", "bah", "bhil", "bl", "bls", "blah"
]
expected_words_max_20 = [
@pytest.fixture
def expected_words_max_20():
return [
"u","un","unn","unrelinquishable","unrelishable","unreliable","unrein","uns",
"unsin","unslain","unshale","unshale","uni","unie","unhale","unhair","unhale",
"uni","unie","un","unn","uni","unie","unrelishable","unreliable",
@@ -104,31 +111,69 @@ expected_words_max_20 = [
"blahs","bless","bless"
]
# Init for word tree (4 letters or less)
board_4 = BoggleBoard(letters, 4)
tree_4 = build_full_boggle_tree(board_4, wordlists_dir)
found_words_4 = list(chain.from_iterable([x.word_paths for x in tree_4.values()]))
# -----------------------------------------------------
# Solved board max 4-letter words
# -----------------------------------------------------
# Init for word tree (20 letters or less)
board_20 = BoggleBoard(letters, 20)
tree_20 = build_full_boggle_tree(board_20, wordlists_dir)
found_words_20 = list(chain.from_iterable([x.word_paths for x in tree_20.values()]))
# Init for word tree (4 board_4x4 or less) and fixtures
@pytest.fixture
def board_4(board_4x4):
return BoggleBoard(board_4x4, 4)
def test_word_count_4_letters_max():
@pytest.fixture
def tree_4(board_4):
return build_full_boggle_tree(board_4, WORDLISTS_DIR)
@pytest.fixture
def found_words_4(tree_4):
return list(chain.from_iterable([x.word_paths for x in tree_4.values()]))
# Tests for solved board with max of 4-letter words
def test_word_count_4_board_4x4_max(expected_words_max_4, tree_4):
assert len(expected_words_max_4) == sum(map(lambda x: len(x.word_paths), tree_4.values()))
def test_words_full_4_letters_max():
def test_words_full_4_board_4x4_max(expected_words_max_4, found_words_4, tree_4):
found_words = [x.word_paths for x in tree_4.values()]
for letter, subtree in tree_4.items():
for i in range(0, min(len(expected_words_max_4), len(found_words_4))):
assert expected_words_max_4[i] == found_words_4[i][0]
def test_word_count_20_letters_max():
def test_format_words_4_to_csv():
pass
def test_format_words_4_to_json():
pass
# -----------------------------------------------------
# Solved board max 20-letter words
# -----------------------------------------------------
# Init for word tree (20 board_4x4 or less) and fixtures
@pytest.fixture
def board_20(board_4x4):
return BoggleBoard(board_4x4, 20)
@pytest.fixture
def tree_20(board_20):
return build_full_boggle_tree(board_20, WORDLISTS_DIR)
@pytest.fixture
def found_words_20(tree_20):
return list(chain.from_iterable([x.word_paths for x in tree_20.values()]))
# Tests for solved board with max of 20-letter words
def test_word_count_20_board_4x4_max(expected_words_max_20, tree_20):
assert len(expected_words_max_20) == sum(map(lambda x: len(x.word_paths), tree_20.values()))
def test_words_full_20_letters_max():
def test_words_full_20_board_4x4_max(expected_words_max_20, found_words_20, tree_20):
found_words = [x.word_paths for x in tree_20.values()]
for letter, subtree in tree_20.items():
for i in range(0, min(len(expected_words_max_20), len(found_words_20))):
assert expected_words_max_20[i] == found_words_20[i][0]
def test_format_words_20_to_csv():
pass
def test_format_words_20_to_json():
pass