DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / t / 030_roles / 001_meta_role.t
CommitLineData
e185c027 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7ff56534 6use Test::More tests => 27;
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
1808c2da 23is($foo_role->name, 'FooRole', 'got the right name of FooRole');
24is($foo_role->version, '0.01', 'got the right version of FooRole');
e185c027 25
26# methods ...
27
1808c2da 28ok($foo_role->has_method('foo'), 'FooRole has the foo method');
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' ],
1808c2da 36 'got the right method list');
d03bd989 37
e185c027 38# attributes ...
39
40is_deeply(
41 [ $foo_role->get_attribute_list() ],
42 [],
1808c2da 43 'got the right attribute list');
e185c027 44
1808c2da 45ok(!$foo_role->has_attribute('bar'), 'FooRole does not have the bar attribute');
e185c027 46
47lives_ok {
48 $foo_role->add_attribute('bar' => (is => 'rw', isa => 'Foo'));
1808c2da 49} 'added the bar attribute okay';
e185c027 50
51is_deeply(
52 [ $foo_role->get_attribute_list() ],
53 [ 'bar' ],
1808c2da 54 'got the right attribute list');
e185c027 55
1808c2da 56ok($foo_role->has_attribute('bar'), 'FooRole does have the bar attribute');
e185c027 57
58is_deeply(
59 $foo_role->get_attribute('bar'),
60 { is => 'rw', isa => 'Foo' },
1808c2da 61 'got the correct description of the bar attribute');
e185c027 62
63lives_ok {
64 $foo_role->add_attribute('baz' => (is => 'ro'));
1808c2da 65} 'added the baz attribute okay';
e185c027 66
67is_deeply(
68 [ sort $foo_role->get_attribute_list() ],
69 [ 'bar', 'baz' ],
1808c2da 70 'got the right attribute list');
e185c027 71
1808c2da 72ok($foo_role->has_attribute('baz'), 'FooRole does have the baz attribute');
e185c027 73
74is_deeply(
75 $foo_role->get_attribute('baz'),
76 { is => 'ro' },
1808c2da 77 'got the correct description of the baz attribute');
e185c027 78
79lives_ok {
80 $foo_role->remove_attribute('bar');
1808c2da 81} 'removed the bar attribute okay';
e185c027 82
83is_deeply(
84 [ $foo_role->get_attribute_list() ],
85 [ 'baz' ],
1808c2da 86 'got the right attribute list');
e185c027 87
1808c2da 88ok(!$foo_role->has_attribute('bar'), 'FooRole does not have the bar attribute');
89ok($foo_role->has_attribute('baz'), 'FooRole does still have the baz attribute');
e185c027 90
0558683c 91# method modifiers
92
1808c2da 93ok(!$foo_role->has_before_method_modifiers('boo'), 'no boo:before modifier');
0558683c 94
95my $method = sub { "FooRole::boo:before" };
96lives_ok {
97 $foo_role->add_before_method_modifier('boo' => $method);
1808c2da 98} 'added a method modifier okay';
0558683c 99
1808c2da 100ok($foo_role->has_before_method_modifiers('boo'), 'now we have a boo:before modifier');
101is(($foo_role->get_before_method_modifiers('boo'))[0], $method, 'got the right method back');
0558683c 102
103is_deeply(
104 [ $foo_role->get_method_modifier_list('before') ],
105 [ 'boo' ],
1808c2da 106 'got the right list of before method modifiers');