コード例 #1
0
    def test_dump_missing_stream(self):
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        data = yaml.map()
        data["a"] = 1
        data["b"] = 2
        with pytest.raises(TypeError):
            yaml.dump(data)
コード例 #2
0
    def test_dump_too_many_args(self, tmpdir):
        from srsly.ruamel_yaml import YAML

        fn = Path(str(tmpdir)) / "test.yaml"
        yaml = YAML()
        data = yaml.map()
        data["a"] = 1
        data["b"] = 2
        with pytest.raises(TypeError):
            yaml.dump(data, fn, True)
コード例 #3
0
    def test_dump_path(self, tmpdir):
        from srsly.ruamel_yaml import YAML

        fn = Path(str(tmpdir)) / "test.yaml"
        yaml = YAML()
        data = yaml.map()
        data["a"] = 1
        data["b"] = 2
        yaml.dump(data, fn)
        assert fn.read_text() == "a: 1\nb: 2\n"
コード例 #4
0
    def test_print(self, capsys):
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        data = yaml.map()
        data["a"] = 1
        data["b"] = 2
        yaml.dump(data, sys.stdout)
        out, err = capsys.readouterr()
        assert out == "a: 1\nb: 2\n"
コード例 #5
0
    def test_dump_file(self, tmpdir):
        from srsly.ruamel_yaml import YAML

        fn = Path(str(tmpdir)) / "test.yaml"
        yaml = YAML()
        data = yaml.map()
        data["a"] = 1
        data["b"] = 2
        with open(str(fn), "w") as fp:
            yaml.dump(data, fp)
        assert fn.read_text() == "a: 1\nb: 2\n"
コード例 #6
0
    def test_flow_style(self, capsys):
        # https://stackoverflow.com/questions/45791712/
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        yaml.default_flow_style = None
        data = yaml.map()
        data["b"] = 1
        data["a"] = [[1, 2], [3, 4]]
        yaml.dump(data, sys.stdout)
        out, err = capsys.readouterr()
        assert out == "b: 1\na:\n- [1, 2]\n- [3, 4]\n"
コード例 #7
0
    def test_transform(self, tmpdir):
        from srsly.ruamel_yaml import YAML

        def tr(s):
            return s.replace(" ", "  ")

        fn = Path(str(tmpdir)) / "test.yaml"
        yaml = YAML()
        data = yaml.map()
        data["a"] = 1
        data["b"] = 2
        yaml.dump(data, fn, transform=tr)
        assert fn.read_text() == "a:  1\nb:  2\n"