File: range_include_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 (41 lines) | stat: -rw-r--r-- 1,291 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
# frozen_string_literal: true

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

  %i[include? member?].each do |method|
    it "autocorrects (a..b).#{method} without parens" do
      new_source = autocorrect_source("(a..b).#{method} 1")
      expect(new_source).to eq '(a..b).cover? 1'
    end

    it "autocorrects (a...b).#{method} without parens" do
      new_source = autocorrect_source("(a...b).#{method} 1")
      expect(new_source).to eq '(a...b).cover? 1'
    end

    it "autocorrects (a..b).#{method} with parens" do
      new_source = autocorrect_source("(a..b).#{method}(1)")
      expect(new_source).to eq '(a..b).cover?(1)'
    end

    it "autocorrects (a...b).#{method} with parens" do
      new_source = autocorrect_source("(a...b).#{method}(1)")
      expect(new_source).to eq '(a...b).cover?(1)'
    end
  end

  it 'formats the error message correctly for (a..b).include? 1' do
    expect_offense(<<~RUBY)
      (a..b).include? 1
             ^^^^^^^^ Use `Range#cover?` instead of `Range#include?`.
    RUBY
  end

  it 'formats the error message correctly for (a..b).member? 1' do
    expect_offense(<<~RUBY)
      (a..b).member? 1
             ^^^^^^^ Use `Range#cover?` instead of `Range#member?`.
    RUBY
  end
end