forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_tag_spec.rb
205 lines (172 loc) · 5.85 KB
/
content_tag_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ContentTag, :config do
context 'Rails 5.0', :rails50 do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
content_tag(:p, 'Hello world!')
RUBY
end
it 'does not register an offense with empty tag' do
expect_no_offenses(<<~RUBY)
content_tag(:br)
RUBY
end
it 'does not register an offense with array of classnames' do
expect_no_offenses(<<~RUBY)
content_tag(:div, "Hello world!", class: ["strong", "highlight"])
RUBY
end
it 'does not register an offense with nested content_tag' do
expect_no_offenses(<<~RUBY)
content_tag(:div) { content_tag(:strong, 'Hi') }
content_tag(:div, content_tag(:span, 'foo'))
RUBY
end
end
context 'Rails 5.1', :rails51 do
it 'corrects an offence' do
expect_offense(<<~RUBY)
content_tag(:p, 'Hello world!')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag.p('Hello world!')
RUBY
end
it 'corrects an offence with empty tag' do
expect_offense(<<~RUBY)
content_tag(:br)
^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag.br()
RUBY
end
it 'corrects an offence with array of classnames' do
expect_offense(<<~RUBY)
content_tag(:div, "Hello world!", class: ["strong", "highlight"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag.div("Hello world!", class: ["strong", "highlight"])
RUBY
end
it 'corrects an offence with nested content_tag' do
expect_offense(<<~RUBY)
content_tag(:div, content_tag(:span, 'foo'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag.div(tag.span('foo'))
RUBY
end
it 'corrects an offence with nested content_tag with block' do
expect_offense(<<~RUBY)
content_tag(:div) { content_tag(:strong, 'Hi') }
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag.div() { tag.strong('Hi') }
RUBY
end
it 'corrects an offence when first argument is hash' do
expect_offense(<<~RUBY)
content_tag({foo: 1})
^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag({foo: 1})
RUBY
end
it 'corrects an offence when first argument is non-identifier string' do
expect_offense(<<~RUBY)
content_tag('foo-bar', 'baz', class: 'strong')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag.foo_bar('baz', class: 'strong')
RUBY
end
it 'corrects an offense when called with options hash and block' do
expect_offense(<<~RUBY)
content_tag :div, { class: 'strong' } do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
'body'
end
RUBY
expect_correction(<<~RUBY)
tag.div({ class: 'strong' }) do
'body'
end
RUBY
end
it 'does not register an offence when `tag` is used with an argument' do
expect_no_offenses(<<~RUBY)
tag.p('Hello world!')
RUBY
end
it 'does not register an offence when `tag` is used without arguments' do
expect_no_offenses(<<~RUBY)
tag.br
RUBY
end
it 'does not register an offence when `tag` is used with arguments' do
expect_no_offenses(<<~RUBY)
tag.div("Hello world!", class: ["strong", "highlight"])
RUBY
end
it 'does not register an offence when `tag` is nested' do
expect_no_offenses(<<~RUBY)
tag.div() { tag.strong('Hi') }
RUBY
end
it 'does not register an offense when `content_tag` is called with no arguments' do
expect_no_offenses(<<~RUBY)
content_tag
RUBY
end
context 'when the first argument is a variable' do
it 'does not register an offence when the first argument is a lvar' do
expect_no_offenses(<<~RUBY)
name = do_something
content_tag(name, "Hello world!", class: ["strong", "highlight"])
RUBY
end
it 'does not register an offence when the first argument is an ivar' do
expect_no_offenses(<<~RUBY)
content_tag(@name, "Hello world!", class: ["strong", "highlight"])
RUBY
end
it 'does not register an offence when the first argument is a cvar' do
expect_no_offenses(<<~RUBY)
content_tag(@@name, "Hello world!", class: ["strong", "highlight"])
RUBY
end
it 'does not register an offence when the first argument is a gvar' do
expect_no_offenses(<<~RUBY)
content_tag($name, "Hello world!", class: ["strong", "highlight"])
RUBY
end
it 'does not register an offence when the first argument is a splat argument' do
expect_no_offenses(<<~RUBY)
content_tag(*args, &block)
RUBY
end
end
context 'when the first argument is a method' do
it 'does not register an offence' do
expect_no_offenses(<<~RUBY)
content_tag(name, "Hello world!", class: ["strong", "highlight"])
RUBY
end
end
context 'when the first argument is a constant' do
it 'does not register an offence' do
expect_no_offenses(<<~RUBY)
content_tag(CONST, "Hello world!", class: ["strong", "highlight"])
RUBY
end
end
end
end