Sha256: 0ed497f19a7ee7a097312cc73dcd7e69c3f9dffae119c7417efba741b9707e11

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8

require 'spec_helper'

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

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

        it 'registers an offence for AND' do
          inspect_source(cop,
                         ['test if a and b'])
          expect(cop.offences.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.offences).to be_empty
        end

        it 'accepts &&' do
          inspect_source(cop,
                         ['test if a && b'])
          expect(cop.offences).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, 'true or false')
          expect(new_source).to eq('true || false')
        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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.13.1 spec/rubocop/cop/style/and_or_spec.rb
rubocop-0.13.0 spec/rubocop/cop/style/and_or_spec.rb