Exemple #1
0
    def test_duplicate_keys_02(self):
        from srsly.ruamel_yaml import YAML
        from srsly.ruamel_yaml.constructor import DuplicateKeyError

        yaml = YAML(typ="safe")
        with pytest.raises(DuplicateKeyError):
            yaml.load("{a: 1, a: 2}")
Exemple #2
0
    def test_issue_135_temporary_workaround(self):
        # never raised error
        from srsly.ruamel_yaml import YAML

        data = {"a": 1, "b": 2}
        yaml = YAML(typ="safe", pure=True)
        yaml.dump(data, sys.stdout)
Exemple #3
0
    def test_issue_176(self):
        # basic request by Stuart Berg
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        seq = yaml.load("[1,2,3]")
        seq[:] = [1, 2, 3, 4]
Exemple #4
0
    def test_issue_288a(self):
        import sys
        from srsly.ruamel_yaml.compat import StringIO
        from srsly.ruamel_yaml import YAML

        yamldoc = dedent("""\
        ---
        # Reusable values
        aliases:
          # First-element comment
          - &firstEntry First entry
          # Second-element comment
          - &secondEntry Second entry

          # Third-element comment is
           # a multi-line value
          - &thirdEntry Third entry

        # EOF Comment
        """)

        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.explicit_start = True
        yaml.preserve_quotes = True
        yaml.width = sys.maxsize
        data = yaml.load(yamldoc)
        buf = StringIO()
        yaml.dump(data, buf)
        assert buf.getvalue() == yamldoc
Exemple #5
0
    def test_issue_290a(self):
        import sys
        from srsly.ruamel_yaml.compat import StringIO
        from srsly.ruamel_yaml import YAML

        yamldoc = dedent("""\
        ---
        aliases:
          # Folded-element comment
          # for a multi-line value
          - &FoldedEntry >
            THIS IS A
            FOLDED, MULTI-LINE
            VALUE

          # Literal-element comment
          # for a multi-line value
          - &literalEntry |
            THIS IS A
            LITERAL, MULTI-LINE
            VALUE

          # Plain-element comment
          - &plainEntry Plain entry
        """)

        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.explicit_start = True
        yaml.preserve_quotes = True
        yaml.width = sys.maxsize
        data = yaml.load(yamldoc)
        buf = StringIO()
        yaml.dump(data, buf)
        assert buf.getvalue() == yamldoc
Exemple #6
0
    def test_multi_load(self):
        # make sure reader, scanner, parser get reset
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        yaml.load("a: 1")
        yaml.load("a: 1")  # did not work in 0.15.4
Exemple #7
0
    def test_write_unicode(self, tmpdir):
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        text_dict = {"text": u"HELLO_WORLD©"}
        file_name = str(tmpdir) + "/tstFile.yaml"
        yaml.dump(text_dict, open(file_name, "w", encoding="utf8", newline="\n"))
        assert open(file_name, "rb").read().decode("utf-8") == u"text: HELLO_WORLD©\n"
    def yaml(self, yaml_version=None):
        from srsly.ruamel_yaml import YAML

        y = YAML()
        y.preserve_quotes = True
        if yaml_version:
            y.version = yaml_version
        return y
Exemple #9
0
    def test_issue_135(self):
        # reported by Andrzej Ostrowski
        from srsly.ruamel_yaml import YAML

        data = {"a": 1, "b": 2}
        yaml = YAML(typ="safe")
        # originally on 2.7: with pytest.raises(TypeError):
        yaml.dump(data, sys.stdout)
Exemple #10
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)
Exemple #11
0
    def test_read_unicode(self, tmpdir):
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        file_name = str(tmpdir) + "/tstFile.yaml"
        with open(file_name, "wb") as fp:
            fp.write(u"text: HELLO_WORLD©\n".encode("utf-8"))
        text_dict = yaml.load(open(file_name, "r", encoding="utf8"))
        assert text_dict["text"] == u"HELLO_WORLD©"
Exemple #12
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)
Exemple #13
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"
Exemple #14
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"
Exemple #15
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"
    def docs(self, path):
        from srsly.ruamel_yaml import YAML

        tyaml = YAML(typ="safe", pure=True)
        tyaml.register_class(YAMLData)
        tyaml.register_class(Python)
        tyaml.register_class(Output)
        tyaml.register_class(Assert)
        return list(tyaml.load_all(path))
Exemple #17
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"
Exemple #18
0
    def test_multi_document_load(self, tmpdir):
        """this went wrong on 3.7 because of StopIteration, PR 37 and Issue 211"""
        from srsly.ruamel_yaml import YAML

        fn = Path(str(tmpdir)) / "test.yaml"
        fn.write_text(
            textwrap.dedent(u"""\
            ---
            - a
            ---
            - b
            ...
            """))
        yaml = YAML()
        assert list(yaml.load_all(fn)) == [["a"], ["b"]]
