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