From e076d5778b5ae89f07a2cb44418fe03d876fafa2 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Wed, 16 Sep 2026 20:35:56 +0200 Subject: [PATCH] test/parse_opts: Replace with Options unit test The parse_opts test is a plain script that constructs an Options from sys.argv and crashed on the first get() call, because the by_index condition in Options.get() is inverted and raises KeyError for an integer index. It has several shortcomings, though: - "make test" doesn't execute it, only make -C test/parse_opts run, so CI doesn't run it - It doesn't adhere to standard janware directory layout - It's very basic Replace the script with a pytest module under test/unit/python/jw/util/Options, following the layout of the sibling unit test directories: a Makefile including py-test.mk and a test.py discovered by pytest. The module covers parsing of option strings, repeated keys, JSON values, flags, get() with defaults and the by_index parameter, __contains__, __iter__, keys(), str() and update(). Signed-off-by: Jan Lindemann Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1 --- test/parse_opts/Makefile | 5 - test/parse_opts/test.py | 32 --- test/unit/python/jw/util/Options/Makefile | 4 + test/unit/python/jw/util/Options/test.py | 250 ++++++++++++++++++++++ 4 files changed, 254 insertions(+), 37 deletions(-) delete mode 100644 test/parse_opts/Makefile delete mode 100644 test/parse_opts/test.py create mode 100644 test/unit/python/jw/util/Options/Makefile create mode 100644 test/unit/python/jw/util/Options/test.py diff --git a/test/parse_opts/Makefile b/test/parse_opts/Makefile deleted file mode 100644 index fa609d2..0000000 --- a/test/parse_opts/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -TOPDIR = ../.. - -include $(TOPDIR)/make/proj.mk -include $(JWBDIR)/make/py-run.mk - diff --git a/test/parse_opts/test.py b/test/parse_opts/test.py deleted file mode 100644 index f032cdc..0000000 --- a/test/parse_opts/test.py +++ /dev/null @@ -1,32 +0,0 @@ -from jw.util.log import NOTICE, slog -from jw.util.Options import Options - -delim = "======================= " -d = "----------------------- " - -s = "this=that a=b doit" -slog(NOTICE, d + s) -a = Options(s) -slog(NOTICE, d + "dump()") -a.dump(NOTICE) -slog(NOTICE, d + "by index") -for i in range(0, 4): - slog(NOTICE, "attr({}) = {}".format(i, a.get(i, by_index = True, default = False))) -slog(NOTICE, d + "by key") -for k in ["this", "that", "b", "a", "doit"]: - slog(NOTICE, "attr({}) = {}".format(k, a[k])) - -s = '"me": "too", "Pfalse": true, "myarr": ["a", "b", "c"], "me": "not"' -slog(NOTICE, delim + s) -a = Options(s) -slog(NOTICE, d + "dump()") -a.dump(NOTICE) -slog(NOTICE, d + "by index") -for i in range(0, 5): - slog(NOTICE, "attr({}) = {}".format(i, a.get(i, by_index = True, default = False))) -slog(NOTICE, d + "by key in keys()") -for k in a.keys(): - slog(NOTICE, "attr({}) = {}".format(k, a[k])) -slog(NOTICE, d + "by key") -for k in ["me", "Pfalse", "myarr"]: - slog(NOTICE, "attr({}) = {}".format(k, a[k])) diff --git a/test/unit/python/jw/util/Options/Makefile b/test/unit/python/jw/util/Options/Makefile new file mode 100644 index 0000000..3f85419 --- /dev/null +++ b/test/unit/python/jw/util/Options/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/Options/test.py b/test/unit/python/jw/util/Options/test.py new file mode 100644 index 0000000..ec7da96 --- /dev/null +++ b/test/unit/python/jw/util/Options/test.py @@ -0,0 +1,250 @@ +"""Unit tests for jw.util.Options.""" + +from __future__ import annotations + +import pytest + +from jw.util.Options import Options + +# -- Construction: simple options string -------------------------------------- + +class TestSimpleSpec: + """Parsing of whitespace-separated key=value options.""" + + def test_pairs(self) -> None: + opts = Options("this=that a=b doit") + assert opts.items() == [("this", "that"), ("a", "b"), ("doit", True)] + + def test_get_by_key(self) -> None: + opts = Options("this=that a=b doit") + assert opts.get("this") == "that" + assert opts["this"] == "that" + + def test_flag_defaults_to_true(self) -> None: + opts = Options("doit") + assert opts["doit"] is True + + def test_flag_custom_true_val(self) -> None: + opts = Options("flag", true_val = "yes") + assert opts["flag"] == "yes" + + def test_value_with_equals(self) -> None: + # Everything after the first '=' is part of the value + opts = Options("a=b=c") + assert opts["a"] == "b c" + + def test_quoted_key_equals_value(self) -> None: + # A quoted token keeps '=' with surrounding spaces intact + opts = Options('"k = v" x') + assert opts["k"] == "v" + assert opts["x"] is True + + def test_empty_lhs_skipped(self) -> None: + opts = Options("=v") + assert opts.items() == [] + + def test_whitespace_around_equals_splits_tokens(self) -> None: + # shlex splits on whitespace before '=' is normalised, so a bare + # 'k = v' becomes three tokens: flag, '=' (skipped), flag + opts = Options("spaced = value") + assert opts.items() == [("spaced", True), ("value", True)] + +# -- Construction: duplicate keys --------------------------------------------- + +class TestDuplicates: + """Repeated keys accumulate into a list.""" + + def test_items_keep_all_pairs(self) -> None: + opts = Options("a=1 a=2 a=3") + assert opts.items() == [("a", "1"), ("a", "2"), ("a", "3")] + + def test_get_merges_into_list(self) -> None: + opts = Options("a=1 a=2 a=3") + assert opts["a"] == ["1", "2", "3"] + assert opts.get("a") == ["1", "2", "3"] + + def test_len_counts_pairs(self) -> None: + assert len(Options("a=1 a=2 a=3")) == 3 + + def test_keys_deduplicate(self) -> None: + opts = Options("a=1 a=2") + assert list(opts.keys()) == ["a"] + + def test_iter_yields_all_keys(self) -> None: + opts = Options("a=1 a=2") + assert list(iter(opts)) == ["a", "a"] + + def test_str_lists_all_pairs(self) -> None: + assert str(Options("a=1 a=2")) == "a: 1, a: 2" + +# -- Construction: JSON spec --------------------------------------------------- + +class TestJsonSpec: + """Parsing of JSON object specs.""" + + def test_json_fragment(self) -> None: + opts = Options('"me": "too", "n": 42') + assert opts.items() == [("me", "too"), ("n", 42)] + + def test_full_json_object(self) -> None: + opts = Options('{"k": "v", "n": 42}') + assert opts["k"] == "v" + assert opts["n"] == 42 + + def test_native_types(self) -> None: + opts = Options('"Pfalse": true, "myarr": ["a", "b", "c"]') + assert opts["Pfalse"] is True + assert opts["myarr"] == ["a", "b", "c"] + + def test_duplicate_keys_merge(self) -> None: + opts = Options('"me": "too", "me": "not"') + assert opts["me"] == ["too", "not"] + + def test_len_counts_pairs(self) -> None: + opts = Options('"a": 1, "b": 2, "a": 3') + assert len(opts) == 3 + + def test_str(self) -> None: + opts = Options('"a": 1, "b": 2') + assert str(opts) == "a: 1, b: 2" + +# -- get() by key --------------------------------------------------------------- + +class TestGetByKey: + """Options.get() without by_index.""" + + def test_found(self) -> None: + opts = Options("a=1 b=2") + assert opts.get("a") == "1" + + def test_found_with_default(self) -> None: + opts = Options("a=1") + assert opts.get("a", default = "d") == "1" + + def test_missing_raises(self) -> None: + opts = Options("a=1") + with pytest.raises(KeyError, match = 'Key "missing" is not present'): + opts.get("missing") + + def test_missing_uses_default(self) -> None: + opts = Options("a=1") + assert opts.get("missing", default = "d") == "d" + + def test_getitem_missing_returns_none(self) -> None: + opts = Options("a=1") + assert opts["missing"] is None + +# -- get() by index --------------------------------------------------------------- + +class TestGetByIndex: + """Options.get() with by_index. + + An integer index selects the key stored at that position in the + parsed option string; a non-integer key is rejected. + """ + + def test_int_index_returns_key(self) -> None: + opts = Options("a=1 b=2") + assert opts.get(0, by_index = True) == "a" + assert opts.get(1, by_index = True) == "b" + + def test_negative_index(self) -> None: + opts = Options("a=1 b=2") + assert opts.get(-1, by_index = True) == "b" + + def test_out_of_range_raises(self) -> None: + opts = Options("a=1 b=2") + with pytest.raises( + KeyError, + match = 'Tried to get value from options string with index 9 of 2', + ): + opts.get(9, by_index = True) + + def test_out_of_range_uses_default(self) -> None: + opts = Options("a=1 b=2") + assert opts.get(9, by_index = True, default = "d") == "d" + + def test_non_int_raises(self) -> None: + opts = Options("a=1 b=2") + with pytest.raises( + KeyError, + match = 'index a of type', + ): + opts.get("a", by_index = True) + +# -- Container protocol ----------------------------------------------------------- + +class TestContainer: + """len, str, repr, iter, keys, items, contains.""" + + def test_len(self) -> None: + assert len(Options("a=1 b=2")) == 2 + + def test_str(self) -> None: + opts = Options("a=1 b=2") + assert str(opts) == "a: 1, b: 2" + + def test_repr_matches_str(self) -> None: + opts = Options("a=1") + assert repr(opts) == str(opts) + + def test_iter(self) -> None: + opts = Options("a=1 b=2") + assert list(iter(opts)) == ["a", "b"] + + def test_keys(self) -> None: + opts = Options("a=1 b=2") + assert list(opts.keys()) == ["a", "b"] + + def test_items(self) -> None: + opts = Options("a=1 b=2") + assert opts.items() == [("a", "1"), ("b", "2")] + + def test_contains_single(self) -> None: + opts = Options("a=1 b=2") + assert "a" in opts + assert "nope" not in opts + + def test_contains_list(self) -> None: + opts = Options("a=1 b=2") + assert ["a", "b"] in opts + assert ["a", "nope"] not in opts + + def test_contains_set(self) -> None: + opts = Options("a=1 b=2") + assert {"a", "b"} in opts + assert {"a", "nope"} not in opts + +# -- Empty spec --------------------------------------------------------------------- + +class TestEmptySpec: + """Construction without a spec string.""" + + def test_none_spec(self) -> None: + opts = Options() + assert len(opts) == 0 + assert str(opts) == "" + + def test_empty_spec(self) -> None: + opts = Options("") + assert len(opts) == 0 + + def test_getitem_missing_returns_none(self) -> None: + assert Options()["x"] is None + + def test_get_missing_uses_default(self) -> None: + assert Options().get("x", default = "d") == "d" + + def test_contains(self) -> None: + assert "x" not in Options() + +# -- update() ------------------------------------------------------------------------ + +class TestUpdate: + """Options.update() with a mapping.""" + + def test_update_dict(self) -> None: + opts = Options("a=1") + opts.update({"x": "y"}) + assert opts["x"] == "y" + assert opts.get("x") == "y"