Exemple #19
0
    def test_dupl_set_00(self):
        # round-trip-loader should except
        from srsly.ruamel_yaml import YAML
        from srsly.ruamel_yaml.constructor import DuplicateKeyError

        yaml = YAML()
        with pytest.raises(DuplicateKeyError):
            yaml.load(
                textwrap.dedent("""\
                !!set
                ? a
                ? b
                ? c
                ? a
                """))
Exemple #20
0
    def test_issue_150(self):
        from srsly.ruamel_yaml import YAML

        inp = """\
        base: &base_key
          first: 123
          second: 234

        child:
          <<: *base_key
          third: 345
        """
        yaml = YAML()
        data = yaml.load(inp)
        child = data["child"]
        assert "second" in dict(**child)
Exemple #21
0
    def test_issue_233a(self):
        from srsly.ruamel_yaml import YAML
        import json

        yaml = YAML()
        data = yaml.load("[]")
        json_str = json.dumps(data)  # NOQA
Exemple #22
0
    def test_single_dump(self, capsys):
        from srsly.ruamel_yaml import YAML

        with YAML(output=sys.stdout) as yaml:
            yaml.dump(single_data)
        out, err = capsys.readouterr()
        print(err)
        assert out == single_doc
Exemple #23
0
    def test_issue_245(self):
        from srsly.ruamel_yaml import YAML

        inp = """
        d: yes
        """
        for typ in ["safepure", "rt", "safe"]:
            if typ.endswith("pure"):
                pure = True
                typ = typ[:-4]
            else:
                pure = None

            yaml = YAML(typ=typ, pure=pure)
            yaml.version = (1, 1)
            d = yaml.load(inp)
            print(typ, yaml.parser, yaml.resolver)
            assert d["d"] is True
Exemple #24
0
    def test_issue_300(self):
        from srsly.ruamel_yaml import YAML

        inp = dedent("""
        %YAML 1.2
        %TAG ! tag:example.com,2019/path#fragment
        ---
        null
        """)
        YAML().load(inp)
Exemple #25
0
    def test_roundtrip(self, capsys):
        from srsly.ruamel_yaml import YAML

        with YAML(output=sys.stdout) as yaml:
            yaml.explicit_start = True
            for data in yaml.load_all(multi_doc):
                yaml.dump(data)

        out, err = capsys.readouterr()
        print(err)
        assert out == multi_doc
Exemple #26
0
    def test_issue_280(self):
        from srsly.ruamel_yaml import YAML
        from srsly.ruamel_yaml.representer import RepresenterError
        from collections import namedtuple
        from sys import stdout

        T = namedtuple("T", ("a", "b"))
        t = T(1, 2)
        yaml = YAML()
        with pytest.raises(RepresenterError, match="cannot represent"):
            yaml.dump({"t": t}, stdout)
Exemple #27
0
    def test_multi_dump(self, capsys):
        from srsly.ruamel_yaml import YAML

        with YAML(output=sys.stdout) as yaml:
            yaml.explicit_start = True
            yaml.dump(multi_doc_data[0])
            yaml.dump(multi_doc_data[1])

        out, err = capsys.readouterr()
        print(err)
        assert out == multi_doc
Exemple #28
0
    def test_issue_213_copy_of_merge(self):
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        d = yaml.load(
            """\
        foo: &foo
          a: a
        foo2:
          <<: *foo
          b: b
        """
        )["foo2"]
        assert d["a"] == "a"
        d2 = d.copy()
        assert d2["a"] == "a"
        print("d", d)
        del d["a"]
        assert "a" not in d
        assert "a" in d2
Exemple #29
0
    def test_issue_196_cast_of_dict(self, capsys):
        from srsly.ruamel_yaml import YAML

        yaml = YAML()
        mapping = yaml.load(
            """\
        anchored: &anchor
          a : 1

        mapping:
          <<: *anchor
          b: 2
        """
        )["mapping"]

        for k in mapping:
            print("k", k)
        for k in mapping.copy():
            print("kc", k)

        print("v", list(mapping.keys()))
        print("v", list(mapping.values()))
        print("v", list(mapping.items()))
        print(len(mapping))
        print("-----")

        # print({**mapping})
        # print(type({**mapping}))
        # assert 'a' in {**mapping}
        assert "a" in mapping
        x = {}
        for k in mapping:
            x[k] = mapping[k]
        assert "a" in x
        assert "a" in mapping.keys()
        assert mapping["a"] == 1
        assert mapping.__getitem__("a") == 1
        assert "a" in dict(mapping)
        assert "a" in dict(mapping.items())
Exemple #30
0
    def test_issue_300a(self):
        import srsly.ruamel_yaml

        inp = dedent("""
        %YAML 1.1
        %TAG ! tag:example.com,2019/path#fragment
        ---
        null
        """)
        yaml = YAML()
        with pytest.raises(srsly.ruamel_yaml.scanner.ScannerError,
                           match="while scanning a directive"):
            yaml.load(inp)