This is an automated email from the git hooks/post-receive script. gregoa pushed a commit to annotated tag v1.77 in repository libdatetime-timezone-perl.
commit a53f35945f6e08a75cea77412bd88cefef3fb71e Author: Dave Rolsky <auta...@urth.org> Date: Tue Nov 4 13:27:43 2014 -0600 Use Module::Runtime and Try::Tiny --- Changes | 2 + lib/DateTime/TimeZone.pm | 49 +++++++++--------- lib/DateTime/TimeZone/Local.pm | 23 ++++---- lib/DateTime/TimeZone/Local/Android.pm | 20 ++++--- lib/DateTime/TimeZone/Local/Unix.pm | 56 ++++++++++---------- lib/DateTime/TimeZone/Local/Win32.pm | 12 +++-- t/02basic.t | 46 +++++++++++----- t/04local.t | 84 ++++++++++++++++++++---------- t/06no-dst.t | 21 +++++--- t/07offset-only.t | 15 ++++-- t/09changes.t | 95 ++++++++++++++++++++-------------- t/14invalid-name.t | 25 +++++---- t/17special-names.t | 6 ++- t/18olson-version-check.t | 13 ++--- 14 files changed, 283 insertions(+), 184 deletions(-) diff --git a/Changes b/Changes index a1dc533..0db3e2f 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,8 @@ - Updated the mapping of Windows -> IANA time zones. Patch by David Pinkowitz. RT #10025. +- Replaced Class::Load with Module::Runtime, and replaced eval with Try::Tiny. + 1.76 2014-10-26 diff --git a/lib/DateTime/TimeZone.pm b/lib/DateTime/TimeZone.pm index 0d2fac6..f2d7208 100644 --- a/lib/DateTime/TimeZone.pm +++ b/lib/DateTime/TimeZone.pm @@ -10,7 +10,9 @@ use DateTime::TimeZone::Floating; use DateTime::TimeZone::Local; use DateTime::TimeZone::OffsetOnly; use DateTime::TimeZone::UTC; +use Module::Runtime qw( require_module ); use Params::Validate 0.72 qw( validate validate_pos SCALAR ARRAYREF BOOLEAN ); +use Try::Tiny; use constant INFINITY => 100**1000; use constant NEG_INFINITY => -1 * ( 100**1000 ); @@ -66,17 +68,19 @@ sub new { my $real_class = "DateTime::TimeZone::$subclass"; - die "The timezone '$p{name}' in an invalid name.\n" + die "The timezone '$p{name}' is an invalid name.\n" unless $real_class =~ /^\w+(::\w+)*$/; unless ( $real_class->can('instance') ) { ($real_class) = $real_class =~ m{\A([a-zA-Z0-9_]+(?:::[a-zA-Z0-9_]+)*)\z}; - my $e = do { - local $@; - local $SIG{__DIE__}; - eval "require $real_class"; - $@; + my $e; + try { + local $SIG{__DIE__} = undef; + require_module($real_class); + } + catch { + $e = $_; }; if ($e) { @@ -394,12 +398,13 @@ sub name { $_[0]->{name} } sub category { ( split /\//, $_[0]->{name}, 2 )[0] } sub is_valid_name { - my $tz; - { - local $@; - local $SIG{__DIE__}; - $tz = eval { $_[0]->new( name => $_[1] ) }; - } + my $class = shift; + my $name = shift; + + my $tz = try { + local $SIG{__DIE__} = undef; + $class->new( name => $name ); + }; return $tz && $tz->isa('DateTime::TimeZone') ? 1 : 0; } @@ -434,13 +439,11 @@ sub STORABLE_thaw { # Functions # sub offset_as_seconds { - { - local $@; - local $SIG{__DIE__}; - shift if eval { $_[0]->isa('DateTime::TimeZone') }; - } - my $offset = shift; + $offset = shift if try { + local $SIG{__DIE__} = undef; + $offset->isa('DateTime::TimeZone'); + }; return undef unless defined $offset; @@ -471,13 +474,11 @@ sub offset_as_seconds { } sub offset_as_string { - { - local $@; - local $SIG{__DIE__}; - shift if eval { $_[0]->isa('DateTime::TimeZone') }; - } - my $offset = shift; + $offset = shift if try { + local $SIG{__DIE__} = undef; + $offset->isa('DateTime::TimeZone'); + }; return undef unless defined $offset; return undef unless $offset >= -359999 && $offset <= 359999; diff --git a/lib/DateTime/TimeZone/Local.pm b/lib/DateTime/TimeZone/Local.pm index e61d3b5..62e8b87 100644 --- a/lib/DateTime/TimeZone/Local.pm +++ b/lib/DateTime/TimeZone/Local.pm @@ -3,9 +3,10 @@ package DateTime::TimeZone::Local; use strict; use warnings; -use Class::Load qw( is_class_loaded load_class try_load_class ); use DateTime::TimeZone; use File::Spec; +use Module::Runtime qw( require_module ); +use Try::Tiny; sub TimeZone { my $class = shift; @@ -44,13 +45,16 @@ sub TimeZone { my $os_name = $subclass{$^O} || $^O; my $subclass = $class . '::' . $os_name; - return $subclass if is_class_loaded($subclass); + return $subclass if $subclass->can('Methods'); - return $subclass if try_load_class($subclass); + return $subclass if try { + local $SIG{__DIE__} = undef; + require_module($subclass); + }; $subclass = $class . '::Unix'; - load_class($subclass); + require_module($subclass); return $subclass; } @@ -61,12 +65,11 @@ sub FromEnv { foreach my $var ( $class->EnvVars() ) { if ( $class->_IsValidName( $ENV{$var} ) ) { - my $tz; - { - local $@; - local $SIG{__DIE__}; - $tz = eval { DateTime::TimeZone->new( name => $ENV{$var} ) }; - } + my $tz = try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $ENV{$var} ); + }; + return $tz if $tz; } } diff --git a/lib/DateTime/TimeZone/Local/Android.pm b/lib/DateTime/TimeZone/Local/Android.pm index 0365712..5e6bd0b 100644 --- a/lib/DateTime/TimeZone/Local/Android.pm +++ b/lib/DateTime/TimeZone/Local/Android.pm @@ -3,6 +3,8 @@ package DateTime::TimeZone::Local::Android; use strict; use warnings; +use Try::Tiny; + use parent 'DateTime::TimeZone::Local'; sub Methods { @@ -19,23 +21,19 @@ sub EnvVars { return 'TZ' } sub FromGetProp { my $name = `getprop persist.sys.timezone`; chomp $name; - my $tz; - { - local $@; - local $SIG{__DIE__}; - $tz = eval { DateTime::TimeZone->new( name => $name ) }; - } + my $tz = try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $name ); + }; return $tz if $tz; } # See the link above. Android always defaults to UTC sub FromDefault { - my $tz; - { - local $@; - local $SIG{__DIE__}; - $tz = eval { DateTime::TimeZone->new( name => 'UTC' ) }; + my $tz = try { + local $SIG{__DIE__} = undef; + $tz = DateTime::TimeZone->new( name => 'UTC' ); } return $tz if $tz; diff --git a/lib/DateTime/TimeZone/Local/Unix.pm b/lib/DateTime/TimeZone/Local/Unix.pm index 0f9beb1..e86a016 100644 --- a/lib/DateTime/TimeZone/Local/Unix.pm +++ b/lib/DateTime/TimeZone/Local/Unix.pm @@ -4,6 +4,8 @@ use strict; use warnings; use Cwd 3; +use Try::Tiny; + use parent 'DateTime::TimeZone::Local'; sub Methods { @@ -54,12 +56,10 @@ sub FromEtcLocaltime { : $parts[$x] ); - my $tz; - { - local $@; - local $SIG{__DIE__}; - $tz = eval { DateTime::TimeZone->new( name => $name ) }; - } + my $tz = try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $name ); + }; return $tz if $tz; } @@ -89,10 +89,10 @@ sub _FindMatchingZoneinfoFile { my $size = -s $file_to_match; my $real_name; - local $@; - local $SIG{__DIE__}; - local $_; - eval { + try { + local $SIG{__DIE__} = undef; + local $_; + File::Find::find( { wanted => sub { @@ -119,12 +119,12 @@ sub _FindMatchingZoneinfoFile { }, $ZoneinfoDir, ); + } + catch { + die $_ unless ref $_ && $_->{found}; }; - if ($@) { - return $real_name if ref $@ && $@->{found}; - die $@; - } + return $real_name; } sub FromEtcTimezone { @@ -142,9 +142,10 @@ sub FromEtcTimezone { return unless $class->_IsValidName($name); - local $@; - local $SIG{__DIE__}; - return eval { DateTime::TimeZone->new( name => $name ) }; + return try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $name ); + }; } sub FromEtcTIMEZONE { @@ -168,9 +169,10 @@ sub FromEtcTIMEZONE { return unless $class->_IsValidName($name); - local $@; - local $SIG{__DIE__}; - return eval { DateTime::TimeZone->new( name => $name ) }; + return try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $name ); + }; } # RedHat uses this @@ -184,9 +186,10 @@ sub FromEtcSysconfigClock { return unless $class->_IsValidName($name); - local $@; - local $SIG{__DIE__}; - return eval { DateTime::TimeZone->new( name => $name ) }; + return try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $name ); + }; } # this is a separate function so that it can be overridden in the test suite @@ -213,9 +216,10 @@ sub FromEtcDefaultInit { return unless $class->_IsValidName($name); - local $@; - local $SIG{__DIE__}; - return eval { DateTime::TimeZone->new( name => $name ) }; + return try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $name ); + }; } # this is a separate function so that it can be overridden in the test diff --git a/lib/DateTime/TimeZone/Local/Win32.pm b/lib/DateTime/TimeZone/Local/Win32.pm index 481ed60..10cba35 100644 --- a/lib/DateTime/TimeZone/Local/Win32.pm +++ b/lib/DateTime/TimeZone/Local/Win32.pm @@ -3,10 +3,11 @@ package DateTime::TimeZone::Local::Win32; use strict; use warnings; -use parent 'DateTime::TimeZone::Local'; - +use Try::Tiny; use Win32::TieRegistry ( 'KEY_READ', Delimiter => q{/} ); +use parent 'DateTime::TimeZone::Local'; + sub Methods { return qw( FromEnv FromRegistry ) } sub EnvVars { return 'TZ' } @@ -219,9 +220,10 @@ sub EnvVars { return 'TZ' } return unless $class->_IsValidName($olson); - local $@; - local $SIG{__DIE__}; - return eval { DateTime::TimeZone->new( name => $olson ) }; + return try { + local $SIG{__DIE__} = undef; + DateTime::TimeZone->new( name => $olson ); + }; } } diff --git a/t/02basic.t b/t/02basic.t index f025ced..da272f9 100644 --- a/t/02basic.t +++ b/t/02basic.t @@ -1,8 +1,11 @@ use strict; use warnings; -use File::Spec; use Test::More; +use Test::Fatal; + +use File::Spec; +use Try::Tiny; use lib File::Spec->catdir( File::Spec->curdir, 't' ); @@ -31,18 +34,31 @@ foreach my $name (@names) { # finish, and it uses up lots of memory too. if ($is_maintainer) { my $dt; - eval { $dt = DateTime->now( time_zone => $name ) }; - is( $@, '', "Can call DateTime->now with $name" ); - eval { $dt->add( years => 50 ) }; - is( $@, '', "Can add 50 years with $name" ); - eval { $dt->subtract( years => 400 ) }; - is( $@, '', "Can subtract 400 years with $name" ); - eval { + is( + exception { $dt = DateTime->now( time_zone => $name ) }, + undef, + "Can call DateTime->now with $name" + ); + + is( + exception { $dt->add( years => 50 ) }, + undef, + "Can add 50 years with $name" + ); + + is( + exception { $dt->subtract( years => 400 ) }, + undef, + "Can subtract 400 years with $name" + ); + + try { $dt = DateTime->new( year => 2000, month => 6, hour => 1, time_zone => $name ); }; is( $dt->hour, 1, 'make sure that local time is always respected' ); - eval { + + try { $dt = DateTime->new( year => 2000, month => 12, hour => 1, time_zone => $name ); }; @@ -123,7 +139,7 @@ my $tz = DateTime::TimeZone->new( name => 'America/Chicago' ); # max year my $tz = DateTime::TimeZone->new( name => 'America/Los_Angeles' ); - my $dt = eval { + my $dt = try { DateTime->new( year => $tz->{max_year} + 1, month => 5, @@ -189,8 +205,14 @@ my $tz = DateTime::TimeZone->new( name => 'America/Chicago' ); # before the LOCAL_END. my $dt = DateTime->from_object( object => TestHack->new ); - eval { $dt->set_time_zone('UTC')->set_time_zone('Australia/Sydney') }; - ok( !$@, 'should be able to set time zone' ); + is( + exception { + $dt->set_time_zone('UTC')->set_time_zone('Australia/Sydney') + }, + undef, + 'should be able to set time zone without error' + ); + ok( $dt->is_dst, 'is_dst should be true' ); } diff --git a/t/04local.t b/t/04local.t index 53ffabc..ced590b 100644 --- a/t/04local.t +++ b/t/04local.t @@ -9,7 +9,10 @@ use File::Spec::Functions qw( catdir catfile curdir ); use File::Path qw( mkpath ); use File::Temp qw( tempdir ); use Sys::Hostname qw( hostname ); +use Try::Tiny; + use Test::More; +use Test::Fatal; use lib catdir( curdir(), 't' ); @@ -25,7 +28,7 @@ DateTime::TimeZone::Local->_load_subclass() =~ /Unix$/ my $IsMaintainer = hostname() =~ /houseabsolute|quasar/ && -d '.hg'; my $CanWriteEtcLocaltime = -w '/etc/localtime' && -l '/etc/localtime'; -my $CanSymlink = eval { symlink q{}, q{}; 1 }; +my $CanSymlink = try { symlink q{}, q{}; 1 }; my ($TestFile) = abs_path($0) =~ /(.+)/; local $ENV{TZ} = undef; @@ -35,7 +38,7 @@ local $ENV{TZ} = undef; for my $alias ( sort keys %{ DateTime::TimeZone::links() } ) { local $ENV{TZ} = $alias; - my $tz = eval { DateTime::TimeZone::Local->TimeZone() }; + my $tz = try { DateTime::TimeZone::Local->TimeZone() }; is( $tz->name(), $links{$alias}, "$alias in \$ENV{TZ} for Local->TimeZone()" @@ -46,7 +49,7 @@ local $ENV{TZ} = undef; { for my $name ( sort DateTime::TimeZone::all_names() ) { local $ENV{TZ} = $name; - my $tz = eval { DateTime::TimeZone::Local->TimeZone() }; + my $tz = try { DateTime::TimeZone::Local->TimeZone() }; is( $tz->name(), $name, "$name in \$ENV{TZ} for Local->TimeZone()" @@ -82,7 +85,7 @@ local $ENV{TZ} = undef; ); local $ENV{TZ} = 0; - $tz = eval { DateTime::TimeZone::Local->TimeZone() }; + $tz = try { DateTime::TimeZone::Local->TimeZone() }; is( $tz->name(), 'UTC', "\$ENV{TZ} set to 0 returns UTC" @@ -107,9 +110,10 @@ local $ENV{TZ} = undef; { local $^O = 'DoesNotExist'; my @err; - local $SIG{__DIE__} = sub { push @err, shift }; - - eval { DateTime::TimeZone::Local->_load_subclass() }; + try { + local $SIG{__DIE__} = sub { push @err, shift }; + DateTime::TimeZone::Local->_load_subclass(); + }; is_deeply( \@err, [], @@ -134,9 +138,11 @@ SKIP: = sub {'/usr/share/zoneinfo/US/Eastern'}; my $tz; - eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() }; is( - $@, q{}, + exception { + $tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() + }, + undef, 'valid time zone name in /etc/localtime symlink should not die' ); is( @@ -158,9 +164,11 @@ SKIP: local *DateTime::TimeZone::Local::Unix::_FindMatchingZoneinfoFile = sub {'America/Los_Angeles'}; - eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() }; is( - $@, q{}, + exception { + $tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() + }, + undef, 'fall back to _FindMatchZoneinfoFile if _Readlink finds nothing' ); is( @@ -182,11 +190,14 @@ SKIP: = sub {'US/Eastern'}; my $tz; - eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcSysconfigClock() }; is( - $@, q{}, + exception { + $tz = DateTime::TimeZone::Local::Unix->FromEtcSysconfigClock() + }, + undef, 'valid time zone name in /etc/sysconfig/clock should not die' ); + is( $tz->name(), 'America/New_York', 'FromEtcSysConfigClock() with _ReadEtcSysconfigClock returning US/Eastern' @@ -206,8 +217,14 @@ SKIP: = sub {'Asia/Tokyo'}; my $tz; - eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcDefaultInit() }; - is( $@, q{}, 'valid time zone name in /etc/default/init should not die' ); + is( + exception { + $tz = DateTime::TimeZone::Local::Unix->FromEtcDefaultInit() + }, + undef, + 'valid time zone name in /etc/default/init should not die' + ); + is( $tz->name(), 'Asia/Tokyo', 'FromEtcDefaultInit with _ReadEtcDefaultInit returning Asia/Tokyo' @@ -240,9 +257,11 @@ SKIP: symlink $tz_file => catfile( $etc_dir, 'localtime' ); my $tz; - eval { $tz = DateTime::TimeZone::Local->TimeZone() }; - is( $@, q{}, - 'valid time zone name in /etc/localtime should not die' ); + is( + exception { $tz = DateTime::TimeZone::Local->TimeZone() }, + undef, + 'valid time zone name in /etc/localtime should not die' + ); is( $tz->name(), 'America/Chicago', '/etc/localtime should link to America/Chicago' @@ -259,8 +278,11 @@ SKIP: = sub {undef}; my $tz; - eval { $tz = DateTime::TimeZone::Local->TimeZone() }; - is( $@, q{}, 'valid time zone name in /etc/timezone should not die' ); + is( + exception { $tz = DateTime::TimeZone::Local->TimeZone() }, + undef, + 'valid time zone name in /etc/timezone should not die' + ); is( $tz->name(), 'America/Chicago', '/etc/timezone should contain America/Chicago' @@ -290,8 +312,11 @@ SKIP: local *DateTime::TimeZone::Local::Unix::FromEtcTIMEZONE = sub {undef}; my $tz; - eval { $tz = DateTime::TimeZone::Local->TimeZone() }; - is( $@, q{}, '/etc/default/init contains TZ=Australia/Melbourne' ); + is( + exception { $tz = DateTime::TimeZone::Local->TimeZone() }, + undef, + '/etc/default/init contains TZ=Australia/Melbourne' + ); is( $tz->name(), 'Australia/Melbourne', '/etc/default/init should contain Australia/Melbourne' @@ -357,8 +382,11 @@ SKIP: my $cwd = cwd(); my $tz; - eval { $tz = DateTime::TimeZone::Local->TimeZone() }; - is( $@, q{}, 'copy of zoneinfo file at /etc/localtime' ); + is( + exception { $tz = DateTime::TimeZone::Local->TimeZone() }, + undef, + 'copy of zoneinfo file at /etc/localtime' + ); is( $tz->name(), 'Asia/Kolkata', '/etc/localtime should be a copy of Asia/Kolkata' @@ -381,7 +409,11 @@ SKIP: local $SIG{__DIE__} = sub { die 'haha'; }; my $tz; - eval { $tz = DateTime::TimeZone::Local->TimeZone() }; + is( + exception { $tz = DateTime::TimeZone::Local->TimeZone() }, + undef, + 'no exception from DateTime::Time::Local->TimeZone' + ); is( $tz->name(), 'Asia/Kolkata', 'a __DIE__ handler did not interfere with our use of File::Find' @@ -391,7 +423,7 @@ SKIP: { local $ENV{TZ} = 'Australia/Melbourne'; - my $tz = eval { DateTime::TimeZone->new( name => 'local' ) }; + my $tz = try { DateTime::TimeZone->new( name => 'local' ) }; is( $tz->name(), 'Australia/Melbourne', q|DT::TZ->new( name => 'local' )| diff --git a/t/06no-dst.t b/t/06no-dst.t index e89de74..dbf93f2 100644 --- a/t/06no-dst.t +++ b/t/06no-dst.t @@ -1,8 +1,11 @@ use strict; use warnings; -use File::Spec; use Test::More; +use Test::Fatal; + +use File::Spec; +use Try::Tiny; use lib File::Spec->catdir( File::Spec->curdir, 't' ); @@ -10,11 +13,17 @@ BEGIN { require 'check_datetime_version.pl' } { my $dt = DateTime->now; - eval { $dt->set_time_zone('Pacific/Tarawa') }; - is( $@, '', "time zone without dst change works" ); + is( + exception { $dt->set_time_zone('Pacific/Tarawa') }, + undef, + 'time zone without dst change works' + ); - eval { $dt->set_time_zone('Asia/Dhaka') }; - is( $@, '', "time zone without dst change works (again)" ); + is( + exception { $dt->set_time_zone('Asia/Dhaka') }, + undef, + 'time zone without dst change works (again)' + ); } # This tests a bug that happened when a time zone has a final rule @@ -38,7 +47,7 @@ BEGIN { require 'check_datetime_version.pl' } [ 11, 29 ], [ 11, 30 ], ) { - my $dt = eval { + my $dt = try { DateTime->new( year => 2007, month => 12, day => 9, hour => $hm->[0], minute => $hm->[1], diff --git a/t/07offset-only.t b/t/07offset-only.t index 41d16b1..f74f37f 100644 --- a/t/07offset-only.t +++ b/t/07offset-only.t @@ -1,16 +1,18 @@ use strict; use warnings; -use File::Spec; use Test::More; +use Test::Fatal; + +use File::Spec; use lib File::Spec->catdir( File::Spec->curdir, 't' ); BEGIN { require 'check_datetime_version.pl' } -eval { DateTime::TimeZone::OffsetOnly->new( offset => 'bad' ) }; is( - $@, "Invalid offset: bad\n", + exception { DateTime::TimeZone::OffsetOnly->new( offset => 'bad' ) }, + "Invalid offset: bad\n", 'test that OffsetOnly does not allow invalid offsets' ); @@ -106,8 +108,11 @@ foreach (@good_offsets) { } foreach (@bad_offsets) { - eval { DateTime::TimeZone::OffsetOnly->new( offset => $_ ) }; - like( $@, qr/Invalid offset/, "$_ is invalid" ); + like( + exception { DateTime::TimeZone::OffsetOnly->new( offset => $_ ) }, + qr/Invalid offset/, + "$_ is invalid" + ); } done_testing(); diff --git a/t/09changes.t b/t/09changes.t index 8168041..6e68fa4 100644 --- a/t/09changes.t +++ b/t/09changes.t @@ -1,8 +1,10 @@ use strict; use warnings; -use File::Spec; use Test::More; +use Test::Fatal; + +use File::Spec; use lib File::Spec->catdir( File::Spec->curdir, 't' ); @@ -151,7 +153,6 @@ BEGIN { require 'check_datetime_version.pl' } # same tests without using UTC as intermediate { - # wrapped in eval because if change data is buggy it can throw exception my $dt = DateTime->new( year => 1982, month => 3, day => 28, hour => 1, minute => 59, @@ -325,34 +326,42 @@ BEGIN { require 'check_datetime_version.pl' } } { - eval { - DateTime->new( - year => 2003, month => 4, day => 6, - hour => 2, time_zone => 'America/Chicago', - ); - }; - - like( $@, qr/Invalid local time .+/, 'exception for invalid time' ); + like( + exception { + DateTime->new( + year => 2003, month => 4, day => 6, + hour => 2, time_zone => 'America/Chicago', + ); + }, + qr/Invalid local time .+/, + 'exception for invalid time' + ); - eval { - DateTime->new( - year => 2003, month => 4, day => 6, - hour => 2, minute => 59, second => 59, - time_zone => 'America/Chicago', - ); - }; - like( $@, qr/Invalid local time .+/, 'exception for invalid time' ); + like( + exception { + DateTime->new( + year => 2003, month => 4, day => 6, + hour => 2, minute => 59, second => 59, + time_zone => 'America/Chicago', + ); + }, + qr/Invalid local time .+/, + 'exception for invalid time' + ); } { - eval { - DateTime->new( - year => 2003, month => 4, day => 6, - hour => 1, minute => 59, second => 59, - time_zone => 'America/Chicago', - ); - }; - ok( !$@, 'no exception for valid time' ); + is( + exception { + DateTime->new( + year => 2003, month => 4, day => 6, + hour => 1, minute => 59, second => 59, + time_zone => 'America/Chicago', + ); + }, + undef, + 'no exception for valid time' + ); SKIP: { @@ -367,9 +376,11 @@ SKIP: time_zone => 'America/Chicago', ); - eval { $dt->add( days => 1 ) }; - like( $@, qr/Invalid local time .+/, - 'exception for invalid time produced via add' ); + like( + exception { $dt->add( days => 1 ) }, + qr/Invalid local time .+/, + 'exception for invalid time produced via add' + ); } } @@ -379,8 +390,11 @@ SKIP: hour => 2, time_zone => 'America/Chicago', ); - eval { $dt->add( hours => 24 ) }; - ok( !$@, 'add 24 hours should work even if add 1 day does not' ); + is( + exception { $dt->add( hours => 24 ) }, + undef, + 'add 24 hours should work even if add 1 day does not' + ); is( $dt->hour, 3, "hour should no be 3" ); } @@ -429,14 +443,17 @@ SKIP: } { - eval { - DateTime->new( - year => 2040, month => 3, day => 11, - hour => 2, minute => 59, second => 59, - time_zone => 'America/Chicago', - ); - }; - like( $@, qr/Invalid local time .+/, 'exception for invalid time' ); + like( + exception { + DateTime->new( + year => 2040, month => 3, day => 11, + hour => 2, minute => 59, second => 59, + time_zone => 'America/Chicago', + ); + }, + qr/Invalid local time .+/, + 'exception for invalid time' + ); } { diff --git a/t/14invalid-name.t b/t/14invalid-name.t index b27cdfd..36a4d1a 100644 --- a/t/14invalid-name.t +++ b/t/14invalid-name.t @@ -1,25 +1,32 @@ use strict; use warnings; -use File::Spec; use Test::More; +use Test::Fatal; + +use File::Spec; use lib File::Spec->catdir( File::Spec->curdir, 't' ); BEGIN { require 'check_datetime_version.pl' } { - my $tz = eval { - DateTime::TimeZone->new( - name => 'America/Chicago; print "hello, world\n";' ); - }; - like( $@, qr/invalid name/, - 'make sure potentially malicious code cannot sneak into eval' ); + like( + exception { + DateTime::TimeZone->new( + name => 'America/Chicago; print "hello, world\n";' ); + }, + qr/invalid name/, + 'make sure potentially malicious code cannot sneak into eval' + ); } { - my $tz = eval { DateTime::TimeZone->new( name => 'Bad/Name' ) }; - like( $@, qr/invalid name/, 'make sure bad names are reported' ); + like( + exception { DateTime::TimeZone->new( name => 'Bad/Name' ) }, + qr/invalid name/, + 'make sure bad names are reported' + ); } done_testing(); diff --git a/t/17special-names.t b/t/17special-names.t index c665489..d05dd3a 100644 --- a/t/17special-names.t +++ b/t/17special-names.t @@ -1,16 +1,18 @@ use strict; use warnings; -use File::Spec; use Test::More; +use File::Spec; +use Try::Tiny; + use lib File::Spec->catdir( File::Spec->curdir, 't' ); BEGIN { require 'check_datetime_version.pl' } for my $name ( qw( EST MST HST CET EET MET WET EST5EDT CST6CDT MST7MDT PST8PDT )) { - my $tz = eval { DateTime::TimeZone->new( name => $name ) }; + my $tz = try { DateTime::TimeZone->new( name => $name ) }; ok( $tz, "got a timezone for name => $name" ); } diff --git a/t/18olson-version-check.t b/t/18olson-version-check.t index 9bdf706..84386f1 100644 --- a/t/18olson-version-check.t +++ b/t/18olson-version-check.t @@ -1,19 +1,14 @@ use strict; use warnings; -use File::Spec; use Test::More; +use Test::Requires qw( Test::Output ); -use lib File::Spec->catdir( File::Spec->curdir, 't' ); +use File::Spec; -BEGIN { - require 'check_datetime_version.pl'; +use lib File::Spec->catdir( File::Spec->curdir, 't' ); - eval { require Test::Output }; - if ($@) { - plan skip_all => 'These tests require Test::Output.'; - } -} +BEGIN { require 'check_datetime_version.pl' } { Test::Output::stderr_like( -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libdatetime-timezone-perl.git _______________________________________________ Pkg-perl-cvs-commits mailing list Pkg-perl-cvs-commits@lists.alioth.debian.org http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits