public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add thorough tests for Time-object #past?, #future? and #today. Fix TimeWithZone 
#today? to use #time instead of #utc for date comparison. Update changelog. 
[#720 state:resolved]
gbuesing (author)
Sun Sep 14 20:56:32 -0700 2008
commit  cce7ae54663243a224e9871f1aac2388842b0c3a
tree    ec225d350eb6c3502cbd41b48b36dba25b4d8dd8
parent  bfa12d7a02ce0e84fcd2b83f2ce6fee1386757e3
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *Edge*
0
 
0
+* Added Time, Date, DateTime and TimeWithZone #past?, #future? and #today? #720 [Clemens Kofler, Geoff Buesing]
0
+
0
 * Fixed Sri Jayawardenepura time zone to map to Asia/Colombo [Jamis Buck]
0
 
0
 * Added Inflector#parameterize for easy slug generation ("Donald E. Knuth".parameterize => "donald-e-knuth") #713 [Matt Darby]
...
172
173
174
175
 
176
177
178
...
172
173
174
 
175
176
177
178
0
@@ -172,7 +172,7 @@ module ActiveSupport
0
     end
0
 
0
     def today?
0
-      utc.today?
0
+      time.today?
0
     end
0
 
0
     def future?
...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
226
227
...
211
212
213
 
 
 
 
 
 
 
 
 
 
 
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
0
@@ -211,17 +211,25 @@ class DateExtCalculationsTest < Test::Unit::TestCase
0
   end
0
 
0
   uses_mocha 'past?, today? and future?' do
0
-    def test_today_past_future
0
-      Date.stubs(:current).returns(Date.civil(2000, 1, 1))
0
-      t2 = Date.civil(2000, 1, 1)
0
-      t1, t3 = t2.yesterday, t2.tomorrow
0
-      t4, t5 = t2 - 1.second, t2 + 1.second
0
-
0
-      assert t1.past?
0
-      assert t2.today?
0
-      assert t3.future?
0
-      assert t4.past?
0
-      assert t5.today?
0
+    def test_today
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal false, Date.new(1999, 12, 31).today?
0
+      assert_equal true, Date.new(2000,1,1).today?
0
+      assert_equal false, Date.new(2000,1,2).today?
0
+    end
0
+    
0
+    def test_past
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal true, Date.new(1999, 12, 31).past?
0
+      assert_equal false, Date.new(2000,1,1).past?
0
+      assert_equal false, Date.new(2000,1,2).past?
0
+    end
0
+    
0
+    def test_future
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal false, Date.new(1999, 12, 31).future?
0
+      assert_equal false, Date.new(2000,1,1).future?
0
+      assert_equal true, Date.new(2000,1,2).future?
0
     end
0
   end
0
 
...
207
208
209
210
211
212
213
214
215
216
217
218
219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
221
222
...
207
208
209
 
 
 
 
 
 
 
 
 
 
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
0
@@ -207,16 +207,68 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
0
     assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema)
0
   end
0
 
