Sha256: 848449d52b496e125f8e41134790d4b28ce6d68801e96faf07410ad31741a0ce

Contents?: true

Size: 1.57 KB

Versions: 13

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

require 'spec_helper'

describe RuboCop::Cop::Lint::StringConversionInInterpolation do
  subject(:cop) { described_class.new }

  it 'registers an offense for #to_s in interpolation' do
    inspect_source(cop, '"this is the #{result.to_s}"')
    expect(cop.offenses.size).to eq(1)
    expect(cop.messages)
      .to eq(['Redundant use of `Object#to_s` in interpolation.'])
  end

  it 'detects #to_s in an interpolation with several expressions' do
    inspect_source(cop, '"this is the #{top; result.to_s}"')
    expect(cop.offenses.size).to eq(1)
  end

  it 'accepts #to_s with arguments in an interpolation' do
    inspect_source(cop, '"this is a #{result.to_s(8)}"')
    expect(cop.offenses).to be_empty
  end

  it 'accepts interpolation without #to_s' do
    inspect_source(cop, '"this is the #{result}"')
    expect(cop.offenses).to be_empty
  end

  it 'does not explode on implicit receiver' do
    inspect_source(cop, '"#{to_s}"')
    expect(cop.offenses.size).to eq(1)
    expect(cop.messages)
      .to eq(['Use `self` instead of `Object#to_s` in interpolation.'])
  end

  it 'does not explode on empty interpolation' do
    inspect_source(cop, '"this is #{} silly"')
    expect(cop.offenses).to be_empty
  end

  it 'autocorrects by removing the redundant to_s' do
    corrected = autocorrect_source(cop, ['"some #{something.to_s}"'])
    expect(corrected).to eq '"some #{something}"'
  end

  it 'autocorrects implicit receiver by replacing to_s with self' do
    corrected = autocorrect_source(cop, ['"some #{to_s}"'])
    expect(corrected).to eq '"some #{self}"'
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.29.1 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.29.0 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.28.0 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.27.1 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.27.0 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.26.1 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.26.0 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.25.0 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.24.1 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.24.0 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb
rubocop-0.23.0 spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb