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
def test_duplicate_keys_01(self): from srsly.ruamel_yaml import YAML from srsly.ruamel_yaml.constructor import DuplicateKeyError yaml = YAML(typ="safe", pure=True) with pytest.raises(DuplicateKeyError): yaml.load("{a: 1, a: 2}")
def test_parse(self): # ensure `parse` method is functional and can parse "unsafe" yaml from srsly.ruamel_yaml import YAML from srsly.ruamel_yaml.constructor import ConstructorError yaml = YAML(typ="safe") s = "- !User0 {age: 18, name: Anthon}" # should fail to load with pytest.raises(ConstructorError): yaml.load(s) # should parse fine yaml = YAML(typ="safe") for _ in yaml.parse(s): pass
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 """))
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]
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©"
def test_duplicate_key_01(self): # so issue https://stackoverflow.com/a/52852106/1307905 from srsly.ruamel_yaml import version_info from srsly.ruamel_yaml.constructor import DuplicateKeyError s = dedent( """\ - &name-name a: 1 - &help-name b: 2 - <<: *name-name <<: *help-name """ ) if version_info < (0, 15, 1): pass else: with pytest.raises(DuplicateKeyError): yaml = YAML(typ="safe") yaml.load(s) with pytest.raises(DuplicateKeyError): yaml = YAML() yaml.load(s)
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)
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_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
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())
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
def test_values_of_merged(self): from srsly.ruamel_yaml import YAML yaml = YAML() data = yaml.load(dedent(self.yaml_str)) assert list(data[2].values()) == [1, 6, "x2", "x3", "y4"]