Commit | Line | Data |
a3f4ab71 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | BEGIN { |
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 | |
15 | String Coercion; Check that we can properly coerce a string. |
16 | |
17 | =head1 DESCRIPTION |
18 | |
19 | Make sure all the utility stuff works as expected |
20 | |
21 | =head1 TESTS |
22 | |
23 | This module defines the following tests. |
24 | |
25 | =head2 Test Class |
26 | |
27 | Create 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 | |
40 | ok my $class = MooseX::Types::DateTimeX::CoercionTest->new |
41 | => 'Created a good class'; |
42 | |
43 | |
44 | =head2 ParseDateTime Capabilities |
45 | |
46 | parse some dates and make sure the system can actually find something. |
47 | |
48 | =cut |
49 | |
50 | ok $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 | |
56 | ok $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 | |
62 | ok $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 | |
71 | ok $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 | |
80 | Stuff like "yesterday". We can make sure they returned something but we have |
81 | no way to make sure the values are really correct. Manual testing suggests |
82 | they work well enough, given the inherent ambiguity we are dealing with. |
83 | |
84 | =cut |
85 | |
86 | ok $class->date('now') |
87 | => "coerced a DateTime from 'now'"; |
88 | |
89 | ok $class->date('yesterday') |
90 | => "coerced a DateTime from 'yesterday'"; |
91 | |
92 | |
93 | ok $class->date('tomorrow') |
94 | => "coerced a DateTime from 'tomorrow'"; |
95 | |
96 | |
97 | ok $class->date('last week') |
98 | => "coerced a DateTime from 'last week'"; |
99 | |
100 | |
101 | =head2 check inherited constraints |
102 | |
103 | Just a few tests to make sure the object, hash, etc coercions and type checks |
104 | still work. |
105 | |
106 | =cut |
107 | |
108 | ok my $datetime = DateTime->now() |
109 | => 'Create a datetime object for testing'; |
110 | |
111 | ok my $anyobject = bless({}, 'Bogus::Does::Not::Exist') |
112 | => 'Created a random object for proving the object constraint'; |
113 | |
114 | ok $class->date($datetime) |
115 | => 'Passed Object type constraint test.'; |
116 | |
117 | isa_ok $class->date => 'DateTime' |
118 | => 'Got a good DateTime Object'; |
119 | |
120 | dies_ok { $class->date($anyobject) } 'Does not allow the bad object'; |
121 | |
122 | ok $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 | |
131 | ok $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 | |
143 | John 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 | |
153 | 1; |
154 | |