spec/date_spec.rb in nydp-0.4.3 vs spec/date_spec.rb in nydp-0.4.5

- old
+ new

@@ -136,10 +136,44 @@ expect(nd._nydp_get(:"next-month").to_s). to eq "2015-07-08" expect(nd._nydp_get(:"beginning-of-month").to_s). to eq "2015-06-01" expect(nd._nydp_get(:"end-of-month").to_s). to eq "2015-06-30" end + it "calculates end-of-month all year around" do + expect(Nydp.r2n(Date.parse("2015-01-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-01-31" + expect(Nydp.r2n(Date.parse("2015-02-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-02-28" + expect(Nydp.r2n(Date.parse("2016-02-08"))._nydp_get(:"end-of-month").to_s).to eq "2016-02-29" + expect(Nydp.r2n(Date.parse("2015-03-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-03-31" + expect(Nydp.r2n(Date.parse("2015-04-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-04-30" + expect(Nydp.r2n(Date.parse("2015-05-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-05-31" + expect(Nydp.r2n(Date.parse("2015-06-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-06-30" + expect(Nydp.r2n(Date.parse("2015-07-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-07-31" + expect(Nydp.r2n(Date.parse("2015-08-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-08-31" + expect(Nydp.r2n(Date.parse("2015-09-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-09-30" + expect(Nydp.r2n(Date.parse("2015-10-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-10-31" + expect(Nydp.r2n(Date.parse("2015-11-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-11-30" + expect(Nydp.r2n(Date.parse("2015-12-08"))._nydp_get(:"end-of-month").to_s).to eq "2015-12-31" + end + + it "manages leap years" do + rd = Date.parse "2020-02-08" + nd = Nydp.r2n rd + + expect(nd._nydp_get(:"last-month").to_s). to eq "2020-01-08" + expect(nd._nydp_get(:"next-month").to_s). to eq "2020-03-08" + expect(nd._nydp_get(:"beginning-of-month").to_s). to eq "2020-02-01" + expect(nd._nydp_get(:"end-of-month").to_s). to eq "2020-02-29" + + rd = Date.parse "2020-02-28" + nd = Nydp.r2n rd + expect(nd._nydp_get(:"tomorrow").to_s). to eq "2020-02-29" + + rd = Date.parse "2020-03-01" + nd = Nydp.r2n rd + expect(nd._nydp_get(:"yesterday").to_s). to eq "2020-02-29" + end + it "returns relative dates by week" do rd = Date.parse "2015-03-12" nd = Nydp.r2n rd expect(nd._nydp_get(:"last-week").to_s). to eq "2015-03-05" @@ -158,7 +192,52 @@ it "returns a negative age relative to today for a date in the future" do rd = Date.parse "2094-11-18" nd = Nydp.r2n rd allow(::Date).to receive_messages(today: Date.parse("2016-06-21")) expect(nd._nydp_get(:age)).to eq({ years: -78, months: -5 }) + end + + describe "#change" do + let(:nd) { Nydp::Date.new(Date.parse("1965-06-08")) } + + it "advances by weeks" do + expect((nd.change 1, :week).to_s).to eq "1965-06-15" + expect((nd.change -1, :week).to_s).to eq "1965-06-01" + expect((nd.change -2, :week).to_s).to eq "1965-05-25" + expect((nd.change 2, :week).to_s).to eq "1965-06-22" + end + + it "advances by days" do + expect((nd.change 1, :day).to_s).to eq "1965-06-09" + expect((nd.change -1, :day).to_s).to eq "1965-06-07" + expect((nd.change -2, :day).to_s).to eq "1965-06-06" + expect((nd.change 2, :day).to_s).to eq "1965-06-10" + end + + it "advances by months" do + expect((nd.change 1, :month).to_s).to eq "1965-07-08" + expect((nd.change -1, :month).to_s).to eq "1965-05-08" + expect((nd.change -2, :month).to_s).to eq "1965-04-08" + expect((nd.change 2, :month).to_s).to eq "1965-08-08" + end + + it "advances by years" do + expect((nd.change 1, :year).to_s).to eq "1966-06-08" + expect((nd.change -1, :year).to_s).to eq "1964-06-08" + expect((nd.change -2, :year).to_s).to eq "1963-06-08" + expect((nd.change 2, :year).to_s).to eq "1967-06-08" + end + + it "handles leap years and small months" do + d = Nydp::Date.new(Date.parse("2019-12-31")) + + expect(d.change( 2, :month).to_s).to eq "2020-02-29" + expect(d.change(12, :month).to_s).to eq "2020-12-31" + expect(d.change(14, :month).to_s).to eq "2021-02-28" + expect(d.change( 6, :month).to_s).to eq "2020-06-30" + expect(d.change( 7, :month).to_s).to eq "2020-07-31" + + d = Nydp::Date.new(Date.parse("2020-02-29")) + expect(d.change(12, :month).to_s).to eq "2021-02-28" + end end end