Unverified Commit d4c04ff7 by Shu Fujita Committed by GitHub

Refactor test cases for AnnotateRoutes.remove_annotations (#748)

Refactor of test cases for AnnotateRoutes.remove_annotations after #736.
parent 3be824bb
...@@ -319,15 +319,16 @@ describe AnnotateRoutes do ...@@ -319,15 +319,16 @@ describe AnnotateRoutes do
end end
end end
describe 'When removing' do describe '.remove_annotations' do
before(:each) do before :each do
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true) expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
end end
it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do context 'When trailing annotation exists' do
route_file_content = <<~EOS let :route_file_content do
<<~EOS
...@@ -340,22 +341,29 @@ describe AnnotateRoutes do ...@@ -340,22 +341,29 @@ describe AnnotateRoutes do
# another good line # another good line
# good line # good line
EOS EOS
end
expected_result = <<~EOS let :expected_result do
<<~EOS
ActionController::Routing... ActionController::Routing...
foo foo
EOS 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
end
it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do context 'When prepended annotation exists' do
route_file_content = <<~EOS let :route_file_content do
<<~EOS
# == Route Map # == Route Map
# #
# another good line # another good line
...@@ -371,8 +379,10 @@ describe AnnotateRoutes do ...@@ -371,8 +379,10 @@ describe AnnotateRoutes do
EOS EOS
end
expected_result = <<~EOS let :expected_result do
<<~EOS
Rails.application.routes.draw do Rails.application.routes.draw do
root 'root#index' root 'root#index'
end end
...@@ -380,14 +390,19 @@ describe AnnotateRoutes do ...@@ -380,14 +390,19 @@ describe AnnotateRoutes do
EOS EOS
end
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
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
end
it 'should not remove custom comments above route map' do context 'When custom comments are above route map' do
route_file_content = <<~EOS let :route_file_content do
<<~EOS
# My comment # My comment
# == Route Map # == Route Map
# #
...@@ -397,18 +412,23 @@ describe AnnotateRoutes do ...@@ -397,18 +412,23 @@ describe AnnotateRoutes do
root 'root#index' root 'root#index'
end end
EOS EOS
end
expected_result = <<~EOS let :expected_result do
<<~EOS
# My comment # My comment
Rails.application.routes.draw do Rails.application.routes.draw do
root 'root#index' root 'root#index'
end end
EOS EOS
end
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) it 'does not remove custom comments above route map' do
expect(mock_file).to receive(:puts).with(expected_result) 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
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