Module: Test::Unit::XML

Included in:
TestCase
Defined in:
lib/test/unit/xml/conditionals.rb,
lib/test/unit/xml/nodeiterator.rb,
lib/test/unit/xml/xml_assertions.rb,
lib/test/unit/xml/xmlequalfilter.rb

Defined Under Namespace

Classes: Conditionals, NodeIterator, XmlEqualFilter

Instance Method Summary collapse

Instance Method Details

#assert_xml_equal(expected_doc, actual_doc, message = nil) ⇒ Object

This method checks whether two well-formed XML documents are equal. Two XML documents are considered equal if:

  • They contain the same type of nodes, in the same order, except for text nodes that are empty, or contain only whitespace. Such text nodes are ignored.

  • The corresponding nodes in the two documents are equal.

Nodes are tested for equality as follows:

XML Declarations

XML declarations are equal if they have the same version, encoding, and stand-alone pseudo-attributes.

Doctype

Doctypes are equal if they fulfill all of the following conditions:

  • They have the same public identifier, or neither has a public identifier

  • If one of the doctypes has a system identifier that is a URN, the other doctype must have a system identifier that is the same URN. System identifiers that are URLs are ignored for comparison purposes. The reason is that the same DTD is very often stored in many different locations (for example different directories on different computers). Therefore the physical location of the DTD does not say anything useful about whether two documents are equal.

  • An entity declaration present in one of the doctype declarations must also be present in the other.

  • A notation declaration present in one of the doctype declarations must also be present in the other.

Internal General Entity Declaration

Internal General entity declarations are equal if they have the same name, and the same value.

External General Entity Declaration

External general entity declarations are equal if they have the same name, and if the identifiers are of the same type (PUBLIC or SYSTEM) and have the same value. Note that if the identifiers are URLs, a comparison may fail even though both URLS point to the same resource, for example if one URL is relative and the other is absolute.

Notation Declaration

Notation declarations are equal if they have the same name, and if the identifiers are of the same type (PUBLIC or SYSTEM) and have the same value.

Elements

Elements are considered equal if they have the same generic identifier (tag name), belong to the same namespace, and have the same attributes. Note that the namespace prefixes of two elements may be different as long as they belong to the same namespace.

Attributes

Attributes are equal if they belong to the same namespace, have the same name, and the same value.

Namespace Declarations

Namespace declarations (attributes named xmlns:prefix) are ignored. There are several reasons for this:

  • As long as two elements or attributes belong to the same namespace, it does not matter what prefixes are used. XML processors may also change prefixes in unpredictable ways without this being an error.

  • XML processors may move namespace declarations from one element to another (usually an ancestor, sometimes a descendant) without this being an error, or under control by the programmer.

  • XML processors may add extraneous namespace declarations in a manner that is hard for programmers to control.

Processing Instructions

Processing instructions are considered equal if the string values of their targets and contents are equal.

Text

Text nodes are equal if their values are equal. However, empty text nodes, and text nodes containing only whitespace are ignored.

CDATA

CDATA nodes are equal if their text content is equal. Whitespace is not normalized.

Comments

Comments are equal if they have the same content.

The expected_doc and actual_doc arguments to this method may be of the following types:

  • A REXML node, usually a REXML::Document or REXML::Element

  • A File or other IO object representing an XML document

  • A string containing an XML document



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/test/unit/xml/xml_assertions.rb', line 96

def assert_xml_equal(expected_doc, actual_doc, message = nil)
  expected_doc = parse_xml(expected_doc)
  actual_doc = parse_xml(actual_doc)
  _wrap_assertion do
    full_message = build_message(message, <<EOT, actual_doc.inspect, expected_doc.inspect)

<?> expected to be equal to
<?> but was not.
EOT
    assert_block(full_message){are_equal?(expected_doc, actual_doc)}
  end
end

#assert_xml_not_equal(expected_doc, actual_doc, message = nil) ⇒ Object

This method compares two XML documents and returns true if they are not equal, false otherwise. This is the inverse of assert_xml_equal.



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/test/unit/xml/xml_assertions.rb', line 112

def assert_xml_not_equal(expected_doc, actual_doc, message = nil)
  expected_doc = parse_xml(expected_doc)
  actual_doc = parse_xml(actual_doc)
  _wrap_assertion do
    full_message = build_message(message, <<EOT, actual_doc.inspect, expected_doc.inspect)

<?> expected not to be equal to
<?> but was equal.
EOT
    assert_block(full_message){ ! are_equal?(expected_doc, actual_doc)}
  end
end