Exemplo n.º 1
0
    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")
Exemplo n.º 2
0
    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")
Exemplo n.º 3
0
 def test_ref__create_True__creates_missing_containers(self):
     a = NestedDict()
     a.ref("k", create=True)
     self.assertEqual(a, {"k": {}})
Exemplo n.º 4
0
 def test_ref__path_arg__returns_value(self):
     a = NestedDict({"k1": {"k2": {"k3": "v"}}})
     self.assertEqual(a.ref(("k1", "k2")), ({"k3": "v"}))
Exemplo n.º 5
0
 def test_ref__string_arg__raises_TypeError(self):
     a = NestedDict({"k": {"foo": "bar"}})
     with self.assertRaises(TypeError):
         a.ref(("k", "foo", "baz"))
Exemplo n.º 6
0
 def test_ref__string_arg__raises_KeyError(self):
     a = NestedDict()
     with self.assertRaises(KeyError):
         a.ref("k")
Exemplo n.º 7
0
 def test_ref__string_arg__returns_value(self):
     a = NestedDict({"k": {"foo": "bar"}})
     self.assertEqual(a.ref("k"), {"foo": "bar"})
Exemplo n.º 8
0
 def test_ref__no_args__returns_all_values(self):
     a = NestedDict({"k": {"foo": "bar"}})
     self.assertEqual(a.ref(), {"k": {"foo": "bar"}})