whitespace
[gitmo/MooseX-Types-DateTime.git] / t / 02_datetimex.t
1 use strict;
2 use warnings;
3
4 BEGIN {
5
6         use Test::More tests => 30;
7         use Test::Exception;
8         use DateTime;
9         
10         use_ok 'MooseX::Types::DateTimeX';
11 }
12
13 =head1 NAME
14
15 t/02_datetimex.t - Check that we can properly coerce a string.
16
17 =head1 DESCRIPTION
18
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.
22
23 =head1 TESTS
24
25 This module defines the following tests.
26
27 =head2 Test Class
28
29 Create a L<Moose> class that is using the L<MooseX::Types::DateTimeX> types.
30
31 =cut
32
33 {
34         package MooseX::Types::DateTimeX::CoercionTest;
35         
36         use Moose;
37         use MooseX::Types::DateTimeX qw(DateTime Duration);
38         
39         has 'date' => (is=>'rw', isa=>DateTime, coerce=>1);
40         has 'duration' => (is=>'rw', isa=>Duration, coerce=>1); 
41 }
42
43 ok my $class = MooseX::Types::DateTimeX::CoercionTest->new
44 => 'Created a good class';
45
46
47 =head2 ParseDateTime Capabilities
48
49 parse some dates and make sure the system can actually find something.
50
51 =cut
52
53 ok $class->date('2/13/1969 noon')
54 => "coerced a DateTime from '2/13/1969 noon'";
55
56 is $class->date, '1969-02-13T12:00:00'
57 => 'got correct date';
58
59 ok $class->date('2/13/1969')
60 => "coerced a DateTime from '2/13/1969'";
61
62         is $class->date, '1969-02-13T00:00:00'
63         => 'got correct date';
64
65 ok $class->date('2/13/1969 America/New_York')
66 => "coerced a DateTime from '2/13/1969 America/New_York'";
67
68         isa_ok $class->date->time_zone => 'DateTime::TimeZone::America::New_York'
69         => 'Got Correct America/New_York TimeZone';
70
71         is $class->date, '1969-02-13T00:00:00'
72         => 'got correct date';
73
74 ok $class->date('jan 1 2006')
75 =>"coerced a DateTime from 'jan 1 2006'";
76
77         is $class->date, '2006-01-01T00:00:00'
78         => 'got correct date';
79         
80
81 =head2 relative dates
82
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.
86
87 =cut
88
89 ok $class->date('now')
90 => "coerced a DateTime from 'now'";
91
92 ok $class->date('yesterday')
93 => "coerced a DateTime from 'yesterday'";
94
95
96 ok $class->date('tomorrow')
97 => "coerced a DateTime from 'tomorrow'";
98
99
100 ok $class->date('last week')
101 => "coerced a DateTime from 'last week'";
102
103
104 =head2 check inherited constraints
105
106 Just a few tests to make sure the object, hash, etc coercions and type checks 
107 still work.
108
109 =cut
110
111 ok my $datetime = DateTime->now()
112 => 'Create a datetime object for testing';
113
114 ok my $anyobject = bless({}, 'Bogus::Does::Not::Exist')
115 => 'Created a random object for proving the object constraint';
116
117 ok $class->date($datetime)
118 => 'Passed Object type constraint test.';
119
120         isa_ok $class->date => 'DateTime'
121         => 'Got a good DateTime Object';
122
123 dies_ok { $class->date($anyobject) } 'Does not allow the bad object';
124
125 ok $class->date(1000)
126 => 'Passed Num coercion test.';
127
128         isa_ok $class->date => 'DateTime'
129         => 'Got a good DateTime Object';
130         
131         is $class->date => '1970-01-01T00:16:40'
132         => 'Got correct DateTime';
133
134 ok $class->date({year=>2000,month=>1,day=>10})
135 => 'Passed HashRef coercion test.';
136
137         isa_ok $class->date => 'DateTime'
138         => 'Got a good DateTime Object';
139         
140         is $class->date => '2000-01-10T00:00:00'
141         => 'Got correct DateTime';
142         
143 =head2 check duration
144
145 make sure the Duration type constraint works as expected
146
147 =cut
148
149 ok $class->duration(100)
150 => 'got duration from integer';
151
152         is $class->duration->seconds, 100
153         => 'got correct duration from integer';
154         
155
156 ok $class->duration('1 minute')
157 => 'got duration from string';
158
159         is $class->duration->seconds, 60
160         => 'got correct duration string';
161         
162         
163 =head1 AUTHOR
164
165 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
166
167 =head1 COPYRIGHT
168
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.
172
173 =cut
174
175 1;
176