1#!/usr/bin/env perl 2 3# Test the date-parsing facilities provided by the DateTime::Format::ISO8601 4# module, if available 5 6use strict; 7use warnings; 8 9use Module::Load; 10use Test::More; 11 12use RPC::XML; 13 14my ($obj, @values, $formatter); 15 16my $datetime_format_iso8601_avail = eval { load DateTime::Format::ISO8601; 1; }; 17# Do not run this suite if the package is not available 18if (! $datetime_format_iso8601_avail) 19{ 20 plan skip_all => 'DateTime::Format::ISO8601 not available'; 21} 22 23# Otherwise, we have to calculate our tests from the content after __DATA__: 24while (defined(my $line = <DATA>)) 25{ 26 next if ($line =~ /^#/); 27 chomp $line; 28 next if (! $line); 29 push @values, [ split /[|]/, $line ]; 30} 31 32plan tests => (scalar(@values) * 2); 33 34# Create a formatter from the DateTime::Format::ISO8601 package, we'll use it 35# to determine what the constructor *should* return: 36$formatter = DateTime::Format::ISO8601->new(); 37 38for my $test (0 .. $#values) 39{ 40 my ($input, $is_error) = @{$values[$test]}; 41 42 $obj = RPC::XML::datetime_iso8601->new($input); 43 if (! $is_error) 44 { 45 my $match = $formatter->parse_datetime($input); 46 $match =~ s/-//g; 47 48 isa_ok($obj, 'RPC::XML::datetime_iso8601', "Input $test \$obj"); 49 is($obj->value, $match, "Input '$input' yielded correct value"); 50 } 51 else 52 { 53 ok(! ref($obj), "Input $test yielded no object"); 54 like($RPC::XML::ERROR, qr/Malformed data [(]$input[)]/, 55 "Input '$input' yielded correct error message"); 56 } 57} 58 59exit 0; 60 61__DATA__ 62# Format is: 63# <Input value>|<Error if set> 64# 65# If the second field is non-blank, then the input should yield an error 66# 67# I am skipping some of the sillier formats, as I don't care if people use them 68# and get unexpected results. Caveat Programmer, and all that... 6920110820 702011-08-20 712011-08 722011 73110820 7411-08-20 75-1108 76-11-08 77--0820 78--08-20 79--08 80---20 812011232 822011-232 8311232 8411-232 85-232 862011W336 872011-W33-6 882011W33 892011-W33 9011W336 9111-W33-6 9211W33 9311-W33 94-1W336 95-1-W33-6 96-1W33 97-1-W33 98-W336 99-W33-6 10017:55:55 10117:55 102175555,50 10317:55:55,50 104175555.50 1051755.50 10617:55.50 10717.50 108-55:00 109-5500,50 110-55.50 111--00.0 112175555Z 11317:55:55Z 1141755Z 11517:55Z 11617Z 117175555.0Z 11817:55:55.0Z 119175555-0700 12017:55:55-07:00 121175555-07 12217:55:55-07 123175555.0-0700 12417:55:55.0-07:00 12517,01|bad 12620110820175555|bad 127