Created MX:T:DateTimeX as extensions to the MX:T:DateTime, created test for new modul...
[gitmo/MooseX-Types-DateTime.git] / t / 02_datetimex.t
CommitLineData
a3f4ab71 1use strict;
2use warnings;
3
4BEGIN {
5
6 use Test::More tests => 26;
7 use Test::Exception;
8 use DateTime;
9
10 use_ok 'MooseX::Types::DateTimeX';
11}
12
13=head1 NAME
14
15String Coercion; Check that we can properly coerce a string.
16
17=head1 DESCRIPTION
18
19Make sure all the utility stuff works as expected
20
21=head1 TESTS
22
23This module defines the following tests.
24
25=head2 Test Class
26
27Create a L<Moose> class that is using the L<MooseX::Types::DateTimeX> types.
28
29=cut
30
31{
32 package MooseX::Types::DateTimeX::CoercionTest;
33
34 use Moose;
35 use MooseX::Types::DateTimeX qw(DateTime);
36
37 has 'date' => (is=>'rw', isa=>DateTime, coerce=>1);
38}
39
40ok my $class = MooseX::Types::DateTimeX::CoercionTest->new
41=> 'Created a good class';
42
43
44=head2 ParseDateTime Capabilities
45
46parse some dates and make sure the system can actually find something.
47
48=cut
49
50ok $class->date('2/13/1969 noon')
51=> "coerced a DateTime from '2/13/1969 noon'";
52
53 is $class->date, '1969-02-13T11:00:00'
54 => 'got correct date';
55
56ok $class->date('2/13/1969')
57=> "coerced a DateTime from '2/13/1969'";
58
59 is $class->date, '1969-02-13T00:00:00'
60 => 'got correct date';
61
62ok $class->date('2/13/1969 America/New_York')
63=> "coerced a DateTime from '2/13/1969 America/New_York'";
64
65 isa_ok $class->date->time_zone => 'DateTime::TimeZone::America::New_York'
66 => 'Got Correct America/New_York TimeZone';
67
68 is $class->date, '1969-02-13T00:00:00'
69 => 'got correct date';
70
71ok $class->date('jan 1 2006')
72=>"coerced a DateTime from 'jan 1 2006'";
73
74 is $class->date, '2006-01-01T00:00:00'
75 => 'got correct date';
76
77
78=head2 relative dates
79
80Stuff like "yesterday". We can make sure they returned something but we have
81no way to make sure the values are really correct. Manual testing suggests
82they work well enough, given the inherent ambiguity we are dealing with.
83
84=cut
85
86ok $class->date('now')
87=> "coerced a DateTime from 'now'";
88
89ok $class->date('yesterday')
90=> "coerced a DateTime from 'yesterday'";
91
92
93ok $class->date('tomorrow')
94=> "coerced a DateTime from 'tomorrow'";
95
96
97ok $class->date('last week')
98=> "coerced a DateTime from 'last week'";
99
100
101=head2 check inherited constraints
102
103Just a few tests to make sure the object, hash, etc coercions and type checks
104still work.
105
106=cut
107
108ok my $datetime = DateTime->now()
109=> 'Create a datetime object for testing';
110
111ok my $anyobject = bless({}, 'Bogus::Does::Not::Exist')
112=> 'Created a random object for proving the object constraint';
113
114ok $class->date($datetime)
115=> 'Passed Object type constraint test.';
116
117 isa_ok $class->date => 'DateTime'
118 => 'Got a good DateTime Object';
119
120dies_ok { $class->date($anyobject) } 'Does not allow the bad object';
121
122ok $class->date(1000)
123=> 'Passed Num coercion test.';
124
125 isa_ok $class->date => 'DateTime'
126 => 'Got a good DateTime Object';
127
128 is $class->date => '1970-01-01T00:16:40'
129 => 'Got correct DateTime';
130
131ok $class->date({year=>2000,month=>1,day=>10})
132=> 'Passed HashRef coercion test.';
133
134 isa_ok $class->date => 'DateTime'
135 => 'Got a good DateTime Object';
136
137 is $class->date => '2000-01-10T00:00:00'
138 => 'Got correct DateTime';
139
140
141=head1 AUTHOR
142
143John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
144
145=head1 COPYRIGHT
146
147 Copyright (c) 2008 John Napiorkowski. All rights reserved
148 This program is free software; you can redistribute
149 it and/or modify it under the same terms as Perl itself.
150
151=cut
152
1531;
154