Skip to content

Commit b12850b

Browse files
author
Rafael Mendonça França
committed
Deprecate assert_tag and assert_no_tag
1 parent 23379ee commit b12850b

File tree

2 files changed

+143
-94
lines changed

2 files changed

+143
-94
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'active_support/deprecation'
12
require 'rails/deprecated_sanitizer/html-scanner'
23

34
module Rails
@@ -96,8 +97,10 @@ module TagAssertions
9697
# that allow optional closing tags (p, li, td). <em>You must explicitly
9798
# close all of your tags to use these assertions.</em>
9899
def assert_tag(*opts)
100+
ActiveSupport::Deprecation.warn("assert_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.")
101+
99102
opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first
100-
tag = find_tag(opts)
103+
tag = _find_tag(opts)
101104

102105
assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
103106
end
@@ -116,21 +119,31 @@ def assert_tag(*opts)
116119
# assert_no_tag tag: "p",
117120
# children: { count: 1..3, only: { tag: "img" } }
118121
def assert_no_tag(*opts)
122+
ActiveSupport::Deprecation.warn("assert_no_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.")
123+
119124
opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first
120-
tag = find_tag(opts)
125+
tag = _find_tag(opts)
121126

122127
assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
123128
end
124129

125130
def find_tag(conditions)
126-
html_scanner_document.find(conditions)
131+
ActiveSupport::Deprecation.warn("find_tag is deprecated and will be removed at Rails 5 without replacement.")
132+
133+
_find_tag(conditions)
127134
end
128135

129136
def find_all_tag(conditions)
137+
ActiveSupport::Deprecation.warn("find_all_tag is deprecated and will be removed at Rails 5 without replacement. Use assert_select to get the same feature.")
138+
130139
html_scanner_document.find_all(conditions)
131140
end
132141

133142
private
143+
def _find_tag(conditions)
144+
html_scanner_document.find(conditions)
145+
end
146+
134147
def html_scanner_document
135148
xml = @response.content_type =~ /xml$/
136149
@html_document ||= HTML::Document.new(@response.body, false, xml)

test/tag_assertions_test.rb

Lines changed: 127 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -36,147 +36,183 @@ def initialize(content_type, body)
3636
end
3737

3838
def test_assert_tag_tag
39-
# there is a 'form' tag
40-
assert_tag tag: 'form'
41-
# there is not an 'hr' tag
42-
assert_no_tag tag: 'hr'
39+
assert_deprecated do
40+
# there is a 'form' tag
41+
assert_tag tag: 'form'
42+
# there is not an 'hr' tag
43+
assert_no_tag tag: 'hr'
44+
end
4345
end
4446

4547
def test_assert_tag_attributes
46-
# there is a tag with an 'id' of 'bar'
47-
assert_tag attributes: { id: "bar" }
48-
# there is no tag with a 'name' of 'baz'
49-
assert_no_tag attributes: { name: "baz" }
48+
assert_deprecated do
49+
# there is a tag with an 'id' of 'bar'
50+
assert_tag attributes: { id: "bar" }
51+
# there is no tag with a 'name' of 'baz'
52+
assert_no_tag attributes: { name: "baz" }
53+
end
5054
end
5155

5256
def test_assert_tag_parent
53-
# there is a tag with a parent 'form' tag
54-
assert_tag parent: { tag: "form" }
55-
# there is no tag with a parent of 'input'
56-
assert_no_tag parent: { tag: "input" }
57+
assert_deprecated do
58+
# there is a tag with a parent 'form' tag
59+
assert_tag parent: { tag: "form" }
60+
# there is no tag with a parent of 'input'
61+
assert_no_tag parent: { tag: "input" }
62+
end
5763
end
5864

5965
def test_assert_tag_child
60-
# there is a tag with a child 'input' tag
61-
assert_tag child: { tag: "input" }
62-
# there is no tag with a child 'strong' tag
63-
assert_no_tag child: { tag: "strong" }
66+
assert_deprecated do
67+
# there is a tag with a child 'input' tag
68+
assert_tag child: { tag: "input" }
69+
# there is no tag with a child 'strong' tag
70+
assert_no_tag child: { tag: "strong" }
71+
end
6472
end
6573

