-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor assertEqualXML into a testtools matcher
Not all tests inheriting from TestCase will need to check XML equality. Moving this functionality to a matcher leaves it up to the test class to decided whether or not it needs it. Change-Id: Ib28ec3b5dd96f662ce0cd90c650434b24c63ad6c Related-Bug: #1226466
- Loading branch information
Showing
5 changed files
with
141 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
|
||
# Copyright 2013 OpenStack Foundation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import six | ||
|
||
from lxml import etree | ||
from testtools import matchers | ||
|
||
|
||
class XMLEquals(object): | ||
"""Parses two XML documents from strings and compares the results. | ||
""" | ||
|
||
def __init__(self, expected): | ||
self.expected = expected | ||
|
||
def __str__(self): | ||
return "%s(%r)" % (self.__class__.__name__, self.expected) | ||
|
||
def match(self, other): | ||
parser = etree.XMLParser(remove_blank_text=True) | ||
|
||
def canonical_xml(s): | ||
s = s.strip() | ||
|
||
fp = six.StringIO() | ||
dom = etree.fromstring(s, parser) | ||
dom.getroottree().write_c14n(fp) | ||
s = fp.getvalue() | ||
|
||
dom = etree.fromstring(s, parser) | ||
return etree.tostring(dom, pretty_print=True) | ||
|
||
expected = canonical_xml(self.expected) | ||
other = canonical_xml(other) | ||
if expected == other: | ||
return | ||
return XMLMismatch(expected, other) | ||
|
||
|
||
class XMLMismatch(matchers.Mismatch): | ||
|
||
def __init__(self, expected, other): | ||
self.expected = expected | ||
self.other = other | ||
|
||
def describe(self): | ||
return 'expected = %s\nactual = %s' % (self.expected, self.other) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
|
||
# Copyright 2013 OpenStack Foundation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import textwrap | ||
|
||
import testtools | ||
from testtools.tests.matchers import helpers | ||
|
||
from keystone.tests import matchers | ||
|
||
|
||
class TestXMLEquals(testtools.TestCase, helpers.TestMatchersInterface): | ||
matches_xml = """ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<test xmlns="http://docs.openstack.org/identity/api/v2.0"> | ||
<success a="a" b="b"/> | ||
</test> | ||
""" | ||
equivalent_xml = """ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<test xmlns="http://docs.openstack.org/identity/api/v2.0"> | ||
<success b="b" a="a"></success> | ||
</test> | ||
""" | ||
mismatches_xml = """ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<test xmlns="http://docs.openstack.org/identity/api/v2.0"> | ||
<nope_it_fails/> | ||
</test> | ||
""" | ||
mismatches_description = textwrap.dedent("""\ | ||
expected = <test xmlns="http://docs.openstack.org/identity/api/v2.0"> | ||
<success a="a" b="b"/> | ||
</test> | ||
actual = <test xmlns="http://docs.openstack.org/identity/api/v2.0"> | ||
<nope_it_fails/> | ||
</test> | ||
""").lstrip() | ||
|
||
matches_matcher = matchers.XMLEquals(matches_xml) | ||
matches_matches = [matches_xml, equivalent_xml] | ||
matches_mismatches = [mismatches_xml] | ||
describe_examples = [ | ||
(mismatches_description, mismatches_xml, matches_matcher), | ||
] | ||
str_examples = [('XMLEquals(%r)' % matches_xml, matches_matcher)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters