diff --git a/lib/rails/dom/testing/assertions/dom_assertions.rb b/lib/rails/dom/testing/assertions/dom_assertions.rb index 7cc6c01..9ed78d3 100644 --- a/lib/rails/dom/testing/assertions/dom_assertions.rb +++ b/lib/rails/dom/testing/assertions/dom_assertions.rb @@ -7,43 +7,61 @@ module DomAssertions # # # 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) + def assert_dom_equal(expected, actual, message = nil, strict: false) expected_dom, actual_dom = fragment(expected), fragment(actual) message ||= "Expected: #{expected}\nActual: #{actual}" - assert compare_doms(expected_dom, actual_dom), message + assert compare_doms(expected_dom, actual_dom, strict), message end # The negated form of +assert_dom_equal+. # # # 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) + def assert_dom_not_equal(expected, actual, message = nil, strict: false) expected_dom, actual_dom = fragment(expected), fragment(actual) message ||= "Expected: #{expected}\nActual: #{actual}" - assert_not compare_doms(expected_dom, actual_dom), message + assert_not compare_doms(expected_dom, actual_dom, strict), message end protected - def compare_doms(expected, actual) - return false unless expected.children.size == actual.children.size + def compare_doms(expected, actual, strict) + expected_children = extract_children(expected, strict) + actual_children = extract_children(actual, strict) + return false unless expected_children.size == actual_children.size - expected.children.each_with_index do |child, i| - return false unless equal_children?(child, actual.children[i]) + expected_children.each_with_index do |child, i| + return false unless equal_children?(child, actual_children[i], strict) end true end - def equal_children?(child, other_child) + def extract_children(node, strict) + if strict + node.children + else + node.children.reject{|n| n.text? && n.text.blank?} + end + end + + def equal_children?(child, other_child, strict) return false unless child.type == other_child.type if child.element? child.name == other_child.name && equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes) && - compare_doms(child, other_child) + compare_doms(child, other_child, strict) else + equal_child?(child, other_child, strict) + end + end + + def equal_child?(child, other_child, strict) + if strict child.to_s == other_child.to_s + else + child.to_s.strip == other_child.to_s.strip end end diff --git a/test/dom_assertions_test.rb b/test/dom_assertions_test.rb index 5b95609..3bb83c6 100644 --- a/test/dom_assertions_test.rb +++ b/test/dom_assertions_test.rb @@ -47,4 +47,62 @@ 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, %{\nhello \nworld}) + assert_dom_equal(canonical, %{ \n \n hello world}) + assert_dom_equal(canonical, %{ \n hello world}) + assert_dom_equal(canonical, %{ \n hello world\n\n}) + assert_dom_equal(canonical, %{\n\thello world}) + assert_dom_equal(canonical, %{\n\thello \n\tworld}) + end + + def test_dom_equal_with_attribute_whitespace + canonical = %(
) + assert_dom_equal(canonical, %(
)) + assert_dom_not_equal(canonical, %(
)) + end + + def test_dom_equal_with_indentation + canonical = %{hello cruel world} + 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