Sha256: 4e6613cad1e19d01b2fd94f79a5e9fd5e603d768099ba3d0ac0d1e774c8f53f7

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

# encoding: utf-8

require 'spec_helper'

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

  it 'registers an offence for a string followed by something' do
    inspect_source(cop,
                   ['puts "%d" % 10'])
    expect(cop.offences.size).to eq(1)
    expect(cop.messages)
      .to eq(['Favor sprintf over String#%.'])
  end

  it 'registers an offence for something followed by an array' do
    inspect_source(cop,
                   ['puts x % [10, 11]'])
    expect(cop.offences.size).to eq(1)
    expect(cop.messages)
      .to eq(['Favor sprintf over String#%.'])
  end

  it 'does not register an offence for numbers' do
    inspect_source(cop,
                   ['puts 10 % 4'])
    expect(cop.offences).to be_empty
  end

  it 'does not register an offence for ambiguous cases' do
    inspect_source(cop,
                   ['puts x % 4'])
    expect(cop.offences).to be_empty

    inspect_source(cop,
                   ['puts x % Y'])
    expect(cop.offences).to be_empty
  end

  it 'works if the first operand contains embedded expressions' do
    inspect_source(cop,
                   ['puts "#{x * 5} %d #{@test}" % 10'])
    expect(cop.offences.size).to eq(1)
    expect(cop.messages)
      .to eq(['Favor sprintf over String#%.'])
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
ridecharge-rubocop-0.0.1 spec/rubocop/cop/style/favor_sprintf_spec.rb
rubocop-0.18.1 spec/rubocop/cop/style/favor_sprintf_spec.rb
rubocop-0.18.0 spec/rubocop/cop/style/favor_sprintf_spec.rb
rubocop-0.17.0 spec/rubocop/cop/style/favor_sprintf_spec.rb
rubocop-0.16.0 spec/rubocop/cop/style/favor_sprintf_spec.rb
rubocop-0.15.0 spec/rubocop/cop/style/favor_sprintf_spec.rb
rubocop-0.14.1 spec/rubocop/cop/style/favor_sprintf_spec.rb
rubocop-0.14.0 spec/rubocop/cop/style/favor_sprintf_spec.rb