-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
content_tag_spec.rb
118 lines (103 loc) · 3.32 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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ContentTag, :config do
subject(:cop) { described_class.new(config) }
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') }
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(: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')
^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag('foo-bar')
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
end
end