1use strict;
2
3use Test::More tests => 9;
4
5use DateTime::Format::Builder;
6
7
8my %parsers = (
9    parsers => {
10	parse_datetime =>
11	{
12	    length => 8,
13	    regex => qr/^abcdef$/,
14	    params => [qw( year month day )],
15	}
16    }
17);
18
19# Verify constructor (non-)creation
20
21# Ensure we don't build a constructor when one isn't asked for
22{
23    my $class = 'SampleClass1';
24    eval q[
25	package SampleClass1;
26	use DateTime::Format::Builder
27	    constructor => undef,
28	    %parsers;
29	1;
30    ];
31    ok( !$@, "No errors when creating the class." );
32
33    diag $@ if $@;
34
35    {
36	no strict 'refs';
37	ok( !( *{"${class}::new"}{CV}), "There is indeed no 'new'" );
38    }
39
40    my $parser = eval { $class->new() };
41    ok( $@, "Error when trying to instantiate (no new)");
42    like( $@, qr/^Can't locate object method "new" via package "$class"/, "Right error" );
43}
44
45# Ensure we don't have people wiping out their constructors
46{
47    my $class = 'SampleClassHasNew';
48    sub SampleClassHasNew::new { return "4" }
49    eval q[
50	package SampleClassHasNew;
51	use DateTime::Format::Builder
52	    constructor => 1,
53	    %parsers;
54	1;
55    ];
56    ok( $@, "Error when creating class." );
57}
58
59# Ensure we're not accidentally overriding when we don't itnend to.
60{
61    my $class = 'SampleClassDont';
62    sub SampleClassDont::new { return "5" }
63    eval q[
64	package SampleClassDont;
65	use DateTime::Format::Builder
66	    constructor => 0,
67	    %parsers;
68	1;
69    ];
70    ok( !$@, "No error when creating class." );
71    diag $@ if $@;
72
73    my $parser = eval { $class->new()  };
74    is( $parser => 5, "Didn't override new()" );
75}
76
77# Ensure we use the given constructor 
78{
79    my $class = 'SampleClassGiven';
80    eval q[
81	package SampleClassGiven;
82	use DateTime::Format::Builder
83	    constructor => sub { return "6" },
84	    %parsers;
85	1;
86    ];
87    ok( !$@, "No error when creating class." );
88    diag $@ if $@;
89
90    my $parser= eval { $class->new() };
91    is( $parser => 6, "Used given new()" );
92}
93