6674
def test_assert_tag_ancestor
67-
# there is a 'li' tag with an ancestor having an id of 'foo'
68-
assert_tag ancestor: { attributes: { id: "foo" } }, tag: "li"
69-
# there is no tag of any kind with an ancestor having an href matching 'foo'
70-
assert_no_tag ancestor: { attributes: { href: /foo/ } }
75+
assert_deprecated do
76+
# there is a 'li' tag with an ancestor having an id of 'foo'
77+
assert_tag ancestor: { attributes: { id: "foo" } }, tag: "li"
78+
# there is no tag of any kind with an ancestor having an href matching 'foo'
79+
assert_no_tag ancestor: { attributes: { href: /foo/ } }
80+
end
7181
end
7282

7383
def test_assert_tag_descendant
74-
# there is a tag with a descendant 'li' tag
75-
assert_tag descendant: { tag: "li" }
76-
# there is no tag with a descendant 'html' tag
77-
assert_no_tag descendant: { tag: "html" }
84+
assert_deprecated do
85+
# there is a tag with a descendant 'li' tag
86+
assert_tag descendant: { tag: "li" }
87+
# there is no tag with a descendant 'html' tag
88+
assert_no_tag descendant: { tag: "html" }
89+
end
7890
end
7991

8092
def test_assert_tag_sibling
81-
# there is a tag with a sibling of class 'item'
82-
assert_tag sibling: { attributes: { class: "item" } }
83-
# there is no tag with a sibling 'ul' tag
84-
assert_no_tag sibling: { tag: "ul" }
93+
assert_deprecated do
94+
# there is a tag with a sibling of class 'item'
95+
assert_tag sibling: { attributes: { class: "item" } }
96+
# there is no tag with a sibling 'ul' tag
97+
assert_no_tag sibling: { tag: "ul" }
98+
end
8599
end
86100

87101
def test_assert_tag_after
88-
# there is a tag following a sibling 'div' tag
89-
assert_tag after: { tag: "div" }
90-
# there is no tag following a sibling tag with id 'bar'
91-
assert_no_tag after: { attributes: { id: "bar" } }
102+
assert_deprecated do
103+
# there is a tag following a sibling 'div' tag
104+
assert_tag after: { tag: "div" }
105+
# there is no tag following a sibling tag with id 'bar'
106+
assert_no_tag after: { attributes: { id: "bar" } }
107+
end
92108
end
93109

94110
def test_assert_tag_before
95-
# there is a tag preceding a tag with id 'bar'
96-
assert_tag before: { attributes: { id: "bar" } }
97-
# there is no tag preceding a 'form' tag
98-
assert_no_tag before: { tag: "form" }
111+
assert_deprecated do
112+
# there is a tag preceding a tag with id 'bar'
113+
assert_tag before: { attributes: { id: "bar" } }
114+
# there is no tag preceding a 'form' tag
115+
assert_no_tag before: { tag: "form" }
116+
end
99117
end
100118

101119
def test_assert_tag_children_count
102-
# there is a tag with 2 children
103-
assert_tag children: { count: 2 }
104-
# in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
105-
assert_tag tag: 'ul', children: { count: 2 }
106-
# there is no tag with 4 children
107-
assert_no_tag children: { count: 4 }
120+
assert_deprecated do
121+
# there is a tag with 2 children
122+
assert_tag children: { count: 2 }
123+
# in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
124+
assert_tag tag: 'ul', children: { count: 2 }
125+
# there is no tag with 4 children
126+
assert_no_tag children: { count: 4 }
127+
end
108128
end
109129

110130
def test_assert_tag_children_less_than
111-
# there is a tag with less than 5 children
112-
assert_tag children: { less_than: 5 }
113-
# there is no 'ul' tag with less than 2 children
114-
assert_no_tag children: { less_than: 2 }, tag: "ul"
131+
assert_deprecated do
132+
# there is a tag with less than 5 children
133+
assert_tag children: { less_than: 5 }
134+
# there is no 'ul' tag with less than 2 children
135+
assert_no_tag children: { less_than: 2 }, tag: "ul"
136+
end
115137
end
116138

117139
def test_assert_tag_children_greater_than
118-
# there is a 'body' tag with more than 1 children
119-
assert_tag children: { greater_than: 1 }, tag: "body"
120-
# there is no tag with more than 10 children
121-
assert_no_tag children: { greater_than: 10 }
140+
assert_deprecated do
141+
# there is a 'body' tag with more than 1 children
142+
assert_tag children: { greater_than: 1 }, tag: "body"
143+
# there is no tag with more than 10 children
144+
assert_no_tag children: { greater_than: 10 }
145+
end
122146
end
123147

