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