Skip to content

Commit cf2e94c

Browse files
authored
Merge pull request #15 from moertel/fix-month-length-issue
Fix multi-step jump for months of different length
2 parents 0cbd7f6 + 917418f commit cf2e94c

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/squcumber-postgres/support/matchers.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ def values_match(actual, expected)
1818
end
1919
end
2020

21-
def timetravel(date, i, method); i > 0 ? timetravel(date.send(method.to_sym), i - 1, method) : date; end
22-
2321
def convert_mock_values(mock_data)
2422
mock_data.map do |entry|
2523
entry.each do |key, value|
@@ -61,35 +59,35 @@ def convert_mock_value(value)
6159
when /today/
6260
Date.today
6361
when /yesterday/
64-
timetravel(Date.today, 1, :prev_day)
62+
Date.today.prev_day
6563
when /tomorrow/
66-
timetravel(Date.today, 1, :next_day)
64+
Date.today.next_day
6765
when /last month/
68-
timetravel(Date.today, 1, :prev_month)
66+
Date.today.prev_month
6967
when /next month/
70-
timetravel(Date.today, 1, :next_month)
68+
Date.today.next_month
7169
when /last year/
72-
timetravel(Date.today, 1, :prev_year)
70+
Date.today.prev_year
7371
when /next year/
74-
timetravel(Date.today, 1, :next_year)
72+
Date.today.next_year
7573
when /\s*\d+\s+month(s)?\s+ago\s*?/
7674
number_of_months = value.match(/\d+/)[0].to_i
77-
timetravel(Date.today, number_of_months, :prev_month)
75+
Date.today.prev_month(number_of_months)
7876
when /\s*\d+\s+day(s)?\s+ago\s*/
7977
number_of_days = value.match(/\d+/)[0].to_i
80-
timetravel(Date.today, number_of_days, :prev_day)
78+
Date.today.prev_day(number_of_days)
8179
when /\s*\d+\s+year(s)?\s+ago\s*/
8280
number_of_years = value.match(/\d+/)[0].to_i
83-
timetravel(Date.today, number_of_years, :prev_year)
81+
Date.today.prev_year(number_of_years)
8482
when /\s*\d+\s+month(s)?\s+from now\s*?/
8583
number_of_months = value.match(/\d+/)[0].to_i
86-
timetravel(Date.today, number_of_months, :next_month)
84+
Date.today.next_month(number_of_months)
8785
when /\s*\d+\s+day(s)?\s+from now\s*/
8886
number_of_days = value.match(/\d+/)[0].to_i
89-
timetravel(Date.today, number_of_days, :next_day)
87+
Date.today.next_day(number_of_days)
9088
when /\s*\d+\s+year(s)?\s+from now\s*/
9189
number_of_years = value.match(/\d+/)[0].to_i
92-
timetravel(Date.today, number_of_years, :next_year)
90+
Date.today.next_year(number_of_years)
9391
else
9492
placeholder
9593
end

spec/squcumber-postgres/support/matchers_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@ module Squcumber
4444
end
4545

4646
context 'with month placeholders' do
47+
context 'during a leap year' do
48+
it 'sets last month' do
49+
allow(Date).to receive(:today).and_return Date.new(2019, 3, 29)
50+
expect(dummy_class.new.convert_mock_value('last month')).to eql('2019-02-28')
51+
end
52+
it 'sets next month' do
53+
allow(Date).to receive(:today).and_return Date.new(2019, 1, 29)
54+
expect(dummy_class.new.convert_mock_value('next month')).to eql('2019-02-28')
55+
end
56+
end
57+
58+
context 'when the length of months differ' do
59+
it 'travels into the past and keeps the day' do
60+
allow(Date).to receive(:today).and_return Date.new(2019, 3, 31)
61+
expect(dummy_class.new.convert_mock_value('2 month ago')).to eql('2019-01-31')
62+
end
63+
it 'travels into the future and keeps the day' do
64+
allow(Date).to receive(:today).and_return Date.new(2019, 1, 31)
65+
expect(dummy_class.new.convert_mock_value('2 months from now')).to eql('2019-03-31')
66+
end
67+
it 'sets beginning of month' do
68+
allow(Date).to receive(:today).and_return Date.new(2019, 1, 31)
69+
expect(dummy_class.new.convert_mock_value('beginning of month 9 months from now')).to eql('2019-10-01')
70+
end
71+
it 'sets end of month' do
72+
allow(Date).to receive(:today).and_return Date.new(2019, 1, 31)
73+
expect(dummy_class.new.convert_mock_value('end of month 9 months from now')).to eql('2019-10-31')
74+
end
75+
end
76+
4777
it 'sets last month' do
4878
expect(dummy_class.new.convert_mock_value('last month')).to eql('2017-06-15')
4979
end

0 commit comments

Comments
 (0)