spec/usaidwat/time_spec.rb in usaidwat-1.1.1 vs spec/usaidwat/time_spec.rb in usaidwat-1.2.0

- old
+ new

@@ -53,10 +53,148 @@ expect(1.year).to eq(31_536_000) end end end + describe Float do + describe "#seconds_ago" do + it "returns the elapsed seconds between now and then" do + then_ = 5.0 + expect(then_.seconds_ago).to be_within(0.01).of(5.0) + end + end + + describe "#minutes_ago" do + it "returns the elapsed minutes between now and then" do + then_ = 90.0 + expect(then_.minutes_ago).to be_within(0.01).of(1.5) + end + end + + describe "#hours_ago" do + it "returns the elapsed hours between now and then" do + then_ = 14976.0 + expect(then_.hours_ago).to be_within(0.01).of(4.16) + end + end + + describe "#days_ago" do + it "returns the elapsed days between now and then" do + then_ = 464832.0 + expect(then_.days_ago).to be_within(0.01).of(5.38) + end + end + + describe "#weeks_ago" do + it "returns the elapsed weeks between now and then" do + then_ = 2437344.0 + expect(then_.weeks_ago).to be_within(0.01).of(4.03) + end + end + + describe "#months_ago" do + it "returns the elapsed months between now and then" do + then_ = 47096640.0 + expect(then_.months_ago).to be_within(0.01).of(18.17) + end + end + + describe "#years_ago" do + it "returns the elapsed years between now and then" do + then_ = 859040640.0 + expect(then_.years_ago).to be_within(0.01).of(27.24) + end + end + + describe "#ago" do + it "should respond when then was a few seconds ago" do + then_ = 5.0 + expect(then_.ago).to eq("less than 5 seconds ago") + end + + it "should respond when then was less than 10 seconds ago" do + then_ = 10.0 + expect(then_.ago).to eq("less than 10 seconds ago") + end + + it "should respond when then was less than 20 seconds ago" do + then_ = 20.0 + expect(then_.ago).to eq("less than 20 seconds ago") + end + + it "should respond when then was less than a half minute ago" do + then_ = 40.0 + expect(then_.ago).to eq("half a minute ago") + end + + it "should respond when then was less than a minute ago" do + then_ = 59.0 + expect(then_.ago).to eq("less than a minute ago") + end + + it "should respond when then was a minute ago" do + then_ = 1.minute.to_f + expect(then_.ago).to eq("a minute ago") + end + + it "should respond when then was several minutes ago" do + then_ = 45.minutes.to_f + expect(then_.ago).to eq("45 minutes ago") + end + + it "should respond when then was an hour ago" do + then_ = 90.minutes.to_f + expect(then_.ago).to eq("about an hour ago") + end + + it "should respond when then was several hours ago" do + then_ = 1440.minutes.to_f + expect(then_.ago).to eq("about 24 hours ago") + end + + it "should respond when then was a day ago" do + then_ = 2880.minutes.to_f + expect(then_.ago).to eq("a day ago") + end + + it "should respond when then was several days ago" do + then_ = 10080.minutes.to_f + expect(then_.ago).to eq("about 7 days ago") + end + + it "should respond when then was a week ago" do + then_ = (1.week + 1.minute).to_f + expect(then_.ago).to eq("about 1 week ago") + end + + it "should respond when then was several weeks ago" do + then_ = 43220.minutes.to_f + expect(then_.ago).to eq("about 4 weeks ago") + end + + it "should respond when then was a month ago" do + then_ = 43221.minutes.to_f + expect(then_.ago).to eq("about 1 month ago") + end + + it "should respond when then was several months ago" do + then_ = 180.days.to_f + expect(then_.ago).to eq("about 6 months ago") + end + + it "should respond when then was a year ago" do + then_ = 1051920.minutes.to_f + expect(then_.ago).to eq("about a year ago") + end + + it "should respond when then was several years ago" do + then_ = 1051921.minutes.to_f + expect(then_.ago).to eq("over 2 years ago") + end + end + end + describe Time do before do Timecop.freeze(2015, 6, 23, 17, 22, 0) end @@ -193,12 +331,38 @@ then_ = Time.now - 1051920.minutes expect(then_.ago).to eq("about a year ago") end it "should respond when then was several years ago" do - then_ = Time.now - 1051920.minutes - 1 + then_ = Time.now - 1051921.minutes expect(then_.ago).to eq("over 2 years ago") end + end + end + + describe "Special Time cases" do + it "should respond when then was almost but not quite 8 years ago" do + now = Time.new(2015, 9, 15, 16, 13, 0) + Timecop.freeze(now) + then_ = Time.new(2007, 11, 19, 10, 4) + expect(then_.ago).to eq("over 7 years ago") + Timecop.return + end + + it "should respond when the delta is between 1 and 2 minutes" do + now = Time.new(2015, 9, 18, 10, 05, 20) + Timecop.freeze(now) + then_ = Time.new(2015, 9, 18, 10, 04, 0) + expect(then_.ago).to eq("a minute ago") + Timecop.return + end + + it "should throw an exception if the delta is negative" do + now = Time.new(2015, 9, 18, 10, 04, 0) + Timecop.freeze(now) + then_ = Time.new(2015, 9, 18, 10, 05, 20) + expect { then_.ago }.to raise_error(ArgumentError, /negative/) + Timecop.return end end end end