2026-07-02 15:34:28 +02:00
|
|
|
"""Unit tests for StringTree — INI-like config file parser."""
|
|
|
|
|
|
|
|
|
|
from typing import Callable
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from jw.util.stree.StringTree import StringTree
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
_St = Callable[[str], StringTree]
|
|
|
|
|
|
2026-07-02 15:34:28 +02:00
|
|
|
@pytest.fixture
|
2026-09-11 17:00:03 +02:00
|
|
|
def st() -> _St:
|
2026-07-02 15:34:28 +02:00
|
|
|
"""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]'),
|
|
|
|
|
]
|
|
|
|
|
)
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_value(st: _St, input: str, expected: str) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
)
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_returns_none(st: _St, path: str) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
"""Both missing key and missing section return None (no default needed)."""
|
|
|
|
|
tree = st('[section]\nkey = val\n')
|
|
|
|
|
assert tree.value(path) is None
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_key_with_default_none(st: _St) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
"""value(section.missing, default=None) returns None."""
|
|
|
|
|
tree = st('[section]\nother = val\n')
|
|
|
|
|
assert tree.value('section.missing', default = None) is None
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_section_with_default_none(st: _St) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
"""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 -----------------------------------------------
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_multiple_keys(st: _St) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_multiple_sections(st: _St) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
tree = st('[one]\nkey = val1\n[two]\nkey = val2\n')
|
|
|
|
|
assert tree.value('one.key') == 'val1'
|
|
|
|
|
assert tree.value('two.key') == 'val2'
|
|
|
|
|
|
|
|
|
|
# -- Edge cases ---------------------------------------------------------------
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_not_an_assignment_returns_none(st: _St) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
tree = st('[section]\nnot an assignment\n')
|
|
|
|
|
assert tree.value('section.not an assignment') is None
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_empty_key_raises(st: _St) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
"""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) ---------------------
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_section_noop(st: _St) -> None:
|
2026-07-02 15:34:28 +02:00
|
|
|
"""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")
|
2026-09-11 17:00:03 +02:00
|
|
|
# def test_line_continuation(st: _St):
|
2026-07-02 15:34:28 +02:00
|
|
|
# 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")
|
2026-09-11 17:00:03 +02:00
|
|
|
# def test_line_continuation_with_comment(st: _St):
|
2026-07-02 15:34:28 +02:00
|
|
|
# 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')
|