0
-  uses_mocha 'past?, today? and future?' do
0
-    def test_past_today_future
0
-      Date.stubs(:current).returns(Date.civil(2000, 1, 1))
0
-      DateTime.stubs(:current).returns(DateTime.civil(2000, 1, 1, 1, 0, 1))
0
-      t1, t2 = DateTime.civil(2000, 1, 1, 1, 0, 0), DateTime.civil(2000, 1, 1, 1, 0, 2)
0
-
0
-      assert t1.past?
0
-      assert t2.future?
0
-      assert t1.today?
0
-      assert t2.today?
0
+  uses_mocha 'Test DateTime past?, today? and future?' do    
0
+    def test_today_with_offset
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal false, DateTime.civil(1999,12,31,23,59,59, Rational(-18000, 86400)).today?
0
+      assert_equal true,  DateTime.civil(2000,1,1,0,0,0, Rational(-18000, 86400)).today?
0
+      assert_equal true,  DateTime.civil(2000,1,1,23,59,59, Rational(-18000, 86400)).today?
0
+      assert_equal false, DateTime.civil(2000,1,2,0,0,0, Rational(-18000, 86400)).today?
0
+    end
0
+    
0
+    def test_today_without_offset
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal false, DateTime.civil(1999,12,31,23,59,59).today?
0
+      assert_equal true,  DateTime.civil(2000,1,1,0).today?
0
+      assert_equal true,  DateTime.civil(2000,1,1,23,59,59).today?
0
+      assert_equal false, DateTime.civil(2000,1,2,0).today?
0
+    end
0
+    
0
+    def test_past_with_offset
0
+      DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
0
+      assert_equal true,  DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).past?
0
+      assert_equal false,  DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).past?
0
+      assert_equal false,  DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).past?
0
+    end
0
+    
0
+    def test_past_without_offset
0
+      DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
0
+      assert_equal true,  DateTime.civil(2005,2,10,20,30,44).past?
0
+      assert_equal false,  DateTime.civil(2005,2,10,20,30,45).past?
0
+      assert_equal false,  DateTime.civil(2005,2,10,20,30,46).past?
0
+    end
0
+    
0
+    def test_future_with_offset
0
+      DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
0
+      assert_equal false,  DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).future?
0
+      assert_equal false,  DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).future?
0
+      assert_equal true,  DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).future?
0
+    end
0
+    
0
+    def test_future_without_offset
0
+      DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
0
+      assert_equal false,  DateTime.civil(2005,2,10,20,30,44).future?
0
+      assert_equal false,  DateTime.civil(2005,2,10,20,30,45).future?
0
+      assert_equal true,  DateTime.civil(2005,2,10,20,30,46).future?
0
+    end
0
+  end
0
+  
0
+  uses_mocha 'TestDateTimeCurrent' do
0
+    def test_current_returns_date_today_when_zone_default_not_set
0
+      with_env_tz 'US/Eastern' do
0
+        Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
0
+        assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
0
+      end
0
+    end
0
+
0
+    def test_current_returns_time_zone_today_when_zone_default_set
0
+      Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
0
+      with_env_tz 'US/Eastern' do
0
+        Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
0
+        assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
0
+      end
0
+    ensure
0
+      Time.zone_default = nil
0
     end
0
   end
0
 
...
563
564
565
566
567
568
569
570
571
572
573
574
575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576
577
578
...
563
564
565
 
 
 
 
 
 
 
 
 
 
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
0
@@ -563,16 +563,71 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
0
     assert_nothing_raised { Time.now.xmlschema }
0
   end
0
 
0
-  uses_mocha 'past?, today? and future?' do
0
-    def test_past_today_future
0
-      Date.stubs(:current).returns(Date.civil(2000, 1, 1))
0
-      Time.stubs(:current).returns(Time.local(2000, 1, 1, 1, 0, 1))
0
-      t1, t2 = Time.local(2000, 1, 1, 1, 0, 0), Time.local(2000, 1, 1, 1, 0, 2)
0
-
0
-      assert t1.past?
0
-      assert t2.future?
0
-      assert t1.today?
0
-      assert t2.today?
0
+  uses_mocha 'Test Time past?, today? and future?' do    
0
+    def test_today_with_time_local
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal false, Time.local(1999,12,31,23,59,59).today?
0
+      assert_equal true,  Time.local(2000,1,1,0).today?
0
+      assert_equal true,  Time.local(2000,1,1,23,59,59).today?
0
+      assert_equal false, Time.local(2000,1,2,0).today?
0
+    end
0
+    
0
+    def test_today_with_time_utc
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal false, Time.utc(1999,12,31,23,59,59).today?
0
+      assert_equal true,  Time.utc(2000,1,1,0).today?
0
+      assert_equal true,  Time.utc(2000,1,1,23,59,59).today?
0
+      assert_equal false, Time.utc(2000,1,2,0).today?
0
+    end
0
+    
0
+    def test_past_with_time_current_as_time_local
0
+      with_env_tz 'US/Eastern' do
0
+        Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
0
+        assert_equal true,  Time.local(2005,2,10,15,30,44).past?
0
+        assert_equal false,  Time.local(2005,2,10,15,30,45).past?
0
+        assert_equal false,  Time.local(2005,2,10,15,30,46).past?
0
+        assert_equal true,  Time.utc(2005,2,10,20,30,44).past?
0
+        assert_equal false,  Time.utc(2005,2,10,20,30,45).past?
0
+        assert_equal false,  Time.utc(2005,2,10,20,30,46).past?
0
+      end
0
+    end
0
+    
0
+    def test_past_with_time_current_as_time_with_zone
0
+      with_env_tz 'US/Eastern' do
0
+        twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
0
+        Time.stubs(:current).returns(twz)
0
+        assert_equal true,  Time.local(2005,2,10,10,30,44).past?
0
+        assert_equal false,  Time.local(2005,2,10,10,30,45).past?
0
+        assert_equal false,  Time.local(2005,2,10,10,30,46).past?
0
+        assert_equal true,  Time.utc(2005,2,10,15,30,44).past?
0
+        assert_equal false,  Time.utc(2005,2,10,15,30,45).past?
0
+        assert_equal false,  Time.utc(2005,2,10,15,30,46).past?
0
+      end
0
+    end
0
+    
0
+    def test_future_with_time_current_as_time_local
0
+      with_env_tz 'US/Eastern' do
0
+        Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
0
+        assert_equal false,  Time.local(2005,2,10,15,30,44).future?
0
+        assert_equal false,  Time.local(2005,2,10,15,30,45).future?
0
+        assert_equal true,  Time.local(2005,2,10,15,30,46).future?
0
+        assert_equal false,  Time.utc(2005,2,10,20,30,44).future?
0
+        assert_equal false,  Time.utc(2005,2,10,20,30,45).future?
0
+        assert_equal true,  Time.utc(2005,2,10,20,30,46).future?
0
+      end
0
+    end
0
+    
0
+    def test_future_with_time_current_as_time_with_zone
0
+      with_env_tz 'US/Eastern' do
0
+        twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
0
+        Time.stubs(:current).returns(twz)
0
+        assert_equal false,  Time.local(2005,2,10,10,30,44).future?
0
+        assert_equal false,  Time.local(2005,2,10,10,30,45).future?
0
+        assert_equal true,  Time.local(2005,2,10,10,30,46).future?
0
+        assert_equal false,  Time.utc(2005,2,10,15,30,44).future?
0
+        assert_equal false,  Time.utc(2005,2,10,15,30,45).future?
0
+        assert_equal true,  Time.utc(2005,2,10,15,30,46).future?
0
+      end
0
     end
