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
|
use strict;
use warnings;
use Test::More;
use DateTime::Format::Builder;
my %parsers = (
parsers => {
parse_datetime => {
length => 8,
regex => qr/^abcdef$/,
params => [qw( year month day )],
}
}
);
# Verify constructor (non-)creation
# Ensure we don't build a constructor when one isn't asked for
{
my $class = 'SampleClass1';
eval q[
package SampleClass1;
use DateTime::Format::Builder
constructor => undef,
%parsers;
1;
];
ok( !$@, "No errors when creating the class." );
diag $@ if $@;
{
no strict 'refs';
ok( !( *{"${class}::new"}{CV} ), "There is indeed no 'new'" );
}
my $parser = eval { $class->new() };
ok( $@, "Error when trying to instantiate (no new)" );
like(
$@, qr/^Can't locate object method "new" via package "$class"/,
"Right error"
);
}
# Ensure we don't have people wiping out their constructors
{
my $class = 'SampleClassHasNew';
sub SampleClassHasNew::new { return "4" }
eval q[
package SampleClassHasNew;
use DateTime::Format::Builder
constructor => 1,
%parsers;
1;
];
ok( $@, "Error when creating class." );
}
# Ensure we're not accidentally overriding when we don't itnend to.
{
my $class = 'SampleClassDont';
sub SampleClassDont::new { return "5" }
eval q[
package SampleClassDont;
use DateTime::Format::Builder
constructor => 0,
%parsers;
1;
];
ok( !$@, "No error when creating class." );
diag $@ if $@;
my $parser = eval { $class->new() };
is( $parser => 5, "Didn't override new()" );
}
# Ensure we use the given constructor
{
my $class = 'SampleClassGiven';
eval q[
package SampleClassGiven;
use DateTime::Format::Builder
constructor => sub { return "6" },
%parsers;
1;
];
ok( !$@, "No error when creating class." );
diag $@ if $@;
my $parser = eval { $class->new() };
is( $parser => 6, "Used given new()" );
}
done_testing();
|