-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
alias_spec.rb
195 lines (166 loc) · 4.75 KB
/
alias_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::Alias, :config do
context 'when EnforcedStyle is prefer_alias_method' do
let(:cop_config) { { 'EnforcedStyle' => 'prefer_alias_method' } }
it 'registers an offense for alias with symbol args' do
expect_offense(<<~RUBY)
alias :ala :bala
^^^^^ Use `alias_method` instead of `alias`.
RUBY
expect_correction(<<~RUBY)
alias_method :ala, :bala
RUBY
end
it 'registers an offense for alias with bareword args' do
expect_offense(<<~RUBY)
alias ala bala
^^^^^ Use `alias_method` instead of `alias`.
RUBY
expect_correction(<<~RUBY)
alias_method :ala, :bala
RUBY
end
it 'does not register an offense for alias_method' do
expect_no_offenses('alias_method :ala, :bala')
end
it 'does not register an offense for alias with gvars' do
expect_no_offenses('alias $ala $bala')
end
it 'does not register an offense for alias in an instance_eval block' do
expect_no_offenses(<<~RUBY)
module M
def foo
instance_eval {
alias bar baz
}
end
end
RUBY
end
end
context 'when EnforcedStyle is prefer_alias' do
let(:cop_config) { { 'EnforcedStyle' => 'prefer_alias' } }
it 'registers an offense for alias with symbol args' do
expect_offense(<<~RUBY)
alias :ala :bala
^^^^^^^^^^ Use `alias ala bala` instead of `alias :ala :bala`.
RUBY
expect_correction(<<~RUBY)
alias ala bala
RUBY
end
it 'does not register an offense for alias with bareword args' do
expect_no_offenses('alias ala bala')
end
it 'registers an offense for alias_method at the top level' do
expect_offense(<<~RUBY)
alias_method :ala, :bala
^^^^^^^^^^^^ Use `alias` instead of `alias_method` at the top level.
RUBY
expect_correction(<<~RUBY)
alias ala bala
RUBY
end
it 'registers an offense for alias_method in a class block' do
expect_offense(<<~RUBY)
class C
alias_method :ala, :bala
^^^^^^^^^^^^ Use `alias` instead of `alias_method` in a class body.
end
RUBY
expect_correction(<<~RUBY)
class C
alias ala bala
end
RUBY
end
it 'registers an offense for alias_method in a module block' do
expect_offense(<<~RUBY)
module M
alias_method :ala, :bala
^^^^^^^^^^^^ Use `alias` instead of `alias_method` in a module body.
end
RUBY
expect_correction(<<~RUBY)
module M
alias ala bala
end
RUBY
end
it 'does not register registers an offense for alias in a def' do
expect_no_offenses(<<~RUBY)
def foo
alias :ala :bala
end
RUBY
end
it 'registers an offense for alias in a defs' do
expect_offense(<<~RUBY)
def some_obj.foo
alias :ala :bala
^^^^^ Use `alias_method` instead of `alias`.
end
RUBY
expect_correction(<<~RUBY)
def some_obj.foo
alias_method :ala, :bala
end
RUBY
end
it 'registers an offense for alias in a block' do
expect_offense(<<~RUBY)
included do
alias :ala :bala
^^^^^ Use `alias_method` instead of `alias`.
end
RUBY
expect_correction(<<~RUBY)
included do
alias_method :ala, :bala
end
RUBY
end
it 'does not register an offense for alias_method with explicit receiver' do
expect_no_offenses(<<~RUBY)
class C
receiver.alias_method :ala, :bala
end
RUBY
end
it 'does not register an offense for alias_method in self.method def' do
expect_no_offenses(<<~RUBY)
def self.method
alias_method :ala, :bala
end
RUBY
end
it 'does not register an offense for alias_method in a block' do
expect_no_offenses(<<~RUBY)
dsl_method do
alias_method :ala, :bala
end
RUBY
end
it 'does not register an offense for alias_method with non-literal constant argument' do
expect_no_offenses(<<~RUBY)
alias_method :bar, FOO
RUBY
end
it 'does not register an offense for alias_method with non-literal method call argument' do
expect_no_offenses(<<~RUBY)
alias_method :baz, foo.bar
RUBY
end
it 'does not register an offense for alias in an instance_eval block' do
expect_no_offenses(<<~RUBY)
module M
def foo
instance_eval {
alias bar baz
}
end
end
RUBY
end
end
end