diff --git a/.gitignore b/.gitignore index f07d092..51a1a00 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ jw-python local.mk .gdb_history *.pyc +conftest.py diff --git a/make/project.conf b/make/project.conf index e662957..7b46f90 100644 --- a/make/project.conf +++ b/make/project.conf @@ -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 diff --git a/test/Makefile b/test/Makefile index e9b4601..4ec130e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/unit/Makefile b/test/unit/Makefile new file mode 100644 index 0000000..b1ec41b --- /dev/null +++ b/test/unit/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/dirs.mk diff --git a/test/unit/python/Makefile b/test/unit/python/Makefile new file mode 100644 index 0000000..62d684a --- /dev/null +++ b/test/unit/python/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/dirs.mk diff --git a/test/unit/python/jw/Makefile b/test/unit/python/jw/Makefile new file mode 100644 index 0000000..5e331b8 --- /dev/null +++ b/test/unit/python/jw/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/dirs.mk diff --git a/test/unit/python/jw/util/Makefile b/test/unit/python/jw/util/Makefile new file mode 100644 index 0000000..8b2498d --- /dev/null +++ b/test/unit/python/jw/util/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/dirs.mk diff --git a/test/unit/python/jw/util/stree/Makefile b/test/unit/python/jw/util/stree/Makefile new file mode 100644 index 0000000..f817ac8 --- /dev/null +++ b/test/unit/python/jw/util/stree/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/dirs.mk diff --git a/test/unit/python/jw/util/stree/StringTree/Makefile b/test/unit/python/jw/util/stree/StringTree/Makefile new file mode 100644 index 0000000..4d258ad --- /dev/null +++ b/test/unit/python/jw/util/stree/StringTree/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/py-test.mk diff --git a/test/unit/python/jw/util/stree/StringTree/test.py b/test/unit/python/jw/util/stree/StringTree/test.py new file mode 100644 index 0000000..1f651a4 --- /dev/null +++ b/test/unit/python/jw/util/stree/StringTree/test.py @@ -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')