-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
relative_date_grammar_spec.rb
31 lines (26 loc) · 1.18 KB
/
relative_date_grammar_spec.rb
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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::RelativeDateGrammar, :config do
it 'accepts ActiveSupport::Duration as a receiver (ActiveSupport::Duration#since)' do
expect_no_offenses(<<~RUBY)
yesterday = 1.day.since(Time.current)
RUBY
end
it 'registers an offense for Date(Time) as a receiver (ActiveSupport::TimeWithZone#ago)' do
expect_offense(<<~RUBY)
last_week = Time.current.ago(1.week)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer ActiveSupport::Duration#ago as a receiver for relative date like `1.week.ago(Time.current)`.
RUBY
expect_correction(<<~RUBY)
last_week = 1.week.ago(Time.current)
RUBY
end
it 'registers an offense when a receiver is presumably Date(Time)' do
expect_offense(<<~RUBY)
expiration_time = purchase.created_at.since(ticket.expires_in.seconds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer ActiveSupport::Duration#since as a receiver for relative date like `ticket.expires_in.seconds.since(purchase.created_at)`.
RUBY
expect_correction(<<~RUBY)
expiration_time = ticket.expires_in.seconds.since(purchase.created_at)
RUBY
end
end