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