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