class V: with_indent = Converter(indent=" ", newlines=True) without_indent = Converter(indent="", newlines=True) without_newlines = Converter(newlines=False) def assertIndenter(self, indenter, nodes, wrap, expected): result = "".join([wrap, indenter(nodes, wrap), wrap]) assert result == expected.strip()
def compare(self, data, result, **kwargs): self.assertEqual.__self__.maxDiff = None converter = Converter(wrap='all', **kwargs) made = converter.build(data) self.assertEqual(result.strip(), made)
# coding: spec from dict2xml import Converter from textwrap import dedent from unittest import mock import pytest describe "Converter": describe "Building": it "creates an indenter, a node, and then calls serialize on the node with the indenter": wrap = mock.Mock("wrap") indent = mock.Mock("indent") newlines = mock.Mock("newlines") converter = Converter(wrap, indent, newlines) node = mock.Mock(name="node") FakeNode = mock.Mock(name="Node", return_value=node) serialized = mock.Mock(name="serialized") node.serialize.return_value = serialized indenter = mock.Mock(name="indenter") make_indenter = mock.Mock(name="make_indenter", return_value=indenter) mip = mock.patch.object(converter, "_make_indenter", make_indenter) fnp = mock.patch("dict2xml.logic.Node", FakeNode) data = mock.Mock(name="data")
def assertResult(result, **kwargs): data = {"a": [1, 2, 3], "b": {"c": "d", "e": {"f": "g"}}, "d": 1} converter = Converter(wrap="all", **kwargs) print(converter.build(data)) assert dedent(result).strip() == converter.build(data)
<a>3</a> <b> <c>d</c> <e> <f>g</f> </e> </b> <d>1</d> </all> """ assertResult(expected, indent=None, newlines=True) it "with just indentation", assertResult: # Indentation requires newlines to work expected = "<all><a>1</a><a>2</a><a>3</a><b><c>d</c><e><f>g</f></e></b><d>1</d></all>" assertResult(expected, indent=" ", newlines=False) it "with no whitespace", assertResult: expected = "<all><a>1</a><a>2</a><a>3</a><b><c>d</c><e><f>g</f></e></b><d>1</d></all>" assertResult(expected, indent=None, newlines=False) it "works on a massive, complex dictionary": with open(os.path.join(examples, "python_dict.json"), "r") as fle: data = json.load(fle) with open(os.path.join(examples, "result.xml"), "r") as fle: result = fle.read() converter = Converter(wrap="all", indent=" ", newlines=True) assert dedent(result).strip() == converter.build(data)