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