Commit | Line | Data |
4664d531 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More 'no_plan'; |
5 | use Test::Exception; |
6 | |
7 | use ok 'MooseX::Types::DateTime'; |
8 | |
9 | use Moose::Util::TypeConstraints; |
10 | |
11 | isa_ok( find_type_constraint($_), "Moose::Meta::TypeConstraint" ) for qw(DateTime DateTime::TimeZone DateTime::Locale); |
12 | |
13 | { |
14 | { |
15 | package Foo; |
16 | use Moose; |
17 | |
18 | has date => ( |
19 | isa => "DateTime", |
20 | is => "rw", |
21 | coerce => 1, |
22 | ); |
23 | } |
24 | |
25 | my $epoch = time; |
26 | |
27 | my $coerced = Foo->new( date => $epoch )->date; |
28 | |
29 | isa_ok( $coerced, "DateTime", "coerced epoch into datetime" ); |
30 | |
31 | is( $coerced->epoch, $epoch, "epoch is correct" ); |
32 | |
ec3f7a50 |
33 | isa_ok( Foo->new( date => { year => 2000, month => 1, day => 1 } )->date, "DateTime" ); |
34 | |
35 | isa_ok( Foo->new( date => 'now' )->date, "DateTime" ); |
36 | |
4664d531 |
37 | throws_ok { Foo->new( date => "junk1!!" ) } qr/DateTime/, "constraint"; |
38 | } |
39 | |
40 | { |
41 | { |
ec3f7a50 |
42 | package Quxx; |
43 | use Moose; |
44 | |
45 | has duration => ( |
46 | isa => "DateTime::Duration", |
47 | is => "rw", |
48 | coerce => 1, |
49 | ); |
50 | } |
51 | |
52 | my $coerced = Quxx->new( duration => 10 )->duration; |
53 | |
54 | isa_ok( $coerced, "DateTime::Duration", "coerced from seconds" ); |
55 | |
56 | my $time = time; |
57 | |
58 | my $now = DateTime->from_epoch( epoch => $time ); |
59 | |
60 | my $future = $now + $coerced; |
61 | |
62 | is( $future->epoch, ( $time + 10 ), "coerced value" ); |
63 | |
64 | isa_ok( Quxx->new( duration => { minutes => 2 } )->duration, "DateTime::Duration", "coerced from hash" ); |
65 | |
66 | throws_ok { Quxx->new( duration => "ahdstkljhat" ) } qr/DateTime/, "constraint"; |
67 | } |
68 | |
69 | { |
70 | { |
4664d531 |
71 | package Bar; |
72 | use Moose; |
73 | |
74 | has time_zone => ( |
75 | isa => "DateTime::TimeZone", |
76 | is => "rw", |
77 | coerce => 1, |
78 | ); |
79 | } |
80 | |
81 | my $tz = Bar->new( time_zone => "Africa/Timbuktu" )->time_zone; |
82 | |
83 | isa_ok( $tz, "DateTime::TimeZone", "coerced string into time zone object" ); |
84 | |
85 | like( $tz->name, qr/^Africa/, "correct time zone" ); |
86 | |
87 | dies_ok { Bar->new( time_zone => "Space/TheMoon" ) } "bad time zone"; |
88 | } |
89 | |
90 | { |
91 | { |
92 | package Gorch; |
93 | use Moose; |
94 | |
95 | has loc => ( |
96 | isa => "DateTime::Locale", |
97 | is => "rw", |
98 | coerce => 1, |
99 | ); |
100 | } |
101 | |
102 | my $loc = Gorch->new( loc => "he_IL" )->loc; |
103 | |
ec3f7a50 |
104 | isa_ok( $loc, "DateTime::Locale::he", "coerced from string" ); |
4664d531 |
105 | |
106 | dies_ok { Gorch->new( loc => "not_a_place_or_a_locale" ) } "bad locale name"; |
107 | |
108 | SKIP: { |
109 | skip "No Locale::Maketext", 2 unless eval { require Locale::Maketext }; |
4de7ea9b |
110 | |
4664d531 |
111 | { |
112 | package Some::L10N; |
113 | our @ISA = qw(Locale::Maketext); |
114 | |
115 | package Some::L10N::ja; |
116 | our @ISA = qw(Some::L10N); |
117 | |
118 | our %Lexicon = ( |
119 | goodbye => "sayonara", |
120 | ); |
121 | } |
122 | |
123 | my $handle = Some::L10N->get_handle("ja"); |
124 | |
125 | isa_ok( $handle, "Some::L10N", "maketext handle" ); |
126 | |
127 | isa_ok( Gorch->new( loc => $handle )->loc, "DateTime::Locale::ja", "coerced from maketext" );; |
128 | } |
129 | } |
6903978c |
130 | |
131 | { |
4de7ea9b |
132 | { |
133 | package Gondor; |
134 | |
135 | use Moose; |
136 | use MooseX::Types::DateTime qw(DateTime Duration); |
137 | |
138 | has 'date' => (is=>'rw', isa=>DateTime, coerce=>1); |
33a23d16 |
139 | has 'duration' => (is=>'rw', isa=>Duration, coerce=>1); |
4de7ea9b |
140 | |
141 | } |
142 | |
143 | my $epoch = time; |
144 | |
145 | ok my $gondor = Gondor->new(date=>$epoch, duration=>10) |
146 | => 'Instantiated object using export types'; |
147 | |
6903978c |
148 | } |