def test_supports_path_access(): cfg = PathDict({"a": {"b": {"c": 4}}}) assert cfg[Path("a")] is cfg.a assert cfg[Path.from_str("a.b")] is cfg.a.b assert cfg[Path.from_str("a.b.c")] is cfg.a.b.c assert cfg[Path.from_str("a.b.c")] == 4 cfg[Path.from_str("a.b.c")] = 6 assert cfg[Path.from_str("a.b.c")] == 6 cfg[Path.from_str("a.b.d")] = 8 assert cfg[Path.from_str("a.b.d")] == 8 cfg[Path.from_str("a.b")] = {"e": 9} assert set(cfg.a.b.keys()) == {"e"} assert cfg[Path.from_str("a.b.e")] == 9
def test_delitem_path(): cfg = PathDict({"lol": {"rofl": 42}}) del cfg[Path.from_str("lol.rofl")] assert_value(cfg, "lol", {}) with pytest.raises(KeyError): _ = cfg["lol.rofl"] with pytest.raises(AttributeError): _ = cfg.lol.rofl
def test_contains_path(): b = PathDict({"ponies": {"are": "pretty!"}}) assert Path.from_str("ponies") in b assert ("foo" in b) is False assert Path.from_str("ponies.are") in b assert Path.from_str("ponies.foo") not in b b["foo"] = 42 assert Path.from_str("foo") in b assert Path.from_str("ponies.foo") not in b b.ponies.foo = "hai" assert Path.from_str("ponies.foo") in b
def test_is_suffix_of(): p = Path.from_str("a.b[2].c[True].e.foo") for i in range(1, len(p)): assert p[i:].is_suffix_of(p) assert not p.is_suffix_of(p[i:])
def test_is_prefix_of(): p = Path.from_str("a.b[2].c[True].e.foo") for i in range(len(p)): assert p[:i].is_prefix_of(p) assert not p.is_prefix_of(p[:i])
def test_from_str_fallback(): p = Path.from_str("a[foobar]") assert p.parts == ("a", "foobar")
def test_from_str_fancy_numbers(str_path, parts): ps = Path.from_str(str_path) pp = Path(*parts) assert ps == pp
def test_from_str(str_path, parts): ps = Path.from_str(str_path) pp = Path(*parts) assert ps == pp assert repr(ps) == str_path