|
3 | 3 | RSpec.describe RuboCop::Cop::Rails::RelativeDateConstant do
|
4 | 4 | subject(:cop) { described_class.new }
|
5 | 5 |
|
6 |
| - it 'registers an offense for ActiveSupport::Duration.since' do |
7 |
| - expect_offense(<<-RUBY.strip_indent) |
8 |
| - class SomeClass |
9 |
| - EXPIRED_AT = 1.week.since |
10 |
| - ^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
11 |
| - end |
12 |
| - RUBY |
13 |
| - end |
| 6 | + context 'direct assignment' do |
| 7 | + it 'accepts a method with arguments' do |
| 8 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 9 | + class SomeClass |
| 10 | + EXPIRED_AT = 1.week.since(base) |
| 11 | + end |
| 12 | + RUBY |
| 13 | + end |
14 | 14 |
|
15 |
| - it 'accepts a method with arguments' do |
16 |
| - expect_no_offenses(<<-RUBY.strip_indent) |
17 |
| - class SomeClass |
18 |
| - EXPIRED_AT = 1.week.since(base) |
19 |
| - end |
20 |
| - RUBY |
21 |
| - end |
| 15 | + it 'accepts a lambda' do |
| 16 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 17 | + class SomeClass |
| 18 | + EXPIRED_AT = -> { 1.year.ago } |
| 19 | + end |
| 20 | + RUBY |
| 21 | + end |
22 | 22 |
|
23 |
| - it 'accepts a lambda' do |
24 |
| - expect_no_offenses(<<-RUBY.strip_indent) |
25 |
| - class SomeClass |
26 |
| - EXPIRED_AT = -> { 1.year.ago } |
27 |
| - end |
28 |
| - RUBY |
29 |
| - end |
| 23 | + it 'accepts a proc' do |
| 24 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 25 | + class SomeClass |
| 26 | + EXPIRED_AT = Proc.new { 1.year.ago } |
| 27 | + end |
| 28 | + RUBY |
| 29 | + end |
30 | 30 |
|
31 |
| - it 'accepts a proc' do |
32 |
| - expect_no_offenses(<<-RUBY.strip_indent) |
33 |
| - class SomeClass |
34 |
| - EXPIRED_AT = Proc.new { 1.year.ago } |
35 |
| - end |
36 |
| - RUBY |
37 |
| - end |
| 31 | + it 'registers an offense for ActiveSupport::Duration.since' do |
| 32 | + expect_offense(<<-RUBY.strip_indent) |
| 33 | + class SomeClass |
| 34 | + EXPIRED_AT = 1.week.since |
| 35 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
| 36 | + end |
| 37 | + RUBY |
38 | 38 |
|
39 |
| - it 'registers an offense for relative date in ||=' do |
40 |
| - expect_offense(<<-RUBY.strip_indent) |
41 |
| - class SomeClass |
42 |
| - EXPIRED_AT ||= 1.week.since |
43 |
| - ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
44 |
| - end |
45 |
| - RUBY |
46 |
| - end |
| 39 | + expect_correction(<<-RUBY.strip_indent) |
| 40 | + class SomeClass |
| 41 | + def self.expired_at |
| 42 | + 1.week.since |
| 43 | + end |
| 44 | + end |
| 45 | + RUBY |
| 46 | + end |
47 | 47 |
|
48 |
| - it 'registers an offense for relative date in multiple assignment' do |
49 |
| - expect_offense(<<-RUBY.strip_indent) |
50 |
| - class SomeClass |
51 |
| - START, A, x = 2.weeks.ago, 1.week.since, 5 |
52 |
| - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign ago to constants as it will be evaluated only once. |
53 |
| - end |
54 |
| - RUBY |
55 |
| - end |
| 48 | + it 'registers an offense for exclusive end range' do |
| 49 | + expect_offense(<<-RUBY.strip_indent) |
| 50 | + class SomeClass |
| 51 | + TRIAL_PERIOD = DateTime.current..1.day.since |
| 52 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
| 53 | + end |
| 54 | + RUBY |
| 55 | + |
| 56 | + expect_correction(<<-RUBY.strip_indent) |
| 57 | + class SomeClass |
| 58 | + def self.trial_period |
| 59 | + DateTime.current..1.day.since |
| 60 | + end |
| 61 | + end |
| 62 | + RUBY |
| 63 | + end |
| 64 | + |
| 65 | + it 'registers an offense for inclusive end range' do |
| 66 | + expect_offense(<<-RUBY.strip_indent) |
| 67 | + class SomeClass |
| 68 | + TRIAL_PERIOD = DateTime.current...1.day.since |
| 69 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
| 70 | + end |
| 71 | + RUBY |
| 72 | + end |
| 73 | + |
| 74 | + it 'registers an offense for exclusive begin range' do |
| 75 | + expect_offense(<<-RUBY.strip_indent) |
| 76 | + class SomeClass |
| 77 | + TRIAL_PERIOD = 1.day.ago..DateTime.current |
| 78 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign ago to constants as it will be evaluated only once. |
| 79 | + end |
| 80 | + RUBY |
| 81 | + end |
56 | 82 |
|
57 |
| - it 'registers an offense for exclusive end range' do |
58 |
| - expect_offense(<<-RUBY.strip_indent) |
59 |
| - class SomeClass |
60 |
| - TRIAL_PERIOD = DateTime.current..1.day.since |
61 |
| - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
62 |
| - end |
63 |
| - RUBY |
| 83 | + it 'registers an offense for inclusive begin range' do |
| 84 | + expect_offense(<<-RUBY.strip_indent) |
| 85 | + class SomeClass |
| 86 | + TRIAL_PERIOD = 1.day.ago...DateTime.current |
| 87 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign ago to constants as it will be evaluated only once. |
| 88 | + end |
| 89 | + RUBY |
| 90 | + end |
64 | 91 | end
|
65 | 92 |
|
66 |
| - it 'registers an offense for inclusive end range' do |
67 |
| - expect_offense(<<-RUBY.strip_indent) |
68 |
| - class SomeClass |
69 |
| - TRIAL_PERIOD = DateTime.current...1.day.since |
70 |
| - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
71 |
| - end |
72 |
| - RUBY |
| 93 | + context 'or assignment' do |
| 94 | + it 'registers an offense for relative date in ||=' do |
| 95 | + expect_offense(<<-RUBY.strip_indent) |
| 96 | + class SomeClass |
| 97 | + EXPIRED_AT ||= 1.week.since |
| 98 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
| 99 | + end |
| 100 | + RUBY |
| 101 | + end |
73 | 102 | end
|
74 | 103 |
|
75 |
| - it 'autocorrects' do |
76 |
| - new_source = autocorrect_source(<<-RUBY.strip_indent) |
77 |
| - class SomeClass |
78 |
| - EXPIRED_AT = 1.week.since |
79 |
| - end |
80 |
| - RUBY |
81 |
| - expect(new_source).to eq(<<-RUBY.strip_indent) |
82 |
| - class SomeClass |
83 |
| - def self.expired_at |
84 |
| - 1.week.since |
| 104 | + context 'mass assignment' do |
| 105 | + it 'registers an offense for relative date in multiple assignment' do |
| 106 | + expect_offense(<<-RUBY.strip_indent) |
| 107 | + class SomeClass |
| 108 | + START, A, x = 2.weeks.ago, 1.week.since, 5 |
| 109 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign ago to constants as it will be evaluated only once. |
| 110 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not assign since to constants as it will be evaluated only once. |
85 | 111 | end
|
86 |
| - end |
87 |
| - RUBY |
| 112 | + RUBY |
| 113 | + end |
88 | 114 | end
|
89 | 115 | end
|
0 commit comments