Resolve a todo
[gitmo/Mouse.git] / t / 020_attributes / 017_attribute_traits_n_meta.t
CommitLineData
a72478f2 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
db53d2b4 6use lib 't/lib';
7
a72478f2 8use Test::More tests => 7;
9use Test::Exception;
a72478f2 10use Test::Mouse;
11
4060c871 12
13
a72478f2 14{
15 package My::Meta::Attribute::DefaultReadOnly;
16 use Mouse;
17
18 extends 'Mouse::Meta::Attribute';
19
20 around 'new' => sub {
21 my $next = shift;
22 my ($self, $name, %options) = @_;
23 $options{is} = 'ro'
24 unless exists $options{is};
25 $next->($self, $name, %options);
26 };
27}
28
29{
30 package My::Attribute::Trait;
31 use Mouse::Role;
32
33 has 'alias_to' => (is => 'ro', isa => 'Str');
34
35 after 'install_accessors' => sub {
36 my $self = shift;
37 $self->associated_class->add_method(
38 $self->alias_to,
39 $self->get_read_method_ref
40 );
41 };
42}
43
44{
45 package My::Class;
46 use Mouse;
47
48 has 'bar' => (
49 metaclass => 'My::Meta::Attribute::DefaultReadOnly',
50 traits => [qw/My::Attribute::Trait/],
51 isa => 'Int',
52 alias_to => 'baz',
53 );
54}
55
56my $c = My::Class->new(bar => 100);
57isa_ok($c, 'My::Class');
58
59is($c->bar, 100, '... got the right value for bar');
60
61can_ok($c, 'baz');
62is($c->baz, 100, '... got the right value for baz');
63
64isa_ok($c->meta->get_attribute('bar'), 'My::Meta::Attribute::DefaultReadOnly');
65does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait');
66is($c->meta->get_attribute('bar')->_is_metadata, 'ro', '... got the right metaclass customization');
67
68
69
70