summaryrefslogtreecommitdiff
path: root/_test/test_add_xxx.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2021-03-09 09:02:50 +0100
committerAnthon van der Neut <anthon@mnt.org>2021-03-09 09:02:50 +0100
commite73562c6f14d1d71a9fea174d58465e1b13f68af (patch)
tree309851cca7d411b31c27753555871d493282c7f0 /_test/test_add_xxx.py
parent96839d9f64f4698bdc519cbfbd48d51178460714 (diff)
downloadruamel.yaml-e73562c6f14d1d71a9fea174d58465e1b13f68af.tar.gz
remove python 2 specific code
add future deprecation warning to old style functions
Diffstat (limited to '_test/test_add_xxx.py')
-rw-r--r--_test/test_add_xxx.py68
1 files changed, 40 insertions, 28 deletions
diff --git a/_test/test_add_xxx.py b/_test/test_add_xxx.py
index 085e352..8beac65 100644
--- a/_test/test_add_xxx.py
+++ b/_test/test_add_xxx.py
@@ -3,7 +3,7 @@
import re
import pytest # NOQA
-from roundtrip import dedent
+from roundtrip import dedent, round_trip_dump # NOQA
# from PyYAML docs
@@ -22,48 +22,50 @@ def dice_constructor(loader, node):
def dice_representer(dumper, data):
- return dumper.represent_scalar(u'!dice', u'{}d{}'.format(*data))
+ return dumper.represent_scalar('!dice', '{}d{}'.format(*data))
def test_dice_constructor():
import ruamel.yaml # NOQA
- ruamel.yaml.add_constructor(u'!dice', dice_constructor)
- data = ruamel.yaml.load('initial hit points: !dice 8d4', Loader=ruamel.yaml.Loader)
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ ruamel.yaml.add_constructor('!dice', dice_constructor)
+ data = yaml.load('initial hit points: !dice 8d4')
assert str(data) == "{'initial hit points': Dice(8,4)}"
def test_dice_constructor_with_loader():
import ruamel.yaml # NOQA
- ruamel.yaml.add_constructor(u'!dice', dice_constructor, Loader=ruamel.yaml.Loader)
- data = ruamel.yaml.load('initial hit points: !dice 8d4', Loader=ruamel.yaml.Loader)
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ ruamel.yaml.add_constructor('!dice', dice_constructor, Loader=ruamel.yaml.Loader)
+ data = yaml.load('initial hit points: !dice 8d4')
assert str(data) == "{'initial hit points': Dice(8,4)}"
def test_dice_representer():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ yaml.default_flow_style = False
ruamel.yaml.add_representer(Dice, dice_representer)
# ruamel.yaml 0.15.8+ no longer forces quotes tagged scalars
- assert (
- ruamel.yaml.dump(dict(gold=Dice(10, 6)), default_flow_style=False)
- == 'gold: !dice 10d6\n'
- )
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(dict(gold=Dice(10, 6)), buf)
+ assert buf.getvalue() == 'gold: !dice 10d6\n'
def test_dice_implicit_resolver():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ yaml.default_flow_style = False
pattern = re.compile(r'^\d+d\d+$')
- ruamel.yaml.add_implicit_resolver(u'!dice', pattern)
- assert (
- ruamel.yaml.dump(dict(treasure=Dice(10, 20)), default_flow_style=False)
- == 'treasure: 10d20\n'
- )
- assert ruamel.yaml.load('damage: 5d10', Loader=ruamel.yaml.Loader) == dict(
- damage=Dice(5, 10)
- )
+ ruamel.yaml.add_implicit_resolver('!dice', pattern)
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(dict(treasure=Dice(10, 20)), buf)
+ assert buf.getvalue() == 'treasure: 10d20\n'
+ assert yaml.load('damage: 5d10') == dict(damage=Dice(5, 10))
class Obj1(dict):
@@ -82,7 +84,7 @@ class Obj1(dict):
class YAMLObj1(object):
- yaml_tag = u'!obj:'
+ yaml_tag = '!obj:'
@classmethod
def from_yaml(cls, loader, suffix, node):
@@ -103,24 +105,30 @@ class YAMLObj1(object):
def test_yaml_obj():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
ruamel.yaml.add_representer(Obj1, YAMLObj1.to_yaml)
ruamel.yaml.add_multi_constructor(YAMLObj1.yaml_tag, YAMLObj1.from_yaml)
- x = ruamel.yaml.load('!obj:x.2\na: 1', Loader=ruamel.yaml.Loader)
+ x = yaml.load('!obj:x.2\na: 1')
print(x)
- assert ruamel.yaml.dump(x) == """!obj:x.2 "{'a': 1}"\n"""
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(x, buf)
+ assert buf.getvalue() == """!obj:x.2 "{'a': 1}"\n"""
def test_yaml_obj_with_loader_and_dumper():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
ruamel.yaml.add_representer(Obj1, YAMLObj1.to_yaml, Dumper=ruamel.yaml.Dumper)
ruamel.yaml.add_multi_constructor(
YAMLObj1.yaml_tag, YAMLObj1.from_yaml, Loader=ruamel.yaml.Loader
)
- x = ruamel.yaml.load('!obj:x.2\na: 1', Loader=ruamel.yaml.Loader)
+ x = yaml.load('!obj:x.2\na: 1')
# x = ruamel.yaml.load('!obj:x.2\na: 1')
print(x)
- assert ruamel.yaml.dump(x) == """!obj:x.2 "{'a': 1}"\n"""
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(x, buf)
+ assert buf.getvalue() == """!obj:x.2 "{'a': 1}"\n"""
# ToDo use nullege to search add_multi_representer and add_path_resolver
@@ -135,7 +143,7 @@ def test_issue_127():
class Ref(ruamel.yaml.YAMLObject):
yaml_constructor = ruamel.yaml.RoundTripConstructor
yaml_representer = ruamel.yaml.RoundTripRepresenter
- yaml_tag = u'!Ref'
+ yaml_tag = '!Ref'
def __init__(self, logical_id):
self.logical_id = logical_id
@@ -163,7 +171,11 @@ def test_issue_127():
- Five Six
- 'Seven Eight'
""")
- data = ruamel.yaml.round_trip_load(document, preserve_quotes=True)
- assert ruamel.yaml.round_trip_dump(data, indent=4, block_seq_indent=2) == document.replace(
- '\n Two and', ' Two and'
- )
+ yaml = ruamel.yaml.YAML()
+ yaml.preserve_quotes = True
+ yaml.default_flow_style = None
+ yaml.indent(sequence=4, offset=2)
+ data = yaml.load(document)
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(data, buf)
+ assert buf.getvalue() == document.replace('\n Two and', ' Two and')