Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / attributes / attribute_traits.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Moose;
8
9
10 {
11     package My::Attribute::Trait;
12     use Moose::Role;
13
14     has 'alias_to' => (is => 'ro', isa => 'Str');
15
16     has foo => ( is => "ro", default => "blah" );
17
18     after 'install_accessors' => sub {
19         my $self = shift;
20         $self->associated_class->add_method(
21             $self->alias_to,
22             $self->get_read_method_ref
23         );
24     };
25 }
26
27 {
28     package My::Class;
29     use Moose;
30
31     has 'bar' => (
32         traits   => [qw/My::Attribute::Trait/],
33         is       => 'ro',
34         isa      => 'Int',
35         alias_to => 'baz',
36     );
37
38     has 'gorch' => (
39         is      => 'ro',
40         isa     => 'Int',
41         default => sub { 10 }
42     );
43 }
44
45 my $c = My::Class->new(bar => 100);
46 isa_ok($c, 'My::Class');
47
48 is($c->bar, 100, '... got the right value for bar');
49 is($c->gorch, 10, '... got the right value for gorch');
50
51 can_ok($c, 'baz');
52 is($c->baz, 100, '... got the right value for baz');
53
54 my $bar_attr = $c->meta->get_attribute('bar');
55 does_ok($bar_attr, 'My::Attribute::Trait');
56 ok($bar_attr->has_applied_traits, '... got the applied traits');
57 is_deeply($bar_attr->applied_traits, [qw/My::Attribute::Trait/], '... got the applied traits');
58 is($bar_attr->foo, "blah", "attr initialized");
59
60 my $gorch_attr = $c->meta->get_attribute('gorch');
61 ok(!$gorch_attr->does('My::Attribute::Trait'), '... gorch doesnt do the trait');
62 ok(!$gorch_attr->has_applied_traits, '... no traits applied');
63 is($gorch_attr->applied_traits, undef, '... no traits applied');
64
65 done_testing;