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