stree.StringTree: Add unit tests
Add pytest-style unit tests for the StringTree class. This commit also adds the needed infrastructure: Dependency on python3-pytest, the unit test directory structure and conftest.py in .gitignore. Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
01942ac5bf
commit
499694e1f9
11 changed files with 155 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -14,3 +14,4 @@ jw-python
|
|||
local.mk
|
||||
.gdb_history
|
||||
*.pyc
|
||||
conftest.py
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ libname = none
|
|||
|
||||
[pkg.requires.os]
|
||||
run = mypy, python3-magic, python3-termcolor, python3-pytimeparse, python3-argcomplete, python3-ldap, python3-networkx
|
||||
build = python3-pytest
|
||||
|
||||
[pkg.requires.jw]
|
||||
run = jw-pkg-run = VERSION
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
TOPDIR = ..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/cleandirs.mk
|
||||
include $(JWBDIR)/make/py-defs.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
|
|
|
|||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
4
test/unit/Makefile
Normal file
4
test/unit/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
4
test/unit/python/Makefile
Normal file
4
test/unit/python/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
4
test/unit/python/jw/Makefile
Normal file
4
test/unit/python/jw/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
4
test/unit/python/jw/util/Makefile
Normal file
4
test/unit/python/jw/util/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
4
test/unit/python/jw/util/stree/Makefile
Normal file
4
test/unit/python/jw/util/stree/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/dirs.mk
|
||||
4
test/unit/python/jw/util/stree/StringTree/Makefile
Normal file
4
test/unit/python/jw/util/stree/StringTree/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-test.mk
|
||||
127
test/unit/python/jw/util/stree/StringTree/test.py
Normal file
127
test/unit/python/jw/util/stree/StringTree/test.py
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
"""Unit tests for StringTree — INI-like config file parser."""
|
||||
|
||||
from typing import Callable
|
||||
|
||||
import pytest
|
||||
|
||||
from jw.util.stree.StringTree import StringTree
|
||||
|
||||
@pytest.fixture
|
||||
def st() -> Callable[[str], StringTree]:
|
||||
"""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, 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, 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) -> 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) -> 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) -> 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) -> 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) -> None:
|
||||
tree = st('[section]\nnot an assignment\n')
|
||||
assert tree.value('section.not an assignment') is None
|
||||
|
||||
def test_empty_key_raises(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) -> 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):
|
||||
# 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):
|
||||
# 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')
|
||||
Loading…
Reference in a new issue