require 'test_helper'
class TestNode < Test::Unit::TestCase
def test_elements
assert doc.elements.size == 1
assert doc.at('root').elements.size == 2
end
def test_leaf?
assert root("").leaf?
assert root("Yo").leaf?
assert root("").leaf?
assert !root("
").leaf?
end
def test_match_of_elements_without_comparing_values
subject = root(<<-XML)
1
3
XML
pattern = root(<<-XML)
2
XML
assert subject.match?(pattern)
end
def test_no_match_of_elements_without_comparing_values
subject = root(<<-XML)
1
XML
pattern = root(<<-XML)
5
XML
assert !subject.match?(pattern)
end
def test_match_with_values
subject = root(<<-XML)
1
3
XML
pattern = root(<<-XML)
3
1
XML
assert subject.match?(pattern, true)
end
def test_no_match_with_values
subject = root(<<-XML)
1
XML
not_matched_pattern = root(<<-XML)
2
XML
assert !subject.match?(not_matched_pattern, true)
end
def test_element_contains_another_element
element = create_element('')
assert !doc.send(:contains?,element)
assert doc.at('root').send(:contains?,element)
end
def test_element_contains_elements
assert doc.root.send(:contains_elements_of?, doc.root)
assert !doc.root.send(:contains_elements_of?, doc(''))
end
def test_comparable_attributes
assert_equal [["id", "boss"], ["name", "Dude"]], create_element(%{}).comparable_attributes
assert_equal [["name", "Dude"]], create_element(%{}).comparable_attributes
end
private
def create_element(xml)
Nokogiri::XML::Document.parse(xml).root
end
def root(xml)
doc(xml).root
end
def doc(xml = nil)
xml ||= <<-XML
1
2
XML
Nokogiri::XML::Document.parse(xml)
end
end