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