|
271 | 271 | end
|
272 | 272 | end
|
273 | 273 |
|
274 |
| - describe 'When adding with older Rake versions' do |
275 |
| - before(:each) do |
| 274 | + describe 'As for Rake versions' do |
| 275 | + before :each do |
276 | 276 | 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") |
278 | 277 | 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) |
281 | 279 |
|
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) |
286 | 281 | end
|
287 | 282 |
|
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 |
294 | 290 |
|
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 |
302 | 298 |
|
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 |
308 | 303 |
|
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 |
313 | 344 | end
|
314 | 345 |
|
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 |
319 | 431 | end
|
320 | 432 | end
|
321 | 433 |
|
|
0 commit comments