2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2013-03-26 10:55:04 -04:00
|
|
|
require_relative 'helper'
|
2010-07-02 20:52:43 -04:00
|
|
|
require 'date'
|
|
|
|
|
|
|
|
module Psych
|
|
|
|
class TestDateTime < TestCase
|
2013-11-26 16:41:48 -05:00
|
|
|
def test_negative_year
|
2017-04-05 09:16:32 -04:00
|
|
|
time = Time.utc(-1, 12, 16)
|
2013-11-26 16:41:48 -05:00
|
|
|
assert_cycle time
|
|
|
|
end
|
|
|
|
|
2017-06-16 03:02:22 -04:00
|
|
|
def test_usec
|
|
|
|
time = Time.utc(2017, 4, 13, 12, 0, 0, 5)
|
|
|
|
assert_cycle time
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_non_utc
|
|
|
|
time = Time.new(2017, 4, 13, 12, 0, 0.5, "+09:00")
|
|
|
|
assert_cycle time
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_timezone_offset
|
|
|
|
times = [Time.new(2017, 4, 13, 12, 0, 0, "+09:00"),
|
|
|
|
Time.new(2017, 4, 13, 12, 0, 0, "-05:00")]
|
|
|
|
cycled = Psych::load(Psych.dump times)
|
|
|
|
assert_match(/12:00:00 \+0900/, cycled.first.to_s)
|
|
|
|
assert_match(/12:00:00 -0500/, cycled.last.to_s)
|
|
|
|
end
|
|
|
|
|
2013-11-26 16:41:48 -05:00
|
|
|
def test_new_datetime
|
|
|
|
assert_cycle DateTime.new
|
|
|
|
end
|
|
|
|
|
2017-06-16 03:02:22 -04:00
|
|
|
def test_datetime_non_utc
|
|
|
|
dt = DateTime.new(2017, 4, 13, 12, 0, 0.5, "+09:00")
|
|
|
|
assert_cycle dt
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_datetime_timezone_offset
|
|
|
|
times = [DateTime.new(2017, 4, 13, 12, 0, 0, "+09:00"),
|
|
|
|
DateTime.new(2017, 4, 13, 12, 0, 0, "-05:00")]
|
|
|
|
cycled = Psych::load(Psych.dump times)
|
|
|
|
assert_match(/12:00:00\+09:00/, cycled.first.to_s)
|
|
|
|
assert_match(/12:00:00-05:00/, cycled.last.to_s)
|
|
|
|
end
|
|
|
|
|
2013-11-26 14:48:03 -05:00
|
|
|
def test_invalid_date
|
|
|
|
assert_cycle "2013-10-31T10:40:07-000000000000033"
|
|
|
|
end
|
|
|
|
|
2010-07-02 20:52:43 -04:00
|
|
|
def test_string_tag
|
|
|
|
dt = DateTime.now
|
|
|
|
yaml = Psych.dump dt
|
|
|
|
assert_match(/DateTime/, yaml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_round_trip
|
|
|
|
dt = DateTime.now
|
|
|
|
assert_cycle dt
|
|
|
|
end
|
2013-07-05 15:51:12 -04:00
|
|
|
|
|
|
|
def test_alias_with_time
|
|
|
|
t = Time.now
|
|
|
|
h = {:a => t, :b => t}
|
|
|
|
yaml = Psych.dump h
|
|
|
|
assert_match('&', yaml)
|
|
|
|
assert_match('*', yaml)
|
|
|
|
end
|
2010-07-02 20:52:43 -04:00
|
|
|
end
|
|
|
|
end
|