250 lines
7.8 KiB
Python
250 lines
7.8 KiB
Python
|
|
"""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"
|