def test_ref__func_arg(self): class CustomClass(NestedDict): def get(self): return {"k2": {"k3": "v"}} def ref_func(haystack, segment, path, i): if isinstance(haystack, CustomClass): return haystack.get() return haystack a = NestedDict({"k1": CustomClass()}) self.assertEqual(a.ref(("k1", "k2", "k3"), func=ref_func), "v")
def test_ref__func_arg__raises_StopIteration(self): class CustomClass(NestedDict): def get(self): return "foo" def ref_func(haystack, segment, path, i): if isinstance(haystack, CustomClass): e = StopIteration() e.haystack = haystack.get() raise e return haystack a = NestedDict({"k1": {"k2": CustomClass()}}) self.assertEqual(a.ref(("k1", "k2", "k3"), func=ref_func), "foo")
def test_ref__create_True__creates_missing_containers(self): a = NestedDict() a.ref("k", create=True) self.assertEqual(a, {"k": {}})
def test_ref__path_arg__returns_value(self): a = NestedDict({"k1": {"k2": {"k3": "v"}}}) self.assertEqual(a.ref(("k1", "k2")), ({"k3": "v"}))
def test_ref__string_arg__raises_TypeError(self): a = NestedDict({"k": {"foo": "bar"}}) with self.assertRaises(TypeError): a.ref(("k", "foo", "baz"))
def test_ref__string_arg__raises_KeyError(self): a = NestedDict() with self.assertRaises(KeyError): a.ref("k")
def test_ref__string_arg__returns_value(self): a = NestedDict({"k": {"foo": "bar"}}) self.assertEqual(a.ref("k"), {"foo": "bar"})
def test_ref__no_args__returns_all_values(self): a = NestedDict({"k": {"foo": "bar"}}) self.assertEqual(a.ref(), {"k": {"foo": "bar"}})