Sha256: 303e1923745df4ddcd0fe14d9127340e604561a0867e2c4444f4094b5de01868

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

require 'spec_helper'

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

  it 'registers an offense for OR' do
    inspect_source(cop,
                   ['test if a or b'])
    expect(cop.offenses.size).to eq(1)
    expect(cop.messages).to eq(['Use || instead of or.'])
  end

  it 'registers an offense for AND' do
    inspect_source(cop,
                   ['test if a and b'])
    expect(cop.offenses.size).to eq(1)
    expect(cop.messages).to eq(['Use && instead of and.'])
  end

  it 'accepts ||' do
    inspect_source(cop,
                   ['test if a || b'])
    expect(cop.offenses).to be_empty
  end

  it 'accepts &&' do
    inspect_source(cop,
                   ['test if a && b'])
    expect(cop.offenses).to be_empty
  end

  it 'auto-corrects "and" with &&' do
    new_source = autocorrect_source(cop, 'true and false')
    expect(new_source).to eq('true && false')
  end

  it 'auto-corrects "or" with ||' do
    new_source = autocorrect_source(cop, ['x = 12345',
                                          'true or false'])
    expect(new_source).to eq(['x = 12345',
                              'true || false'].join("\n"))
  end

  it 'leaves *or* as is if auto-correction changes the meaning' do
    src = "teststring.include? 'a' or teststring.include? 'b'"
    new_source = autocorrect_source(cop, src)
    expect(new_source).to eq(src)
  end

  it 'leaves *and* as is if auto-correction changes the meaning' do
    src = 'x = a + b and return x'
    new_source = autocorrect_source(cop, src)
    expect(new_source).to eq(src)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.19.1 spec/rubocop/cop/style/and_or_spec.rb
rubocop-0.19.0 spec/rubocop/cop/style/and_or_spec.rb