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