Sha256: a6277e260280891c886c91c4cc45a876f0b4c105f6211ec1837efc243e04f8a2

Contents?: true

Size: 1.45 KB

Versions: 13

Compression:

Stored size: 1.45 KB

Contents

# encoding: utf-8

require 'spec_helper'

module Rubocop
  module Cop
    describe MultilineTernaryOperator do
      let(:op) { MultilineTernaryOperator.new }

      it 'registers an offence for a multiline ternary operator expression' do
        inspect_source(op, 'file.rb', ['a = cond ?',
                                        '  b : c'])
        expect(op.offences.map(&:message)).to eq(
          ['Avoid multi-line ?: (the ternary operator); use if/unless ' +
           'instead.'])
      end

      it 'accepts a single line ternary operator expression' do
        inspect_source(op, 'file.rb', ['a = cond ? b : c'])
        expect(op.offences.map(&:message)).to be_empty
      end
    end

    describe NestedTernaryOperator do
      let(:op) { NestedTernaryOperator.new }

      it 'registers an offence for a nested ternary operator expression' do
        inspect_source(op, 'file.rb', ['a ? (b ? b1 : b2) : a2'])
        expect(op.offences.map(&:message)).to eq(
          ['Ternary operators must not be nested. Prefer if/else constructs ' +
           'instead.'])
      end

      it 'accepts a non-nested ternary operator within an if' do
        inspect_source(op, 'file.rb', ['a = if x',
                                       '  cond ? b : c',
                                       'else',
                                       '  d',
                                       'end'])
        expect(op.offences.map(&:message)).to be_empty
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-0.7.2 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.7.1 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.7.0 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.6.1 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.6.0 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.5.0 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.4.6 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.4.5 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.4.4 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.4.3 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.4.2 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.4.1 spec/rubocop/cops/ternary_operator_spec.rb
rubocop-0.4.0 spec/rubocop/cops/ternary_operator_spec.rb