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