Allow overriding of metaclass and traits in has '+$foo' + tests as discussed on the...
[gitmo/Moose.git] / t / 020_attributes / 016_attribute_traits_registered.t
CommitLineData
3bb22459 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
39d37838 6use Test::More tests => 13;
3bb22459 7use Test::Exception;
8use Test::Moose;
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');
19
20 after 'install_accessors' => sub {
21 my $self = shift;
22 $self->associated_class->add_method(
23 $self->alias_to,
24 $self->get_read_method_ref
25 );
26 };
27
28 package Moose::Meta::Attribute::Custom::Trait::Aliased;
29 sub register_implementation { 'My::Attribute::Trait' }
30}
31
32{
39d37838 33 package My::Other::Attribute::Trait;
34 use Moose::Role;
35
36 my $method = sub {
37 42;
38 };
39
40 after 'install_accessors' => sub {
41 my $self = shift;
42 $self->associated_class->add_method(
43 'additional_method',
44 $method
45 );
46 };
47
48 package Moose::Meta::Attribute::Custom::Trait::Other;
49 sub register_implementation { 'My::Other::Attribute::Trait' }
50}
51
52{
3bb22459 53 package My::Class;
54 use Moose;
55
56 has 'bar' => (
57 traits => [qw/Aliased/],
58 is => 'ro',
59 isa => 'Int',
60 alias_to => 'baz',
61 );
62}
63
39d37838 64{
65 package My::Derived::Class;
66 use Moose;
67
68 extends 'My::Class';
69
70 has '+bar' => (
71 traits => [qw/Other/],
72 );
73}
74
3bb22459 75my $c = My::Class->new(bar => 100);
76isa_ok($c, 'My::Class');
77
78is($c->bar, 100, '... got the right value for bar');
79
39d37838 80can_ok($c, 'baz') and
3bb22459 81is($c->baz, 100, '... got the right value for baz');
82
83does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait');
39d37838 84
85my $quux = My::Derived::Class->new(bar => 1000);
86
87is($quux->bar, 1000, '... got the right value for bar');
88
89can_ok($quux, 'baz');
90is($quux->baz, 1000, '... got the right value for baz');
91ok($quux->meta->get_attribute('bar')->does('My::Attribute::Trait'));
92
93TODO: {
94 local $TODO = 'These do not pass - bug?';
95 SKIP: {
96 skip 'no additional_method, so cannot test its value', 1 if !can_ok($quux, 'additional_method');
97 is($quux->additional_method, 42, '... got the right value for additional_method');
98 }
99 ok($quux->meta->get_attribute('bar')->does('My::Other::Attribute::Trait'));
100}