Redid conversion to Test::Fatal
[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;
b10dde3a 7use Test::Fatal;
e185c027 8
7ff56534 9use Moose::Meta::Role;
f785aad8 10use Moose::Util::TypeConstraints ();
e185c027 11
12{
13 package FooRole;
d03bd989 14
e185c027 15 our $VERSION = '0.01';
d03bd989 16
e185c027 17 sub foo { 'FooRole::foo' }
18}
19
68efb014 20my $foo_role = Moose::Meta::Role->initialize('FooRole');
e185c027 21isa_ok($foo_role, 'Moose::Meta::Role');
68efb014 22isa_ok($foo_role, 'Class::MOP::Module');
e185c027 23
24is($foo_role->name, 'FooRole', '... got the right name of FooRole');
25is($foo_role->version, '0.01', '... got the right version of FooRole');
26
27# methods ...
28
29ok($foo_role->has_method('foo'), '... FooRole has the foo method');
093b12c2 30is($foo_role->get_method('foo')->body, \&FooRole::foo, '... FooRole got the foo method');
e185c027 31
a7d0cd00 32isa_ok($foo_role->get_method('foo'), 'Moose::Meta::Role::Method');
33
e185c027 34is_deeply(
35 [ $foo_role->get_method_list() ],
36 [ 'foo' ],
37 '... got the right method list');
d03bd989 38
e185c027 39# attributes ...
40
41is_deeply(
42 [ $foo_role->get_attribute_list() ],
43 [],
44 '... got the right attribute list');
45
46ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
47
b10dde3a 48is( exception {
e185c027 49 $foo_role->add_attribute('bar' => (is => 'rw', isa => 'Foo'));
b10dde3a 50}, undef, '... added the bar attribute okay' );
e185c027 51
52is_deeply(
53 [ $foo_role->get_attribute_list() ],
54 [ 'bar' ],
55 '... got the right attribute list');
56
57ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
58
f785aad8 59my $bar = $foo_role->get_attribute('bar');
60is_deeply( $bar->original_options, { is => 'rw', isa => 'Foo' },
61 'original options for bar attribute' );
62my $bar_for_class = $bar->attribute_for_class('Moose::Meta::Attribute');
63is(
64 $bar_for_class->type_constraint,
65 Moose::Util::TypeConstraints::class_type('Foo'),
66 'bar has a Foo class type'
67);
e185c027 68
b10dde3a 69is( exception {
e185c027 70 $foo_role->add_attribute('baz' => (is => 'ro'));
b10dde3a 71}, undef, '... added the baz attribute okay' );
e185c027 72
73is_deeply(
74 [ sort $foo_role->get_attribute_list() ],
75 [ 'bar', 'baz' ],
76 '... got the right attribute list');
77
78ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
79
f785aad8 80my $baz = $foo_role->get_attribute('baz');
81is_deeply( $baz->original_options, { is => 'ro' },
82 'original options for baz attribute' );
e185c027 83
b10dde3a 84is( exception {
e185c027 85 $foo_role->remove_attribute('bar');
b10dde3a 86}, undef, '... removed the bar attribute okay' );
e185c027 87
88is_deeply(
89 [ $foo_role->get_attribute_list() ],
90 [ 'baz' ],
91 '... got the right attribute list');
92
93ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
94ok($foo_role->has_attribute('baz'), '... FooRole does still have the baz attribute');
95
0558683c 96# method modifiers
97
98ok(!$foo_role->has_before_method_modifiers('boo'), '... no boo:before modifier');
99
100my $method = sub { "FooRole::boo:before" };
b10dde3a 101is( exception {
0558683c 102 $foo_role->add_before_method_modifier('boo' => $method);
b10dde3a 103}, undef, '... added a method modifier okay' );
0558683c 104
105ok($foo_role->has_before_method_modifiers('boo'), '... now we have a boo:before modifier');
106is(($foo_role->get_before_method_modifiers('boo'))[0], $method, '... got the right method back');
107
108is_deeply(
109 [ $foo_role->get_method_modifier_list('before') ],
110 [ 'boo' ],
111 '... got the right list of before method modifiers');
a28e50e4 112
113done_testing;