Moose now warns when you try to load it from the main package. Added a
[gitmo/Moose.git] / t / 020_attributes / 020_trigger_and_coerce.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 11;
7 use Test::Exception;
8
9
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
46 Mortgage->meta->make_immutable;
47 ok(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