Commit 8ff44fcb authored by Jacopo's avatar Jacopo

Uses the rubocop helpers expect_offense and expect_no_offenses

parent b752ee4a
...@@ -9,60 +9,45 @@ describe RuboCop::Cop::AvoidBreakFromStrongMemoize do ...@@ -9,60 +9,45 @@ describe RuboCop::Cop::AvoidBreakFromStrongMemoize do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags violation for break inside strong_memoize' do it 'flags violation for break inside strong_memoize' do
source = <<~RUBY expect_offense(<<~RUBY)
strong_memoize(:result) do strong_memoize(:result) do
break if something break if something
^^^^^ Do not use break inside strong_memoize, use next instead.
do_an_heavy_calculation do_an_heavy_calculation
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses.size).to eq(1)
offense = cop.offenses.first
expect(offense.line).to eq(2)
expect(cop.highlights).to eq(['break'])
expect(offense.message).to eq('Do not use break inside strong_memoize, use next instead.')
end end
it 'flags violation for break inside strong_memoize nested blocks' do it 'flags violation for break inside strong_memoize nested blocks' do
source = <<~RUBY expect_offense(<<~RUBY)
strong_memoize do strong_memoize do
items.each do |item| items.each do |item|
break item break item
^^^^^^^^^^ Do not use break inside strong_memoize, use next instead.
end end
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses.size).to eq(1)
end end
it "doesn't flag violation for next inside strong_memoize" do it "doesn't flag violation for next inside strong_memoize" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
strong_memoize(:result) do strong_memoize(:result) do
next if something next if something
do_an_heavy_calculation do_an_heavy_calculation
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
it "doesn't flag violation for break inside blocks" do it "doesn't flag violation for break inside blocks" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
call do call do
break if something break if something
do_an_heavy_calculation do_an_heavy_calculation
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
it "doesn't call add_offense twice for nested blocks" do it "doesn't call add_offense twice for nested blocks" do
...@@ -81,12 +66,9 @@ describe RuboCop::Cop::AvoidBreakFromStrongMemoize do ...@@ -81,12 +66,9 @@ describe RuboCop::Cop::AvoidBreakFromStrongMemoize do
end end
it "doesn't check when block is empty" do it "doesn't check when block is empty" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
strong_memoize(:result) do strong_memoize(:result) do
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
end end
...@@ -9,20 +9,13 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do ...@@ -9,20 +9,13 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags violation for return inside a block' do it 'flags violation for return inside a block' do
source = <<~RUBY expect_offense(<<~RUBY)
call do call do
do_something do_something
return if something_else return if something_else
^^^^^^ Do not return from a block, use next or break instead.
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses.size).to eq(1)
offense = cop.offenses.first
expect(offense.line).to eq(3)
expect(cop.highlights).to eq(['return'])
expect(offense.message).to eq('Do not return from a block, use next or break instead.')
end end
it "doesn't call add_offense twice for nested blocks" do it "doesn't call add_offense twice for nested blocks" do
...@@ -40,36 +33,28 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do ...@@ -40,36 +33,28 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do
end end
it 'flags violation for return inside included > def > block' do it 'flags violation for return inside included > def > block' do
source = <<~RUBY expect_offense(<<~RUBY)
included do included do
def a_method def a_method
return if something return if something
call do call do
return if something_else return if something_else
^^^^^^ Do not return from a block, use next or break instead.
end end
end end
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses.size).to eq(1)
offense = cop.offenses.first
expect(offense.line).to eq(6)
end end
shared_examples 'examples with whitelisted method' do |whitelisted_method| shared_examples 'examples with whitelisted method' do |whitelisted_method|
it "doesn't flag violation for return inside #{whitelisted_method}" do it "doesn't flag violation for return inside #{whitelisted_method}" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
items.#{whitelisted_method} do |item| items.#{whitelisted_method} do |item|
do_something do_something
return if something_else return if something_else
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
end end
...@@ -79,7 +64,7 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do ...@@ -79,7 +64,7 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do
shared_examples 'examples with def methods' do |def_method| shared_examples 'examples with def methods' do |def_method|
it "doesn't flag violation for return inside #{def_method}" do it "doesn't flag violation for return inside #{def_method}" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
helpers do helpers do
#{def_method} do #{def_method} do
return if something return if something
...@@ -88,9 +73,6 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do ...@@ -88,9 +73,6 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do
end end
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
end end
...@@ -99,19 +81,16 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do ...@@ -99,19 +81,16 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do
end end
it "doesn't flag violation for return inside a lambda" do it "doesn't flag violation for return inside a lambda" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
lambda do lambda do
do_something do_something
return if something_else return if something_else
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
it "doesn't flag violation for return used inside a method definition" do it "doesn't flag violation for return used inside a method definition" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
describe Klass do describe Klass do
def a_method def a_method
do_something do_something
...@@ -119,42 +98,30 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do ...@@ -119,42 +98,30 @@ describe RuboCop::Cop::AvoidReturnFromBlocks do
end end
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
it "doesn't flag violation for next inside a block" do it "doesn't flag violation for next inside a block" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
call do call do
do_something do_something
next if something_else next if something_else
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
it "doesn't flag violation for break inside a block" do it "doesn't flag violation for break inside a block" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
call do call do
do_something do_something
break if something_else break if something_else
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
it "doesn't check when block is empty" do it "doesn't check when block is empty" do
source = <<~RUBY expect_no_offenses(<<~RUBY)
call do call do
end end
RUBY RUBY
inspect_source(source)
expect(cop.offenses).to be_empty
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment