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