Sha256: 19bc276144da1fc0b7d30678a0a2ced26a97597bb2918e7f6277e469a8f4a74c

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

# encoding: utf-8

require 'spec_helper'

module Rubocop
  module Cop
    describe AmpersandsPipesVsAndOr do
      let (:amp) { AmpersandsPipesVsAndOr.new }

      it 'registers an offence for AND used in condition of if statement' do
        check('if', 'and', '&&')
      end

      it 'registers an offence for OR used in condition of if statement' do
        check('if', 'or', '||')
      end

      it 'registers an offence for AND used in condition of unless' do
        check('unless', 'and', '&&')
      end

      it 'registers an offence for OR used in condition of unless' do
        check('unless', 'or', '||')
      end

      it 'registers an offence for AND used in condition of while' do
        check('while', 'and', '&&')
      end

      it 'registers an offence for OR used in condition of while' do
        check('while', 'or', '||')
      end

      it 'registers an offence for AND used in condition of until' do
        check('until', 'and', '&&')
      end

      it 'registers an offence for OR used in condition of until' do
        check('until', 'or', '||')
      end

      def check(keyword, bad_operator, good_operator)
        inspect_source(amp, 'file.rb', ["#{keyword} a #{bad_operator} b",
                                        '  c',
                                        'end',
                                        "#{keyword} a #{good_operator} b",
                                        '  c',
                                        'end'])
        # Just one offence should be registered. The good_operator
        # should be accepted.
        amp.offences.map(&:message).should ==
          ['Use &&/|| for boolean expressions, and/or for control flow.']
        amp.offences[0].line_number.should == 1
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.3.2 spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb
rubocop-0.3.1 spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb
rubocop-0.3.0 spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb