Skip to content

Commit

Permalink
Merge pull request #8 from wimglenn/pyyaml410
Browse files Browse the repository at this point in the history
Support PyYAML 4.1.0 names
  • Loading branch information
wimglenn authored Jun 27, 2018
2 parents 155ec0c + 6a41ce7 commit 11d36a8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ python:
- "pypy"
- "pypy3.5"

env:
- PYYAML_VERSION="3.12"
- PYYAML_VERSION="4.1"

matrix:
fast_finish: true
allow_failures:
- python: "nightly"

install:
- pip install pyyaml~=$PYYAML_VERSION
- pip install --editable .
- pip install pytest-cov coveralls

Expand Down
17 changes: 12 additions & 5 deletions oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ def map_constructor(loader, node):
loader.flatten_mapping(node)
return OrderedDict(loader.construct_pairs(node))


pyyaml.add_representer(dict, map_representer)
pyyaml.add_representer(OrderedDict, map_representer)
pyyaml.add_representer(dict, map_representer, Dumper=pyyaml.dumper.SafeDumper)
pyyaml.add_representer(OrderedDict, map_representer, Dumper=pyyaml.dumper.SafeDumper)
if pyyaml.safe_dump is pyyaml.dump:
# PyYAML >= 4
SafeDumper = pyyaml.dumper.Dumper
DangerDumper = pyyaml.dumper.DangerDumper
else:
SafeDumper = pyyaml.dumper.SafeDumper
DangerDumper = pyyaml.dumper.Dumper

pyyaml.add_representer(dict, map_representer, Dumper=SafeDumper)
pyyaml.add_representer(OrderedDict, map_representer, Dumper=SafeDumper)
pyyaml.add_representer(dict, map_representer, Dumper=DangerDumper)
pyyaml.add_representer(OrderedDict, map_representer, Dumper=DangerDumper)


if sys.version_info < (3, 7):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='oyaml',
version='0.4',
version='0.5',
description='Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering',
long_description=open('README.rst').read(),
author='Wim Glenn',
Expand Down
51 changes: 48 additions & 3 deletions test_oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,28 @@ def test_safe_dump():
assert yaml.safe_dump(data) == '{x: 1, z: 3, y: 2}\n'


@pytest.mark.skipif(yaml.pyyaml.__version__ < '4', reason="requires PyYAML version >= 4")
def test_danger_dump():
assert yaml.danger_dump(data) == '{x: 1, z: 3, y: 2}\n'


def test_dump_all():
assert yaml.dump_all(documents=[data, {}]) == '{x: 1, z: 3, y: 2}\n--- {}\n'


@pytest.mark.skipif(yaml.pyyaml.__version__ >= '4', reason="requires PyYAML version < 4")
def test_dump_and_safe_dump_match():
mydict = {'x': 1, 'z': 2, 'y': 3}
# don't know if mydict is ordered in the implementation or not (but don't care)
assert yaml.dump(mydict) == yaml.safe_dump(mydict)


@pytest.mark.skipif(yaml.pyyaml.__version__ < '4', reason="requires PyYAML version >= 4")
def test_danger_dump_and_safe_dump_match():
mydict = {'x': 1, 'z': 2, 'y': 3}
assert yaml.danger_dump(mydict) == yaml.safe_dump(mydict)


def test_safe_dump_all():
assert yaml.safe_dump_all(documents=[data, {}]) == '{x: 1, z: 3, y: 2}\n--- {}\n'

Expand Down Expand Up @@ -58,18 +70,28 @@ def test_loads_to_std_dict():
assert isinstance(loaded, dict)


def test_subclass_dump():
class MyOrderedDict(OrderedDict):
pass

class MyOrderedDict(OrderedDict):
pass

@pytest.mark.skipif(yaml.pyyaml.__version__ >= '4', reason="requires PyYAML version < 4")
def test_subclass_dump_pyyaml3():
data = MyOrderedDict([('x', 1), ('y', 2)])
assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml.dump(data)
with pytest.raises(yaml.pyyaml.representer.RepresenterError) as cm:
yaml.safe_dump(data)
assert str(cm.value) == "cannot represent an object: MyOrderedDict([('x', 1), ('y', 2)])"


@pytest.mark.skipif(yaml.pyyaml.__version__ < '4', reason="requires PyYAML version >= 4")
def test_subclass_dump_pyyaml4():
data = MyOrderedDict([('x', 1), ('y', 2)])
assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml.danger_dump(data)
with pytest.raises(yaml.pyyaml.representer.RepresenterError) as cm:
yaml.dump(data)
assert str(cm.value) == "('cannot represent an object', MyOrderedDict([('x', 1), ('y', 2)]))"


def test_anchors_and_references():
text = '''
defaults:
Expand Down Expand Up @@ -103,3 +125,26 @@ def test_anchors_and_references():
},
}
assert yaml.load(text) == expected_load


def test_omap():
text = '''
Bestiary: !!omap
- aardvark: African pig-like ant eater. Ugly.
- anteater: South-American ant eater. Two species.
- anaconda: South-American constrictor snake. Scaly.
'''
expected_load = {
'Bestiary': ([
('aardvark', 'African pig-like ant eater. Ugly.'),
('anteater', 'South-American ant eater. Two species.'),
('anaconda', 'South-American constrictor snake. Scaly.'),
])
}
assert yaml.load(text) == expected_load


def test_omap_flow_style():
text = 'Numbers: !!omap [ one: 1, two: 2, three : 3 ]'
expected_load = {'Numbers': ([('one', 1), ('two', 2), ('three', 3)])}
assert yaml.load(text) == expected_load

0 comments on commit 11d36a8

Please sign in to comment.