blob: 8ab430072b1d1d653a68f4f30a9904835bb77d75 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# frozen_string_literal: true
require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/rails/avoid_time_comparison'
RSpec.describe RuboCop::Cop::Rails::AvoidTimeComparison, feature_category: :shared do
shared_examples 'using time comparison' do
let(:violation_string_length) { "datetime > #{time}".length }
it 'flags violation' do
expect_offense(<<~RUBY)
datetime > #{time}
#{'^' * violation_string_length} Avoid time comparison, use `.past?` or `.future?` instead.
RUBY
expect_offense(<<~RUBY)
datetime < #{time}
#{'^' * violation_string_length} Avoid time comparison, use `.past?` or `.future?` instead.
RUBY
expect_offense(<<~RUBY)
#{time} < datetime
#{'^' * violation_string_length} Avoid time comparison, use `.past?` or `.future?` instead.
RUBY
end
end
context 'when comparing with Time.now', :aggregate_failures do
let(:time) { 'Time.now' }
it_behaves_like 'using time comparison'
end
context 'when comparing with ::Time.now', :aggregate_failures do
let(:time) { '::Time.now' }
it_behaves_like 'using time comparison'
end
context 'when comparing with Time.zone.now', :aggregate_failures do
let(:time) { 'Time.zone.now' }
it_behaves_like 'using time comparison'
end
context 'when comparing with Time.current', :aggregate_failures do
let(:time) { 'Time.current' }
it_behaves_like 'using time comparison'
end
it 'does not flag assigning time methods to variables' do
expect_no_offenses(<<~RUBY)
datetime = Time.now
RUBY
end
end
|