jw-python/test/unit/python/jw/util/stree/StringTree/test.py
Jan Lindemann 7885f69b44
test: Fix mypy strict errors in test tree
The test tree needs to be aligned with type checking, too, because jw-pkg
is going to required it for test, too. The load_yaml and StringTree tests
receive their fixtures as bare Callable and Generator without type
arguments, and the StringTree tests pass the st fixture without an
annotation. The cast script suppresses errors with un-coded type ignore
comments, and the parse_opts script declares its loop variable as
"str | int", so indexing the Options object with it is rejected.

Introduce _YamlFile, _TempDir, and _St aliases for the fixture types and
give the st parameters an annotation. Code the type ignore comments with
the argument mismatch they suppress, and split the parse_opts loop variable
into i for the index loops and k for the key loops.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-09-13 13:50:15 +02:00

129 lines
4.8 KiB
Python

"""Unit tests for StringTree — INI-like config file parser."""
from typing import Callable
import pytest
from jw.util.stree.StringTree import StringTree
_St = Callable[[str], StringTree]
@pytest.fixture
def st() -> _St:
"""Helper that creates a StringTree from raw config text."""
def _load(text: str) -> StringTree:
import tempfile as _tmpfile_mod
with _tmpfile_mod.NamedTemporaryFile(mode = 'w',
suffix = '.conf',
delete = False) as fh:
fh.write(text)
fh.flush()
return StringTree.read(fh.name)
return _load
# -- Basic construction and value extraction ----------------------------------
@pytest.mark.parametrize(
'input,expected',
[
# Basic value extraction
('[section]\nkey = value\n', 'value'),
# Quoted value (unquoting applied)
('[section]\nkey = "hello world"\n', 'hello world'),
# Unquoted multi-word value
('[section]\nkey = plain value\n', 'plain value'),
# Trailing whitespace stripped
('[section]\nkey = value \n', 'value'),
# Quoted value with internal spaces preserved
('[section]\nkey = " spaced "\n', ' spaced '),
# Quoted hash inside quotes is preserved
('[section]\nkey = "value # not a comment"\n', 'value # not a comment'),
# Inline comment outside quotes
('[section]\nkey = value # this is a comment\n', 'value'),
# Quoted value containing inline-comment delimiter
('[section]\nkey = "has # inside"\n', 'has # inside'),
# Whitespace around key and value
('[section]\n key = value \n', 'value'),
# Value that looks like a section header
('[section]\nkey = [bracket]\n', '[bracket]'),
]
)
def test_value(st: _St, input: str, expected: str) -> None:
tree = st(input)
assert tree.value('section.key') == expected
# -- Missing keys and sections ------------------------------------------------
@pytest.mark.parametrize(
'path',
[
('section.missing'), # missing key in existing section
('nonexistent.key'), # missing section entirely
]
)
def test_missing_returns_none(st: _St, path: str) -> None:
"""Both missing key and missing section return None (no default needed)."""
tree = st('[section]\nkey = val\n')
assert tree.value(path) is None
def test_missing_key_with_default_none(st: _St) -> None:
"""value(section.missing, default=None) returns None."""
tree = st('[section]\nother = val\n')
assert tree.value('section.missing', default = None) is None
def test_missing_section_with_default_none(st: _St) -> None:
"""value(nonexistent.key, default=None) returns None."""
tree = st('[section]\nkey = val\n')
assert tree.value('nonexistent.key', default = None) is None
# -- Multiple keys and sections -----------------------------------------------
def test_multiple_keys(st: _St) -> None:
tree = st('[section]\nfirst = one\nsecond = two\nthird = three\n')
assert tree.value('section.first') == 'one'
assert tree.value('section.second') == 'two'
assert tree.value('section.third') == 'three'
def test_multiple_sections(st: _St) -> None:
tree = st('[one]\nkey = val1\n[two]\nkey = val2\n')
assert tree.value('one.key') == 'val1'
assert tree.value('two.key') == 'val2'
# -- Edge cases ---------------------------------------------------------------
def test_not_an_assignment_returns_none(st: _St) -> None:
tree = st('[section]\nnot an assignment\n')
assert tree.value('section.not an assignment') is None
def test_empty_key_raises(st: _St) -> None:
"""Empty key raises StringTree.Error during parsing."""
with pytest.raises(StringTree.Error):
st('[section]\n= value\n')
# -- Missing section (no-op, originally had no assertion) ---------------------
def test_missing_section_noop(st: _St) -> None:
"""Previously this test called value() without asserting anything;
add an explicit check so the test is meaningful."""
tree = st('[section]\nkey = val\n')
assert tree.value('missing.key') is None
# -- Line continuation (Not yet implemented) ----------------------------------
#
# @pytest.mark.skip(reason="Line continuation not yet implemented")
# def test_line_continuation(st: _St):
# tree = st('[section]\nkey = first \\\n second \\\n third\n')
# assert tree.value('section.key') == 'first second third'
#
#
# @pytest.mark.skip(reason="Line continuation not yet implemented")
# def test_line_continuation_with_comment(st: _St):
# tree = st('[section]\nkey = first \\\n second # comment\n')
# assert tree.value('section.key') == 'first second'
#
#
# def test_unfinished_continuation_raises():
# with pytest.raises(StringTree.Error):
# st('[section]\nkey = value \\\n')