File: string_replacement_spec.rb

package info (click to toggle)
ruby-rubocop-performance 1.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: ruby: 6,722; makefile: 8
file content (391 lines) | stat: -rw-r--r-- 11,706 bytes parent folder | download | duplicates (3)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Performance::StringReplacement do
  subject(:cop) { described_class.new }

  it 'accepts methods other than gsub' do
    expect_no_offenses("'abc'.insert(2, 'a')")
  end

  shared_examples 'accepts' do |method|
    context 'non deterministic parameters' do
      it 'accepts gsub when the length of the pattern is greater than 1' do
        expect_no_offenses("'abc'.#{method}('ab', 'de')")
      end

      it 'accepts the first param being a variable' do
        expect_no_offenses(<<~RUBY)
          regex = /a/
          'abc'.#{method}(regex, '1')
        RUBY
      end

      it 'accepts the second param being a variable' do
        expect_no_offenses(<<~RUBY)
          replacement = 'e'
          'abc'.#{method}('abc', replacement)
        RUBY
      end

      it 'accepts the both params being a variables' do
        expect_no_offenses(<<~RUBY)
          regex = /a/
          replacement = 'e'
          'abc'.#{method}(regex, replacement)
        RUBY
      end

      it 'accepts gsub with only one param' do
        expect_no_offenses("'abc'.#{method}('a')")
      end

      it 'accepts gsub with a block' do
        expect_no_offenses("'abc'.#{method}('a') { |s| s.upcase } ")
      end

      it 'accepts a pattern with string interpolation' do
        expect_no_offenses(<<~RUBY)
          foo = 'a'
          'abc'.#{method}(\"\#{foo}\", '1')
        RUBY
      end

      it 'accepts a replacement with string interpolation' do
        expect_no_offenses(<<~RUBY)
          foo = '1'
          'abc'.#{method}('a', \"\#{foo}\")
        RUBY
      end

      it 'allows empty regex literal pattern' do
        expect_no_offenses("'abc'.#{method}(//, '1')")
      end

      it 'allows empty regex pattern from string' do
        expect_no_offenses("'abc'.#{method}(Regexp.new(''), '1')")
      end

      it 'allows empty regex pattern from regex' do
        expect_no_offenses("'abc'.#{method}(Regexp.new(//), '1')")
      end

      it 'allows regex literals with options' do
        expect_no_offenses("'abc'.#{method}(/a/i, '1')")
      end

      it 'allows regex with options' do
        expect_no_offenses("'abc'.#{method}(Regexp.new(/a/i), '1')")
      end

      it 'allows empty string pattern' do
        expect_no_offenses("'abc'.#{method}('', '1')")
      end
    end

    it 'accepts calls to gsub when the length of the pattern is shorter than ' \
       'the length of the replacement' do
      expect_no_offenses("'abc'.#{method}('a', 'ab')")
    end

    it 'accepts calls to gsub when the length of the pattern is longer than ' \
       'the length of the replacement' do
      expect_no_offenses("'abc'.#{method}('ab', 'd')")
    end
  end

  it_behaves_like('accepts', 'gsub')
  it_behaves_like('accepts', 'gsub!')

  describe 'deterministic regex' do
    describe 'regex literal' do
      it 'registers an offense when using space' do
        expect_offense(<<~RUBY)
          'abc'.gsub(/ /, '')
                ^^^^^^^^^^^^^ Use `delete` instead of `gsub`.
        RUBY
      end

      %w[a b c ' " % ! = < > # & ; : ` ~ 1 2 3 - _ , \r \\\\ \y \u1234
         \x65].each do |str|
        it "registers an offense when replacing #{str} with a literal" do
          inspect_source("'abc'.gsub(/#{str}/, 'a')")
          expect(cop.messages).to eq(['Use `tr` instead of `gsub`.'])
        end

        it "registers an offense when deleting #{str}" do
          inspect_source("'abc'.gsub(/#{str}/, '')")
          expect(cop.messages).to eq(['Use `delete` instead of `gsub`.'])
        end
      end

      it 'allows deterministic regex when the length of the pattern ' \
         'and the length of the replacement do not match' do
        expect_no_offenses(%('abc'.gsub(/a/, 'def')))
      end

      it 'registers an offense when escape characters in regex' do
        inspect_source(%('abc'.gsub(/\n/, ',')))

        expect(cop.messages).to eq(['Use `tr` instead of `gsub`.'])
      end

      it 'registers an offense when using %r notation' do
        expect_offense(<<~RUBY)
          '/abc'.gsub(%r{a}, 'd')
                 ^^^^^^^^^^^^^^^^ Use `tr` instead of `gsub`.
        RUBY
      end
    end

    describe 'regex constructor' do
      it 'registers an offense when only using word characters' do
        expect_offense(<<~RUBY)
          'abc'.gsub(Regexp.new('b'), '2')
                ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tr` instead of `gsub`.
        RUBY
      end

      it 'registers an offense when regex is built from regex' do
        expect_offense(<<~RUBY)
          'abc'.gsub(Regexp.new(/b/), '2')
                ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tr` instead of `gsub`.
        RUBY
      end

      it 'registers an offense when using compile' do
        expect_offense(<<~RUBY)
          '123'.gsub(Regexp.compile('1'), 'a')
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tr` instead of `gsub`.
        RUBY
      end
    end
  end

  describe 'non deterministic regex' do
    it 'allows regex containing a +' do
      expect_no_offenses("'abc'.gsub(/a+/, 'def')")
    end

    it 'allows regex containing a *' do
      expect_no_offenses("'abc'.gsub(/a*/, 'def')")
    end

    it 'allows regex containing a ^' do
      expect_no_offenses("'abc'.gsub(/^/, '')")
    end

    it 'allows regex containing a $' do
      expect_no_offenses("'abc'.gsub(/$/, '')")
    end

    it 'allows regex containing a ?' do
      expect_no_offenses("'abc'.gsub(/a?/, 'def')")
    end

    it 'allows regex containing a .' do
      expect_no_offenses("'abc'.gsub(/./, 'a')")
    end

    it 'allows regex containing a |' do
      expect_no_offenses("'abc'.gsub(/a|b/, 'd')")
    end

    it 'allows regex containing ()' do
      expect_no_offenses("'abc'.gsub(/(ab)/, 'd')")
    end

    it 'allows regex containing escaped ()' do
      expect_no_offenses("'(abc)'.gsub(/(ab)/, 'd')")
    end

    it 'allows regex containing {}' do
      expect_no_offenses("'abc'.gsub(/a{3,}/, 'd')")
    end

    it 'allows regex containing []' do
      expect_no_offenses("'abc'.gsub(/[a-z]/, 'd')")
    end

    it 'allows regex containing a backslash' do
      expect_no_offenses('"abc".gsub(/\\s/, "d")')
    end

    it 'allows regex literal containing interpolations' do
      expect_no_offenses(<<~'RUBY')
        foo = 'a'
        "abc".gsub(/#{foo}/, "d")
      RUBY
    end

    it 'allows regex constructor containing a string with interpolations' do
      expect_no_offenses(<<~'RUBY')
        foo = 'a'
        "abc".gsub(Regexp.new("#{foo}"), "d")
      RUBY
    end

    it 'allows regex constructor containing regex with interpolations' do
      expect_no_offenses(<<~'RUBY')
        foo = 'a'
        "abc".gsub(Regexp.new(/#{foo}/), "d")
      RUBY
    end
  end

  it 'registers an offense when the pattern has non deterministic regex ' \
     'as a string' do
    expect_offense(<<~RUBY)
      'a + c'.gsub('+', '-')
              ^^^^^^^^^^^^^^ Use `tr` instead of `gsub`.
    RUBY
  end

  it 'registers an offense when using gsub to find and replace ' \
     'a single character' do
    expect_offense(<<~RUBY)
      'abc'.gsub('a', '1')
            ^^^^^^^^^^^^^^ Use `tr` instead of `gsub`.
    RUBY
  end

  it 'registers an offense when using gsub! to find and replace ' \
     'a single character ' do
    expect_offense(<<~RUBY)
      'abc'.gsub!('a', '1')
            ^^^^^^^^^^^^^^^ Use `tr!` instead of `gsub!`.
    RUBY
  end

  it 'registers an offense for gsub! when deleting one characters' do
    expect_offense(<<~RUBY)
      'abc'.gsub!('a', '')
            ^^^^^^^^^^^^^^ Use `delete!` instead of `gsub!`.
    RUBY
  end

  it 'registers an offense when using escape characters in the replacement' do
    inspect_source("'abc'.gsub('a', '\n')")

    expect(cop.messages).to eq(['Use `tr` instead of `gsub`.'])
  end

  it 'registers an offense when using escape characters in the pattern' do
    inspect_source("'abc'.gsub('\n', ',')")

    expect(cop.messages).to eq(['Use `tr` instead of `gsub`.'])
  end

  context 'auto-correct' do
    describe 'corrects to tr' do
      it 'corrects when the length of the pattern and replacement are one' do
        new_source = autocorrect_source("'abc'.gsub('a', 'd')")

        expect(new_source).to eq("'abc'.tr('a', 'd')")
      end

      it 'corrects when the pattern is a regex literal' do
        new_source = autocorrect_source("'abc'.gsub(/a/, '1')")

        expect(new_source).to eq("'abc'.tr('a', '1')")
      end

      it 'corrects when the pattern is a regex literal using %r' do
        new_source = autocorrect_source("'abc'.gsub(%r{a}, '1')")

        expect(new_source).to eq("'abc'.tr('a', '1')")
      end

      it 'corrects when the pattern uses Regexp.new' do
        new_source = autocorrect_source("'abc'.gsub(Regexp.new('a'), '1')")

        expect(new_source).to eq("'abc'.tr('a', '1')")
      end

      it 'corrects when the pattern uses Regexp.compile' do
        new_source = autocorrect_source("'abc'.gsub(Regexp.compile('a'), '1')")

        expect(new_source).to eq("'abc'.tr('a', '1')")
      end

      it 'corrects when the replacement contains a new line character' do
        new_source = autocorrect_source("'abc'.gsub('a', '\n')")

        expect(new_source).to eq("'abc'.tr('a', '\n')")
      end

      it 'corrects when the replacement contains escape backslash' do
        new_source = autocorrect_source("\"\".gsub('/', '\\\\')")

        expect(new_source).to eq("\"\".tr('/', '\\\\')")
      end

      it 'corrects when the pattern contains a new line character' do
        new_source = autocorrect_source("'abc'.gsub('\n', ',')")

        expect(new_source).to eq("'abc'.tr('\n', ',')")
      end

      it 'corrects when the pattern contains double backslash' do
        new_source = autocorrect_source("''.gsub('\\\\', '')")

        expect(new_source).to eq("''.delete('\\\\')")
      end

      it 'corrects when replacing to a single quote' do
        new_source = autocorrect_source('"a`b".gsub("`", "\'")')

        expect(new_source).to eq('"a`b".tr("`", "\'")')
      end

      it 'corrects when replacing to a double quote' do
        new_source = autocorrect_source('"a`b".gsub("`", "\"")')

        expect(new_source).to eq('"a`b".tr("`", "\"")')
      end
    end

    describe 'corrects to delete' do
      it 'corrects when deleting a single character' do
        new_source = autocorrect_source("'abc'.gsub!('a', '')")

        expect(new_source).to eq("'abc'.delete!('a')")
      end

      it 'corrects when the pattern is a regex literal' do
        new_source = autocorrect_source("'abc'.gsub(/a/, '')")

        expect(new_source).to eq("'abc'.delete('a')")
      end

      it 'corrects when deleting an escape character' do
        new_source = autocorrect_source("'abc'.gsub('\n', '')")

        expect(new_source).to eq("'abc'.delete('\n')")
      end

      it 'corrects when the pattern uses Regexp.new' do
        new_source = autocorrect_source("'abc'.gsub(Regexp.new('a'), '')")

        expect(new_source).to eq("'abc'.delete('a')")
      end

      it 'corrects when the pattern uses Regexp.compile' do
        new_source = autocorrect_source("'ab'.gsub(Regexp.compile('a'), '')")

        expect(new_source).to eq("'ab'.delete('a')")
      end

      it 'corrects when there are no brackets' do
        new_source = autocorrect_source("'abc'.gsub! 'a', ''")

        expect(new_source).to eq("'abc'.delete! 'a'")
      end

      it 'corrects when a regexp contains escapes' do
        new_source = autocorrect_source("'abc'.gsub(/\\n/, '')")

        expect(new_source).to eq(%('abc'.delete("\\n")))
      end
    end
  end
end