diff --git a/lib/rails/dom/testing/assertions/dom_assertions.rb b/lib/rails/dom/testing/assertions/dom_assertions.rb index 7cc6c01..b3b3e82 100644 --- a/lib/rails/dom/testing/assertions/dom_assertions.rb +++ b/lib/rails/dom/testing/assertions/dom_assertions.rb @@ -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 'Apples', 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 @@ -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 'Apples', 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 @@ -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 diff --git a/test/dom_assertions_test.rb b/test/dom_assertions_test.rb index 5b95609..93a8716 100644 --- a/test/dom_assertions_test.rb +++ b/test/dom_assertions_test.rb @@ -47,4 +47,52 @@ def test_unequal_dom_attributes_in_children %{} ) end -end \ No newline at end of file + + def test_dom_equal_with_whitespace_strict + canonical = %{hello world} + assert_dom_not_equal(canonical, %{\nhello\n world}, strict: true) + assert_dom_not_equal(canonical, %{ \n \n hello world}, strict: true) + assert_dom_not_equal(canonical, %{\n\thello world}, strict: true) + assert_dom_equal(canonical, %{hello world}, strict: true) + end + + def test_dom_equal_with_whitespace + canonical = %{hello world} + assert_dom_equal(canonical, %{\nhello\n world}) + assert_dom_equal(canonical, %{ \n \n hello world}) + assert_dom_equal(canonical, %{\n\thello world}) + end + + def test_dom_equal_with_indentation + canonical = %{hellocruelworld} + assert_dom_equal(canonical, <<-HTML) + + hello + cruel + world + + HTML + + assert_dom_equal(canonical, <<-HTML) + +hello +cruel +world + + HTML + + assert_dom_equal(canonical, <<-HTML) +hello + + cruel + + world + HTML + end + + def test_dom_not_equal_with_interior_whitespace + with_space = %{hello world} + without_space = %{helloworld} + assert_dom_not_equal(with_space, without_space) + end +end