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