Esempio 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")
Esempio 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")
Esempio n. 3
0
 def test_ref__create_True__creates_missing_containers(self):
     a = NestedDict()
     a.ref("k", create=True)
     self.assertEqual(a, {"k": {}})
Esempio 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"}))
Esempio 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"))
Esempio n. 6
0
 def test_ref__string_arg__raises_KeyError(self):
     a = NestedDict()
     with self.assertRaises(KeyError):
         a.ref("k")
Esempio n. 7
0
 def test_ref__string_arg__returns_value(self):
     a = NestedDict({"k": {"foo": "bar"}})
     self.assertEqual(a.ref("k"), {"foo": "bar"})
Esempio n. 8
0
 def test_ref__no_args__returns_all_values(self):
     a = NestedDict({"k": {"foo": "bar"}})
     self.assertEqual(a.ref(), {"k": {"foo": "bar"}})