class TestCrosswordIndexing: def test_index_setter_word(self, xw): xw[ACROSS, 5] = "qZb" assert xw[ACROSS, 5].value == "QZB" assert xw[DOWN, 1].value == "BBZ" def test_index_setter_letter(self, xw): xw[2, 1] = "z" assert xw[ACROSS, 5].value == " Z " assert xw[DOWN, 1].value == "BBZ" def test_wrong_word_length(self, xw): for bad_val in ["AA", "AAAA"]: with pytest.raises(ValueError): xw[ACROSS, 5] = bad_val with pytest.raises(ValueError): xw[1, 0] = "AA" @pytest.mark.parametrize( "bad_val", [["A", "A"], 72, Crossword(2)[ACROSS, 1]]) def test_wrong_types(self, xw, bad_val): with pytest.raises(ValueError): xw[ACROSS, 5] = bad_val @pytest.mark.parametrize("bad_index", [(ACROSS, 2), DOWN, (1, 1, 0), (1, slice(1, 2))]) def test_invalid_index(self, xw, bad_index): with pytest.raises(IndexError): xw[bad_index] with pytest.raises(IndexError): xw[bad_index] = "A"
def test_parsing_indices(self): xw = Crossword(5) BLACK_inds = [(0, 0), (0, 1), (2, 2), (4, 4), (4, 3)] for ind in BLACK_inds: xw[ind] = BLACK expected_keys = [ (ACROSS, 1), (ACROSS, 4), (ACROSS, 6), (ACROSS, 7), (ACROSS, 8), (ACROSS, 10), (DOWN, 1), (DOWN, 2), (DOWN, 3), (DOWN, 4), (DOWN, 5), (DOWN, 9), ] assert set(expected_keys) == set([w.index for w in xw.iterwords()])
def xw(word_list: WordList) -> Crossword: """ ┌───┬───┬───┬───┐ │███│ B │ C │ D │ ├───┼───┼───┼───┤ │ A │ B │ C │ D │ ├───┼───┼───┼───┤ │ │ │ │███│ └───┴───┴───┴───┘ ┌───┬───┬───┬───┐ │███│1 │2 │3 │ ├───┼───┼───┼───┤ │4 │ │ │ │ ├───┼───┼───┼───┤ │5 │ │ │███│ └───┴───┴───┴───┘ """ xw = Crossword(3, 4, word_list=word_list) xw[0, 0] = BLACK xw[ACROSS, 1] = "BCD" xw[ACROSS, 4] = "ABCD" return xw
def test_symmetric_image(xw): assert xw[DOWN, 3].symmetric_image.index == (DOWN, 4) diag_xw = Crossword(5, symmetry=Symmetry.NW_DIAGONAL) assert diag_xw[ACROSS, 6].symmetric_image.index == (DOWN, 2)
def test_from_puz(self): Crossword.from_puz(Path(__file__).parent / "dummy.puz")
def test_to_puz(self, xw, tmp_path): filename = tmp_path / "test.puz" xw.to_puz(filename) assert tmp_path.exists() loaded = Crossword.from_puz(filename) assert all(l == x for l, x in zip(loaded.itercells(), xw.itercells()))
def test_symmetry_requirements(): with pytest.raises(ValueError): Crossword(5, 7, symmetry=Symmetry.FULL)
def test_get_word_at_index(self, xw: Crossword): assert xw.get_word_at_index((1, 1), ACROSS) == xw[ACROSS, 4] assert xw.get_word_at_index((1, 1), DOWN) == xw[DOWN, 1] assert xw.get_word_at_index((0, 0), ACROSS) is None
def test_num_rows_cols(self): xw = Crossword(5, 7) assert xw.num_rows == 5 assert xw.num_cols == 7
def test_symmetric_cell_index(self): xw = Crossword(5, 7) assert xw.get_symmetric_cell_index((0, 1)) == (4, 5) assert xw.get_symmetric_cell_index((2, 3)) == (2, 3)