forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecated_constants_spec.rb
172 lines (144 loc) · 4.64 KB
/
deprecated_constants_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::DeprecatedConstants, :config do
let(:cop_config) do
{
'DeprecatedConstants' => {
'NIL' => { 'Alternative' => 'nil', 'DeprecatedVersion' => '2.4' },
'TRUE' => { 'Alternative' => 'true', 'DeprecatedVersion' => '2.4' },
'FALSE' => { 'Alternative' => 'false', 'DeprecatedVersion' => '2.4' },
'Net::HTTPServerException' => {
'Alternative' => 'Net::HTTPClientException', 'DeprecatedVersion' => '2.6'
},
'Random::DEFAULT' => { 'Alternative' => 'Random.new', 'DeprecatedVersion' => '3.0' },
'Triple::Nested::Constant' => { 'Alternative' => 'Value', 'DeprecatedVersion' => '2.4' },
'Have::No::Alternative' => { 'DeprecatedVersion' => '2.4' },
'Have::No::DeprecatedVersion' => { 'Alternative' => 'Value' }
}
}
end
it 'registers and corrects an offense when using `NIL`' do
expect_offense(<<~RUBY)
NIL
^^^ Use `nil` instead of `NIL`, deprecated since Ruby 2.4.
RUBY
expect_correction(<<~RUBY)
nil
RUBY
end
it 'registers and corrects an offense when using `TRUE`' do
expect_offense(<<~RUBY)
TRUE
^^^^ Use `true` instead of `TRUE`, deprecated since Ruby 2.4.
RUBY
expect_correction(<<~RUBY)
true
RUBY
end
it 'registers and corrects an offense when using `FALSE`' do
expect_offense(<<~RUBY)
FALSE
^^^^^ Use `false` instead of `FALSE`, deprecated since Ruby 2.4.
RUBY
expect_correction(<<~RUBY)
false
RUBY
end
it 'registers and corrects an offense when using `Net::HTTPServerException`' do
expect_offense(<<~RUBY)
Net::HTTPServerException
^^^^^^^^^^^^^^^^^^^^^^^^ Use `Net::HTTPClientException` instead of `Net::HTTPServerException`, deprecated since Ruby 2.6.
RUBY
expect_correction(<<~RUBY)
Net::HTTPClientException
RUBY
end
context 'Ruby <= 2.7', :ruby27 do
it 'does not register an offense when using `Random::DEFAULT`' do
expect_no_offenses(<<~RUBY)
Random::DEFAULT
RUBY
end
end
context 'Ruby >= 3.0', :ruby30 do
it 'registers and corrects an offense when using `Random::DEFAULT`' do
expect_offense(<<~RUBY)
Random::DEFAULT
^^^^^^^^^^^^^^^ Use `Random.new` instead of `Random::DEFAULT`, deprecated since Ruby 3.0.
RUBY
expect_correction(<<~RUBY)
Random.new
RUBY
end
it 'registers and corrects an offense when using `::Random::DEFAULT`' do
expect_offense(<<~RUBY)
::Random::DEFAULT
^^^^^^^^^^^^^^^^^ Use `Random.new` instead of `::Random::DEFAULT`, deprecated since Ruby 3.0.
RUBY
expect_correction(<<~RUBY)
Random.new
RUBY
end
end
it 'registers and corrects an offense when using `::NIL`' do
expect_offense(<<~RUBY)
::NIL
^^^^^ Use `nil` instead of `::NIL`, deprecated since Ruby 2.4.
RUBY
expect_correction(<<~RUBY)
nil
RUBY
end
it 'registers and corrects an offense when using `::TRUE`' do
expect_offense(<<~RUBY)
::TRUE
^^^^^^ Use `true` instead of `::TRUE`, deprecated since Ruby 2.4.
RUBY
expect_correction(<<~RUBY)
true
RUBY
end
it 'registers and corrects an offense when using `::FALSE`' do
expect_offense(<<~RUBY)
::FALSE
^^^^^^^ Use `false` instead of `::FALSE`, deprecated since Ruby 2.4.
RUBY
expect_correction(<<~RUBY)
false
RUBY
end
it 'registers and corrects an offense when using `::Triple::Nested::Constant`' do
expect_offense(<<~RUBY)
::Triple::Nested::Constant
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Value` instead of `::Triple::Nested::Constant`, deprecated since Ruby 2.4.
RUBY
expect_correction(<<~RUBY)
Value
RUBY
end
it 'registers and corrects an offense when using deprecated methods that have no alternative' do
expect_offense(<<~RUBY)
Have::No::Alternative
^^^^^^^^^^^^^^^^^^^^^ Do not use `Have::No::Alternative`, deprecated since Ruby 2.4.
RUBY
expect_no_corrections
end
it 'registers and corrects an offense when using deprecated methods that have no deprecated version' do
expect_offense(<<~RUBY)
Have::No::DeprecatedVersion
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Value` instead of `Have::No::DeprecatedVersion`.
RUBY
expect_correction(<<~RUBY)
Value
RUBY
end
it 'does not register an offense when not using deprecated constant' do
expect_no_offenses(<<~RUBY)
Foo::TRUE
RUBY
end
it 'does not register an offense when using `__ENCODING__' do
expect_no_offenses(<<~RUBY)
__ENCODING__
RUBY
end
end