Files
nairobi/spec/lib/age_spec.rb
Javi Martín 6268ae9274 Add and apply RSpec/BeNil rubocop rule
This rule was added in rubocop-rspec 2.9.0.

We were using `be_nil` 50% of the time, and `be nil` the rest of the
time. No strong preference for either one, but IMHO we don't lose
anything be being consistent.
2023-09-06 19:00:56 +02:00

59 lines
2.5 KiB
Ruby

require "rails_helper"
describe Age do
describe ".in_years" do
it "handles nils" do
expect(Age.in_years(nil)).to be nil
end
it "calculates age correctly for common dates" do
d = Date.new(1980, 3, 13)
expect(Age.in_years(d, Date.new(2000, 3, 12))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 3, 13))).to eq(20)
expect(Age.in_years(d, Date.new(2000, 3, 14))).to eq(20)
end
it "calculates age correctly for people born near a year's limit" do
d = Date.new(1980, 12, 31)
expect(Age.in_years(d, Date.new(2000, 12, 30))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 12, 31))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 1, 1))).to eq(20)
d = Date.new(1980, 1, 1)
expect(Age.in_years(d, Date.new(2000, 12, 31))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 1, 1))).to eq(21)
expect(Age.in_years(d, Date.new(2001, 1, 2))).to eq(21)
end
it "calculates age correctly for people born around February the 29th" do
# 1980 and 2000 are leap years. 2001 is a regular year
d = Date.new(1980, 2, 29)
expect(Age.in_years(d, Date.new(2000, 2, 27))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 2, 28))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 2, 29))).to eq(20)
expect(Age.in_years(d, Date.new(2000, 3, 1))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 2, 27))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 2, 28))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 3, 1))).to eq(21)
d = Date.new(1980, 2, 28)
expect(Age.in_years(d, Date.new(2000, 2, 27))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 2, 28))).to eq(20)
expect(Age.in_years(d, Date.new(2000, 2, 29))).to eq(20)
expect(Age.in_years(d, Date.new(2000, 3, 1))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 2, 27))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 2, 28))).to eq(21)
expect(Age.in_years(d, Date.new(2001, 3, 1))).to eq(21)
d = Date.new(1980, 3, 1)
expect(Age.in_years(d, Date.new(2000, 2, 27))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 2, 28))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 2, 29))).to eq(19)
expect(Age.in_years(d, Date.new(2000, 3, 1))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 2, 27))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 2, 28))).to eq(20)
expect(Age.in_years(d, Date.new(2001, 3, 1))).to eq(21)
end
end
end