¡@

Home 

OpenStack Study: matchers.py

OpenStack Index

**** CubicPower OpenStack Study ****

# 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

**** CubicPower OpenStack Study ****

class XMLEquals(object):

"""Parses two XML documents from strings and compares the results.

"""

**** CubicPower OpenStack Study ****

    def __init__(self, expected):

        self.expected = expected

**** CubicPower OpenStack Study ****

    def __str__(self):

        return "%s(%r)" % (self.__class__.__name__, self.expected)

**** CubicPower OpenStack Study ****

    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)

**** CubicPower OpenStack Study ****

        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)

**** CubicPower OpenStack Study ****

class XMLMismatch(matchers.Mismatch):

**** CubicPower OpenStack Study ****

    def __init__(self, expected, other):

        self.expected = expected

        self.other = other

**** CubicPower OpenStack Study ****

    def describe(self):

        return 'expected = %s\nactual = %s' % (self.expected, self.other)