def test___copy__(self):
     a1 = NestedList([[["foo"]]])
     a2 = copy.copy(a1)
     self.assertIsNot(a1, a2)
     self.assertEqual(a1, a2)
     a1.set((ListIndex(0), ListIndex(0), ListIndex(0)), "bar")
     self.assertEqual(a1, a2)
    def test_collapse__function_arg(self):
        a = NestedList([[["v"]]])

        def detect_operator(key, value, container):
            return container is NestedList and value == "v"

        b = a.collapse(func=detect_operator)

        self.assertEqual(b, {(ListIndex(0), ListIndex(0)): ["v"]})
Beispiel #3
0
    def _decode(self, value):
        """ Decode collapsed path format to a list.
        """

        decoded = []
        for v in value.split(self.delimiter):
            if v[:2] == "::":
                v = ListIndex(int(v[2:]))
            decoded.append(v)

        return decoded
 def test___ne____different_type(self):
     self.assertNotEqual(ListIndex(0), 1)
     self.assertNotEqual(ListIndex(0), "foo")
 def test_unset__cleanup_arg_True__removes_empty_containers(self):
     a = NestedList([[["v"]]])
     a.unset((ListIndex(0), ListIndex(0)), cleanup=True)
     self.assertEqual(a, [])
 def test_pull__path_arg__raises_TypeError(self):
     a = NestedList(["v"])
     with self.assertRaises(TypeError):
         a.pull((ListIndex(0), ListIndex(0)), "v")
 def test_pull__path_arg__raises_ValueError(self):
     a = NestedList([["foo"]])
     with self.assertRaises(ValueError):
         a.pull(ListIndex(0), "bar")
 def test_pull__cleanup_arg_True__removes_empty_containers(self):
     a = NestedList([[["v"]]])
     a.pull((ListIndex(0), ListIndex(0)), "v", cleanup=True)
     self.assertEqual(a, [[]])
 def test_clone(self):
     a1 = NestedList([[["v"]]])
     a2 = a1.clone()
     self.assertEqual(a1, a2)
     a1[ListIndex(0)] = "foo"
     self.assertEqual(a1, a2)
 def test___eq____same_type(self):
     self.assertEqual(ListIndex(0), ListIndex(0))
 def test_has__path_arg__returns_False(self):
     a = NestedList([[["v"]]])
     self.assertFalse(a.has((ListIndex(1), )))
 def test_has__path_arg__returns_True(self):
     a = NestedList([[["v"]]])
     self.assertTrue(a.has((ListIndex(0), ListIndex(0), ListIndex(0))))
 def test_get__path_arg__returns_value(self):
     a = NestedList([[["v"]]])
     self.assertEqual(a.get((ListIndex(0), ListIndex(0), ListIndex(0))),
                      "v")
 def test_get__string_arg__raises_IndexError(self):
     a = NestedList([["v"]])
     with self.assertRaises(IndexError):
         a.get((ListIndex(0), ListIndex(0), ListIndex(1)))
 def test_ref__True_create_arg_key_missing__creates_missing_containers(
         self):
     a = NestedList()
     a.ref((ListIndex(0), ListIndex(0)), create=True)
     self.assertEqual(a.data, [[[]]])
 def test_push__False_create_arg__raises_TypeError(self):
     a = NestedList(["foo"])
     with self.assertRaises(AttributeError):
         a.push(ListIndex(0), "bar", create=False)
 def test_pull__index_arg(self):
     a = NestedList([["foo"]])
     a.pull(ListIndex(0), "foo")
     self.assertEqual(a, [[]])
 def test_set__index_arg(self):
     a = NestedList(["foo"])
     a.set(ListIndex(0), "bar")
     self.assertEqual(a, ["bar"])
 def test_pull__path_arg___raises_IndexError(self):
     a = NestedList()
     with self.assertRaises(IndexError):
         a.pull(ListIndex(0), "v")
 def test_set__raises_TypeError(self):
     a = NestedList([["foo"]])
     with self.assertRaises(TypeError):
         a.set((ListIndex(0), ListIndex(0), ListIndex(0)),
               "bar",
               create=False)
 def test___ne____same_type(self):
     self.assertNotEqual(ListIndex(0), ListIndex(1))
 def test___eq____different_type(self):
     self.assertEqual(ListIndex(0), 0)
 def test_unset__path_arg(self):
     a = NestedList([[["v"]]])
     a.unset((ListIndex(0), ListIndex(0)))
     self.assertEqual(a, [[]])
 def test_push__index_arg__convert_existing_value(self):
     a = NestedList(["foo"])
     a.push(ListIndex(0), "bar")
     self.assertEqual(a, [["foo", "bar"]])
 def test_unset__path_arg__raises_IndexError(self):
     a = NestedList([[["v"]]])
     with self.assertRaises(IndexError):
         a.unset((ListIndex(1), ListIndex(0)))
 def test_push__path_arg(self):
     a = NestedList([[["foo"]]])
     a.push((ListIndex(0), ListIndex(0)), "bar")
     self.assertEqual(a, [[["foo", "bar"]]])
 def test_collapse(self):
     a = NestedList(["v"])
     b = a.collapse()
     self.assertEqual(b, {(ListIndex(0), ): 'v'})
 def test_push__True_create_arg__creates_list(self):
     a = NestedList([[]])
     a.push(ListIndex(0), "bar")
     self.assertEqual(a, [["bar"]])
 def test___hash__(self):
     a1 = hash(ListIndex(0))
     a2 = hash(ListIndex(0))
     self.assertTrue(a1 == a2)
 def test_push__False_create_arg__raises_IndexError(self):
     a = NestedList()
     with self.assertRaises(IndexError):
         a.push(ListIndex(0), "foo", create=False)