diff options
author | Robert Speicher <rspeicher@gmail.com> | 2017-02-08 17:09:16 -0500 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2017-06-14 13:16:44 -0500 |
commit | 69ad827e829175bebb985c8afe76174f42fc60bc (patch) | |
tree | 635f61331140628d439497b36f2c67aeb570a648 /spec | |
parent | 557cbba7df35e2f3a27995f1caf77d40a3c223fa (diff) | |
download | gitlab-ce-69ad827e829175bebb985c8afe76174f42fc60bc.tar.gz |
Add a custom RSpec/SingleLineHook cop
This cop adds an offense when `before`, `after`, or `around` are used as
single-line blocks.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/rubocop/cop/rspec/single_line_hook_spec.rb | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/spec/rubocop/cop/rspec/single_line_hook_spec.rb b/spec/rubocop/cop/rspec/single_line_hook_spec.rb new file mode 100644 index 00000000000..f3eb8de63ac --- /dev/null +++ b/spec/rubocop/cop/rspec/single_line_hook_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' +require 'rubocop-rspec' + +require_relative '../../../../rubocop/cop/rspec/single_line_hook' + +describe RuboCop::Cop::RSpec::SingleLineHook do + include CopHelper + + subject(:cop) { described_class.new } + + # Override `CopHelper#inspect_source` to always appear to be in a spec file, + # so that our RSpec-only cop actually runs + def inspect_source(*args) + super(*args, 'foo_spec.rb') + end + + it 'registers an offense for a single-line `before` block' do + inspect_source(cop, 'before { do_something }') + + expect(cop.offenses.size).to eq(1) + expect(cop.offenses.map(&:line)).to eq([1]) + expect(cop.highlights).to eq(['before { do_something }']) + end + + it 'registers an offense for a single-line `after` block' do + inspect_source(cop, 'after(:each) { undo_something }') + + expect(cop.offenses.size).to eq(1) + expect(cop.offenses.map(&:line)).to eq([1]) + expect(cop.highlights).to eq(['after(:each) { undo_something }']) + end + + it 'registers an offense for a single-line `around` block' do + inspect_source(cop, 'around { |ex| do_something_else }') + + expect(cop.offenses.size).to eq(1) + expect(cop.offenses.map(&:line)).to eq([1]) + expect(cop.highlights).to eq(['around { |ex| do_something_else }']) + end + + it 'ignores a multi-line `before` block' do + inspect_source(cop, ['before do', + ' do_something', + 'end']) + + expect(cop.offenses.size).to eq(0) + end + + it 'ignores a multi-line `after` block' do + inspect_source(cop, ['after(:each) do', + ' undo_something', + 'end']) + + expect(cop.offenses.size).to eq(0) + end + + it 'ignores a multi-line `around` block' do + inspect_source(cop, ['around do |ex|', + ' do_something_else', + 'end']) + + expect(cop.offenses.size).to eq(0) + end +end |