4 use Test::More tests => 3;
6 # this tests that multiple type coercions on a given attribute get
7 # applied in the expected order.
12 # This is just a simple class representing a date - in real life we'd use DateTime.
27 my %d; @d{qw(year month day)} = (gmtime shift)[5,4,3];
36 my %d; @d{qw(year month day)} = split /\W/, shift;
44 sprintf "%4d-%02d-%02d",
52 use Mouse::Util::TypeConstraints;
54 # These coercions must be applied in the right order - since a
55 # number can be interpreted as a string, but not vice-versa, the
56 # Int coercion should be applied first to get a correct answer.
58 => from 'Int' # a timestamp
59 => via { Date->from_epoch($_) }
61 => from 'Str' # <YYYY>-<MM>-<DD>
62 => via { Date->from_string($_) };
73 my $date = Date->new(year => 2001, month => 1, day => 1);
74 my $str = $date->to_string;
75 is $str, "2001-01-01", "initial date is correct: $str";
77 my $event = Event->new(date => $date);
79 $str = $event->date->to_string;
80 is $str, "2001-01-01", "initial date field correct: $str";
82 # check the order is applied correctly when given an Int
83 my $timestamp = 1238778317; # Fri Apr 3 17:05:17 2009
84 $event->date($timestamp);
86 $str = $event->date->to_string;
87 is $str, "2009-04-03", "coerced timestamp $timestamp to date field $str correctly";