​a_maze_ing /
tests / test_config.py
159 commits
08-06-2026
323 lines 10 kB
  1from pathlib import Path
  2import pytest
  3from config import (
  4    parse_config,
  5    ConfigError,
  6)
  7
  8
  9def create_config_file(tmp_path: Path, content: str) -> str:
 10    file_path = tmp_path / "test_config.txt"
 11    file_path.write_text(content)
 12    return str(file_path)
 13
 14
 15def test_parse_valid_config_with_comments_and_spaces(tmp_path: Path) -> None:
 16    config_content = """
 17    # Test config
 18    WIDTH=10
 19    HEIGHT  =   20
 20    ENTRY=0, 0 # Test
 21    EXIT=9, 19
 22    # This is a comment
 23    OUTPUT_FILE=maze.hex
 24    PERFECT=True # test
 25    SEED =42
 26    """
 27    file_path = create_config_file(tmp_path, config_content)
 28    config = parse_config(file_path)
 29
 30    assert config.width == 10
 31    assert config.height == 20
 32    assert config.entry == (0, 0)
 33    assert config.exit_ == (9, 19)
 34    assert config.output_file == "maze.hex"
 35    assert config.perfect is True
 36    assert config.seed == 42
 37
 38
 39def test_missing_file() -> None:
 40    with pytest.raises(ConfigError, match="cannot read config"):
 41        parse_config("non_existent_file.txt")
 42
 43
 44def test_missing_mandatory_keys(tmp_path: Path) -> None:
 45    config_content = (
 46        "WIDTH=10\nENTRY=0,0\nEXIT=1,1\nOUTPUT_FILE=out.hex\nPERFECT=True\n"
 47    )
 48    file_path = create_config_file(tmp_path, config_content)
 49
 50    with pytest.raises(ConfigError, match="missing mandatory keys: HEIGHT"):
 51        parse_config(file_path)
 52
 53
 54def test_duplicate_key(tmp_path: Path) -> None:
 55    config_content = "WIDTH=10\nWIDTH=20\n"
 56    file_path = create_config_file(tmp_path, config_content)
 57
 58    with pytest.raises(ConfigError, match="duplicate key `WIDTH`"):
 59        parse_config(file_path)
 60
 61
 62@pytest.mark.parametrize(
 63    "width, height",
 64    [
 65        (1, 10),
 66        (10, 1),
 67        (-5, -5),
 68    ],
 69)
 70def test_invalid_dimensions(tmp_path: Path, width: int, height: int) -> None:
 71    config_content = (
 72        f"WIDTH={width}\nHEIGHT={height}\nENTRY=0,0\n"
 73        f"EXIT=1,1\nOUTPUT_FILE=out.hex\nPERFECT=True\n"
 74    )
 75    file_path = create_config_file(tmp_path, config_content)
 76
 77    with pytest.raises(ConfigError, match="must be >= 2"):
 78        parse_config(file_path)
 79
 80
 81def test_entry_and_exit_are_same(tmp_path: Path) -> None:
 82    config_content = (
 83        "WIDTH=10\nHEIGHT=10\nENTRY=5,5\n"
 84        "EXIT=5,5\nOUTPUT_FILE=out.hex\nPERFECT=True\n"
 85    )
 86    file_path = create_config_file(tmp_path, config_content)
 87
 88    with pytest.raises(ConfigError, match="ENTRY and EXIT must be distinct"):
 89        parse_config(file_path)
 90
 91
 92def _base_config(**overrides: str) -> str:
 93    """Build a valid config body, optionally overriding/adding keys."""
 94    keys = {
 95        "WIDTH": "10",
 96        "HEIGHT": "10",
 97        "ENTRY": "0,0",
 98        "EXIT": "9,9",
 99        "OUTPUT_FILE": "out.hex",
100        "PERFECT": "True",
101    }
102    keys.update(overrides)
103    return "".join(f"{k}={v}\n" for k, v in keys.items())
104
105
106def test_unknown_key_rejected(tmp_path: Path) -> None:
107    file_path = create_config_file(tmp_path, _base_config(BOGUS="1"))
108    with pytest.raises(ConfigError, match="unknown key `BOGUS`"):
109        parse_config(file_path)
110
111
112def test_malformed_line(tmp_path: Path) -> None:
113    file_path = create_config_file(tmp_path, "WIDTH 10\n")
114    with pytest.raises(ConfigError, match="malformed"):
115        parse_config(file_path)
116
117
118def test_perfect_false_case_insensitive(tmp_path: Path) -> None:
119    file_path = create_config_file(tmp_path, _base_config(PERFECT="fAlSe"))
120    config = parse_config(file_path)
121    assert config.perfect is False
122
123
124def test_perfect_invalid_value(tmp_path: Path) -> None:
125    file_path = create_config_file(tmp_path, _base_config(PERFECT="yes"))
126    with pytest.raises(ConfigError, match="PERFECT must be True or False"):
127        parse_config(file_path)
128
129
130def test_seed_not_integer(tmp_path: Path) -> None:
131    file_path = create_config_file(tmp_path, _base_config(SEED="abc"))
132    with pytest.raises(ConfigError, match="SEED must be an integer"):
133        parse_config(file_path)
134
135
136def test_entry_out_of_bounds(tmp_path: Path) -> None:
137    file_path = create_config_file(tmp_path, _base_config(ENTRY="99,0"))
138    with pytest.raises(ConfigError, match="ENTRY .* out of bounds"):
139        parse_config(file_path)
140
141
142def test_entry_not_pair(tmp_path: Path) -> None:
143    file_path = create_config_file(tmp_path, _base_config(ENTRY="5"))
144    with pytest.raises(ConfigError, match="ENTRY must be `x,y`"):
145        parse_config(file_path)
146
147
148def test_algorithm_default_is_dfs(tmp_path: Path) -> None:
149    file_path = create_config_file(tmp_path, _base_config())
150    config = parse_config(file_path)
151    assert config.algorithm == "dfs"
152
153
154@pytest.mark.parametrize("value", ["prim", "Prim", "PRIM", "dfs"])
155def test_algorithm_valid_case_insensitive(tmp_path: Path, value: str) -> None:
156    file_path = create_config_file(tmp_path, _base_config(ALGORITHM=value))
157    config = parse_config(file_path)
158    assert config.algorithm == value.lower()
159
160
161def test_algorithm_invalid_value(tmp_path: Path) -> None:
162    file_path = create_config_file(tmp_path, _base_config(ALGORITHM="bfs"))
163    with pytest.raises(ConfigError, match="ALGORITHM must be dfs or prim"):
164        parse_config(file_path)
165
166
167def test_glyph_default_is_true(tmp_path: Path) -> None:
168    file_path = create_config_file(tmp_path, _base_config())
169    config = parse_config(file_path)
170    assert config.glyph is True
171
172
173def test_glyph_false_allows_small_maze(tmp_path: Path) -> None:
174    config_content = (
175        "WIDTH=8\nHEIGHT=6\nENTRY=0,0\n"
176        "EXIT=7,5\nOUTPUT_FILE=out.hex\nPERFECT=True\nGLYPH=False\n"
177    )
178    file_path = create_config_file(tmp_path, config_content)
179    config = parse_config(file_path)
180    assert config.glyph is False
181    assert config.width == 8
182
183
184def test_glyph_invalid_value(tmp_path: Path) -> None:
185    file_path = create_config_file(tmp_path, _base_config(GLYPH="yes"))
186    with pytest.raises(ConfigError, match="GLYPH must be True or False"):
187        parse_config(file_path)
188
189
190def test_braid_default_is_zero(tmp_path: Path) -> None:
191    file_path = create_config_file(tmp_path, _base_config())
192    config = parse_config(file_path)
193    assert config.braid == 0.0
194
195
196@pytest.mark.parametrize("value", ["0.0", "0.5", "1.0", "0"])
197def test_braid_valid(tmp_path: Path, value: str) -> None:
198    file_path = create_config_file(tmp_path, _base_config(BRAID=value))
199    config = parse_config(file_path)
200    assert config.braid == float(value)
201
202
203def test_braid_out_of_range(tmp_path: Path) -> None:
204    file_path = create_config_file(tmp_path, _base_config(BRAID="1.5"))
205    with pytest.raises(ConfigError, match="BRAID must be in"):
206        parse_config(file_path)
207
208
209def test_braid_negative(tmp_path: Path) -> None:
210    file_path = create_config_file(tmp_path, _base_config(BRAID="-0.1"))
211    with pytest.raises(ConfigError, match="BRAID must be in"):
212        parse_config(file_path)
213
214
215def test_braid_non_float(tmp_path: Path) -> None:
216    file_path = create_config_file(tmp_path, _base_config(BRAID="half"))
217    with pytest.raises(ConfigError, match="BRAID must be a number"):
218        parse_config(file_path)
219
220
221def test_randomize_endpoints_default_is_false(tmp_path: Path) -> None:
222    file_path = create_config_file(tmp_path, _base_config())
223    config = parse_config(file_path)
224    assert config.randomize_endpoints is False
225
226
227@pytest.mark.parametrize("value,expected", [("True", True), ("False", False)])
228def test_randomize_endpoints_valid(
229    tmp_path: Path, value: str, expected: bool
230) -> None:
231    file_path = create_config_file(
232        tmp_path, _base_config(RANDOMIZE_ENDPOINTS=value)
233    )
234    config = parse_config(file_path)
235    assert config.randomize_endpoints is expected
236
237
238def test_randomize_endpoints_invalid_value(tmp_path: Path) -> None:
239    file_path = create_config_file(
240        tmp_path, _base_config(RANDOMIZE_ENDPOINTS="maybe")
241    )
242    with pytest.raises(
243        ConfigError, match="RANDOMIZE_ENDPOINTS must be True or False"
244    ):
245        parse_config(file_path)
246
247
248def test_clamp_endpoints_default_is_false(tmp_path: Path) -> None:
249    file_path = create_config_file(tmp_path, _base_config())
250    config = parse_config(file_path)
251    assert config.clamp_endpoints is False
252
253
254@pytest.mark.parametrize("value,expected", [("True", True), ("False", False)])
255def test_clamp_endpoints_valid(
256    tmp_path: Path, value: str, expected: bool
257) -> None:
258    file_path = create_config_file(
259        tmp_path, _base_config(CLAMP_ENDPOINTS=value)
260    )
261    config = parse_config(file_path)
262    assert config.clamp_endpoints is expected
263
264
265def test_clamp_endpoints_invalid_value(tmp_path: Path) -> None:
266    file_path = create_config_file(
267        tmp_path, _base_config(CLAMP_ENDPOINTS="maybe")
268    )
269    with pytest.raises(
270        ConfigError, match="CLAMP_ENDPOINTS must be True or False"
271    ):
272        parse_config(file_path)
273
274
275def test_clamp_endpoints_true_clamps_oob_entry(tmp_path: Path) -> None:
276    file_path = create_config_file(
277        tmp_path, _base_config(ENTRY="99,99", CLAMP_ENDPOINTS="True")
278    )
279    config = parse_config(file_path)
280    assert config.entry == (0, 0)
281
282
283def test_clamp_endpoints_true_clamps_oob_exit(tmp_path: Path) -> None:
284    file_path = create_config_file(
285        tmp_path, _base_config(EXIT="99,99", CLAMP_ENDPOINTS="True")
286    )
287    config = parse_config(file_path)
288    assert config.exit_ == (9, 9)
289
290
291def test_clamp_endpoints_true_per_axis(tmp_path: Path) -> None:
292    file_path = create_config_file(
293        tmp_path, _base_config(EXIT="5,99", CLAMP_ENDPOINTS="True")
294    )
295    config = parse_config(file_path)
296    assert config.exit_ == (5, 9)
297
298
299def test_clamp_endpoints_true_still_rejects_non_pair(tmp_path: Path) -> None:
300    file_path = create_config_file(
301        tmp_path, _base_config(ENTRY="5", CLAMP_ENDPOINTS="True")
302    )
303    with pytest.raises(ConfigError, match="ENTRY must be `x,y`"):
304        parse_config(file_path)
305
306
307def test_clamp_endpoints_true_collision_still_rejected(
308    tmp_path: Path,
309) -> None:
310    file_path = create_config_file(
311        tmp_path,
312        _base_config(ENTRY="99,99", EXIT="0,0", CLAMP_ENDPOINTS="True"),
313    )
314    with pytest.raises(ConfigError, match="ENTRY and EXIT must be distinct"):
315        parse_config(file_path)
316
317
318def test_clamp_endpoints_false_still_rejects_oob(tmp_path: Path) -> None:
319    file_path = create_config_file(
320        tmp_path, _base_config(ENTRY="99,0", CLAMP_ENDPOINTS="False")
321    )
322    with pytest.raises(ConfigError, match="ENTRY .* out of bounds"):
323        parse_config(file_path)