DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / t / 020_attributes / 007_attribute_custom_metaclass.t
CommitLineData
b0ea39ef 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7ff56534 6use Test::More tests => 16;
b0ea39ef 7use Test::Exception;
8
7ff56534 9
b0ea39ef 10
d03bd989 11{
b0ea39ef 12 package Foo::Meta::Attribute;
d500266f 13 use Moose;
d03bd989 14
d500266f 15 extends 'Moose::Meta::Attribute';
d03bd989 16
d500266f 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 };
b0ea39ef 23
24 package Foo;
b0ea39ef 25 use Moose;
d03bd989 26
b0ea39ef 27 has 'foo' => (metaclass => 'Foo::Meta::Attribute');
28}
c1935ade 29{
30 my $foo = Foo->new;
31 isa_ok($foo, 'Foo');
b0ea39ef 32
c1935ade 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');
b0ea39ef 36
1808c2da 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');
b0ea39ef 39
1808c2da 40 ok($foo_attr->has_type_constraint, 'our meta-attrubute created the type_constraint for us');
b0ea39ef 41
c1935ade 42 my $foo_attr_type_constraint = $foo_attr->type_constraint;
43 isa_ok($foo_attr_type_constraint, 'Moose::Meta::TypeConstraint');
590868a3 44
1808c2da 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');
c1935ade 47}
590868a3 48{
49 package Bar::Meta::Attribute;
d500266f 50 use Moose;
d03bd989 51
52 extends 'Class::MOP::Attribute';
53
590868a3 54 package Bar;
590868a3 55 use Moose;
d03bd989 56
d500266f 57 ::lives_ok {
d03bd989 58 has 'bar' => (metaclass => 'Bar::Meta::Attribute');
1808c2da 59 } 'the attribute metaclass need not be a Moose::Meta::Attribute as long as it behaves';
590868a3 60}
61
c1935ade 62{
63 package Moose::Meta::Attribute::Custom::Foo;
64 sub register_implementation { 'Foo::Meta::Attribute' }
d03bd989 65
c1935ade 66 package Moose::Meta::Attribute::Custom::Bar;
67 use Moose;
d03bd989 68
c1935ade 69 extends 'Moose::Meta::Attribute';
d03bd989 70
c1935ade 71 package Another::Foo;
72 use Moose;
d03bd989 73
c1935ade 74 ::lives_ok {
d03bd989 75 has 'foo' => (metaclass => 'Foo');
1808c2da 76 } 'the attribute metaclass alias worked correctly';
d03bd989 77
c1935ade 78 ::lives_ok {
ccd4cff9 79 has 'bar' => (metaclass => 'Bar', is => 'bare');
1808c2da 80 } 'the attribute metaclass alias worked correctly';
c1935ade 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');
d03bd989 87
c1935ade 88 my $bar_attr = Another::Foo->meta->get_attribute('bar');
89 isa_ok($bar_attr, 'Moose::Meta::Attribute::Custom::Bar');
d03bd989 90 isa_ok($bar_attr, 'Moose::Meta::Attribute');
c1935ade 91}
92
93