6 use Test::More tests => 30;
10 use_ok 'MooseX::Types::DateTimeX';
15 t/02_datetimex.t - Check that we can properly coerce a string.
19 Run some tests to make sure the the Duration and DateTime types continue to
20 work exactly as from the L<MooseX::Types::DateTime> class, as well as perform
21 the correct string to object coercions.
25 This module defines the following tests.
29 Create a L<Moose> class that is using the L<MooseX::Types::DateTimeX> types.
34 package MooseX::Types::DateTimeX::CoercionTest;
37 use MooseX::Types::DateTimeX qw(DateTime Duration);
39 has 'date' => (is=>'rw', isa=>DateTime, coerce=>1);
40 has 'duration' => (is=>'rw', isa=>Duration, coerce=>1);
43 ok my $class = MooseX::Types::DateTimeX::CoercionTest->new
44 => 'Created a good class';
47 =head2 ParseDateTime Capabilities
49 parse some dates and make sure the system can actually find something.
53 ok $class->date('2/13/1969 noon')
54 => "coerced a DateTime from '2/13/1969 noon'";
56 is $class->date, '1969-02-13T11:00:00'
57 => 'got correct date';
59 ok $class->date('2/13/1969')
60 => "coerced a DateTime from '2/13/1969'";
62 is $class->date, '1969-02-13T00:00:00'
63 => 'got correct date';
65 ok $class->date('2/13/1969 America/New_York')
66 => "coerced a DateTime from '2/13/1969 America/New_York'";
68 isa_ok $class->date->time_zone => 'DateTime::TimeZone::America::New_York'
69 => 'Got Correct America/New_York TimeZone';
71 is $class->date, '1969-02-13T00:00:00'
72 => 'got correct date';
74 ok $class->date('jan 1 2006')
75 =>"coerced a DateTime from 'jan 1 2006'";
77 is $class->date, '2006-01-01T00:00:00'
78 => 'got correct date';
83 Stuff like "yesterday". We can make sure they returned something but we have
84 no way to make sure the values are really correct. Manual testing suggests
85 they work well enough, given the inherent ambiguity we are dealing with.
89 ok $class->date('now')
90 => "coerced a DateTime from 'now'";
92 ok $class->date('yesterday')
93 => "coerced a DateTime from 'yesterday'";
96 ok $class->date('tomorrow')
97 => "coerced a DateTime from 'tomorrow'";
100 ok $class->date('last week')
101 => "coerced a DateTime from 'last week'";
104 =head2 check inherited constraints
106 Just a few tests to make sure the object, hash, etc coercions and type checks
111 ok my $datetime = DateTime->now()
112 => 'Create a datetime object for testing';
114 ok my $anyobject = bless({}, 'Bogus::Does::Not::Exist')
115 => 'Created a random object for proving the object constraint';
117 ok $class->date($datetime)
118 => 'Passed Object type constraint test.';
120 isa_ok $class->date => 'DateTime'
121 => 'Got a good DateTime Object';
123 dies_ok { $class->date($anyobject) } 'Does not allow the bad object';
125 ok $class->date(1000)
126 => 'Passed Num coercion test.';
128 isa_ok $class->date => 'DateTime'
129 => 'Got a good DateTime Object';
131 is $class->date => '1970-01-01T00:16:40'
132 => 'Got correct DateTime';
134 ok $class->date({year=>2000,month=>1,day=>10})
135 => 'Passed HashRef coercion test.';
137 isa_ok $class->date => 'DateTime'
138 => 'Got a good DateTime Object';
140 is $class->date => '2000-01-10T00:00:00'
141 => 'Got correct DateTime';
143 =head2 check duration
145 make sure the Duration type constraint works as expected
149 ok $class->duration(100)
150 => 'got duration from integer';
152 is $class->duration->seconds, 100
153 => 'got correct duration from integer';
156 ok $class->duration('1 minute')
157 => 'got duration from string';
159 is $class->duration->seconds, 60
160 => 'got correct duration string';
165 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
169 Copyright (c) 2008 John Napiorkowski. All rights reserved
170 This program is free software; you can redistribute
171 it and/or modify it under the same terms as Perl itself.