From b80135b761b56d853531497a2d16e0c629804da3 Mon Sep 17 00:00:00 2001 From: Phil Ross Date: Mon, 19 Sep 2016 19:31:02 +0100 Subject: [PATCH] Add tests to cover ignoring and preserving offsets for #52. --- test/tc_time_or_datetime.rb | 63 ++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/test/tc_time_or_datetime.rb b/test/tc_time_or_datetime.rb index a3b97766..72ceb154 100644 --- a/test/tc_time_or_datetime.rb +++ b/test/tc_time_or_datetime.rb @@ -18,15 +18,6 @@ def test_initialize_time_local assert(tdt.to_orig.utc?) end - def test_initialize_time_local_preserve_offset - t = Time.new(2006, 3, 24, 15, 32, 3, '+03:00') - tdt = TimeOrDateTime.new(t, false) - assert_equal(t, tdt.to_time) - assert_equal(t, tdt.to_orig) - assert(!tdt.to_time.utc?) - assert(!tdt.to_orig.utc?) - end - def test_intialize_time_local_usec tdt = TimeOrDateTime.new(Time.local(2006, 3, 24, 15, 32, 3, 721123)) assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123), tdt.to_time) @@ -64,10 +55,30 @@ def test_initialize_time_utc_local end end - def test_initialize_datetime_offset + def test_initialize_time_ignore_offset + tdt = TimeOrDateTime.new(Time.new(2006, 3, 24, 15, 32, 3, '+03:00')) + assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), tdt.to_orig) + assert_equal(0, tdt.to_orig.utc_offset) + end + + def test_initialize_time_preserve_offset + t = Time.new(2006, 3, 24, 15, 32, 3, '+03:00') + tdt = TimeOrDateTime.new(t, false) + assert_equal(t, tdt.to_orig) + assert_equal(10800, tdt.to_orig.utc_offset) + end + + def test_initialize_datetime_ignore_offset tdt = TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3).new_offset(Rational(5, 24))) - assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), tdt.to_datetime) - assert_equal(0, tdt.to_datetime.offset) + assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), tdt.to_orig) + assert_equal(0, tdt.to_orig.offset) + end + + def test_initialize_datetime_preserve_offset + dt = DateTime.new(2006, 3, 24, 15, 32, 3).new_offset(Rational(5, 24)) + tdt = TimeOrDateTime.new(dt, false) + assert_equal(dt, tdt.to_orig) + assert_equal(Rational(5, 24), tdt.to_orig.offset) end def test_initialize_datetime @@ -575,6 +586,18 @@ def test_wrap_timeordatetime assert_same(t, t2) end + def test_wrap_offset_ignored + t = TimeOrDateTime.wrap(Time.new(2006, 3, 24, 15, 32, 3, '+03:00')) + assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), t.to_orig) + assert_equal(0, t.to_orig.utc_offset) + end + + def test_wrap_offset_preserved + t = TimeOrDateTime.wrap(Time.new(2006, 3, 24, 15, 32, 3, '+03:00'), false) + assert_equal(Time.new(2006, 3, 24, 15, 32, 3, '+03:00'), t.to_orig) + assert_equal(10800, t.to_orig.utc_offset) + end + def test_wrap_block_time assert_equal(Time.utc(2006, 3, 24, 15, 32, 4), TimeOrDateTime.wrap(Time.utc(2006, 3, 24, 15, 32, 3)) {|t| assert_instance_of(TimeOrDateTime, t) @@ -619,4 +642,20 @@ def test_wrap_block_timeordatetime assert_instance_of(TimeOrDateTime, t2) assert_equal(1143214324, t2.to_orig) end + + def test_wrap_block_offset_ignored + assert_equal(Time.utc(2006, 3, 24, 15, 32, 4), TimeOrDateTime.wrap(Time.new(2006, 3, 24, 15, 32, 3, '+03:00')) do |t| + assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), t.to_orig) + assert_equal(0, t.to_orig.utc_offset) + t + 1 + end) + end + + def test_wrap_block_offset_preserved + assert_equal(Time.utc(2006, 3, 24, 15, 32, 4), TimeOrDateTime.wrap(Time.new(2006, 3, 24, 15, 32, 3, '+03:00'), false) do |t| + assert_equal(Time.new(2006, 3, 24, 15, 32, 3, '+03:00'), t.to_orig) + assert_equal(10800, t.to_orig.utc_offset) + t + 1 + end) + end end