Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom start date for Faker::Date.forward #2791

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 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,27 @@ def test_forward
end
end

def test_forward_with_from_parameter
from = Date.parse('2012-01-01')
five_days_after_from = from + 5
random_date = @tester.forward(from: from, days: 5)

assert random_date > from, "Expected > \"#{from}\", but got #{random_date}"
assert five_days_after_from > from, "Expected < \"#{from}\", but got #{random_date}"
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
Comment on lines +80 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
100.times do
random_date = @tester.forward(from: from, days: 5)
assert random_date > from_date, "Expected > \"#{from}\", but got #{random_date}"
end
random_date = @tester.forward(from: from, days: 5)
assert random_date > from_date, "Expected > \"#{from}\", but got #{random_date}"

end

def test_backward
today = Date.today

Expand Down