Skip to content

Commit d902dc4

Browse files
authored
Refactor test cases of AnnotateRoutes as for Rake versions (#754)
Refactor test cases for entire rewrite in the future.
1 parent ec0b3b6 commit d902dc4

File tree

2 files changed

+150
-38
lines changed

2 files changed

+150
-38
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-02-05 17:13:49 -1000 using RuboCop version 0.68.1.
3+
# on 2020-02-12 18:43:06 +0900 using RuboCop version 0.68.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -221,7 +221,7 @@ Naming/AccessorMethodName:
221221
Exclude:
222222
- 'lib/annotate.rb'
223223

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

531-
# Offense count: 353
531+
# Offense count: 344
532532
# Cop supports --auto-correct.
533533
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
534534
# URISchemes: http, https

spec/lib/annotate/annotate_routes_spec.rb

Lines changed: 147 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -271,51 +271,163 @@
271271
end
272272
end
273273

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

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

288-
it 'should not add a newline if there are empty lines' do
289-
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
290-
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
291-
AnnotateRoutes.do_annotations
292-
end
293-
end
283+
context 'with older Rake versions' do
284+
let :rake_routes_result do
285+
<<~EOS.chomp
286+
(in /bad/line)
287+
good line
288+
EOS
289+
end
294290

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

303-
it 'should annotate and add a newline!' do
304-
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
305-
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
306-
AnnotateRoutes.do_annotations
307-
end
299+
let :expected_result do
300+
<<~EOS
301+
ActionController::Routing...
302+
foo
308303
309-
it 'should not add a newline if there are empty lines' do
310-
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
311-
expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
312-
AnnotateRoutes.do_annotations
304+
# == Route Map
305+
#
306+
# good line
307+
EOS
308+
end
309+
310+
it 'annotates with an empty line' do
311+
expect(mock_file).to receive(:puts).with(expected_result)
312+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
313+
314+
AnnotateRoutes.do_annotations
315+
end
316+
end
317+
318+
context 'When the route file ends with an empty line' do
319+
let :route_file_content do
320+
<<~EOS
321+
ActionController::Routing...
322+
foo
323+
EOS
324+
end
325+
326+
let :expected_result do
327+
<<~EOS
328+
ActionController::Routing...
329+
foo
330+
331+
# == Route Map
332+
#
333+
# good line
334+
EOS
335+
end
336+
337+
it 'annotates without an empty line' do
338+
expect(mock_file).to receive(:puts).with(expected_result)
339+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
340+
341+
AnnotateRoutes.do_annotations
342+
end
343+
end
313344
end
314345

315-
it 'should add a timestamp when :timestamp is passed' do
316-
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
317-
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/)
318-
AnnotateRoutes.do_annotations timestamp: true
346+
context 'with newer Rake versions' do
347+
let :rake_routes_result do
348+
<<~EOS.chomp
349+
another good line
350+
good line
351+
EOS
352+
end
353+
354+
context 'When the route file does not end with an empty line' do
355+
context 'When no option is passed' do
356+
let :route_file_content do
357+
<<~EOS.chomp
358+
ActionController::Routing...
359+
foo
360+
EOS
361+
end
362+
363+
let :expected_result do
364+
<<~EOS
365+
ActionController::Routing...
366+
foo
367+
368+
# == Route Map
369+
#
370+
# another good line
371+
# good line
372+
EOS
373+
end
374+
375+
it 'annotates with an empty line' do
376+
expect(mock_file).to receive(:puts).with(expected_result)
377+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
378+
379+
AnnotateRoutes.do_annotations
380+
end
381+
end
382+
end
383+
384+
context 'When the route file ends with an empty line' do
385+
let :route_file_content do
386+
<<~EOS
387+
ActionController::Routing...
388+
foo
389+
EOS
390+
end
391+
392+
let :expected_result do
393+
<<~EOS
394+
ActionController::Routing...
395+
foo
396+
397+
# == Route Map
398+
#
399+
# another good line
400+
# good line
401+
EOS
402+
end
403+
404+
it 'annotates without an empty line' do
405+
expect(mock_file).to receive(:puts).with(expected_result)
406+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
407+
408+
AnnotateRoutes.do_annotations
409+
end
410+
end
411+
412+
context 'When option "timestamp" is passed' do
413+
let :route_file_content do
414+
<<~EOS.chomp
415+
ActionController::Routing...
416+
foo
417+
EOS
418+
end
419+
420+
let :expected_result do
421+
/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/
422+
end
423+
424+
it 'annotates with the timestamp and an empty line' do
425+
expect(mock_file).to receive(:puts).with(expected_result)
426+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED)
427+
428+
AnnotateRoutes.do_annotations timestamp: true
429+
end
430+
end
319431
end
320432
end
321433

0 commit comments

Comments
 (0)