ruby-changes:53079
From: nobu <ko1@a...>
Date: Mon, 22 Oct 2018 08:53:43 +0900 (JST)
Subject: [ruby-changes:53079] nobu:r65293 (trunk): test_time_tz.rb: refactor dummy TZ tests and add another timezone
nobu 2018-10-22 08:53:39 +0900 (Mon, 22 Oct 2018) New Revision: 65293 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65293 Log: test_time_tz.rb: refactor dummy TZ tests and add another timezone Modified files: trunk/test/ruby/test_time_tz.rb Index: test/ruby/test_time_tz.rb =================================================================== --- test/ruby/test_time_tz.rb (revision 65292) +++ test/ruby/test_time_tz.rb (revision 65293) @@ -508,76 +508,77 @@ End https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time_tz.rb#L508 end module TestTimeTZ::WithTZ - def tz - @tz ||= TestTimeTZ::TZ.new(tzname, abbr, utc_offset) - end - - def test_new - t = Time.new(2018, 9, 1, 12, 0, 0, tz) + def subtest_new(time_class, tz, tzname, abbr, utc_offset) + t = time_class.new(2018, 9, 1, 12, 0, 0, tz) assert_equal([2018, 9, 1, 12, 0, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone]) h, m = (-utc_offset / 60).divmod(60) - assert_equal(Time.utc(2018, 9, 1, 12+h, m, 0).to_i, t.to_i) + assert_equal(time_class.utc(2018, 9, 1, 12+h, m, 0).to_i, t.to_i) end - def test_getlocal - t = Time.utc(2018, 9, 1, 12, 0, 0).getlocal(tz) + def subtest_getlocal(time_class, tz, tzname, abbr, utc_offset) + t = time_class.utc(2018, 9, 1, 12, 0, 0).getlocal(tz) h, m = (utc_offset / 60).divmod(60) assert_equal([2018, 9, 1, 12+h, m, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone]) - assert_equal(Time.utc(2018, 9, 1, 12, 0, 0), t) + assert_equal(time_class.utc(2018, 9, 1, 12, 0, 0), t) end - def test_strftime - t = Time.new(2018, 9, 1, 12, 0, 0, tz) + def subtest_strftime(time_class, tz, tzname, abbr, utc_offset) + t = time_class.new(2018, 9, 1, 12, 0, 0, tz) h, m = (utc_offset.abs / 60).divmod(60) h = -h if utc_offset < 0 assert_equal("%+.2d%.2d %s" % [h, m, abbr], t.strftime("%z %Z")) end - def test_plus - t = Time.new(2018, 9, 1, 12, 0, 0, tz) + 4000 + def subtest_plus(time_class, tz, tzname, abbr, utc_offset) + t = time_class.new(2018, 9, 1, 12, 0, 0, tz) + 4000 assert_equal([2018, 9, 1, 13, 6, 40, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone]) m, s = (4000-utc_offset).divmod(60) h, m = m.divmod(60) - assert_equal(Time.utc(2018, 9, 1, 12+h, m, s), t) + assert_equal(time_class.utc(2018, 9, 1, 12+h, m, s), t) end - def test_marshal - t = Time.new(2018, 9, 1, 12, 0, 0, tz) + def subtest_marshal(time_class, tz, tzname, abbr, utc_offset) + t = time_class.new(2018, 9, 1, 12, 0, 0, tz) t2 = Marshal.load(Marshal.dump(t)) assert_equal(t, t2) assert_equal(t.utc_offset, t2.utc_offset) assert_equal(t.utc_offset, (t2+1).utc_offset) end -end -class TestTimeTZ::JST < Test::Unit::TestCase - include TestTimeTZ::WithTZ - - def tzname - "Asia/Tokyo" - end - - def abbr - "JST" - end - - def utc_offset - +9*3600 + ZONES = { + "Asia/Tokyo" => ["JST", +9*3600], + "America/Los_Angeles" => ["PDT", -7*3600], + "Africa/Ndjamena" => ["WAT", +1*3600], + } + + def make_timezone(tzname, abbr, utc_offset) + self.class::TIME_CLASS.find_timezone(tzname) + end + + instance_methods(false).grep(/\Asub(?=test_)/) do |subtest| + test = $' + ZONES.each_pair do |tzname, (abbr, utc_offset)| + define_method("#{test}@#{tzname}") do + tz = make_timezone(tzname, abbr, utc_offset) + time_class = self.class::TIME_CLASS + __send__(subtest, time_class, tz, tzname, abbr, utc_offset) + end + end end end -class TestTimeTZ::EDT < Test::Unit::TestCase +class TestTimeTZ::DummyTZ < Test::Unit::TestCase include TestTimeTZ::WithTZ - def tzname - "America/New_York" - end - - def abbr - "EDT" + class TIME_CLASS < ::Time + ZONES = TestTimeTZ::WithTZ::ZONES + def self.find_timezone(tzname) + tz = ZONES[tzname] or raise ArgumentError, "Unknown timezone: #{name}" + TestTimeTZ::TZ.new(tzname, *tz) + end end - def utc_offset - -4*3600 + def self.make_timezone(tzname, abbr, utc_offset) + TestTimeTZ::TZ.new(tzname, abbr, utc_offset) end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/