Commit | Line | Data |
4078709c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
4078709c |
7 | use Test::Exception; |
8 | |
7ff56534 |
9 | |
41fd598a |
10 | { |
11 | |
4078709c |
12 | package Fake::DateTime; |
13 | use Moose; |
41fd598a |
14 | |
15 | has 'string_repr' => ( is => 'ro' ); |
16 | |
4078709c |
17 | package Mortgage; |
18 | use Moose; |
19 | use Moose::Util::TypeConstraints; |
20 | |
41fd598a |
21 | coerce 'Fake::DateTime' => from 'Str' => |
22 | via { Fake::DateTime->new( string_repr => $_ ) }; |
4078709c |
23 | |
24 | has 'closing_date' => ( |
41fd598a |
25 | is => 'rw', |
26 | isa => 'Fake::DateTime', |
27 | coerce => 1, |
28 | trigger => sub { |
29 | my ( $self, $val ) = @_; |
30 | ::pass('... trigger is being called'); |
31 | ::isa_ok( $self->closing_date, 'Fake::DateTime' ); |
32 | ::isa_ok( $val, 'Fake::DateTime' ); |
33 | } |
4078709c |
34 | ); |
35 | } |
36 | |
37 | { |
38 | my $mtg = Mortgage->new( closing_date => 'yesterday' ); |
41fd598a |
39 | isa_ok( $mtg, 'Mortgage' ); |
4078709c |
40 | |
41 | # check that coercion worked |
41fd598a |
42 | isa_ok( $mtg->closing_date, 'Fake::DateTime' ); |
4078709c |
43 | } |
44 | |
45 | Mortgage->meta->make_immutable; |
41fd598a |
46 | ok( Mortgage->meta->is_immutable, '... Mortgage is now immutable' ); |
4078709c |
47 | |
48 | { |
49 | my $mtg = Mortgage->new( closing_date => 'yesterday' ); |
41fd598a |
50 | isa_ok( $mtg, 'Mortgage' ); |
4078709c |
51 | |
52 | # check that coercion worked |
41fd598a |
53 | isa_ok( $mtg->closing_date, 'Fake::DateTime' ); |
4078709c |
54 | } |
55 | |
a28e50e4 |
56 | done_testing; |