die on attributes with no methods and no is => 'bare'
[gitmo/Moose.git] / t / 020_attributes / 007_attribute_custom_metaclass.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 16;
7 use Test::Exception;
8
9
10
11 {
12     package Foo::Meta::Attribute;
13     use Moose;
14
15     extends 'Moose::Meta::Attribute';
16
17     around 'new' => sub {
18         my $next = shift;
19         my $self = shift;
20         my $name = shift;
21         $next->($self, $name, (is => 'rw', isa => 'Foo'), @_);
22     };
23
24     package Foo;
25     use Moose;
26
27     has 'foo' => (metaclass => 'Foo::Meta::Attribute');
28 }
29 {
30     my $foo = Foo->new;
31     isa_ok($foo, 'Foo');
32
33     my $foo_attr = Foo->meta->get_attribute('foo');
34     isa_ok($foo_attr, 'Foo::Meta::Attribute');
35     isa_ok($foo_attr, 'Moose::Meta::Attribute');
36
37     is($foo_attr->name, 'foo', '... got the right name for our meta-attribute');
38     ok($foo_attr->has_accessor, '... our meta-attrubute created the accessor for us');
39
40     ok($foo_attr->has_type_constraint, '... our meta-attrubute created the type_constraint for us');
41
42     my $foo_attr_type_constraint = $foo_attr->type_constraint;
43     isa_ok($foo_attr_type_constraint, 'Moose::Meta::TypeConstraint');
44
45     is($foo_attr_type_constraint->name, 'Foo', '... got the right type constraint name');
46     is($foo_attr_type_constraint->parent->name, 'Object', '... got the right type constraint parent name');
47 }
48 {
49     package Bar::Meta::Attribute;
50     use Moose;
51
52     extends 'Class::MOP::Attribute';
53
54     package Bar;
55     use Moose;
56
57     ::lives_ok {
58         has 'bar' => (metaclass => 'Bar::Meta::Attribute');
59     } '... the attribute metaclass need not be a Moose::Meta::Attribute as long as it behaves';
60 }
61
62 {
63     package Moose::Meta::Attribute::Custom::Foo;
64     sub register_implementation { 'Foo::Meta::Attribute' }
65
66     package Moose::Meta::Attribute::Custom::Bar;
67     use Moose;
68
69     extends 'Moose::Meta::Attribute';
70
71     package Another::Foo;
72     use Moose;
73
74     ::lives_ok {
75         has 'foo' => (metaclass => 'Foo');
76     } '... the attribute metaclass alias worked correctly';
77
78     ::lives_ok {
79         has 'bar' => (metaclass => 'Bar', is => 'bare');
80     } '... the attribute metaclass alias worked correctly';
81 }
82
83 {
84     my $foo_attr = Another::Foo->meta->get_attribute('foo');
85     isa_ok($foo_attr, 'Foo::Meta::Attribute');
86     isa_ok($foo_attr, 'Moose::Meta::Attribute');
87
88     my $bar_attr = Another::Foo->meta->get_attribute('bar');
89     isa_ok($bar_attr, 'Moose::Meta::Attribute::Custom::Bar');
90     isa_ok($bar_attr, 'Moose::Meta::Attribute');
91 }
92
93