def test_is_empty(self): items = [1, 2, 3] p = YamlList(content=items) self.assertTrue(p) p = YamlList() self.assertFalse(p) p = YamlList(content=[]) self.assertFalse(p)
def __getattr__(self, item): if item.startswith("__") and item.endswith("__"): raise AttributeError if item == "_YamlDict__content": return self.__content if item == "_YamlDict__property_name": return self.__property_name # FIXME I'm not sure why the YamlMissing gets called here even from the parent if (item == "_YamlDict__create_if_missing" or item == "_YamlMissing__create_if_missing"): return self.__create_if_missing if item not in self.__content: return YamlMissing( parent_property=self, property_name=item, full_property_name=f"{self.__property_name}.{item}", ) result = self.__content[item] if isinstance(result, dict): return YamlDict(property_name=f"{self.__property_name}.{item}", content=result) elif isinstance(result, list): return YamlList(property_name=f"{self.__property_name}.{item}", content=result) return result
def test_set_other_yaml_navigator(self): a = YamlList(property_name="a", content=["a"]) b = YamlDict() a[0] = b self.assertFalse(isinstance(a._raw[0], YamlNavigator))
def convert_type(*, property_name: str, value: Any) -> Any: if isinstance(value, dict): return YamlDict(property_name=property_name, content=value) elif isinstance(value, list): return YamlList(property_name=property_name, content=value) return value
def test_iteration_gives_yaml(self): items = YamlList(content=[{"x": 1}]) for item in items: self.assertTrue( isinstance(item, YamlNavigator), "The iterated instance should be a navigator.", )
def test_iteration_as_iterable(self): p = YamlList(content=["x", "y", "z"]) items = set() for item in p: items.add(item) self.assertSetEqual({"x", "y", "z"}, items)
def test_deep_copy_really_deep_copies(self): items = [1, 2, 3] p = YamlList(content=items) p_copy = copy.deepcopy(p) p_copy[0] = 0 self.assertEqual(0, p_copy[0]) self.assertEqual(1, items[0]) self.assertEqual(1, p[0])
def test_repr(self): p = YamlList(property_name="a.b", content=[1, 2, 3]) representation = f"{p}" self.assertEqual("YamlList(a.b) [1, 2, 3]", representation)
def test_removal(self): p = YamlList(content=[1, 2, 3]) del p[0] self.assertEqual([2, 3], p._raw)
def test_len_works(self): items = [1, 2, 3] p = YamlList(content=items) self.assertEqual(3, len(p))
def test_write_with_set(self): p = YamlList(content=["original"]) p[0] = "new" self.assertEqual("new", p[0])
def test_read_via_get(self): p = YamlList(content=[[1, 2, 3]]) self.assertEqual([1, 2, 3], p[0]._raw)
def test_yaml_gets_pickle_serialized(self): a = YamlList(property_name="a", content=["a"]) pickle.dumps(a)
def test_simple_property_read(self): p = YamlList(content=[{"x": 3}]) self.assertEqual(3, p[0].x)