Skip to content

Commit 47506c4

Browse files
authored
Merge pull request #84 from jduff/ignore_whitespace_2
Make assert_dom_equal ignore insignificant whitespace when walking the node tree
2 parents 6c9ed39 + 8b074b9 commit 47506c4

File tree

2 files changed

+104
-11
lines changed

2 files changed

+104
-11
lines changed

lib/rails/dom/testing/assertions/dom_assertions.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,61 @@ module DomAssertions
77
#
88
# # assert that the referenced method generates the appropriate HTML string
99
# assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")
10-
def assert_dom_equal(expected, actual, message = nil)
10+
def assert_dom_equal(expected, actual, message = nil, strict: false)
1111
expected_dom, actual_dom = fragment(expected), fragment(actual)
1212
message ||= "Expected: #{expected}\nActual: #{actual}"
13-
assert compare_doms(expected_dom, actual_dom), message
13+
assert compare_doms(expected_dom, actual_dom, strict), message
1414
end
1515

1616
# The negated form of +assert_dom_equal+.
1717
#
1818
# # assert that the referenced method does not generate the specified HTML string
1919
# assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
20-
def assert_dom_not_equal(expected, actual, message = nil)
20+
def assert_dom_not_equal(expected, actual, message = nil, strict: false)
2121
expected_dom, actual_dom = fragment(expected), fragment(actual)
2222
message ||= "Expected: #{expected}\nActual: #{actual}"
23-
assert_not compare_doms(expected_dom, actual_dom), message
23+
assert_not compare_doms(expected_dom, actual_dom, strict), message
2424
end
2525

2626
protected
2727

28-
def compare_doms(expected, actual)
29-
return false unless expected.children.size == actual.children.size
28+
def compare_doms(expected, actual, strict)
29+
expected_children = extract_children(expected, strict)
30+
actual_children = extract_children(actual, strict)
31+
return false unless expected_children.size == actual_children.size
3032

31-
expected.children.each_with_index do |child, i|
32-
return false unless equal_children?(child, actual.children[i])
33+
expected_children.each_with_index do |child, i|
34+
return false unless equal_children?(child, actual_children[i], strict)
3335
end
3436

3537
true
3638
end
3739

38-
def equal_children?(child, other_child)
40+
def extract_children(node, strict)
41+
if strict
42+
node.children
43+
else
44+
node.children.reject{|n| n.text? && n.text.blank?}
45+
end
46+
end
47+
48+
def equal_children?(child, other_child, strict)
3949
return false unless child.type == other_child.type
4050

4151
if child.element?
4252
child.name == other_child.name &&
4353
equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes) &&
44-
compare_doms(child, other_child)
54+
compare_doms(child, other_child, strict)
4555
else
56+
equal_child?(child, other_child, strict)
57+
end
58+
end
59+
60+
def equal_child?(child, other_child, strict)
61+
if strict
4662
child.to_s == other_child.to_s
63+
else
64+
child.to_s.split == other_child.to_s.split
4765
end
4866
end
4967

test/dom_assertions_test.rb

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,79 @@ def test_unequal_dom_attributes_in_children
4747
%{<a><b c="2" /></a>}
4848
)
4949
end
50-
end
50+
51+
def test_dom_equal_with_whitespace_strict
52+
canonical = %{<a><b>hello</b> world</a>}
53+
assert_dom_not_equal(canonical, %{<a>\n<b>hello\n </b> world</a>}, strict: true)
54+
assert_dom_not_equal(canonical, %{<a> \n <b>\n hello</b> world</a>}, strict: true)
55+
assert_dom_not_equal(canonical, %{<a>\n\t<b>hello</b> world</a>}, strict: true)
56+
assert_dom_equal(canonical, %{<a><b>hello</b> world</a>}, strict: true)
57+
end
58+
59+
def test_dom_equal_with_whitespace
60+
canonical = %{<a><b>hello</b> world</a>}
61+
assert_dom_equal(canonical, %{<a>\n<b>hello\n </b> world</a>})
62+
assert_dom_equal(canonical, %{<a>\n<b>hello </b>\nworld</a>})
63+
assert_dom_equal(canonical, %{<a> \n <b>\n hello</b> world</a>})
64+
assert_dom_equal(canonical, %{<a> \n <b> hello </b>world</a>})
65+
assert_dom_equal(canonical, %{<a> \n <b>hello </b>world\n</a>\n})
66+
assert_dom_equal(canonical, %{<a>\n\t<b>hello</b> world</a>})
67+
assert_dom_equal(canonical, %{<a>\n\t<b>hello </b>\n\tworld</a>})
68+
end
69+
70+
def test_dom_equal_with_attribute_whitespace
71+
canonical = %(<div data-wow="Don't strip this">)
72+
assert_dom_equal(canonical, %(<div data-wow="Don't strip this">))
73+
assert_dom_not_equal(canonical, %(<div data-wow="Don't strip this">))
74+
end
75+
76+
def test_dom_equal_with_indentation
77+
canonical = %{<a>hello <b>cruel</b> world</a>}
78+
assert_dom_equal(canonical, <<-HTML)
79+
<a>
80+
hello
81+
<b>cruel</b>
82+
world
83+
</a>
84+
HTML
85+
86+
assert_dom_equal(canonical, <<-HTML)
87+
<a>
88+
hello
89+
<b>cruel</b>
90+
world
91+
</a>
92+
HTML
93+
94+
assert_dom_equal(canonical, <<-HTML)
95+
<a>hello
96+
<b>
97+
cruel
98+
</b>
99+
world</a>
100+
HTML
101+
end
102+
103+
def test_dom_equal_with_surrounding_whitespace
104+
canonical = %{<p>Lorem ipsum dolor</p><p>sit amet, consectetur adipiscing elit</p>}
105+
assert_dom_equal(canonical, <<-HTML)
106+
<p>
107+
Lorem
108+
ipsum
109+
dolor
110+
</p>
111+
112+
<p>
113+
sit amet,
114+
consectetur
115+
adipiscing elit
116+
</p>
117+
HTML
118+
end
119+
120+
def test_dom_not_equal_with_interior_whitespace
121+
with_space = %{<a><b>hello world</b></a>}
122+
without_space = %{<a><b>helloworld</b></a>}
123+
assert_dom_not_equal(with_space, without_space)
124+
end
125+
end

0 commit comments

Comments
 (0)