コード例 #1
0
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    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
ファイル: YamlDictClass.py プロジェクト: bmustiata/yamldict
    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
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    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
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    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
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    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
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    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
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    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
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    def test_removal(self):
        p = YamlList(content=[1, 2, 3])
        del p[0]

        self.assertEqual([2, 3], p._raw)
コード例 #10
0
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    def test_len_works(self):
        items = [1, 2, 3]
        p = YamlList(content=items)

        self.assertEqual(3, len(p))
コード例 #11
0
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    def test_write_with_set(self):
        p = YamlList(content=["original"])

        p[0] = "new"

        self.assertEqual("new", p[0])
コード例 #12
0
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    def test_read_via_get(self):
        p = YamlList(content=[[1, 2, 3]])

        self.assertEqual([1, 2, 3], p[0]._raw)
コード例 #13
0
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    def test_yaml_gets_pickle_serialized(self):
        a = YamlList(property_name="a", content=["a"])

        pickle.dumps(a)
コード例 #14
0
ファイル: test_YamlList.py プロジェクト: bmustiata/yamldict
    def test_simple_property_read(self):
        p = YamlList(content=[{"x": 3}])

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