@@ -7,43 +7,61 @@ module DomAssertions
7
7
#
8
8
# # assert that the referenced method generates the appropriate HTML string
9
9
# 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 )
11
11
expected_dom , actual_dom = fragment ( expected ) , fragment ( actual )
12
12
message ||= "Expected: #{ expected } \n Actual: #{ actual } "
13
- assert compare_doms ( expected_dom , actual_dom ) , message
13
+ assert compare_doms ( expected_dom , actual_dom , strict ) , message
14
14
end
15
15
16
16
# The negated form of +assert_dom_equal+.
17
17
#
18
18
# # assert that the referenced method does not generate the specified HTML string
19
19
# 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 )
21
21
expected_dom , actual_dom = fragment ( expected ) , fragment ( actual )
22
22
message ||= "Expected: #{ expected } \n Actual: #{ actual } "
23
- assert_not compare_doms ( expected_dom , actual_dom ) , message
23
+ assert_not compare_doms ( expected_dom , actual_dom , strict ) , message
24
24
end
25
25
26
26
protected
27
27
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
30
32
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 )
33
35
end
34
36
35
37
true
36
38
end
37
39
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 )
39
49
return false unless child . type == other_child . type
40
50
41
51
if child . element?
42
52
child . name == other_child . name &&
43
53
equal_attribute_nodes? ( child . attribute_nodes , other_child . attribute_nodes ) &&
44
- compare_doms ( child , other_child )
54
+ compare_doms ( child , other_child , strict )
45
55
else
56
+ equal_child? ( child , other_child , strict )
57
+ end
58
+ end
59
+
60
+ def equal_child? ( child , other_child , strict )
61
+ if strict
46
62
child . to_s == other_child . to_s
63
+ else
64
+ child . to_s . split == other_child . to_s . split
47
65
end
48
66
end
49
67
0 commit comments