示例#1
0
    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)
示例#2
0
    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
示例#3
0
    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))
示例#4
0
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
示例#5
0
    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.",
            )
示例#6
0
    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)
示例#7
0
    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])
示例#8
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)
示例#9
0
    def test_removal(self):
        p = YamlList(content=[1, 2, 3])
        del p[0]

        self.assertEqual([2, 3], p._raw)
示例#10
0
    def test_len_works(self):
        items = [1, 2, 3]
        p = YamlList(content=items)

        self.assertEqual(3, len(p))
示例#11
0
    def test_write_with_set(self):
        p = YamlList(content=["original"])

        p[0] = "new"

        self.assertEqual("new", p[0])
示例#12
0
    def test_read_via_get(self):
        p = YamlList(content=[[1, 2, 3]])

        self.assertEqual([1, 2, 3], p[0]._raw)
示例#13
0
    def test_yaml_gets_pickle_serialized(self):
        a = YamlList(property_name="a", content=["a"])

        pickle.dumps(a)
示例#14
0
    def test_simple_property_read(self):
        p = YamlList(content=[{"x": 3}])

        self.assertEqual(3, p[0].x)