Real attribute objects in roles is now working, with a few hacks and changes to the...
[gitmo/Moose.git] / t / 030_roles / 001_meta_role.t
CommitLineData
e185c027 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
e185c027 7use Test::Exception;
8
7ff56534 9use Moose::Meta::Role;
e185c027 10
11{
12 package FooRole;
d03bd989 13
e185c027 14 our $VERSION = '0.01';
d03bd989 15
e185c027 16 sub foo { 'FooRole::foo' }
17}
18
68efb014 19my $foo_role = Moose::Meta::Role->initialize('FooRole');
e185c027 20isa_ok($foo_role, 'Moose::Meta::Role');
68efb014 21isa_ok($foo_role, 'Class::MOP::Module');
e185c027 22
23is($foo_role->name, 'FooRole', '... got the right name of FooRole');
24is($foo_role->version, '0.01', '... got the right version of FooRole');
25
26# methods ...
27
28ok($foo_role->has_method('foo'), '... FooRole has the foo method');
093b12c2 29is($foo_role->get_method('foo')->body, \&FooRole::foo, '... FooRole got the foo method');
e185c027 30
a7d0cd00 31isa_ok($foo_role->get_method('foo'), 'Moose::Meta::Role::Method');
32
e185c027 33is_deeply(
34 [ $foo_role->get_method_list() ],
35 [ 'foo' ],
36 '... got the right method list');
d03bd989 37
e185c027 38# attributes ...
39
40is_deeply(
41 [ $foo_role->get_attribute_list() ],
42 [],
43 '... got the right attribute list');
44
45ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
46
47lives_ok {
48 $foo_role->add_attribute('bar' => (is => 'rw', isa => 'Foo'));
49} '... added the bar attribute okay';
50
51is_deeply(
52 [ $foo_role->get_attribute_list() ],
53 [ 'bar' ],
54 '... got the right attribute list');
55
56ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
57
721b5f29 58my $bar = $foo_role->get_attribute('bar');
59is( $bar->get_read_method, 'bar', 'bar has a reader named bar' );
60is( $bar->get_write_method, 'bar', 'bar has a writer named bar' );
61is(
62 $bar->type_constraint,
63 Moose::Util::TypeConstraints::class_type('Foo'),
64 'bar has a Foo class type'
65);
e185c027 66
67lives_ok {
68 $foo_role->add_attribute('baz' => (is => 'ro'));
69} '... added the baz attribute okay';
70
71is_deeply(
72 [ sort $foo_role->get_attribute_list() ],
73 [ 'bar', 'baz' ],
74 '... got the right attribute list');
75
76ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
77
721b5f29 78my $baz = $foo_role->get_attribute('baz');
79is( $baz->get_read_method, 'baz', 'baz has a reader named baz' );
80is( $baz->get_write_method, undef, 'baz does not have a writer' );
e185c027 81
82lives_ok {
83 $foo_role->remove_attribute('bar');
84} '... removed the bar attribute okay';
85
86is_deeply(
87 [ $foo_role->get_attribute_list() ],
88 [ 'baz' ],
89 '... got the right attribute list');
90
91ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
92ok($foo_role->has_attribute('baz'), '... FooRole does still have the baz attribute');
93
0558683c 94# method modifiers
95
96ok(!$foo_role->has_before_method_modifiers('boo'), '... no boo:before modifier');
97
98my $method = sub { "FooRole::boo:before" };
99lives_ok {
100 $foo_role->add_before_method_modifier('boo' => $method);
101} '... added a method modifier okay';
102
103ok($foo_role->has_before_method_modifiers('boo'), '... now we have a boo:before modifier');
104is(($foo_role->get_before_method_modifiers('boo'))[0], $method, '... got the right method back');
105
106is_deeply(
107 [ $foo_role->get_method_modifier_list('before') ],
108 [ 'boo' ],
109 '... got the right list of before method modifiers');
a28e50e4 110
111done_testing;