Exemple #1
0
def test_converts_subdicts_in_init():
    cfg = PathDict({"a": {"b": {"c": 4}}})
    assert set(cfg.keys()) == {"a"}
    assert isinstance(cfg.a, PathDict)
    assert set(cfg.a.keys()) == {"b"}
    assert isinstance(cfg.a.b, PathDict)
    assert set(cfg.a.b.items()) == {("c", 4)}
    assert cfg.a.b.c == 4
Exemple #2
0
def test_init_from_dict():
    cfg = PathDict({"a": 12, "b": "foo"})

    assert_value(cfg, "a", 12)
    assert_value(cfg, "b", "foo")

    assert set(cfg.keys()) == {"a", "b"}
    assert set(cfg.values()) == {12, "foo"}
    assert set(cfg.items()) == {("a", 12), ("b", "foo")}
Exemple #3
0
def test_attr_access():
    cfg = PathDict()
    cfg.a = 12
    cfg["b"] = "foo"

    assert_value(cfg, "a", 12)
    assert_value(cfg, "b", "foo")

    assert set(cfg.keys()) == {"a", "b"}
    assert set(cfg.values()) == {12, "foo"}
    assert set(cfg.items()) == {("a", 12), ("b", "foo")}
Exemple #4
0
def test_dict_access():
    cfg = PathDict()
    cfg[7] = 8
    cfg["foo"] = "bar"
    cfg[True] = False
    cfg[0.5] = 1.5

    assert_value(cfg, 7, 8)
    assert_value(cfg, "foo", "bar")
    assert_value(cfg, True, False)
    assert_value(cfg, 0.5, 1.5)

    assert set(cfg.keys()) == {7, True, "foo", 0.5}
    assert set(cfg.values()) == {8, "bar", False, 1.5}
    assert set(cfg.items()) == {(7, 8), ("foo", "bar"), (True, False), (0.5, 1.5)}
Exemple #5
0
def test_initialized_empty():
    ad = PathDict()
    assert len(ad) == 0

    assert_not_in(ad, "a")
    assert_not_in(ad, 1)
    assert_not_in(ad, True)
    assert_not_in(ad, (17, "b"))

    assert list(ad.keys()) == []
    assert list(ad.values()) == []
    assert list(ad.items()) == []

    for k in ad:
        assert not k and k

    assert repr(ad) == "PathDict({})"
    assert not ad
    assert not bool(ad)