metaclass and traits interpolation moved to Meta::Attribute
[gitmo/Moose.git] / t / 020_attributes / 016_attribute_traits_registered.t
CommitLineData
3bb22459 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
3c693def 6use Test::More tests => 16;
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;
3c693def 17
3bb22459 18 has 'alias_to' => (is => 'ro', isa => 'Str');
3c693def 19
20 has foo => ( is => "ro", default => "blah" );
21
3bb22459 22 after 'install_accessors' => sub {
23 my $self = shift;
24 $self->associated_class->add_method(
3c693def 25 $self->alias_to,
3bb22459 26 $self->get_read_method_ref
27 );
28 };
3c693def 29
3bb22459 30 package Moose::Meta::Attribute::Custom::Trait::Aliased;
31 sub register_implementation { 'My::Attribute::Trait' }
32}
33
34{
39d37838 35 package My::Other::Attribute::Trait;
36 use Moose::Role;
3c693def 37
39d37838 38 my $method = sub {
39 42;
3c693def 40 };
41
42 has bar => ( isa => "Str", default => "oink" );
43
39d37838 44 after 'install_accessors' => sub {
45 my $self = shift;
46 $self->associated_class->add_method(
3c693def 47 'additional_method',
39d37838 48 $method
49 );
50 };
3c693def 51
39d37838 52 package Moose::Meta::Attribute::Custom::Trait::Other;
53 sub register_implementation { 'My::Other::Attribute::Trait' }
54}
55
56{
3bb22459 57 package My::Class;
58 use Moose;
3c693def 59
3bb22459 60 has 'bar' => (
61 traits => [qw/Aliased/],
62 is => 'ro',
63 isa => 'Int',
64 alias_to => 'baz',
65 );
66}
67
3c693def 68{
39d37838 69 package My::Derived::Class;
70 use Moose;
71
72 extends 'My::Class';
73
74 has '+bar' => (
75 traits => [qw/Other/],
76 );
77}
78
3bb22459 79my $c = My::Class->new(bar => 100);
80isa_ok($c, 'My::Class');
81
82is($c->bar, 100, '... got the right value for bar');
83
39d37838 84can_ok($c, 'baz') and
3bb22459 85is($c->baz, 100, '... got the right value for baz');
86
3c693def 87my $bar_attr = $c->meta->get_attribute('bar');
88does_ok($bar_attr, 'My::Attribute::Trait');
89is($bar_attr->foo, "blah", "attr initialized");
39d37838 90
91my $quux = My::Derived::Class->new(bar => 1000);
92
93is($quux->bar, 1000, '... got the right value for bar');
94
95can_ok($quux, 'baz');
96is($quux->baz, 1000, '... got the right value for baz');
3c693def 97
98my $derived_bar_attr = $quux->meta->get_attribute("bar");
99does_ok($derived_bar_attr, 'My::Attribute::Trait' );
100
101is( $derived_bar_attr->foo, "blah", "attr initialized" );
39d37838 102
103TODO: {
3c693def 104 local $TODO = 'traits in clone_and_inherit dont work yet';
105 does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' );
106
107 is( eval { $derived_bar_attr->bar }, "oink", "attr initialized" );
108
109 can_ok($quux, 'additional_method');
110 is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');
39d37838 111}