Skip to content

Commit

Permalink
Refactor test cases for AnnotateRoutes.remove_annotations (#748)
Browse files Browse the repository at this point in the history
Refactor of test cases for AnnotateRoutes.remove_annotations after #736.
  • Loading branch information
nard-tech committed Feb 6, 2020
1 parent 3be824b commit d4c04ff
Showing 1 changed file with 79 additions and 59 deletions.
138 changes: 79 additions & 59 deletions spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,96 +319,116 @@
end
end

describe 'When removing' do
before(:each) do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED)
describe '.remove_annotations' do
before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
end

it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
route_file_content = <<~EOS
context 'When trailing annotation exists' do
let :route_file_content do
<<~EOS
ActionController::Routing...
foo
ActionController::Routing...
foo
# == Route Map
#
# another good line
# good line
EOS
# == Route Map
#
# another good line
# good line
EOS
end

expected_result = <<~EOS
let :expected_result do
<<~EOS
ActionController::Routing...
foo
EOS
ActionController::Routing...
foo
EOS
end

it 'removes trailing annotation and trim trailing newlines, but leave leading newlines alone' do
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once

expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
expect(mock_file).to receive(:puts).with(expected_result)
AnnotateRoutes.remove_annotations
AnnotateRoutes.remove_annotations
end
end

it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do
route_file_content = <<~EOS
# == Route Map
#
# another good line
# good line
context 'When prepended annotation exists' do
let :route_file_content do
<<~EOS
# == Route Map
#
# another good line
# good line
Rails.application.routes.draw do
root 'root#index'
end
Rails.application.routes.draw do
root 'root#index'
end
EOS
EOS
end

expected_result = <<~EOS
Rails.application.routes.draw do
root 'root#index'
end
let :expected_result do
<<~EOS
Rails.application.routes.draw do
root 'root#index'
end
EOS
EOS
end

expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
expect(mock_file).to receive(:puts).with(expected_result)
AnnotateRoutes.remove_annotations
it 'removes prepended annotation and trim leading newlines, but leave trailing newlines alone' do
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once

AnnotateRoutes.remove_annotations
end
end

it 'should not remove custom comments above route map' do
route_file_content = <<~EOS
# My comment
# == Route Map
#
# another good line
# good line
Rails.application.routes.draw do
root 'root#index'
end
EOS
context 'When custom comments are above route map' do
let :route_file_content do
<<~EOS
# My comment
# == Route Map
#
# another good line
# good line
Rails.application.routes.draw do
root 'root#index'
end
EOS
end

expected_result = <<~EOS
# My comment
Rails.application.routes.draw do
root 'root#index'
end
EOS
let :expected_result do
<<~EOS
# My comment
Rails.application.routes.draw do
root 'root#index'
end
EOS
end

expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
expect(mock_file).to receive(:puts).with(expected_result)
it 'does not remove custom comments above route map' do
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once

AnnotateRoutes.remove_annotations
AnnotateRoutes.remove_annotations
end
end
end
end

0 comments on commit d4c04ff

Please sign in to comment.