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