Skip to content

make assert_dom_equal ignore insignificant whitespace #83

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

Closed
wants to merge 1 commit 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
13 changes: 8 additions & 5 deletions lib/rails/dom/testing/assertions/dom_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module Dom
module Testing
module Assertions
module DomAssertions
WHITESPACE_REGEXP = /[\s]*\n[\s]*/

# \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
# assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")
def assert_dom_equal(expected, actual, message = nil)
expected_dom, actual_dom = fragment(expected), fragment(actual)
def assert_dom_equal(expected, actual, message = nil, strict: false)
expected_dom, actual_dom = fragment(expected, strict), fragment(actual, strict)
message ||= "Expected: #{expected}\nActual: #{actual}"
assert compare_doms(expected_dom, actual_dom), message
end
Expand All @@ -17,8 +19,8 @@ def assert_dom_equal(expected, actual, message = nil)
#
# # assert that the referenced method does not generate the specified HTML string
# assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
def assert_dom_not_equal(expected, actual, message = nil)
expected_dom, actual_dom = fragment(expected), fragment(actual)
def assert_dom_not_equal(expected, actual, message = nil, strict: false)
expected_dom, actual_dom = fragment(expected, strict), fragment(actual, strict)
message ||= "Expected: #{expected}\nActual: #{actual}"
assert_not compare_doms(expected_dom, actual_dom), message
end
Expand Down Expand Up @@ -66,7 +68,8 @@ def equal_attribute?(attr, other_attr)

private

def fragment(text)
def fragment(text, strict)
text = text.gsub(WHITESPACE_REGEXP, '') unless strict
Nokogiri::HTML::DocumentFragment.parse(text)
end
end
Expand Down
50 changes: 49 additions & 1 deletion test/dom_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,52 @@ def test_unequal_dom_attributes_in_children
%{<a><b c="2" /></a>}
)
end
end

def test_dom_equal_with_whitespace_strict
canonical = %{<a><b>hello</b> world</a>}
assert_dom_not_equal(canonical, %{<a>\n<b>hello\n </b> world</a>}, strict: true)
assert_dom_not_equal(canonical, %{<a> \n <b>\n hello</b> world</a>}, strict: true)
assert_dom_not_equal(canonical, %{<a>\n\t<b>hello</b> world</a>}, strict: true)
assert_dom_equal(canonical, %{<a><b>hello</b> world</a>}, strict: true)
end

def test_dom_equal_with_whitespace
canonical = %{<a><b>hello</b> world</a>}
assert_dom_equal(canonical, %{<a>\n<b>hello\n </b> world</a>})
assert_dom_equal(canonical, %{<a> \n <b>\n hello</b> world</a>})
assert_dom_equal(canonical, %{<a>\n\t<b>hello</b> world</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
end