-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
hook_argument_spec.rb
171 lines (149 loc) · 5.25 KB
/
hook_argument_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::HookArgument, :config do
let(:cop_config) do
{ 'EnforcedStyle' => enforced_style }
end
shared_examples 'ignored hooks' do
it 'ignores :context and :suite' do
expect_no_offenses(<<-RUBY)
before(:suite) { true }
after(:suite) { true }
before(:context) { true }
after(:context) { true }
RUBY
end
it 'ignores hooks with more than one argument' do
expect_no_offenses(<<-RUBY)
before(:each, :something_custom) { true }
RUBY
end
it 'ignores non-rspec hooks' do
expect_no_offenses(<<-RUBY)
setup(:each) { true }
RUBY
end
end
shared_examples 'hook autocorrect' do |output|
include_examples 'autocorrect', 'before(:each) { }', output
include_examples 'autocorrect', 'before(:example) { }', output
include_examples 'autocorrect', 'before { }', output
include_examples 'autocorrect', 'config.before(:each) { }',
'config.' + output
include_examples 'autocorrect', 'config.before(:example) { }',
'config.' + output
include_examples 'autocorrect', 'config.before { }',
'config.' + output
end
shared_examples 'an example hook' do
include_examples 'ignored hooks'
include_examples 'detects style', 'before(:each) { foo }', 'each'
include_examples 'detects style', 'before(:example) { foo }', 'example'
include_examples 'detects style', 'before { foo }', 'implicit'
end
context 'when EnforcedStyle is :implicit' do
let(:enforced_style) { :implicit }
it 'detects :each for hooks' do
expect_offense(<<-RUBY)
before(:each) { true }
^^^^^^^^^^^^^ Omit the default `:each` argument for RSpec hooks.
after(:each) { true }
^^^^^^^^^^^^ Omit the default `:each` argument for RSpec hooks.
around(:each) { true }
^^^^^^^^^^^^^ Omit the default `:each` argument for RSpec hooks.
config.after(:each) { true }
^^^^^^^^^^^^^^^^^^^ Omit the default `:each` argument for RSpec hooks.
RUBY
end
it 'detects :example for hooks' do
expect_offense(<<-RUBY)
before(:example) { true }
^^^^^^^^^^^^^^^^ Omit the default `:example` argument for RSpec hooks.
after(:example) { true }
^^^^^^^^^^^^^^^ Omit the default `:example` argument for RSpec hooks.
around(:example) { true }
^^^^^^^^^^^^^^^^ Omit the default `:example` argument for RSpec hooks.
config.before(:example) { true }
^^^^^^^^^^^^^^^^^^^^^^^ Omit the default `:example` argument for RSpec hooks.
RUBY
end
it 'does not flag hooks without default scopes' do
expect_no_offenses(<<-RUBY)
before { true }
after { true }
config.before { true }
RUBY
end
include_examples 'an example hook'
include_examples 'hook autocorrect', 'before { }'
end
context 'when EnforcedStyle is :each' do
let(:enforced_style) { :each }
it 'detects :each for hooks' do
expect_no_offenses(<<-RUBY)
before(:each) { true }
after(:each) { true }
around(:each) { true }
config.before(:each) { true }
RUBY
end
it 'detects :example for hooks' do
expect_offense(<<-RUBY)
before(:example) { true }
^^^^^^^^^^^^^^^^ Use `:each` for RSpec hooks.
after(:example) { true }
^^^^^^^^^^^^^^^ Use `:each` for RSpec hooks.
around(:example) { true }
^^^^^^^^^^^^^^^^ Use `:each` for RSpec hooks.
config.before(:example) { true }
^^^^^^^^^^^^^^^^^^^^^^^ Use `:each` for RSpec hooks.
RUBY
end
it 'detects hooks without default scopes' do
expect_offense(<<-RUBY)
before { true }
^^^^^^ Use `:each` for RSpec hooks.
after { true }
^^^^^ Use `:each` for RSpec hooks.
config.before { true }
^^^^^^ Use `:each` for RSpec hooks.
RUBY
end
include_examples 'an example hook'
include_examples 'hook autocorrect', 'before(:each) { }'
end
context 'when EnforcedStyle is :example' do
let(:enforced_style) { :example }
it 'detects :example for hooks' do
expect_no_offenses(<<-RUBY)
before(:example) { true }
after(:example) { true }
around(:example) { true }
config.before(:example) { true }
RUBY
end
it 'detects :each for hooks' do
expect_offense(<<-RUBY)
before(:each) { true }
^^^^^^^^^^^^^ Use `:example` for RSpec hooks.
after(:each) { true }
^^^^^^^^^^^^ Use `:example` for RSpec hooks.
around(:each) { true }
^^^^^^^^^^^^^ Use `:example` for RSpec hooks.
config.before(:each) { true }
^^^^^^^^^^^^^^^^^^^^ Use `:example` for RSpec hooks.
RUBY
end
it 'does not flag hooks without default scopes' do
expect_offense(<<-RUBY)
before { true }
^^^^^^ Use `:example` for RSpec hooks.
after { true }
^^^^^ Use `:example` for RSpec hooks.
config.before { true }
^^^^^^ Use `:example` for RSpec hooks.
RUBY
end
include_examples 'an example hook'
include_examples 'hook autocorrect', 'before(:example) { }'
end
end