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