Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make assert_dom_equal ignore insignificant whitespace #71

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion lib/rails/dom/testing/assertions/dom_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Dom
module Testing
module Assertions
module DomAssertions
NODE_PADDING = ' '.freeze

# \Test two HTML strings for equivalency (e.g., equal even when attributes are in another order)
#
# # assert that the referenced method generates the appropriate HTML string
Expand Down Expand Up @@ -67,7 +69,55 @@ def equal_attribute?(attr, other_attr)
private

def fragment(text)
Nokogiri::HTML::DocumentFragment.parse(text)
Nokogiri::HTML::DocumentFragment.parse(text).tap do |fragment|
pad_tags(fragment.children)
condense_whitespace(fragment.children)
end
end

def pad_tags(node)
return node.each { |n| pad_tags(n) } if node.is_a?(Nokogiri::XML::NodeSet)
return unless node.element?

if (previous = node.previous_sibling) && previous.text?
previous.content += NODE_PADDING
else
node.add_previous_sibling(NODE_PADDING)
end

if (first = node.children.first) && first.text?
first.content = NODE_PADDING + first.content
else
node.prepend_child(NODE_PADDING)
end

if (last = node.children.last) && last.text?
last.content = NODE_PADDING + last.content
else
node.add_child(NODE_PADDING)
end

if (after = node.next_sibling) && after.text?
after.content += NODE_PADDING
else
node.add_next_sibling(NODE_PADDING)
end

pad_tags(node.children)
end

def condense_whitespace(node)
if node.is_a?(Nokogiri::XML::NodeSet)
node.each { |element| condense_whitespace(element) }
elsif node.element?
condense_whitespace(node.children)
elsif node.text?
text = node.text
text.gsub!(/\s+/, ' ')
text.strip!

text.empty? ? node.remove : node.content = text
end
end
end
end
Expand Down
48 changes: 48 additions & 0 deletions test/dom_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,54 @@ def test_equal_doms_with_different_order_attributes
assert_dom_equal(attributes, reverse_attributes)
end

def test_dom_equal_up_to_whitespace
canonical = %{<a><b>hello</b>world</a>}
assert_dom_equal(canonical, %{<a>\n<b>hello </b>\nworld</a>})
assert_dom_equal(canonical, %{<a> \n <b> hello </b>world</a>})
assert_dom_equal(canonical, %{<a> \n <b>hello </b>world\n</a>\n})
assert_dom_equal(canonical, %{<a>\n\t<b>hello </b>\n\tworld</a>})
end

def test_dom_equal_with_indentation
canonical = %{<a>hello <b>cruel</b> world</a>}
assert_dom_equal(canonical, <<-HTML)
<a>
hello
<b>cruel</b>
world
</a>
HTML

assert_dom_equal(canonical, <<-HTML)
<a>
hello
<b>cruel</b>
world
</a>
HTML

assert_dom_equal(canonical, <<-HTML)
<a>hello
<b>
cruel
</b>
world</a>
HTML
end

def test_dom_not_equal_with_interior_whitespace
with_space = %{<a><b>hello world</b></a>}
without_space = %{<a><b>helloworld</b></a>}
assert_dom_not_equal(with_space, without_space)
end

def test_explicit_processing_of_text_whitespace
valid_cdata = %{<script>//<![CDATA[\n1+1\n//]]></script>}
incorrect = %{<script>//<![CDATA[ 1+1 //]]></script>}

assert_dom_not_equal(incorrect, valid_cdata)
end

def test_dom_not_equal
assert_dom_not_equal('<a></a>', '<b></b>')
end
Expand Down