aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorEugene Kenny <elkenny@gmail.com>2018-06-25 13:48:35 +0100
committerEugene Kenny <elkenny@gmail.com>2018-06-25 13:48:35 +0100
commit376b687cb70d7cd8d84bd5f7c2acc45153359985 (patch)
tree549a6fc8005720cff0dec6308fa4c0462d919acf /activesupport
parentb2eb1d1c55a59fee1e6c4cba7030d8ceb524267c (diff)
downloadrails-376b687cb70d7cd8d84bd5f7c2acc45153359985.tar.gz
rails-376b687cb70d7cd8d84bd5f7c2acc45153359985.tar.bz2
rails-376b687cb70d7cd8d84bd5f7c2acc45153359985.zip
Add tests for duration multiplication and division
When multiplying or dividing a duration by a scalar, it's tempting to operate directly on the duration's value in seconds and recompute the parts from the result. However this loses information, as there are multiple combinations of parts that map to any given number of seconds (e.g. `2.weeks` or `336.hours`). This is especially problematic when dealing with durations on the scale of months or years, as converting an exact number of seconds to one of those intervals and then using the resulting duration to modify a date will give the wrong result.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/test/core_ext/duration_test.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index 240ae3bde0..63934e2433 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -158,6 +158,18 @@ class DurationTest < ActiveSupport::TestCase
assert_equal Date.civil(2017, 1, 3), Date.civil(2017, 1, 1) + 1.day * 2
end
+ def test_date_added_with_multiplied_duration_larger_than_one_month
+ assert_equal Date.civil(2017, 2, 15), Date.civil(2017, 1, 1) + 1.day * 45
+ end
+
+ def test_date_added_with_divided_duration
+ assert_equal Date.civil(2017, 1, 3), Date.civil(2017, 1, 1) + 4.days / 2
+ end
+
+ def test_date_added_with_divided_duration_larger_than_one_month
+ assert_equal Date.civil(2017, 2, 15), Date.civil(2017, 1, 1) + 90.days / 2
+ end
+
def test_plus_with_time
assert_equal 1 + 1.second, 1.second + 1, "Duration + Numeric should == Numeric + Duration"
end