aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-01-15 06:54:50 +0000
committerMichael Koziarski <michael@koziarski.com>2007-01-15 06:54:50 +0000
commit276c9f29cde80fafa23814b0039f67504255e0fd (patch)
tree15a17ddcaff52bd1aaabf70281dbe7c7ac2d1433 /activesupport/test/core_ext
parent51d840e272941023ad62d021fa2b4a491fe9953a (diff)
downloadrails-276c9f29cde80fafa23814b0039f67504255e0fd.tar.gz
rails-276c9f29cde80fafa23814b0039f67504255e0fd.tar.bz2
rails-276c9f29cde80fafa23814b0039f67504255e0fd.zip
Make 1.months and friends accurate by introducing a Duration class. #6835 [eventualbuddha]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5940 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r--activesupport/test/core_ext/duration_test.rb13
-rw-r--r--activesupport/test/core_ext/numeric_ext_test.rb47
2 files changed, 60 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
new file mode 100644
index 0000000000..78fc999052
--- /dev/null
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -0,0 +1,13 @@
+require File.dirname(__FILE__) + '/../abstract_unit'
+
+class DurationTest < Test::Unit::TestCase
+ def test_inspect
+ assert_equal '1 month', 1.month.inspect
+ assert_equal '1 month and 1 day', (1.month + 1.day).inspect
+ assert_equal '6 months and -2 days', (6.months - 2.days).inspect
+ assert_equal '10 seconds', 10.seconds.inspect
+ assert_equal '10 years, 2 months, and 1 day', (10.years + 2.months + 1.day).inspect
+ assert_equal '7 days', 1.week.inspect
+ assert_equal '14 days', 1.fortnight.inspect
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb
index 1d04cf7fe8..a594d25805 100644
--- a/activesupport/test/core_ext/numeric_ext_test.rb
+++ b/activesupport/test/core_ext/numeric_ext_test.rb
@@ -34,6 +34,53 @@ class NumericExtTimeTest < Test::Unit::TestCase
assert seconds.from_now >= now + seconds
end
end
+
+ def test_irregular_durations
+ assert_equal @now.advance(:days => 3000), 3000.days.since(@now)
+ assert_equal @now.advance(:months => 1), 1.month.since(@now)
+ assert_equal @now.advance(:months => -1), 1.month.until(@now)
+ assert_equal @now.advance(:years => 20), 20.years.since(@now)
+ end
+
+ def test_duration_addition
+ assert_equal @now.advance(:days => 1, :months => 1), (1.day + 1.month).since(@now)
+ assert_equal @now.advance(:days => 7), (1.week + 5.seconds - 5.seconds).since(@now)
+ assert_equal @now.advance(:years => 2), (4.years - 2.years).since(@now)
+ end
+
+ def test_time_plus_duration
+ assert_equal @now + 8, @now + 8.seconds
+ assert_equal @now + 22.9, @now + 22.9.seconds
+ assert_equal @now.advance(:days => 15), @now + 15.days
+ assert_equal @now.advance(:months => 1), @now + 1.month
+ end
+
+ def test_chaining_duration_operations
+ assert_equal @now.advance(:days => 2, :months => -3), @now + 2.days - 3.months
+ assert_equal @now.advance(:days => 1, :months => 2), @now + 1.day + 2.months
+ end
+
+ def test_duration_after_convertion_is_no_longer_accurate
+ assert_equal 30.days.to_i.since(@now), 1.month.to_i.since(@now)
+ assert_equal 365.25.days.to_f.since(@now), 1.year.to_f.since(@now)
+ end
+end
+
+class NumericExtDateTest < Test::Unit::TestCase
+ def setup
+ @today = Date.today
+ end
+
+ def test_date_plus_duration
+ assert_equal @today + 1, @today + 1.day
+ assert_equal @today >> 1, @today + 1.month
+ assert_raises(ArgumentError) { @today + 1.second }
+ end
+
+ def test_chaining_duration_operations
+ assert_equal @today.advance(:days => 2, :months => -3), @today + 2.days - 3.months
+ assert_equal @today.advance(:days => 1, :months => 2), @today + 1.day + 2.months
+ end
end
class NumericExtSizeTest < Test::Unit::TestCase