added more test cases
[gitmo/Mouse.git] / t / 800_shikabased / 003-make_immutable.t
CommitLineData
fc1d8369 1use strict;
2use warnings;
7ef69340 3use Test::More tests => 18;
eab81545 4use Test::Exception;
7ef69340 5use Scalar::Util qw/isweak/;
fc1d8369 6
7{
7ef69340 8 package Headers;
fc1d8369 9 use Mouse;
7ef69340 10 has data => (
fc1d8369 11 is => 'rw',
7ef69340 12 isa => 'Str',
fc1d8369 13 );
14 no Mouse;
fc1d8369 15}
16
17{
7ef69340 18 package Types;
19 use MouseX::Types -declare => [qw/Foo/];
20 use MouseX::Types::Mouse 'HashRef';
21 class_type Foo, { class => 'Headers' };
22 coerce Foo,
23 from HashRef,
24 via {
25 Headers->new($_);
26 };
27}
28
29
30&main; exit;
31
32sub construct {
33 my $class = shift;
34 eval <<"...";
35 package $class;
fc1d8369 36 use Mouse;
7ef69340 37 BEGIN { Types->import('Foo') }
fc1d8369 38 has bone => (
39 is => 'rw',
40 required => 1,
41 );
7ef69340 42 has foo => (
43 is => 'rw',
44 isa => Foo,
45 coerce => 1,
46 );
47 has weak_foo => (
48 is => 'rw',
49 weak_ref => 1,
50 );
51 has trigger_foo => (
52 is => 'rw',
53 trigger => sub { \$_[0]->bone('eat') },
54 );
63d74d7a 55 sub BUILD { main::ok "calling BUILD in SoftDog" }
fc1d8369 56 no Mouse;
7ef69340 57...
58 die $@ if $@;
fc1d8369 59}
60
7ef69340 61sub test {
62 my $class = shift;
63 lives_ok { $class->new(bone => 'moo') } "$class new";
64 throws_ok { $class->new() } qr/\QAttribute (bone) is required/;
65 is($class->new(bone => 'moo', foo => { data => 3 })->foo->data, 3);
fc1d8369 66
7ef69340 67 my $foo = Headers->new();
68 ok(Scalar::Util::isweak($class->new(bone => 'moo', weak_foo => $foo)->{weak_foo}));
69
70 {
71 my $o = $class->new(bone => 'moo');
72 $o->trigger_foo($foo);
73 is($o->bone, 'eat');
74 }
75}
76
77sub main {
78 construct('SoftDog');
79 test('SoftDog');
80
81 construct('HardDog');
82 HardDog->meta->make_immutable;
83 test('HardDog');
84}
fc1d8369 85