0
   end
0
 
...
152
153
154
155
156
157
158
159
160
161
162
163
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
166
167
...
702
703
704
 
 
 
 
 
 
 
 
705
706
707
...
152
153
154
 
 
 
 
 
 
 
 
 
 
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
...
733
734
735
736
737
738
739
740
741
742
743
744
745
746
0
@@ -152,16 +152,47 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2))
0
   end
0
 
0
-  uses_mocha 'past?, today? and future?' do
0
-    def test_past_today_future
0
-      Time.stubs(:current).returns(@twz.utc)
0
-      Date.stubs(:current).returns(@twz.utc.to_date)
0
-      t1, t2 = @twz - 1.second, @twz + 1.second
0
-
0
-      assert t1.past?
0
-      assert t2.future?
0
-      assert !t1.today?
0
-      assert t2.today?
0
+  uses_mocha 'TimeWithZone past?, today? and future?' do    
0
+    def test_today
0
+      Date.stubs(:current).returns(Date.new(2000, 1, 1))
0
+      assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(1999,12,31,23,59,59) ).today?
0
+      assert_equal true,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,0) ).today?
0
+      assert_equal true,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,23,59,59) ).today?
0
+      assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,2,0) ).today?
0
+    end
0
+    
0
+    def test_past_with_time_current_as_time_local
0
+      with_env_tz 'US/Eastern' do
0
+        Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
0
+        assert_equal true,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
0
+        assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
0
+        assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
0
+      end
0
+    end
0
+    
0
+    def test_past_with_time_current_as_time_with_zone
0
+      twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
0
+      Time.stubs(:current).returns(twz)
0
+      assert_equal true,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
0
+      assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
0
+      assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
0
+    end
0
+    
0
+    def test_future_with_time_current_as_time_local
0
+      with_env_tz 'US/Eastern' do
0
+        Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
0
+        assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
0
+        assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
0
+        assert_equal true,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
0
+      end
0
+    end
0
+    
0
+    def future_with_time_current_as_time_with_zone
0
+      twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
0
+      Time.stubs(:current).returns(twz)
0
+      assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
0
+      assert_equal false,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
0
+      assert_equal true,  ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
0
     end
0
   end
0
 
0
@@ -702,6 +733,14 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", twz.years_ago(1).inspect
0
     assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", (twz - 1.year).inspect
0
   end
0
+  
0
+  protected
0
+    def with_env_tz(new_tz = 'US/Eastern')
0
+      old_tz, ENV['TZ'] = ENV['TZ'], new_tz
0
+      yield
0
+    ensure
0
+      old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
0
+    end
0
 end
0
 
0
 class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase

Comments