7 plan skip_all => "DateTime::Format::DateManip required" unless eval { require DateTime::Format::DateManip };
14 use ok 'MooseX::Types::DateTime::MoreCoercions';
18 t/02_datetimex.t - Check that we can properly coerce a string.
22 Run some tests to make sure the the Duration and DateTime types continue to
23 work exactly as from the L<MooseX::Types::DateTime> class, as well as perform
24 the correct string to object coercions.
28 This module defines the following tests.
32 Create a L<Moose> class that is using the L<MooseX::Types::DateTime::MoreCoercions> types.
37 package MooseX::Types::DateTime::MoreCoercions::CoercionTest;
40 use MooseX::Types::DateTime::MoreCoercions qw(DateTime Duration);
42 has 'date' => (is=>'rw', isa=>DateTime, coerce=>1);
43 has 'duration' => (is=>'rw', isa=>Duration, coerce=>1);
46 ok my $class = MooseX::Types::DateTime::MoreCoercions::CoercionTest->new
47 => 'Created a good class';
50 =head2 ParseDateTime Capabilities
52 parse some dates and make sure the system can actually find something.
57 my ( $date, $canon ) = @_;
58 local $Test::Builder::Level = $Test::Builder::Level + 1;
61 skip "DateTimeX::Easy couldn't parse '$date'", $canon ? 2 : 1 unless DateTimeX::Easy->new($date);
62 ok( $class->date($date), "coerced a DateTime from '$date'" );
63 is( $class->date, $canon, 'got correct date' ) if $canon;
67 ## Skip this test until I can figure out better timezone handling
68 #coerce_ok ('2/13/1969 noon', '1969-02-13T11:00:00' );
71 coerce_ok( '2/13/1969', '1969-02-13T00:00:00' );
73 coerce_ok( '2/13/1969 America/New_York', '1969-02-13T00:00:00' );
76 skip "couldn't parse", 1 unless $class->date;
77 isa_ok $class->date->time_zone => 'DateTime::TimeZone::America::New_York'
78 => 'Got Correct America/New_York TimeZone';
81 coerce_ok( 'jan 1 2006', '2006-01-01T00:00:00' );
85 Stuff like "yesterday". We can make sure they returned something but we have
86 no way to make sure the values are really correct. Manual testing suggests
87 they work well enough, given the inherent ambiguity we are dealing with.
93 coerce_ok("yesterday");
95 coerce_ok("tomorrow");
97 coerce_ok("last week");
99 =head2 check inherited constraints
101 Just a few tests to make sure the object, hash, etc coercions and type checks
106 ok my $datetime = DateTime->now()
107 => 'Create a datetime object for testing';
109 ok my $anyobject = bless({}, 'Bogus::Does::Not::Exist')
110 => 'Created a random object for proving the object constraint';
112 ok $class->date($datetime)
113 => 'Passed Object type constraint test.';
115 isa_ok $class->date => 'DateTime'
116 => 'Got a good DateTime Object';
118 dies_ok { $class->date($anyobject) } 'Does not allow the bad object';
120 ok $class->date(1000)
121 => 'Passed Num coercion test.';
123 isa_ok $class->date => 'DateTime'
124 => 'Got a good DateTime Object';
126 is $class->date => '1970-01-01T00:16:40'
127 => 'Got correct DateTime';
129 ok $class->date({year=>2000,month=>1,day=>10})
130 => 'Passed HashRef coercion test.';
132 isa_ok $class->date => 'DateTime'
133 => 'Got a good DateTime Object';
135 is $class->date => '2000-01-10T00:00:00'
136 => 'Got correct DateTime';
138 =head2 check duration
140 make sure the Duration type constraint works as expected
144 ok $class->duration(100)
145 => 'got duration from integer';
147 is $class->duration->seconds, 100
148 => 'got correct duration from integer';
151 ok $class->duration('1 minute')
152 => 'got duration from string';
154 is $class->duration->seconds, 60
155 => 'got correct duration string';
160 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
164 Copyright (c) 2008 John Napiorkowski. All rights reserved
165 This program is free software; you can redistribute
166 it and/or modify it under the same terms as Perl itself.