Sha256: c33bf90f004052383a2aff38449ec4f8420480806b1b005968dcfd675441fb03

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8

require 'spec_helper'

describe RuboCop::Cop::Style::InfiniteLoop do
  subject(:cop) { described_class.new }

  %w(1 2.0 [1] {}).each do |lit|
    it "registers an offense for a while loop with #{lit} as condition" do
      inspect_source(cop,
                     ["while #{lit}",
                      '  top',
                      'end'
                     ])
      expect(cop.offenses.size).to eq(1)
    end
  end

  %w(false nil).each do |lit|
    it "registers an offense for a until loop with #{lit} as condition" do
      inspect_source(cop,
                     ["until #{lit}",
                      '  top',
                      'end'
                     ])
      expect(cop.offenses.size).to eq(1)
    end
  end

  it 'accepts Kernel#loop' do
    inspect_source(cop,
                   'loop { break if something }')

    expect(cop.offenses).to be_empty
  end

  it 'auto-corrects the usage of "while/until" with do' do
    new_source = autocorrect_source(cop, ['while true do',
                                          'end'])
    expect(new_source).to eq("loop do\nend")
  end

  it 'auto-corrects the usage of "while/until" without do' do
    new_source = autocorrect_source(cop, ['while 1',
                                          'end'])
    expect(new_source).to eq("loop do\nend")
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.29.1 spec/rubocop/cop/style/infinite_loop_spec.rb
rubocop-0.29.0 spec/rubocop/cop/style/infinite_loop_spec.rb