Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 020_attributes / 016_attribute_traits_registered.t
CommitLineData
a72478f2 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9864f0e4 6use lib 't/lib';
7
8use Test::More tests => 23;
a72478f2 9use Test::Exception;
ad087d11 10use Test::Mouse;
a72478f2 11
4060c871 12
9864f0e4 13
a72478f2 14{
15 package My::Attribute::Trait;
ad087d11 16 use Mouse::Role;
a72478f2 17
18 has 'alias_to' => (is => 'ro', isa => 'Str');
19
20 has foo => ( is => "ro", default => "blah" );
21
22 after 'install_accessors' => sub {
23 my $self = shift;
24 $self->associated_class->add_method(
25 $self->alias_to,
26 $self->get_read_method_ref
27 );
28 };
29
ad087d11 30 package Mouse::Meta::Attribute::Custom::Trait::Aliased;
a72478f2 31 sub register_implementation { 'My::Attribute::Trait' }
32}
33
34{
35 package My::Other::Attribute::Trait;
ad087d11 36 use Mouse::Role;
a72478f2 37
38 my $method = sub {
39 42;
40 };
41
42 has the_other_attr => ( isa => "Str", is => "rw", default => "oink" );
43
44 after 'install_accessors' => sub {
45 my $self = shift;
46 $self->associated_class->add_method(
47 'additional_method',
48 $method
49 );
50 };
51
ad087d11 52 package Mouse::Meta::Attribute::Custom::Trait::Other;
a72478f2 53 sub register_implementation { 'My::Other::Attribute::Trait' }
54}
55
56{
57 package My::Class;
ad087d11 58 use Mouse;
a72478f2 59
60 has 'bar' => (
61 traits => [qw/Aliased/],
62 is => 'ro',
63 isa => 'Int',
64 alias_to => 'baz',
65 );
66}
67
68{
69 package My::Derived::Class;
ad087d11 70 use Mouse;
a72478f2 71
72 extends 'My::Class';
73
74 has '+bar' => (
75 traits => [qw/Other/],
76 );
77}
78
79my $c = My::Class->new(bar => 100);
80isa_ok($c, 'My::Class');
81
82is($c->bar, 100, '... got the right value for bar');
83
84can_ok($c, 'baz') and
85is($c->baz, 100, '... got the right value for baz');
86
87my $bar_attr = $c->meta->get_attribute('bar');
88does_ok($bar_attr, 'My::Attribute::Trait');
89is($bar_attr->foo, "blah", "attr initialized");
90
91ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
9864f0e4 92{
93local $TODO = 'aliased name is not supported';
4060c871 94ok($bar_attr->does('Aliased'), "attr->does uses aliases");
9864f0e4 95}
a72478f2 96ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
97ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
98
99my $quux = My::Derived::Class->new(bar => 1000);
100
101is($quux->bar, 1000, '... got the right value for bar');
102
103can_ok($quux, 'baz');
104is($quux->baz, 1000, '... got the right value for baz');
105
106my $derived_bar_attr = $quux->meta->get_attribute("bar");
107does_ok($derived_bar_attr, 'My::Attribute::Trait' );
108
109is( $derived_bar_attr->foo, "blah", "attr initialized" );
110
111does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' );
112
113is($derived_bar_attr->the_other_attr, "oink", "attr initialized" );
114
115ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
9864f0e4 116{
117local $TODO = 'aliased name is not supported';
4060c871 118ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases");
9864f0e4 119}
a72478f2 120ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
121ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
122
123can_ok($quux, 'additional_method');
124is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');
125