summaryrefslogtreecommitdiff
path: root/t/roles/meta_role.t
blob: 284d28b87f2ccc7237903dc0cc3be35982f6e908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use strict;
use warnings;

use Test::More;
use Test::Fatal;

use Moose::Meta::Role;
use Moose::Util::TypeConstraints ();

{
    package FooRole;

    our $VERSION = '0.01';

    sub foo { 'FooRole::foo' }
}

my $foo_role = Moose::Meta::Role->initialize('FooRole');
isa_ok($foo_role, 'Moose::Meta::Role');
isa_ok($foo_role, 'Class::MOP::Module');

is($foo_role->name, 'FooRole', '... got the right name of FooRole');
is($foo_role->version, '0.01', '... got the right version of FooRole');

# methods ...

ok($foo_role->has_method('foo'), '... FooRole has the foo method');
is($foo_role->get_method('foo')->body, \&FooRole::foo, '... FooRole got the foo method');

isa_ok($foo_role->get_method('foo'), 'Moose::Meta::Role::Method');

is_deeply(
    [ $foo_role->get_method_list() ],
    [ 'foo' ],
    '... got the right method list');

# attributes ...

is_deeply(
    [ $foo_role->get_attribute_list() ],
    [],
    '... got the right attribute list');

ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');

is( exception {
    $foo_role->add_attribute('bar' => (is => 'rw', isa => 'Foo'));
}, undef, '... added the bar attribute okay' );

is_deeply(
    [ $foo_role->get_attribute_list() ],
    [ 'bar' ],
    '... got the right attribute list');

ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');

my $bar = $foo_role->get_attribute('bar');
is_deeply( $bar->original_options, { is => 'rw', isa => 'Foo' },
    'original options for bar attribute' );
my $bar_for_class = $bar->attribute_for_class('Moose::Meta::Attribute');
is(
    $bar_for_class->type_constraint,
    Moose::Util::TypeConstraints::class_type('Foo'),
    'bar has a Foo class type'
);

is( exception {
    $foo_role->add_attribute('baz' => (is => 'ro'));
}, undef, '... added the baz attribute okay' );

is_deeply(
    [ sort $foo_role->get_attribute_list() ],
    [ 'bar', 'baz' ],
    '... got the right attribute list');

ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');

my $baz = $foo_role->get_attribute('baz');
is_deeply( $baz->original_options, { is => 'ro' },
    'original options for baz attribute' );

is( exception {
    $foo_role->remove_attribute('bar');
}, undef, '... removed the bar attribute okay' );

is_deeply(
    [ $foo_role->get_attribute_list() ],
    [ 'baz' ],
    '... got the right attribute list');

ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
ok($foo_role->has_attribute('baz'), '... FooRole does still have the baz attribute');

# method modifiers

ok(!$foo_role->has_before_method_modifiers('boo'), '... no boo:before modifier');

my $method = sub { "FooRole::boo:before" };
is( exception {
    $foo_role->add_before_method_modifier('boo' => $method);
}, undef, '... added a method modifier okay' );

ok($foo_role->has_before_method_modifiers('boo'), '... now we have a boo:before modifier');
is(($foo_role->get_before_method_modifiers('boo'))[0], $method, '... got the right method back');

is_deeply(
    [ $foo_role->get_method_modifier_list('before') ],
    [ 'boo' ],
    '... got the right list of before method modifiers');

done_testing;