summaryrefslogtreecommitdiff
path: root/_test/test_add_xxx.py
diff options
context:
space:
mode:
Diffstat (limited to '_test/test_add_xxx.py')
-rw-r--r--_test/test_add_xxx.py50
1 files changed, 33 insertions, 17 deletions
diff --git a/_test/test_add_xxx.py b/_test/test_add_xxx.py
index f2e976f..031b89f 100644
--- a/_test/test_add_xxx.py
+++ b/_test/test_add_xxx.py
@@ -1,7 +1,7 @@
# coding: utf-8
import re
-import pytest # NOQA
+import pytest # NOQA
from roundtrip import dedent
@@ -12,7 +12,7 @@ class Dice(tuple):
return tuple.__new__(cls, [a, b])
def __repr__(self):
- return "Dice(%s,%s)" % self
+ return 'Dice(%s,%s)' % self
def dice_constructor(loader, node):
@@ -27,6 +27,7 @@ def dice_representer(dumper, 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)
assert str(data) == "{'initial hit points': Dice(8,4)}"
@@ -34,6 +35,7 @@ def test_dice_constructor():
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)
assert str(data) == "{'initial hit points': Dice(8,4)}"
@@ -41,20 +43,27 @@ def test_dice_constructor_with_loader():
def test_dice_representer():
import ruamel.yaml # NOQA
+
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"
+ assert (
+ ruamel.yaml.dump(dict(gold=Dice(10, 6)), default_flow_style=False)
+ == 'gold: !dice 10d6\n'
+ )
def test_dice_implicit_resolver():
import ruamel.yaml # NOQA
+
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))
+ 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)
+ )
class Obj1(dict):
@@ -78,6 +87,7 @@ class YAMLObj1(object):
@classmethod
def from_yaml(cls, loader, suffix, node):
import ruamel.yaml # NOQA
+
obj1 = Obj1(suffix)
if isinstance(node, ruamel.yaml.MappingNode):
obj1.add_node(loader.construct_mapping(node))
@@ -92,22 +102,25 @@ class YAMLObj1(object):
def test_yaml_obj():
import ruamel.yaml # NOQA
+
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)
print(x)
- assert ruamel.yaml.dump(x) == '''!obj:x.2 "{'a': 1}"\n'''
+ assert ruamel.yaml.dump(x) == """!obj:x.2 "{'a': 1}"\n"""
def test_yaml_obj_with_loader_and_dumper():
import ruamel.yaml # NOQA
+
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)
+ 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 = ruamel.yaml.load('!obj:x.2\na: 1')
print(x)
- assert ruamel.yaml.dump(x) == '''!obj:x.2 "{'a': 1}"\n'''
+ assert ruamel.yaml.dump(x) == """!obj:x.2 "{'a': 1}"\n"""
# ToDo use nullege to search add_multi_representer and add_path_resolver
@@ -134,12 +147,13 @@ def test_issue_127():
@classmethod
def to_yaml(cls, dumper, data):
if isinstance(data.logical_id, ruamel.yaml.scalarstring.ScalarString):
- style = data.logical_id.style # ruamel.yaml>0.15.8
+ style = data.logical_id.style # ruamel.yaml>0.15.8
else:
style = None
return dumper.represent_scalar(cls.yaml_tag, data.logical_id, style=style)
- document = dedent('''\
+ document = dedent(
+ """\
AList:
- !Ref One
- !Ref 'Two'
@@ -149,7 +163,9 @@ def test_issue_127():
CList:
- 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')
+ assert ruamel.yaml.round_trip_dump(data, indent=4, block_seq_indent=2) == document.replace(
+ '\n Two and', ' Two and'
+ )