Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed bug when attributes in a branch element were not compared

  • Loading branch information...
commit cc20cdf6b73990444a19ade5a017ae3fc7507752 1 parent 960cb9b
@alovak authored
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- test_xml (0.0.4)
+ test_xml (0.1.1)
nokogiri (>= 1.3.2)
GEM
View
2  lib/test_xml/matcher_methods.rb
@@ -23,7 +23,7 @@ def self.xml_structure_equal(subject, pattern)
private
def self.parse_xml(subject, pattern)
- [Nokogiri::XML.parse(subject), Nokogiri::XML.parse(pattern)]
+ [Nokogiri::XML.parse(subject).root, Nokogiri::XML.parse(pattern).root]
end
end
end
View
12 lib/test_xml/nokogiri/node.rb
@@ -5,8 +5,16 @@ def match?(element, compare_value = false)
if compare_value && element.leaf?
comparable_attributes == element.comparable_attributes and equal_text?(element)
else
- contains_elements_of?(element) &&
- element.elements.all? {|el| matches_at_least_one?(el, compare_value) }
+
+ #TODO clean this part of the code
+ if compare_value
+ (comparable_attributes == element.comparable_attributes) &&
+ contains_elements_of?(element) &&
+ element.elements.all? {|el| matches_at_least_one?(el, compare_value) }
+ else
+ contains_elements_of?(element) &&
+ element.elements.all? {|el| matches_at_least_one?(el, compare_value) }
+ end
end
end
View
33 spec/matchers/equal_xml_spec.rb
@@ -30,6 +30,39 @@
end
end
+ context "when subject has elements with attributes" do
+ subject {
+ <<-XML
+ <xml test="true">
+ <one type="text" attr="hello">1</one>
+ <two type="integer">2</two>
+ </xml>
+ XML
+ }
+
+ context "and xml has similar elements with attributes" do
+ it "should pass" do
+ should equal_xml(<<-XML)
+ <xml test="true">
+ <one type="text" attr="hello">1</one>
+ <two type="integer">2</two>
+ </xml>
+ XML
+ end
+ end
+
+ context "and xml has elements with different attributes" do
+ it "should fail" do
+ should_not equal_xml(<<-XML)
+ <xml>
+ <one type="text" attr="hello">1</one>
+ <two type="integer">2</two>
+ </xml>
+ XML
+ end
+ end
+ end
+
context "when xml is equal with subject" do
it "should pass" do
should equal_xml(<<-XML)
View
28 test/nokogiri/test_node.rb
@@ -7,15 +7,15 @@ def test_elements
end
def test_leaf?
- assert doc("<a/>").root.leaf?
- assert doc("<a>Yo</a>").root.leaf?
- assert doc("<a></a>").root.leaf?
- assert !doc("<div><a/></div>").root.leaf?
+ assert root("<a/>").leaf?
+ assert root("<a>Yo</a>").leaf?
+ assert root("<a></a>").leaf?
+ assert !root("<div><a/></div>").leaf?
end
def test_match_of_elements_without_comparing_values
- subject = doc(<<-XML)
+ subject = root(<<-XML)
<root>
<one>1</one>
<two>
@@ -24,7 +24,7 @@ def test_match_of_elements_without_comparing_values
</root>
XML
- pattern = doc(<<-XML)
+ pattern = root(<<-XML)
<root>
<two><three/></two>
<one>2</one>
@@ -35,14 +35,14 @@ def test_match_of_elements_without_comparing_values
end
def test_no_match_of_elements_without_comparing_values
- subject = doc(<<-XML)
+ subject = root(<<-XML)
<root>
<one>1</one>
<two/>
</root>
XML
- pattern = doc(<<-XML)
+ pattern = root(<<-XML)
<root>
<four/>
<five>5</five>
@@ -53,14 +53,14 @@ def test_no_match_of_elements_without_comparing_values
end
def test_match_with_values
- subject = doc(<<-XML)
+ subject = root(<<-XML)
<root>
<one>1</one>
<two><three>3</three></two>
</root>
XML
- pattern = doc(<<-XML)
+ pattern = root(<<-XML)
<root>
<two><three>3</three></two>
<one>1</one>
@@ -71,13 +71,13 @@ def test_match_with_values
end
def test_no_match_with_values
- subject = doc(<<-XML)
+ subject = root(<<-XML)
<root>
<one>1</one>
</root>
XML
- not_matched_pattern = doc(<<-XML)
+ not_matched_pattern = root(<<-XML)
<root>
<one>2</one>
</root>
@@ -111,6 +111,10 @@ def create_element(xml)
Nokogiri::XML::Document.parse(xml).root
end
+ def root(xml)
+ doc(xml).root
+ end
+
def doc(xml = nil)
xml ||= <<-XML
<root>
View
37 test/test_unit/test_assertions.rb
@@ -130,6 +130,24 @@ def test_assert_xml_equal_with_attributes
assert_xml_equal expected, actual
end
+
+ def test_assert_xml_equal_with_attributes_in_branch_element
+ expected = <<-XML
+ <root type="test">
+ <one>1</one>
+ <two/>
+ </root>
+ XML
+
+ actual = <<-XML
+ <root type="test">
+ <one>1</one>
+ <two/>
+ </root>
+ XML
+
+ assert_xml_equal expected, actual
+ end
def test_assert_not_xml_equal_with_attributes
expected = <<-XML
@@ -146,6 +164,7 @@ def test_assert_not_xml_equal_with_attributes
assert_not_xml_equal expected, actual
end
+
def test_assert_not_xml_equal_with_attributes_and_no_text
expected = <<-XML
@@ -163,6 +182,24 @@ def test_assert_not_xml_equal_with_attributes_and_no_text
assert_not_xml_equal expected, actual
end
+ def test_assert_not_xml_equal_with_attributes_in_branch_element
+ expected = <<-XML
+ <root type="test">
+ <one>1</one>
+ <two/>
+ </root>
+ XML
+
+ actual = <<-XML
+ <root>
+ <one>1</one>
+ <two/>
+ </root>
+ XML
+
+ assert_not_xml_equal expected, actual
+ end
+
def test_assert_not_xml_equal
expected = <<-XML
<root>
Please sign in to comment.
Something went wrong with that request. Please try again.