Allow type coercions to be declared from anon types
[gitmo/Moose.git] / t / 020_attributes / 016_attribute_traits_registered.t
CommitLineData
3bb22459 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 6;
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{
33 package My::Class;
34 use Moose;
35
36 has 'bar' => (
37 traits => [qw/Aliased/],
38 is => 'ro',
39 isa => 'Int',
40 alias_to => 'baz',
41 );
42}
43
44my $c = My::Class->new(bar => 100);
45isa_ok($c, 'My::Class');
46
47is($c->bar, 100, '... got the right value for bar');
48
49can_ok($c, 'baz');
50is($c->baz, 100, '... got the right value for baz');
51
52does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait');