summaryrefslogtreecommitdiff
path: root/_test/test_issues.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2023-05-05 20:08:37 +0200
committerAnthon van der Neut <anthon@mnt.org>2023-05-05 20:08:37 +0200
commita6a96c99a78cdc34e291af8d7d2d17fd1bf42a42 (patch)
tree99c4e94587427d628bb5679a8ee85b8c2487bf01 /_test/test_issues.py
parent199f4f52fa9c1700de3b4d3709de81c34b7402a4 (diff)
downloadruamel.yaml-a6a96c99a78cdc34e291af8d7d2d17fd1bf42a42.tar.gz
fixes issue: 458,454, stackoverflow reports
Diffstat (limited to '_test/test_issues.py')
-rw-r--r--_test/test_issues.py404
1 files changed, 274 insertions, 130 deletions
diff --git a/_test/test_issues.py b/_test/test_issues.py
index 47e8e87..ce78cce 100644
--- a/_test/test_issues.py
+++ b/_test/test_issues.py
@@ -5,7 +5,8 @@ from typing import Any
import pytest # type: ignore # NOQA
-from roundtrip import (
+# cannot do "from .roundtrip" because of pytest, so mypy cannot find this
+from roundtrip import ( # type: ignore
round_trip,
na_round_trip,
round_trip_load,
@@ -18,7 +19,8 @@ from roundtrip import (
class TestIssues:
def test_issue_61(self) -> None:
- s = dedent("""
+ s = dedent(
+ """
def1: &ANCHOR1
key1: value1
def: &ANCHOR
@@ -26,70 +28,73 @@ class TestIssues:
key: value
comb:
<<: *ANCHOR
- """)
+ """
+ )
data = round_trip_load(s)
assert str(data['comb']) == str(data['def'])
assert str(data['comb']) == "{'key': 'value', 'key1': 'value1'}"
-# def test_issue_82(self, tmpdir):
-# program_src = r'''
-# from ruamel import yaml
-# import re
-#
-# class SINumber(yaml.YAMLObject):
-# PREFIXES = {'k': 1e3, 'M': 1e6, 'G': 1e9}
-# yaml_loader = yaml.Loader
-# yaml_dumper = yaml.Dumper
-# yaml_tag = '!si'
-# yaml_implicit_pattern = re.compile(
-# r'^(?P<value>[0-9]+(?:\.[0-9]+)?)(?P<prefix>[kMG])$')
-#
-# @classmethod
-# def from_yaml(cls, loader, node):
-# return cls(node.value)
-#
-# @classmethod
-# def to_yaml(cls, dumper, data):
-# return dumper.represent_scalar(cls.yaml_tag, str(data))
-#
-# def __init__(self, *args):
-# m = self.yaml_implicit_pattern.match(args[0])
-# self.value = float(m.groupdict()['value'])
-# self.prefix = m.groupdict()['prefix']
-#
-# def __str__(self) -> None:
-# return str(self.value)+self.prefix
-#
-# def __int__(self) -> None:
-# return int(self.value*self.PREFIXES[self.prefix])
-#
-# # This fails:
-# yaml.add_implicit_resolver(SINumber.yaml_tag, SINumber.yaml_implicit_pattern)
-#
-# ret = yaml.load("""
-# [1,2,3, !si 10k, 100G]
-# """, Loader=yaml.Loader)
-# for idx, l in enumerate([1, 2, 3, 10000, 100000000000]):
-# assert int(ret[idx]) == l
-# '''
-# assert save_and_run(dedent(program_src), tmpdir) == 0
+ # def test_issue_82(self, tmpdir):
+ # program_src = r'''
+ # from ruamel import yaml
+ # import re
+ #
+ # class SINumber(yaml.YAMLObject):
+ # PREFIXES = {'k': 1e3, 'M': 1e6, 'G': 1e9}
+ # yaml_loader = yaml.Loader
+ # yaml_dumper = yaml.Dumper
+ # yaml_tag = '!si'
+ # yaml_implicit_pattern = re.compile(
+ # r'^(?P<value>[0-9]+(?:\.[0-9]+)?)(?P<prefix>[kMG])$')
+ #
+ # @classmethod
+ # def from_yaml(cls, loader, node):
+ # return cls(node.value)
+ #
+ # @classmethod
+ # def to_yaml(cls, dumper, data):
+ # return dumper.represent_scalar(cls.yaml_tag, str(data))
+ #
+ # def __init__(self, *args):
+ # m = self.yaml_implicit_pattern.match(args[0])
+ # self.value = float(m.groupdict()['value'])
+ # self.prefix = m.groupdict()['prefix']
+ #
+ # def __str__(self) -> None:
+ # return str(self.value)+self.prefix
+ #
+ # def __int__(self) -> None:
+ # return int(self.value*self.PREFIXES[self.prefix])
+ #
+ # # This fails:
+ # yaml.add_implicit_resolver(SINumber.yaml_tag, SINumber.yaml_implicit_pattern)
+ #
+ # ret = yaml.load("""
+ # [1,2,3, !si 10k, 100G]
+ # """, Loader=yaml.Loader)
+ # for idx, l in enumerate([1, 2, 3, 10000, 100000000000]):
+ # assert int(ret[idx]) == l
+ # '''
+ # assert save_and_run(dedent(program_src), tmpdir) == 0
def test_issue_82rt(self, tmpdir: Any) -> None:
yaml_str = '[1, 2, 3, !si 10k, 100G]\n'
x = round_trip(yaml_str, preserve_quotes=True) # NOQA
def test_issue_102(self) -> None:
- yaml_str = dedent("""
+ yaml_str = dedent(
+ """
var1: #empty
var2: something #notempty
var3: {} #empty object
var4: {a: 1} #filled object
var5: [] #empty array
- """)
+ """
+ )
x = round_trip(yaml_str, preserve_quotes=True) # NOQA
def test_issue_150(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
inp = """\
base: &base_key
@@ -107,54 +112,65 @@ class TestIssues:
def test_issue_160(self) -> None:
from ruamel.yaml.compat import StringIO
- s = dedent("""\
+
+ s = dedent(
+ """\
root:
# a comment
- {some_key: "value"}
foo: 32
bar: 32
- """)
+ """
+ )
a = round_trip_load(s)
del a['root'][0]['some_key']
buf = StringIO()
round_trip_dump(a, buf, block_seq_indent=4)
- exp = dedent("""\
+ exp = dedent(
+ """\
root:
# a comment
- {}
foo: 32
bar: 32
- """)
+ """
+ )
assert buf.getvalue() == exp
def test_issue_161(self) -> None:
- yaml_str = dedent("""\
+ yaml_str = dedent(
+ """\
mapping-A:
key-A:{}
mapping-B:
- """)
+ """
+ )
for comment in ['', ' # no-newline', ' # some comment\n', '\n']:
s = yaml_str.format(comment)
res = round_trip(s) # NOQA
def test_issue_161a(self) -> None:
- yaml_str = dedent("""\
+ yaml_str = dedent(
+ """\
mapping-A:
key-A:{}
mapping-B:
- """)
+ """
+ )
for comment in ['\n# between']:
s = yaml_str.format(comment)
res = round_trip(s) # NOQA
def test_issue_163(self) -> None:
- s = dedent("""\
+ s = dedent(
+ """\
some-list:
# List comment
- {}
- """)
+ """
+ )
x = round_trip(s, preserve_quotes=True) # NOQA
json_str = (
@@ -171,14 +187,15 @@ class TestIssues:
def test_issue_176(self) -> None:
# basic request by Stuart Berg
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
yaml = YAML()
seq = yaml.load('[1,2,3]')
seq[:] = [1, 2, 3, 4]
def test_issue_176_preserve_comments_on_extended_slice_assignment(self) -> None:
- yaml_str = dedent("""\
+ yaml_str = dedent(
+ """\
- a
- b # comment
- c # commment c
@@ -186,7 +203,8 @@ class TestIssues:
- d
- e # comment
- """)
+ """
+ )
seq = round_trip_load(yaml_str)
seq[1::2] = ['B', 'D']
res = round_trip_dump(seq)
@@ -243,28 +261,34 @@ class TestIssues:
assert m == []
def test_issue_184(self) -> None:
- yaml_str = dedent("""\
+ yaml_str = dedent(
+ """\
test::test:
# test
foo:
bar: baz
- """)
+ """
+ )
d = round_trip_load(yaml_str)
d['bar'] = 'foo'
d.yaml_add_eol_comment('test1', 'bar')
assert round_trip_dump(d) == yaml_str + 'bar: foo # test1\n'
def test_issue_219(self) -> None:
- yaml_str = dedent("""\
+ yaml_str = dedent(
+ """\
[StackName: AWS::StackName]
- """)
+ """
+ )
d = round_trip_load(yaml_str) # NOQA
def test_issue_219a(self) -> None:
- yaml_str = dedent("""\
+ yaml_str = dedent(
+ """\
[StackName:
AWS::StackName]
- """)
+ """
+ )
d = round_trip_load(yaml_str) # NOQA
def test_issue_220(self, tmpdir: Any) -> None:
@@ -289,103 +313,119 @@ class TestIssues:
a + [4, 5]
def test_issue_221_sort(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
yaml = YAML()
- inp = dedent("""\
+ inp = dedent(
+ """\
- d
- a # 1
- c # 3
- e # 5
- b # 2
- """)
+ """
+ )
a = yaml.load(dedent(inp))
a.sort()
buf = StringIO()
yaml.dump(a, buf)
- exp = dedent("""\
+ exp = dedent(
+ """\
- a # 1
- b # 2
- c # 3
- d
- e # 5
- """)
+ """
+ )
assert buf.getvalue() == exp
def test_issue_221_sort_reverse(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
yaml = YAML()
- inp = dedent("""\
+ 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("""\
+ exp = dedent(
+ """\
- e # 5
- d
- c # 3
- b # 2
- a # 1
- """)
+ """
+ )
assert buf.getvalue() == exp
def test_issue_221_sort_key(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
yaml = YAML()
- inp = dedent("""\
+ inp = dedent(
+ """\
- four
- One # 1
- Three # 3
- five # 5
- two # 2
- """)
+ """
+ )
a = yaml.load(dedent(inp))
a.sort(key=str.lower)
buf = StringIO()
yaml.dump(a, buf)
- exp = dedent("""\
+ exp = dedent(
+ """\
- five # 5
- four
- One # 1
- Three # 3
- two # 2
- """)
+ """
+ )
assert buf.getvalue() == exp
def test_issue_221_sort_key_reverse(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
yaml = YAML()
- inp = dedent("""\
+ 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("""\
+ exp = dedent(
+ """\
- two # 2
- Three # 3
- One # 1
- four
- five # 5
- """)
+ """
+ )
assert buf.getvalue() == exp
def test_issue_222(self) -> None:
@@ -414,7 +454,7 @@ class TestIssues:
yaml.load('{]')
def test_issue_233(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
import json
yaml = YAML()
@@ -422,7 +462,7 @@ class TestIssues:
json_str = json.dumps(data) # NOQA
def test_issue_233a(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
import json
yaml = YAML()
@@ -430,16 +470,18 @@ class TestIssues:
json_str = json.dumps(data) # NOQA
def test_issue_234(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
- inp = dedent("""\
+ inp = dedent(
+ """\
- key: key1
ctx: [one, two]
help: one
cmd: >
foo bar
foo bar
- """)
+ """
+ )
yaml = YAML(typ='safe', pure=True)
data = yaml.load(inp)
fold = data[0]['cmd']
@@ -513,7 +555,8 @@ class TestIssues:
assert d0['a'] == 'b'
def test_issue_245(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
+
inp = """
d: yes
"""
@@ -532,19 +575,23 @@ class TestIssues:
def test_issue_249(self) -> None:
yaml = YAML()
- inp = dedent("""\
+ inp = dedent(
+ """\
# comment
-
- 1
- 2
- 3
- """)
- exp = dedent("""\
+ """
+ )
+ exp = dedent(
+ """\
# comment
- - 1
- 2
- 3
- """)
+ """
+ )
yaml.round_trip(inp, outp=exp) # NOQA
def test_issue_250(self) -> None:
@@ -560,18 +607,20 @@ class TestIssues:
# @pytest.mark.xfail(strict=True, reason='bla bla', raises=AssertionError)
def test_issue_279(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
yaml = YAML()
yaml.indent(sequence=4, offset=2)
- inp = dedent("""\
+ inp = dedent(
+ """\
experiments:
- datasets:
# ATLAS EWK
- {dataset: ATLASWZRAP36PB, frac: 1.0}
- {dataset: ATLASZHIGHMASS49FB, frac: 1.0}
- """)
+ """
+ )
a = yaml.load(inp)
buf = StringIO()
yaml.dump(a, buf)
@@ -579,10 +628,11 @@ class TestIssues:
assert buf.getvalue() == inp
def test_issue_280(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
from ruamel.yaml.representer import RepresenterError
from collections import namedtuple
from sys import stdout
+
T = namedtuple('T', ('a', 'b'))
t = T(1, 2)
yaml = YAML()
@@ -592,6 +642,7 @@ class TestIssues:
def test_issue_282(self) -> None:
# update from list of tuples caused AttributeError
import ruamel.yaml
+
yaml_data = ruamel.yaml.comments.CommentedMap([('a', 'apple'), ('b', 'banana')])
yaml_data.update([('c', 'cantaloupe')])
yaml_data.update({'d': 'date', 'k': 'kiwi'})
@@ -600,12 +651,15 @@ class TestIssues:
def test_issue_284(self) -> None:
import ruamel.yaml
- inp = dedent("""\
+
+ inp = dedent(
+ """\
plain key: in-line value
: # Both empty
"quoted key":
- entry
- """)
+ """
+ )
yaml = ruamel.yaml.YAML(typ='rt')
yaml.version = (1, 2)
d = yaml.load(inp)
@@ -617,17 +671,19 @@ class TestIssues:
d = yaml.load(inp)
def test_issue_285(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
yaml = YAML()
- inp = dedent("""\
+ inp = dedent(
+ """\
%YAML 1.1
---
- y
- n
- Y
- N
- """)
+ """
+ )
a = yaml.load(inp)
assert a[0]
assert a[2]
@@ -635,15 +691,17 @@ class TestIssues:
assert not a[3]
def test_issue_286(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
yaml = YAML()
- inp = dedent("""\
+ inp = dedent(
+ """\
parent_key:
- sub_key: sub_value
- # xxx""")
+ # xxx"""
+ )
a = yaml.load(inp)
a['new_key'] = 'new_value'
buf = StringIO()
@@ -653,9 +711,10 @@ class TestIssues:
def test_issue_288(self) -> None:
import sys
from ruamel.yaml.compat import StringIO
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
- yamldoc = dedent("""\
+ yamldoc = dedent(
+ """\
---
# Reusable values
aliases:
@@ -669,7 +728,8 @@ class TestIssues:
- &thirdEntry Third entry
# EOF Comment
- """)
+ """
+ )
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
@@ -684,9 +744,10 @@ class TestIssues:
def test_issue_288a(self) -> None:
import sys
from ruamel.yaml.compat import StringIO
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
- yamldoc = dedent("""\
+ yamldoc = dedent(
+ """\
---
# Reusable values
aliases:
@@ -700,7 +761,8 @@ class TestIssues:
- &thirdEntry Third entry
# EOF Comment
- """)
+ """
+ )
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
@@ -715,9 +777,10 @@ class TestIssues:
def test_issue_290(self) -> None:
import sys
from ruamel.yaml.compat import StringIO
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
- yamldoc = dedent("""\
+ yamldoc = dedent(
+ """\
---
aliases:
# Folded-element comment
@@ -736,7 +799,8 @@ class TestIssues:
# Plain-element comment
- &plainEntry Plain entry
- """)
+ """
+ )
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
@@ -751,9 +815,10 @@ class TestIssues:
def test_issue_290a(self) -> None:
import sys
from ruamel.yaml.compat import StringIO
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
- yamldoc = dedent("""\
+ yamldoc = dedent(
+ """\
---
aliases:
# Folded-element comment
@@ -772,7 +837,8 @@ class TestIssues:
# Plain-element comment
- &plainEntry Plain entry
- """)
+ """
+ )
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
@@ -791,7 +857,8 @@ class TestIssues:
# some old merge_comment code
import copy
- inp = dedent("""
+ inp = dedent(
+ """
A:
b:
# comment
@@ -805,34 +872,40 @@ class TestIssues:
- - l31
- l32
- l33: '5'
- """)
+ """
+ )
data = round_trip_load(inp) # NOQA
dc = copy.deepcopy(data)
assert round_trip_dump(dc) == inp
def test_issue_300(self) -> None:
- from ruamel.yaml import YAML # type: ignore
+ from ruamel.yaml import YAML
- inp = dedent("""
+ inp = dedent(
+ """
%YAML 1.2
%TAG ! tag:example.com,2019/path#fragment
---
null
- """)
+ """
+ )
YAML().load(inp)
def test_issue_300a(self) -> None:
import ruamel.yaml
- inp = dedent("""
+ inp = dedent(
+ """
%YAML 1.1
%TAG ! tag:example.com,2019/path#fragment
---
null
- """)
+ """
+ )
yaml = YAML()
- with pytest.raises(ruamel.yaml.scanner.ScannerError,
- match='while scanning a directive'):
+ with pytest.raises(
+ ruamel.yaml.scanner.ScannerError, match='while scanning a directive'
+ ):
yaml.load(inp)
def test_issue_304(self) -> None:
@@ -864,12 +937,83 @@ class TestIssues:
"""
d = na_round_trip(inp) # NOQA
+ def test_issue_445(self) -> None:
+ from ruamel.yaml import YAML
+ from ruamel.yaml.compat import StringIO
+
+ yaml = YAML()
+ yaml.version = '1.1'
+ data = yaml.load('quote: I have seen things')
+ buf = StringIO()
+ yaml.dump(data, buf)
+ assert buf.getvalue() == '%YAML 1.1\n---\nquote: I have seen things\n'
+ yaml = YAML()
+ yaml.version = [1, 1]
+ data = yaml.load('quote: I have seen things')
+ buf = StringIO()
+ yaml.dump(data, buf)
+ assert buf.getvalue() == '%YAML 1.1\n---\nquote: I have seen things\n'
+
def test_issue_449(self) -> None:
inp = """\
emoji_index: !!python/name:materialx.emoji.twemoji
"""
d = na_round_trip(inp) # NOQA
+ def test_issue_455(self) -> None:
+ from ruamel.yaml import YAML
+
+ cm = YAML().map(a=97, b=98)
+ cm.update({'c': 42, 'd': 196})
+ cm.update(c=99, d=100)
+ prev = None
+ for k, v in cm.items():
+ if prev is not None:
+ assert prev + 1 == v
+ prev = v
+ assert ord(k) == v
+ assert len(cm) == 4
+
+ def test_issue_454(self) -> None:
+ inp = """
+ test1: 🎉
+ test2: "🎉"
+ test3: '🎉'
+ """
+ d = round_trip(inp, preserve_quotes=True) # NOQA
+
+ def test_so_75631454(self) -> None:
+ from ruamel.yaml import YAML
+ from ruamel.yaml.compat import StringIO
+
+ inp = dedent(
+ """
+ test:
+ long: "This is a sample text
+ across two lines."
+ """
+ )
+ yaml = YAML()
+ yaml.preserve_quotes = True
+ yaml.indent(mapping=4)
+ yaml.width = 27
+ data = yaml.load(inp)
+ buf = StringIO()
+ yaml.dump(data, buf)
+ assert buf.getvalue() == inp
+
+ def test_issue_458(self) -> None:
+ from io import StringIO
+ from ruamel.yaml import YAML
+
+ yaml = YAML()
+ out_stream = StringIO()
+ in_string = "a" * 128
+ yaml.dump(in_string, out_stream)
+ result = out_stream.getvalue()
+ assert in_string == result.splitlines()[0]
+
+
# @pytest.mark.xfail(strict=True, reason='bla bla', raises=AssertionError)
# def test_issue_ xxx(self) -> None:
# inp = """