diff --git a/test/active_time_test.rb b/test/active_time_test.rb index 9dcdf7f..da5a284 100644 --- a/test/active_time_test.rb +++ b/test/active_time_test.rb @@ -10,6 +10,26 @@ class ActiveTimeTest < ActiveSupport::TestCase teardown_db end + def year + 2008 + end + + def month + 11 + end + + def day + 14 + end + + def starting + Time.gm(2007, 11, 14) + end + + def ending + Time.gm(2008, 11, 14) + end + context "ActiveRecord::Base subclasses" do should "respond to the in_date_range named scope" do @@ -20,28 +40,176 @@ class ActiveTimeTest < ActiveSupport::TestCase context "ActiveTime" do - context "when incorrectly initialized" do - - should "raise ArgumentError when no args are provided" do + context "when initialized with no args" do + + should "raise ArgumentError" do assert_raise ArgumentError do ActiveTime.new() end end + + end + + context "when initialized with only one time object as an arg instead of two" do - should "raise ArgumentError when one Time arg is provided" do + should "raise ArgumentError" do assert_raise ArgumentError do ActiveTime.new(Time.now) end end + + end + + context "when initialized with any more than three integer args" do - should "not raise ArgumentError when two Time args are provided" do - assert_nothing_raised ArgumentError do - ActiveTime.new(Time.now, 1.minute.from_now) + should "raise ArgumentError" do + assert_raise ArgumentError do + ActiveTime.new(1, 2, 3, 4) end end end - + + context "when initialized with a year arg" do + + setup do + @target = Time.gm(year) + @object = ActiveTime.new(year) + end + + should "have a :year #range" do + assert_equal :year, @object.range + end + + should "have a #starting year equal to the target year" do + assert_equal year, @object.starting.year + end + + should "have an #ending year equal to the target year" do + assert_equal year, @object.ending.year + end + + should "have a description" do + assert_equal "in 2008", @object.description + end + + end + + context "when initialized with year and month args" do + + setup do + @target = Time.gm(year, month) + @object = ActiveTime.new(year, month) + end + + should "have a :month #range" do + assert_equal :month, @object.range + end + + should "have a #starting year and month equal to the target year and month" do + assert_equal year, @object.starting.year + assert_equal month, @object.starting.month + end + + should "have an #ending year and month equal to the target year and month" do + assert_equal year, @object.ending.year + assert_equal month, @object.ending.month + end + + should "have a description" do + assert_equal "in November 2008", @object.description + end + + end + + context "when initialized with year, month and day args" do + + setup do + @starting = Time.gm(year, month, day) + @object = ActiveTime.new(year, month, day) + end + + should "have a :day #range" do + assert_equal :day, @object.range + end + + should "have a #starting year, month and day equal to the target year, month and day" do + assert_equal year, @object.starting.year + assert_equal month, @object.starting.month + assert_equal day, @object.starting.day + end + + should "have an #ending year, month and day equal to the target year, month and day" do + assert_equal year, @object.ending.year + assert_equal month, @object.ending.month + assert_equal day, @object.ending.day + end + + should "have a description" do + assert_equal "on November 14, 2008", @object.description + end + + end + + context "when initialized with two time objects as args" do + + setup do + @object = ActiveTime.new(starting, ending) + end + + should "have a :custom #range" do + assert_equal :custom, @object.range + end + + should "have a #starting equal to the target starting time" do + assert_equal starting, @object.starting + end + + should "have an #ending equal to the target ending time" do + assert_equal ending, @object.ending + end + + should "have a description" do + assert_equal "between November 14 2007 00:00:00 and November 14 2007 00:00:00", @object.description + end + + end + + context "when initialized correctly" do + + setup do + @object = ActiveTime.new(year) + end + + should "respond to #range and return a symbol" do + assert @object.respond_to?(:range) + assert @object.range.is_a?(Symbol) + end + + should "respond to #time and return a Time object in UTC" do + assert @object.respond_to?(:time) + assert @object.time.is_a?(Time) + assert @object.time.utc? + end + + should "respond to #starting and return a Time object in UTC" do + assert @object.respond_to?(:starting) + assert @object.starting.is_a?(Time) + assert @object.starting.utc? + end + + should "respond to #ending and return a Time object in UTC" do + assert @object.respond_to?(:ending) + assert @object.ending.is_a?(Time) + assert @object.ending.utc? + end + + should "return an equal value for #time and #starting (aliases)" do + assert_equal @object.starting, @object.time + end + + end + end end