1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 1.t'
use warnings;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Duration;
#########################
use Test::More tests=>40;
my $x = 0; # Test Counter
$strf = DateTime::Format::Duration->new(
normalise => 0,
pattern => '%P%F %r',
);
$strf->{diagnostic} = join('',@ARGV) =~ /\!/;
test(
['0000-00-00 00:00:01', '0000-00-00 00:00:01'],
['0000-00-00 00:00:59', '0000-00-00 00:00:59'],
['0000-00-00 00:00:60', '0000-00-00 00:00:60'],
['0000-00-00 00:00:61', '0000-00-00 00:00:61'],
['0000-00-00 00:00:119', '0000-00-00 00:00:119'],
['0000-00-00 00:00:120', '0000-00-00 00:00:120'],
['0000-00-00 00:00:121', '0000-00-00 00:00:121'],
['0000-00-00 00:01:00', '0000-00-00 00:01:00'],
['0000-00-00 00:59:00', '0000-00-00 00:59:00'],
['0000-00-00 00:60:00', '0000-00-00 00:60:00'],
['0000-00-00 00:61:00', '0000-00-00 00:61:00'],
['0000-00-00 00:01:59', '0000-00-00 00:01:59'],
['0000-00-00 00:01:60', '0000-00-00 00:01:60'],
['0000-00-00 00:01:61', '0000-00-00 00:01:61'],
['0000-00-00 00:59:60', '0000-00-00 00:59:60'],
['0000-00-00 00:60:60', '0000-00-00 00:60:60'],
['0000-00-00 01:00:00', '0000-00-00 01:00:00'],
['0000-00-00 23:00:00', '0000-00-00 23:00:00'],
['0000-00-00 24:00:00', '0000-00-00 24:00:00'],
['0000-00-00 25:00:00', '0000-00-00 25:00:00'],
['0000-00-00 01:59:00', '0000-00-00 01:59:00'],
['0000-00-00 01:60:00', '0000-00-00 01:60:00'],
['0000-00-00 01:61:00', '0000-00-00 01:61:00'],
['0000-00-00 23:60:00', '0000-00-00 23:60:00'],
['0000-00-00 24:60:00', '0000-00-00 24:60:00'],
['0000-00-00 00:00:86400', '0000-00-00 00:00:86400'], # Overflows
['0000-00-00 00:1440:00', '0000-00-00 00:1440:00'],
['0000-00-00 240:00:00', '0000-00-00 240:00:00',],
['0000-00-45 00:00:00', '0000-00-45 00:00:00',],
['0000-240-00 00:00:00', '0000-240-00 00:00:00',],
['0000-00-00 00:00:-86400', '0000-00-00 00:00:-86400'], # Underflows
['0000-00-00 00:-1440:00', '0000-00-00 00:-1440:00'],
['0000-00-00 -240:00:00', '0000-00-00 -240:00:00',],
['0000-00--45 00:00:00', '0000-00--45 00:00:00',],
['0000--240-00 00:00:00', '0000--240-00 00:00:00',],
['0000-00-00 00:00:-01', '0000-00-00 00:00:-1'],
['0000-00-00 00:00:-59', '0000-00-00 00:00:-59'],
['0000-00-00 00:00:-60', '0000-00-00 00:00:-60'],
['0000-00-00 00:00:-61', '0000-00-00 00:00:-61'],
['0000-00-00 -01:01:-01', '0000-00-00 -1:01:-1'], # Mixed positivity
);
# ------------------ TESTING ROUTINES -------------------------
sub test {
my @tests = @_;
foreach my $test (@tests) {
my $w = ($x++ < 9) ? 22 : 21;
next unless in_range($x);
diag($test->[2]) if $test->[2];
is(
$strf->format_duration_from_deltas(
$strf->parse_duration_as_deltas(
$test->[0]
)
),
$test->[1],
sprintf("Test %2d: %${w}s should %s %s", $x, $test->[0], ($test->[0] eq $test->[1]) ? 'stay ':'become', $test->[1]) # . (($test->[2]) ? sprintf(" (%s)",$test->[2]) : '')
) or diag( "If you send an error report, please include the output of:\n $^X $0 $x!" );
}
}
sub in_range {
# see if this test is in our list of tests:
return 1 unless $ARGV[0];
my $test = shift;
$argv = join(',', @ARGV);
$argv=~s/,\.\.,/../g;
$argv=~s/,,/,/g;
$argv=~s/\!//;
return 1 if $argv=~/\b$test\b/;
foreach my $part( split(/,\s*/, $argv) ) {
my ($start, $end) = $part =~ /(\d+)\s*\.\.\s*(\d+)/;
($start, $end) = ($end, $start) if $end < $start;
next if $start > $test;
return 1 if $end > $test
}
return 0;
}
sub Dump {
eval{
require Data::Dumper
};
return "<Couldn't load Data::Dumper>" if $@;
return Data::Dumper::Dumper(@_)
}
# Oct 30 (24 hrs) # Oct 31 (25 hrs) # Nov 1 (24 hrs) #
#-----------|-----------#------------|-----------#-----------|-----------#
# 48 Hours == 1 day, 24 hours:
#-----------------------------------------------#
#---- 1 day ------------#------ 24 hours -------#
# 2 Days, -48 Hours == 1 hour:
#------------------- 2 days --------------------#
#------------------ 48 hours ------------------#
# 1 hour
|