Skip to content

Commit

Permalink
Adds FFaker::Date.birthday (#542)
Browse files Browse the repository at this point in the history
* Adds FFaker::Date.birthday

* Update lib/ffaker/date.rb

Co-authored-by: marocchino <marocchino@users.noreply.github.com>

* Update test/test_date.rb

Co-authored-by: marocchino <marocchino@users.noreply.github.com>

* Fixes tests to pass

* Turns out that even birthdays are hard to remember

---------

Co-authored-by: marocchino <marocchino@users.noreply.github.com>
  • Loading branch information
professor and marocchino committed Sep 13, 2023
1 parent 54b835a commit 4aa92e0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## development

- Add your change HERE
- Adds FFaker::Date.birthday [@professor]
- Adds FFaker::Crypto.sha256 [@professor]
- Update README [@professor]
- Adds FFaker::Number.between [@professor]
Expand Down
1 change: 1 addition & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,7 @@
| ------ | ------- |
| `backward` | 2023-07-09, 2023-05-11, 2022-09-09 |
| `between`(..., ...) | |
| `birthday(min_age:, max_age:)` | |
| `forward` | 2024-07-09, 2024-06-16, 2024-06-20 |

## FFaker::DizzleIpsum
Expand Down
9 changes: 9 additions & 0 deletions lib/ffaker/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,14 @@ def forward(days = 365)

between(from, to)
end

# Random birthday date (maximum age between 18 and 65)
# Keyword arguments: min_age, max_age
def birthday(min_age: 18, max_age: 65)
from = ::Date.today.prev_year(max_age + 1).next_day
to = ::Date.today.prev_year(min_age)

between(from, to)
end
end
end
10 changes: 9 additions & 1 deletion test/test_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
@tester = FFaker::Date
end

assert_methods_are_deterministic(FFaker::Date, :backward, :forward)
assert_methods_are_deterministic(FFaker::Date, :backward, :forward, :birthday)

def test_between
from = Date.new(2015, 1, 1)
Expand All @@ -34,4 +34,12 @@ def test_forward
assert_random_between(today + 1..today + 30) { @tester.forward(30) }
assert_instance_of Date, @tester.forward
end

def test_birthday
today = Date.today

assert_random_between(today.prev_year(65).next_day..today.prev_year(18)) { @tester.birthday }
assert_random_between(today.prev_year(43).next_day..today.prev_year(42)) { @tester.birthday(min_age: 42, max_age: 42) }
assert_instance_of Date, @tester.birthday
end
end

0 comments on commit 4aa92e0

Please sign in to comment.