Skip to content

Commit

Permalink
Merge pull request #5 from wimglenn/safe_dump
Browse files Browse the repository at this point in the history
Allow to safe_dump with OrderedDict instances
  • Loading branch information
wimglenn authored Apr 17, 2018
2 parents 1e8c8ff + de97b8b commit ab8da29
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def map_constructor(loader, node):

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


if sys.version_info < (3, 7):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from distutils.core import setup
from setuptools import setup

setup(
name='oyaml',
version='0.2',
version='0.3',
description='Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering',
author='Wim Glenn',
author_email='[email protected]',
Expand Down
46 changes: 40 additions & 6 deletions test_oyaml.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
import sys
from collections import OrderedDict
from types import GeneratorType

import pytest
from yaml.representer import RepresenterError

import oyaml as yaml


d = OrderedDict([('k1', 'v1'), ('k3', 'v3'), ('k2', 'v2')])
data = OrderedDict([('x', 1), ('z', 3), ('y', 2)])


def test_dump():
assert yaml.dump(d) == '{k1: v1, k3: v3, k2: v2}\n'
assert yaml.dump(data) == '{x: 1, z: 3, y: 2}\n'


def test_safe_dump():
assert yaml.safe_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'


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


def test_load():
loaded = yaml.load('{k1: v1, k3: v3, k2: v2}')
assert loaded == {'k1': 'v1', 'k3': 'v3', 'k2': 'v2'}
loaded = yaml.load('{x: 1, z: 3, y: 2}')
assert loaded == {'x': 1, 'z': 3, 'y': 2}


def test_load_all():
gen = yaml.load_all('{x: 1, z: 3, y: 2}\n--- {}\n')
assert isinstance(gen, GeneratorType)
ordered_data, empty_dict = gen
assert empty_dict == {}
assert ordered_data == data


@pytest.mark.skipif(sys.version_info >= (3,7), reason="requires python3.6-")
def test_loads_to_ordered_dict():
loaded = yaml.load('{k1: v1, k3: v3, k2: v2}')
loaded = yaml.load('{x: 1, z: 3, y: 2}')
assert isinstance(loaded, OrderedDict)


@pytest.mark.skipif(sys.version_info < (3,7), reason="requires python3.7+")
def test_loads_to_std_dict():
loaded = yaml.load('{k1: v1, k3: v3, k2: v2}')
loaded = yaml.load('{x: 1, z: 3, y: 2}')
assert not isinstance(loaded, OrderedDict)
assert isinstance(loaded, dict)


def test_subclass_dump():

class MyOrderedDict(OrderedDict):
pass

data = MyOrderedDict([('x', 1), ('y', 2)])
assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml.dump(data)
with pytest.raises(RepresenterError) as cm:
yaml.safe_dump(data)
assert str(cm.value) == "cannot represent an object: MyOrderedDict([('x', 1), ('y', 2)])"


def test_anchors_and_references():
text = '''
defaults:
Expand Down

0 comments on commit ab8da29

Please sign in to comment.