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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Moose();
{
my $exception = exception {
Moose::Meta::Method::Delegation->new;
};
like(
$exception,
qr/You must supply an attribute to construct with/,
"no attribute is given");
isa_ok(
$exception,
"Moose::Exception::MustSupplyAnAttributeToConstructWith",
"no attribute is given");
}
{
my $exception = exception {
Moose::Meta::Method::Delegation->new( attribute => "foo" );
};
like(
$exception,
qr/\QYou must supply an attribute which is a 'Moose::Meta::Attribute' instance/,
"attribute is not an instance of Moose::Meta::Attribute");
isa_ok(
$exception,
"Moose::Exception::MustSupplyAMooseMetaAttributeInstance",
"attribute is not an instance of Moose::Meta::Attribute");
}
{
my $attr = Moose::Meta::Attribute->new("foo");
my $exception = exception {
Moose::Meta::Method::Delegation->new( attribute => $attr );
};
like(
$exception,
qr/You must supply the package_name and name parameters/,
"package_name and name are not given");
isa_ok(
$exception,
"Moose::Exception::MustSupplyPackageNameAndName",
"package_name and name are not given");
}
{
my $attr = Moose::Meta::Attribute->new("foo");
my $exception = exception {
Moose::Meta::Method::Delegation->new( attribute => $attr, package_name => "Foo", name => "Foo" );
};
like(
$exception,
qr/You must supply a delegate_to_method which is a method name or a CODE reference/,
"delegate_to_method is not given");
isa_ok(
$exception,
"Moose::Exception::MustSupplyADelegateToMethod",
"delegate_to_method is not given");
}
{
my $attr = Moose::Meta::Attribute->new("foo");
my $exception = exception {
Moose::Meta::Method::Delegation->new( attribute => $attr,
package_name => "Foo",
name => "Foo",
delegate_to_method => sub {},
curried_arguments => {} );
};
like(
$exception,
qr/You must supply a curried_arguments which is an ARRAY reference/,
"curried_arguments not given");
isa_ok(
$exception,
"Moose::Exception::MustSupplyArrayRefAsCurriedArguments",
"curried_arguments not given");
}
{
{
package BadClass;
use Moose;
has 'foo' => (
is => 'ro',
handles => { get_count => 'count' }
);
}
my $object = BadClass->new;
my $exception = exception {
$object->get_count;
};
like(
$exception,
qr/Cannot delegate get_count to count because the value of foo is not defined/,
"foo is not set");
isa_ok(
$exception,
"Moose::Exception::AttributeValueIsNotDefined",
"foo is not set");
is(
$exception->instance,
$object,
"foo is not set");
is(
$exception->attribute->name,
"foo",
"foo is not set");
}
{
{
package BadClass2;
use Moose;
has 'foo' => (
is => 'ro',
handles => { get_count => 'count' }
);
}
my $array = [12];
my $object = BadClass2->new( foo => $array );
my $exception = exception {
$object->get_count;
};
like(
$exception,
qr/\QCannot delegate get_count to count because the value of foo is not an object (got '$array')/,
"value of foo is an ARRAY ref");
#Cannot delegate get_count to count because the value of foo is not an object (got 'ARRAY(0x223f578)')
isa_ok(
$exception,
"Moose::Exception::AttributeValueIsNotAnObject",
"value of foo is an ARRAY ref");
is(
$exception->given_value,
$array,
"value of foo is an ARRAY ref");
is(
$exception->attribute->name,
"foo",
"value of foo is an ARRAY ref");
}
done_testing;
|