Skip to content

Commit

Permalink
Refactor test cases of AnnotateRoutes as for Rake versions (#754)
Browse files Browse the repository at this point in the history
Refactor test cases for entire rewrite in the future.
  • Loading branch information
nard-tech authored Feb 13, 2020
1 parent ec0b3b6 commit d902dc4
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 38 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-02-05 17:13:49 -1000 using RuboCop version 0.68.1.
# on 2020-02-12 18:43:06 +0900 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -221,7 +221,7 @@ Naming/AccessorMethodName:
Exclude:
- 'lib/annotate.rb'

# Offense count: 82
# Offense count: 93
# Configuration parameters: Blacklist.
# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
Naming/HeredocDelimiterNaming:
Expand Down Expand Up @@ -528,7 +528,7 @@ Style/UnneededPercentQ:
Exclude:
- 'annotate.gemspec'

# Offense count: 353
# Offense count: 344
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
182 changes: 147 additions & 35 deletions spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,51 +271,163 @@
end
end

describe 'When adding with older Rake versions' do
before(:each) do
describe 'As for Rake versions' do
before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
end
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)

it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result)
end

it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations
end
end
context 'with older Rake versions' do
let :rake_routes_result do
<<~EOS.chomp
(in /bad/line)
good line
EOS
end

describe 'When adding with newer Rake versions' do
before(:each) do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("another good line\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
end
context 'When the route file does not end with an empty line' do
let :route_file_content do
<<~EOS.chomp
ActionController::Routing...
foo
EOS
end

it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations
end
let :expected_result do
<<~EOS
ActionController::Routing...
foo
it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations
# == Route Map
#
# good line
EOS
end

it 'annotates with an empty line' do
expect(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations
end
end

context 'When the route file ends with an empty line' do
let :route_file_content do
<<~EOS
ActionController::Routing...
foo
EOS
end

let :expected_result do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# good line
EOS
end

it 'annotates without an empty line' do
expect(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations
end
end
end

it 'should add a timestamp when :timestamp is passed' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations timestamp: true
context 'with newer Rake versions' do
let :rake_routes_result do
<<~EOS.chomp
another good line
good line
EOS
end

context 'When the route file does not end with an empty line' do
context 'When no option is passed' do
let :route_file_content do
<<~EOS.chomp
ActionController::Routing...
foo
EOS
end

let :expected_result do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# another good line
# good line
EOS
end

it 'annotates with an empty line' do
expect(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations
end
end
end

context 'When the route file ends with an empty line' do
let :route_file_content do
<<~EOS
ActionController::Routing...
foo
EOS
end

let :expected_result do
<<~EOS
ActionController::Routing...
foo
# == Route Map
#
# another good line
# good line
EOS
end

it 'annotates without an empty line' do
expect(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations
end
end

context 'When option "timestamp" is passed' do
let :route_file_content do
<<~EOS.chomp
ActionController::Routing...
foo
EOS
end

let :expected_result do
/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/
end

it 'annotates with the timestamp and an empty line' do
expect(mock_file).to receive(:puts).with(expected_result)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)

AnnotateRoutes.do_annotations timestamp: true
end
end
end
end

Expand Down

0 comments on commit d902dc4

Please sign in to comment.