Sha256: 74bc281dca46b0abac8d82d7f41b251614f855ded905458b84a758f4fd625a4f

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8

require 'spec_helper'

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

  it 'registers an offense for while with exclamation point condition' do
    inspect_source(cop,
                   ['while !a_condition',
                    '  some_method',
                    'end',
                    'some_method while !a_condition'
                   ])
    expect(cop.messages).to eq(
      ['Favor `until` over `while` for negative conditions.'] * 2)
  end

  it 'registers an offense for while with "not" condition' do
    inspect_source(cop,
                   ['while (not a_condition)',
                    '  some_method',
                    'end',
                    'some_method while not a_condition'])
    expect(cop.messages).to eq(
      ['Favor `until` over `while` for negative conditions.'] * 2)
    expect(cop.offenses.map(&:line)).to eq([1, 4])
  end

  it 'accepts an while where only part of the contition is negated' do
    inspect_source(cop,
                   ['while !a_condition && another_condition',
                    '  some_method',
                    'end',
                    'while not a_condition or another_condition',
                    '  some_method',
                    'end',
                    'some_method while not a_condition or other_cond'])
    expect(cop.messages).to be_empty
  end

  it 'autocorrects by replacing while not with until' do
    corrected = autocorrect_source(cop, 'something while !x.even?')
    expect(corrected).to eq 'something until x.even?'
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.20.0 spec/rubocop/cop/style/negated_while_spec.rb