Skip to content

Commit

Permalink
Fix #2093 Allow custom start for forward date generator
Browse files Browse the repository at this point in the history
 *Allow set from as String or Date type
  • Loading branch information
luciagirasoles committed Jun 29, 2023
1 parent 4558f1b commit c841384
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/faker/default/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,26 @@ def between_except(from:, to:, excepted:)
##
# Produce a random date in the future (up to N days).
#
# @param from [Integer] The start of the usable forward date range.
# @param days [Integer] The maximum number of days to go into the future.
# @return [Date]
#
# @example
# @example if used with or without Rails (Active Support)
# Faker::Date.forward(days: 23) #=> #<Date: 2014-10-03>
#
# @example if used with Rails (Active Support)
# Faker::Date.forward(from: Date.current, days: 17) #=> #<Date: 2022-06-22>
#
# @example if used with or without Rails (Active Support)
# Faker::Date.forward(from: '2022-06-03', days: 10) #=> #<Date: 2022-10-13>
#
# @faker.version 1.0.0
def forward(days: 365)
from = ::Date.today + 1
to = ::Date.today + days
def forward(from: ::Date.today, days: 365)
start_date = get_date_object(from)
since = start_date + 1
to = start_date + days

between(from: from, to: to).to_date
between(from: since, to: to).to_date
end

##
Expand Down
22 changes: 22 additions & 0 deletions test/faker/default/test_faker_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ def test_forward
end
end

def test_forward_with_from_parameter
from = Date.parse('2012-01-01')

100.times do
random_date = @tester.forward(from: from, days: 5)

assert random_date > from, "Expected > \"#{from}\", but got #{random_date}"
end
end

def test_forward_with_string_parameter
from = '2012-01-01'

from_date = Date.parse(from)

100.times do
random_date = @tester.forward(from: from, days: 5)

assert random_date > from_date, "Expected > \"#{from}\", but got #{random_date}"
end
end

def test_backward
today = Date.today

Expand Down

0 comments on commit c841384

Please sign in to comment.