coolio
[gitmo/Moose.git] / t / 036_custom_attribute_metaclass.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 11;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');           
11 }
12
13 {    
14     package Foo::Meta::Attribute;
15     use strict;
16     use warnings;
17     use Moose;
18     
19     extends 'Moose::Meta::Attribute';
20     
21     around 'new' => sub {
22         my $next = shift;
23         my $self = shift;
24         my $name = shift;
25         $next->($self, $name, (is => 'rw', isa => 'Foo'), @_);
26     };
27
28     package Foo;
29     use strict;
30     use warnings;
31     use Moose;
32     
33     has 'foo' => (metaclass => 'Foo::Meta::Attribute');
34 }
35
36 my $foo = Foo->new;
37 isa_ok($foo, 'Foo');
38
39 my $foo_attr = Foo->meta->get_attribute('foo');
40 isa_ok($foo_attr, 'Foo::Meta::Attribute');
41 isa_ok($foo_attr, 'Moose::Meta::Attribute');
42
43 is($foo_attr->name, 'foo', '... got the right name for our meta-attribute');
44 ok($foo_attr->has_accessor, '... our meta-attrubute created the accessor for us');
45
46 ok($foo_attr->has_type_constraint, '... our meta-attrubute created the type_constraint for us');
47
48 my $foo_attr_type_constraint = $foo_attr->type_constraint;
49 isa_ok($foo_attr_type_constraint, 'Moose::Meta::TypeConstraint');
50
51 is($foo_attr_type_constraint->name, 'Foo', '... got the right type constraint name');
52 is($foo_attr_type_constraint->parent->name, 'Object', '... got the right type constraint parent name');
53
54 {
55     package Bar::Meta::Attribute;
56     use strict;
57     use warnings;
58     use Moose;
59     
60     extends 'Class::MOP::Attribute';   
61     
62     package Bar;
63     use strict;
64     use warnings;
65     use Moose;
66     
67     ::lives_ok {
68         has 'bar' => (metaclass => 'Bar::Meta::Attribute');     
69     } '... the attribute metaclass need not be a Moose::Meta::Attribute as long as it behaves';
70 }
71