124148
def test_assert_tag_children_only
125-
# there is a tag containing only one child with an id of 'foo'
126-
assert_tag children: { count: 1,
127-
only: { attributes: { id: "foo" } } }
128-
# there is no tag containing only one 'li' child
129-
assert_no_tag children: { count: 1, only: { tag: "li" } }
149+
assert_deprecated do
150+
# there is a tag containing only one child with an id of 'foo'
151+
assert_tag children: { count: 1,
152+
only: { attributes: { id: "foo" } } }
153+
# there is no tag containing only one 'li' child
154+
assert_no_tag children: { count: 1, only: { tag: "li" } }
155+
end
130156
end
131157

132158
def test_assert_tag_content
133-
# the output contains the string "Name"
134-
assert_tag content: /Name/
135-
# the output does not contain the string "test"
136-
assert_no_tag content: /test/
159+
assert_deprecated do
160+
# the output contains the string "Name"
161+
assert_tag content: /Name/
162+
# the output does not contain the string "test"
163+
assert_no_tag content: /test/
164+
end
137165
end
138166

139167
def test_assert_tag_multiple
140-
# there is a 'div', id='bar', with an immediate child whose 'action'
141-
# attribute matches the regexp /somewhere/.
142-
assert_tag tag: "div", attributes: { id: "bar" },
143-
child: { attributes: { action: /somewhere/ } }
144-
145-
# there is no 'div', id='foo', with a 'ul' child with more than
146-
# 2 "li" children.
147-
assert_no_tag tag: "div", attributes: { id: "foo" },
148-
child: { tag: "ul",
149-
children: { greater_than: 2, only: { tag: "li" } } }
168+
assert_deprecated do
169+
# there is a 'div', id='bar', with an immediate child whose 'action'
170+
# attribute matches the regexp /somewhere/.
171+
assert_tag tag: "div", attributes: { id: "bar" },
172+
child: { attributes: { action: /somewhere/ } }
173+
174+
# there is no 'div', id='foo', with a 'ul' child with more than
175+
# 2 "li" children.
176+
assert_no_tag tag: "div", attributes: { id: "foo" },
177+
child: { tag: "ul",
178+
children: { greater_than: 2, only: { tag: "li" } } }
179+
end
150180
end
151181

152182
def test_assert_tag_children_without_content
153-
# there is a form tag with an 'input' child which is a self closing tag
154-
assert_tag tag: "form",
155-
children: { count: 1,
156-
only: { tag: "input" } }
157-
158-
# the body tag has an 'a' child which in turn has an 'img' child
159-
assert_tag tag: "body",
160-
children: { count: 1,
161-
only: { tag: "a",
183+
assert_deprecated do
184+
# there is a form tag with an 'input' child which is a self closing tag
185+
assert_tag tag: "form",
186+
children: { count: 1,
187+
only: { tag: "input" } }
188+
189+
# the body tag has an 'a' child which in turn has an 'img' child
190+
assert_tag tag: "body",
162191
children: { count: 1,
163-
only: { tag: "img" } } } }
192+
only: { tag: "a",
193+
children: { count: 1,
194+
only: { tag: "img" } } } }
195+
end
164196
end
165197

166198
def test_assert_tag_attribute_matching
167-
@response.body = '<input type="text" name="my_name">'
168-
assert_tag tag: 'input',
169-
attributes: { name: /my/, type: 'text' }
170-
assert_no_tag tag: 'input',
171-
attributes: { name: 'my', type: 'text' }
172-
assert_no_tag tag: 'input',
173-
attributes: { name: /^my$/, type: 'text' }
199+
assert_deprecated do
200+
@response.body = '<input type="text" name="my_name">'
201+
assert_tag tag: 'input',
202+
attributes: { name: /my/, type: 'text' }
203+
assert_no_tag tag: 'input',
204+
attributes: { name: 'my', type: 'text' }
205+
assert_no_tag tag: 'input',
206+
attributes: { name: /^my$/, type: 'text' }
207+
end
174208
end
175209

176210
def test_assert_tag_content_matching
177-
@response.body = "<p>hello world</p>"
178-
assert_tag tag: "p", content: "hello world"
179-
assert_tag tag: "p", content: /hello/
180-
assert_no_tag tag: "p", content: "hello"
211+
assert_deprecated do
212+
@response.body = "<p>hello world</p>"
213+
assert_tag tag: "p", content: "hello world"
214+
assert_tag tag: "p", content: /hello/
215+
assert_no_tag tag: "p", content: "hello"
216+
end
181217
end
182-
end
218+
end

0 commit comments

Comments
 (0)