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)
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)
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 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)
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)
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"
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"
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 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"
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"
def test_qualified_name01(tmpdir): """issue 214""" from srsly.ruamel_yaml import YAML import srsly.ruamel_yaml.comments from srsly.ruamel_yaml.compat import StringIO with pytest.raises(ValueError): yaml = YAML(typ="unsafe", pure=True) yaml.explicit_end = True buf = StringIO() yaml.dump(srsly.ruamel_yaml.comments.CommentedBase.yaml_anchor, buf) res = buf.getvalue() assert ( res == "!!python/name:srsly.ruamel_yaml.comments.CommentedBase.yaml_anchor ''\n...\n" ) x = yaml.load(res) assert x == srsly.ruamel_yaml.comments.CommentedBase.yaml_anchor
def test_issue_221_sort_key_reverse(self): from srsly.ruamel_yaml import YAML from srsly.ruamel_yaml.compat import StringIO yaml = YAML() inp = dedent("""\ - four - One # 1 - Three # 3 - five # 5 - two # 2 """) a = yaml.load(dedent(inp)) a.sort(key=str.lower, reverse=True) buf = StringIO() yaml.dump(a, buf) exp = dedent("""\ - two # 2 - Three # 3 - One # 1 - four - five # 5 """) assert buf.getvalue() == exp
def test_issue_221_sort_reverse(self): from srsly.ruamel_yaml import YAML from srsly.ruamel_yaml.compat import StringIO yaml = YAML() inp = dedent("""\ - d - a # 1 - c # 3 - e # 5 - b # 2 """) a = yaml.load(dedent(inp)) a.sort(reverse=True) buf = StringIO() yaml.dump(a, buf) exp = dedent("""\ - e # 5 - d - c # 3 - b # 2 - a # 1 """) assert buf.getvalue() == exp