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