Implement own method modifiers
[gitmo/Mouse.git] / t / 020_attributes / 015_attribute_traits.t
CommitLineData
85a83bed 1#!/usr/bin/perl
2use lib 't/lib';
3
4use strict;
5use warnings;
6
4fcab488 7use Test::More;
8BEGIN{
9 if(eval{ require Class::Method::Modifiers::Fast } || eval{ require Class::Method::Modifiers }){
10 plan tests => 12;
11 }
12 else{
13 plan skip_all => 'This test requires Class::Method::Modifiers(::Fast)?';
14 }
15}
85a83bed 16use Test::Exception;
17use Test::Mouse;
18
19
20
21{
22 package My::Attribute::Trait;
23 use Mouse::Role;
24
25 has 'alias_to' => (is => 'ro', isa => 'Str');
26
27 has foo => ( is => "ro", default => "blah" );
28
29 after 'install_accessors' => sub {
30 my $self = shift;
31 my $reader = $self->get_read_method;
32
33 $self->associated_class->add_method(
34 $self->alias_to,
35 sub { shift->$reader(@_) },
36 );
37 };
38}
39
40{
41 package My::Class;
42 use Mouse;
43
44 has 'bar' => (
45 traits => [qw/My::Attribute::Trait/],
46 is => 'ro',
47 isa => 'Int',
48 alias_to => 'baz',
49 );
50
51 has 'gorch' => (
52 is => 'ro',
53 isa => 'Int',
54 default => sub { 10 }
55 );
56}
57
58my $c = My::Class->new(bar => 100);
59isa_ok($c, 'My::Class');
60
61is($c->bar, 100, '... got the right value for bar');
62is($c->gorch, 10, '... got the right value for gorch');
63
64can_ok($c, 'baz');
65is($c->baz, 100, '... got the right value for baz');
66
67my $bar_attr = $c->meta->get_attribute('bar');
68
69does_ok($bar_attr, 'My::Attribute::Trait');
70ok($bar_attr->has_applied_traits, '... got the applied traits');
71is_deeply($bar_attr->applied_traits, [qw/My::Attribute::Trait/], '... got the applied traits');
72is($bar_attr->foo, "blah", "attr initialized");
73
74my $gorch_attr = $c->meta->get_attribute('gorch');
75ok(!$gorch_attr->does('My::Attribute::Trait'), '... gorch doesnt do the trait');
76ok(!$gorch_attr->has_applied_traits, '... no traits applied');
77is($gorch_attr->applied_traits, undef, '... no traits applied');
78
79
80