Skip to content

Commit

Permalink
Merge pull request #8251 from Fatsoma/refactor/spec_correction_gemspec
Browse files Browse the repository at this point in the history
Refactor specs in Gemspec cops
  • Loading branch information
koic committed Jul 7, 2020
2 parents cebe471 + 1933033 commit 4a0ff7c
Showing 1 changed file with 22 additions and 83 deletions.
105 changes: 22 additions & 83 deletions spec/rubocop/cop/gemspec/ordered_dependencies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
}
end
let(:treat_comments_as_group_separators) { false }
let(:message) do
'Dependencies should be sorted in an alphabetical order within their ' \
'section of the gemspec. Dependency `%s` should appear before `%s`.'
end

shared_examples 'ordered dependency' do |add_dependency|
context "when #{add_dependency}" do
Expand All @@ -26,20 +22,16 @@
end

context 'when gems are not alphabetically sorted' do
let(:source) { <<~RUBY }
Gem::Specification.new do |spec|
spec.#{add_dependency} 'rubocop'
spec.#{add_dependency} 'rspec'
end
RUBY

it 'registers an offense' do
expect_offense(offense_message)
end
expect_offense(<<~RUBY, add_dependency: add_dependency)
Gem::Specification.new do |spec|
spec.%{add_dependency} 'rubocop'
spec.%{add_dependency} 'rspec'
^^^^^^{add_dependency}^^^^^^^^ Dependencies should be sorted in an alphabetical order within their section of the gemspec. Dependency `rspec` should appear before `rubocop`.
end
RUBY

it 'autocorrects' do
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY)
Gem::Specification.new do |spec|
spec.#{add_dependency} 'rspec'
spec.#{add_dependency} 'rubocop'
Expand Down Expand Up @@ -91,12 +83,18 @@

context 'with TreatCommentsAsGroupSeparators: false' do
it 'registers an offense' do
expect_offense(offense_message_with_multiline_comment)
end
expect_offense(<<~RUBY, add_dependency: add_dependency)
Gem::Specification.new do |spec|
# For code quality
spec.%{add_dependency} 'rubocop'
# For
# test
spec.%{add_dependency} 'rspec'
^^^^^^{add_dependency}^^^^^^^^ Dependencies should be sorted in an alphabetical order within their section of the gemspec. Dependency `rspec` should appear before `rubocop`.
end
RUBY

it 'autocorrects' do
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY)
Gem::Specification.new do |spec|
# For
# test
Expand All @@ -111,68 +109,9 @@
end
end

it_behaves_like 'ordered dependency', 'add_dependency' do
let(:offense_message) { <<~RUBY }
Gem::Specification.new do |spec|
spec.add_dependency 'rubocop'
spec.add_dependency 'rspec'
^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(message, 'rspec', 'rubocop')}
end
RUBY

let(:offense_message_with_multiline_comment) { <<~RUBY }
Gem::Specification.new do |spec|
# For code quality
spec.add_dependency 'rubocop'
# For
# test
spec.add_dependency 'rspec'
^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(message, 'rspec', 'rubocop')}
end
RUBY
end

it_behaves_like 'ordered dependency', 'add_runtime_dependency' do
let(:offense_message) { <<~RUBY }
Gem::Specification.new do |spec|
spec.add_runtime_dependency 'rubocop'
spec.add_runtime_dependency 'rspec'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(message, 'rspec', 'rubocop')}
end
RUBY

let(:offense_message_with_multiline_comment) { <<~RUBY }
Gem::Specification.new do |spec|
# For code quality
spec.add_runtime_dependency 'rubocop'
# For
# test
spec.add_runtime_dependency 'rspec'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(message, 'rspec', 'rubocop')}
end
RUBY
end

it_behaves_like 'ordered dependency', 'add_development_dependency' do
let(:offense_message) { <<~RUBY }
Gem::Specification.new do |spec|
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'rspec'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(message, 'rspec', 'rubocop')}
end
RUBY

let(:offense_message_with_multiline_comment) { <<~RUBY }
Gem::Specification.new do |spec|
# For code quality
spec.add_development_dependency 'rubocop'
# For
# test
spec.add_development_dependency 'rspec'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(message, 'rspec', 'rubocop')}
end
RUBY
end
it_behaves_like 'ordered dependency', 'add_dependency'
it_behaves_like 'ordered dependency', 'add_runtime_dependency'
it_behaves_like 'ordered dependency', 'add_development_dependency'

context 'when different dependencies are consecutive' do
it 'does not register any offenses' do
Expand Down

0 comments on commit 4a0ff7c

Please sign in to comment.