We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / attributes / attribute_traits.t
CommitLineData
d9bb6c63 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
3bb22459 7use Test::Moose;
d9bb6c63 8
7ff56534 9
d9bb6c63 10{
11 package My::Attribute::Trait;
12 use Moose::Role;
d03bd989 13
d9bb6c63 14 has 'alias_to' => (is => 'ro', isa => 'Str');
3c693def 15
16 has foo => ( is => "ro", default => "blah" );
d03bd989 17
d9bb6c63 18 after 'install_accessors' => sub {
19 my $self = shift;
20 $self->associated_class->add_method(
d03bd989 21 $self->alias_to,
d9bb6c63 22 $self->get_read_method_ref
23 );
24 };
25}
26
27{
28 package My::Class;
29 use Moose;
d03bd989 30
d9bb6c63 31 has 'bar' => (
32 traits => [qw/My::Attribute::Trait/],
33 is => 'ro',
34 isa => 'Int',
35 alias_to => 'baz',
36 );
d03bd989 37
587e457d 38 has 'gorch' => (
39 is => 'ro',
40 isa => 'Int',
41 default => sub { 10 }
d03bd989 42 );
d9bb6c63 43}
44
45my $c = My::Class->new(bar => 100);
46isa_ok($c, 'My::Class');
47
48is($c->bar, 100, '... got the right value for bar');
587e457d 49is($c->gorch, 10, '... got the right value for gorch');
d9bb6c63 50
51can_ok($c, 'baz');
52is($c->baz, 100, '... got the right value for baz');
3bb22459 53
82a5b1a7 54my $bar_attr = $c->meta->get_attribute('bar');
55does_ok($bar_attr, 'My::Attribute::Trait');
88f23977 56ok($bar_attr->has_applied_traits, '... got the applied traits');
82a5b1a7 57is_deeply($bar_attr->applied_traits, [qw/My::Attribute::Trait/], '... got the applied traits');
3c693def 58is($bar_attr->foo, "blah", "attr initialized");
3bb22459 59
88f23977 60my $gorch_attr = $c->meta->get_attribute('gorch');
61ok(!$gorch_attr->does('My::Attribute::Trait'), '... gorch doesnt do the trait');
62ok(!$gorch_attr->has_applied_traits, '... no traits applied');
63is($gorch_attr->applied_traits, undef, '... no traits applied');
3bb22459 64
a28e50e4 65done_testing;