forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
useless_ruby2_keywords_spec.rb
174 lines (152 loc) · 5 KB
/
useless_ruby2_keywords_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::UselessRuby2Keywords, :config do
context 'when `ruby2_keywords` is given a `def` node' do
it 'registers an offense for a method without arguments' do
expect_offense(<<~RUBY)
ruby2_keywords def foo
^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
end
RUBY
end
it 'registers an offense for a method with only positional args' do
expect_offense(<<~RUBY)
ruby2_keywords def foo(arg)
^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
end
RUBY
end
it 'registers an offense for a method with only `kwrestarg`' do
expect_offense(<<~RUBY)
ruby2_keywords def foo(**kwargs)
^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
end
RUBY
end
it 'registers an offense for a method with only keyword args' do
expect_offense(<<~RUBY)
ruby2_keywords def foo(i:, j:)
^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
end
RUBY
end
it 'registers an offense for a method with a `restarg` and keyword args' do
expect_offense(<<~RUBY)
ruby2_keywords def foo(*args, i:, j:)
^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
end
RUBY
end
it 'registers an offense for a method with a `restarg` and `kwoptarg`' do
expect_offense(<<~RUBY)
ruby2_keywords def foo(*args, i: 1)
^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
end
RUBY
end
it 'registers an offense for a method with a `restarg` and `kwrestarg`' do
expect_offense(<<~RUBY)
ruby2_keywords def foo(*args, **kwargs)
^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
end
RUBY
end
it 'does not register an offense for a method with a `restarg` and no `kwrestarg`' do
expect_no_offenses(<<~RUBY)
ruby2_keywords def foo(*args)
end
RUBY
end
it 'does not register an offense for a method with a `restarg` other positional args' do
expect_no_offenses(<<~RUBY)
ruby2_keywords def foo(arg1, arg2, *rest)
end
RUBY
end
it 'does not register an offense for a method with a `restarg` other optional args' do
expect_no_offenses(<<~RUBY)
ruby2_keywords def foo(arg1 = 5, *rest)
end
RUBY
end
it 'does not register an offense for a method with a `restarg` and `blockarg`' do
expect_no_offenses(<<~RUBY)
ruby2_keywords def foo(*rest, &block)
end
RUBY
end
end
context 'when `ruby2_keywords` is given a symbol' do
it 'registers an offense for an unnecessary `ruby2_keywords`' do
expect_offense(<<~RUBY)
def foo(**kwargs)
end
ruby2_keywords :foo
^^^^^^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
RUBY
end
it 'does not register an offense for an allowed def' do
expect_no_offenses(<<~RUBY)
def foo(*args)
end
ruby2_keywords :foo
RUBY
end
it 'does not register an offense when there is no `def`' do
expect_no_offenses(<<~RUBY)
ruby2_keywords :foo
RUBY
end
it 'does not register an offense when the `def` is at a different depth' do
expect_no_offenses(<<~RUBY)
class C
class D
def foo(**kwargs)
end
end
ruby2_keywords :foo
end
RUBY
end
end
context 'with a dynamically defined method' do
it 'registers an offense for an unnecessary `ruby2_keywords`' do
expect_offense(<<~RUBY)
define_method(:foo) { |**kwargs| }
ruby2_keywords :foo
^^^^^^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
RUBY
end
it 'does not register an offense for an allowed `ruby2_keywords`' do
expect_no_offenses(<<~RUBY)
define_method(:foo) { |*args| }
ruby2_keywords :foo
RUBY
end
it 'registers an offense when the method has a `shadowarg`' do
expect_offense(<<~RUBY)
define_method(:foo) { |x; y| }
ruby2_keywords :foo
^^^^^^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
RUBY
end
it 'does not register an offense when the method has a `restarg` and a `shadowarg`' do
expect_no_offenses(<<~RUBY)
define_method(:foo) { |*args; y| }
ruby2_keywords :foo
RUBY
end
it 'registers an offense for a numblock', :ruby27 do
expect_offense(<<~RUBY)
define_method(:foo) { _1 }
ruby2_keywords :foo
^^^^^^^^^^^^^^^^^^^ `ruby2_keywords` is unnecessary for method `foo`.
RUBY
end
it 'does not register an offense for `Proc#ruby2_keywords`' do
expect_no_offenses(<<~RUBY)
block = proc { |_, *args| klass.new(*args) }
block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
RUBY
end
end
end