|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe(RuboCop::Cop::Rails::Timecop, :config) do |
| 4 | + subject(:cop) { described_class.new(config) } |
| 5 | + |
| 6 | + describe 'Timecop.freeze' do |
| 7 | + context 'without a block' do |
| 8 | + context 'without arguments' do |
| 9 | + it 'adds an offense' do |
| 10 | + expect_offense(<<~RUBY) |
| 11 | + Timecop.freeze |
| 12 | + ^^^^^^^^^^^^^^ Use `freeze_time` instead of `Timecop.freeze` |
| 13 | + RUBY |
| 14 | + end |
| 15 | + |
| 16 | + it 'autocorrects to `freeze_time`' do |
| 17 | + expect(autocorrect_source('Timecop.freeze')).to(eq('freeze_time')) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + context 'with arguments' do |
| 22 | + it 'adds an offense' do |
| 23 | + expect_offense(<<~RUBY) |
| 24 | + Timecop.freeze(123) |
| 25 | + ^^^^^^^^^^^^^^^^^^^ Use `travel` or `travel_to` instead of `Timecop.freeze` |
| 26 | + RUBY |
| 27 | + end |
| 28 | + |
| 29 | + it 'does not autocorrect' do |
| 30 | + source = 'Timecop.freeze(123)' |
| 31 | + |
| 32 | + expect(autocorrect_source(source)).to(eq(source)) |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context 'with a block' do |
| 38 | + context 'without arguments' do |
| 39 | + it 'adds an offense' do |
| 40 | + expect_offense(<<~RUBY) |
| 41 | + Timecop.freeze { } |
| 42 | + ^^^^^^^^^^^^^^ Use `freeze_time` instead of `Timecop.freeze` |
| 43 | + RUBY |
| 44 | + end |
| 45 | + |
| 46 | + it 'autocorrects to `freeze_time`' do |
| 47 | + expect(autocorrect_source('Timecop.freeze { }')).to(eq('freeze_time { }')) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context 'with arguments' do |
| 52 | + it 'adds an offense' do |
| 53 | + expect_offense(<<~RUBY) |
| 54 | + Timecop.freeze(123) { } |
| 55 | + ^^^^^^^^^^^^^^^^^^^ Use `travel` or `travel_to` instead of `Timecop.freeze` |
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + # FIXME: Is this how NOT autocorrecting something should be tested? |
| 60 | + it 'does not autocorrect' do |
| 61 | + source = 'Timecop.freeze(123) { }' |
| 62 | + |
| 63 | + expect(autocorrect_source(source)).to(eq(source)) |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe 'Timecop.return' do |
| 70 | + context 'without a block' do |
| 71 | + it 'adds an offense' do |
| 72 | + expect_offense(<<~RUBY) |
| 73 | + Timecop.return |
| 74 | + ^^^^^^^^^^^^^^ Use `travel_back` instead of `Timecop.return` |
| 75 | + RUBY |
| 76 | + end |
| 77 | + |
| 78 | + it 'autocorrects to `travel_back`' do |
| 79 | + expect(autocorrect_source('Timecop.return')).to(eq('travel_back')) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + context 'with a block' do |
| 84 | + it 'adds an offense' do |
| 85 | + expect_offense(<<~RUBY) |
| 86 | + Timecop.return { } |
| 87 | + ^^^^^^^^^^^^^^ Use `travel_back` instead of `Timecop.return` |
| 88 | + RUBY |
| 89 | + end |
| 90 | + |
| 91 | + it 'autocorrects to `travel_back`' do |
| 92 | + expect(autocorrect_source('Timecop.return { }')).to(eq('travel_back { }')) |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe 'Timecop.travel' do |
| 98 | + it 'adds an offense' do |
| 99 | + expect_offense(<<~RUBY) |
| 100 | + Timecop.travel(123) { } |
| 101 | + ^^^^^^^^^^^^^^^^^^^ Use `travel` or `travel_to` instead of `Timecop.travel`. If you need time to keep flowing, simulate it by travelling again. |
| 102 | + RUBY |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + describe 'Timecop.*' do |
| 107 | + it 'adds an offense' do |
| 108 | + expect_offense(<<~RUBY) |
| 109 | + Timecop.foo |
| 110 | + ^^^^^^^ Use `ActiveSupport::Testing::TimeHelpers` instead of `Timecop` |
| 111 | + RUBY |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + describe 'Timecop' do |
| 116 | + it 'adds an offense' do |
| 117 | + expect_offense(<<~RUBY) |
| 118 | + Timecop.foo |
| 119 | + ^^^^^^^ Use `ActiveSupport::Testing::TimeHelpers` instead of `Timecop` |
| 120 | + RUBY |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe '::Timecop' do |
| 125 | + it 'adds an offense' do |
| 126 | + expect_offense(<<~RUBY) |
| 127 | + ::Timecop.foo |
| 128 | + ^^^^^^^^^ Use `ActiveSupport::Testing::TimeHelpers` instead of `Timecop` |
| 129 | + RUBY |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments