We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / attributes / attribute_traits_registered.t
CommitLineData
3bb22459 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
3bb22459 7use Test::Moose;
8
7ff56534 9
3bb22459 10{
11 package My::Attribute::Trait;
12 use Moose::Role;
3c693def 13
3bb22459 14 has 'alias_to' => (is => 'ro', isa => 'Str');
3c693def 15
16 has foo => ( is => "ro", default => "blah" );
17
3bb22459 18 after 'install_accessors' => sub {
19 my $self = shift;
20 $self->associated_class->add_method(
3c693def 21 $self->alias_to,
3bb22459 22 $self->get_read_method_ref
23 );
24 };
3c693def 25
3bb22459 26 package Moose::Meta::Attribute::Custom::Trait::Aliased;
27 sub register_implementation { 'My::Attribute::Trait' }
28}
29
30{
39d37838 31 package My::Other::Attribute::Trait;
32 use Moose::Role;
3c693def 33
39d37838 34 my $method = sub {
35 42;
3c693def 36 };
37
c32c2c61 38 has the_other_attr => ( isa => "Str", is => "rw", default => "oink" );
3c693def 39
39d37838 40 after 'install_accessors' => sub {
41 my $self = shift;
42 $self->associated_class->add_method(
3c693def 43 'additional_method',
39d37838 44 $method
45 );
46 };
3c693def 47
39d37838 48 package Moose::Meta::Attribute::Custom::Trait::Other;
49 sub register_implementation { 'My::Other::Attribute::Trait' }
50}
51
52{
3bb22459 53 package My::Class;
54 use Moose;
3c693def 55
3bb22459 56 has 'bar' => (
57 traits => [qw/Aliased/],
58 is => 'ro',
59 isa => 'Int',
60 alias_to => 'baz',
61 );
62}
63
3c693def 64{
39d37838 65 package My::Derived::Class;
66 use Moose;
67
68 extends 'My::Class';
69
70 has '+bar' => (
71 traits => [qw/Other/],
72 );
73}
74
3bb22459 75my $c = My::Class->new(bar => 100);
76isa_ok($c, 'My::Class');
77
78is($c->bar, 100, '... got the right value for bar');
79
39d37838 80can_ok($c, 'baz') and
3bb22459 81is($c->baz, 100, '... got the right value for baz');
82
3c693def 83my $bar_attr = $c->meta->get_attribute('bar');
84does_ok($bar_attr, 'My::Attribute::Trait');
85is($bar_attr->foo, "blah", "attr initialized");
39d37838 86
0db4f1d7 87ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
88ok($bar_attr->does('Aliased'), "attr->does uses aliases");
89ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
90ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
91
39d37838 92my $quux = My::Derived::Class->new(bar => 1000);
93
94is($quux->bar, 1000, '... got the right value for bar');
95
96can_ok($quux, 'baz');
97is($quux->baz, 1000, '... got the right value for baz');
3c693def 98
99my $derived_bar_attr = $quux->meta->get_attribute("bar");
100does_ok($derived_bar_attr, 'My::Attribute::Trait' );
101
102is( $derived_bar_attr->foo, "blah", "attr initialized" );
39d37838 103
c32c2c61 104does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' );
3c693def 105
c32c2c61 106is($derived_bar_attr->the_other_attr, "oink", "attr initialized" );
107
0db4f1d7 108ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
109ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases");
110ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
111ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
112
c32c2c61 113can_ok($quux, 'additional_method');
114is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');
3c693def 115
a28e50e4 116done_testing;