Skip to content

Commit

Permalink
Make purpose of test cases more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
nard-tech committed Feb 21, 2020
1 parent f5cff71 commit 6de2d6e
Showing 1 changed file with 143 additions and 141 deletions.
284 changes: 143 additions & 141 deletions spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,188 +45,190 @@
end

context 'When "config/routes.rb" exists' do
before(:each) 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(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once
end

context 'When the result of `rake routes` is present' do
context 'When the file does not contain magic comment' do
let :rake_routes_result do
<<-EOS
context 'When the result of `rake routes` does not contain Rake version' do
context 'When the file does not contain magic comment' do
let :rake_routes_result do
<<-EOS
Prefix Verb URI Pattern Controller#Action
myaction1 GET /url1(.:format) mycontroller1#action
myaction2 POST /url2(.:format) mycontroller2#action
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
EOS
end

let :route_file_content do
''
end
let :route_file_content do
''
end

context 'When the file does not contain annotation yet' do
context 'When no option is passed' do
let :expected_result do
<<~EOS
context 'When the file does not contain annotation yet' do
context 'When no option is passed' do
let :expected_result do
<<~EOS
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end

it 'annotates normally' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
it 'annotates normally' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations
AnnotateRoutes.do_annotations
end
end
end

context 'When the option "format_markdown" is passed' do
let :expected_result do
<<~EOS
context 'When the option "format_markdown" is passed' do
let :expected_result do
<<~EOS
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
end
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
end

it 'annotates in Markdown format' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
it 'annotates in Markdown format' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations(format_markdown: true)
AnnotateRoutes.do_annotations(format_markdown: true)
end
end
end

context 'When the options "wrapper_open" and "wrapper_close" are passed' do
let :expected_result do
<<~EOS
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
let :expected_result do
<<~EOS
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
end
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
end

it 'annotates and wraps annotation with specified words' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
it 'annotates and wraps annotation with specified words' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
end
end
end
end
end

context 'When the file contains magic comments' do
MAGIC_COMMENTS.each do |magic_comment|
describe "magic comment: #{magic_comment.inspect}" do
let :route_file_content do
<<~EOS
#{magic_comment}
EOS
end
context 'When the file contains magic comments' do
MAGIC_COMMENTS.each do |magic_comment|
describe "magic comment: #{magic_comment.inspect}" do
let :route_file_content do
<<~EOS
#{magic_comment}
EOS
end

let :rake_routes_result do
<<-EOS
let :rake_routes_result do
<<-EOS
Prefix Verb URI Pattern Controller#Action
myaction1 GET /url1(.:format) mycontroller1#action
myaction2 POST /url2(.:format) mycontroller2#action
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end

context 'When the file does not contain annotation yet' do
context 'When no option is passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end

it 'annotates normally' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations
end
EOS
end

context 'When the option "format_markdown" is passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
end

it 'annotates in Markdown format' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations(format_markdown: true)
context 'When the file does not contain annotation yet' do
context 'When no option is passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
EOS
end

it 'annotates normally' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations
end
end
end

context 'When the options "wrapper_open" and "wrapper_close" are passed' do
let :expected_result do
<<~EOS
#{magic_comment}

# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
context 'When the option "format_markdown" is passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# ## Route Map
#
# Prefix | Verb | URI Pattern | Controller#Action
# --------- | ---------- | --------------- | --------------------
# myaction1 | GET | /url1(.:format) | mycontroller1#action
# myaction2 | POST | /url2(.:format) | mycontroller2#action
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
EOS
end

it 'annotates in Markdown format' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations(format_markdown: true)
end
end

it 'annotates and wraps annotation with specified words' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
let :expected_result do
<<~EOS
#{magic_comment}
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END
EOS
end

it 'annotates and wraps annotation with specified words' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
expect(mock_file).to receive(:puts).with(expected_result).once
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once

AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
end
end
end
end
Expand Down

0 comments on commit 6de2d6e

Please sign in to comment.