Sha256: 3bedb2391252df7f16cf1c57a12fa4179fd2ef6ee268107abb83a80956df5a92

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8

require 'spec_helper'

describe Rubocop::Cop::Style::VariableInterpolation do
  subject(:cop) { described_class.new }

  it 'registers an offense for interpolated global variables' do
    inspect_source(cop,
                   ['puts "this is a #$test"'])
    expect(cop.offenses.size).to eq(1)
    expect(cop.highlights).to eq(['$test'])
    expect(cop.messages)
      .to eq(['Replace interpolated variable `$test`' \
              ' with expression `#{$test}`.'])
  end

  it 'registers an offense for interpolated regexp back references' do
    inspect_source(cop,
                   ['puts "this is a #$1"'])
    expect(cop.offenses.size).to eq(1)
    expect(cop.highlights).to eq(['$1'])
    expect(cop.messages)
      .to eq(['Replace interpolated variable `$1` with expression `#{$1}`.'])
  end

  it 'registers an offense for interpolated instance variables' do
    inspect_source(cop,
                   ['puts "this is a #@test"'])
    expect(cop.offenses.size).to eq(1)
    expect(cop.highlights).to eq(['@test'])
    expect(cop.messages)
      .to eq(['Replace interpolated variable `@test`' \
              ' with expression `#{@test}`.'])
  end

  it 'registers an offense for interpolated class variables' do
    inspect_source(cop,
                   ['puts "this is a #@@t"'])
    expect(cop.offenses.size).to eq(1)
    expect(cop.highlights).to eq(['@@t'])
    expect(cop.messages)
      .to eq(['Replace interpolated variable `@@t` with expression `#{@@t}`.'])
  end

  it 'does not register an offense for variables in expressions' do
    inspect_source(cop,
                   ['puts "this is a #{@test} #{@@t} #{$t} #{$1}"'])
    expect(cop.offenses).to be_empty
  end

  it 'autocorrects by adding the missing {}' do
    corrected = autocorrect_source(cop, ['"some #@var"'])
    expect(corrected).to eq '"some #{@var}"'
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.22.0 spec/rubocop/cop/style/variable_interpolation_spec.rb
rubocop-0.21.0 spec/rubocop/cop/style/variable_interpolation_spec.rb
rubocop-0.20.1 spec/rubocop/cop/style/variable_interpolation_spec.rb
rubocop-0.20.0 spec/rubocop/cop/style/variable_interpolation_spec.rb