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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
use Moo::_strictures;
use Test::More;
use Test::Fatal;
sub run_for {
my $class = shift;
my $obj = $class->new(less_than_three => 1);
is($obj->less_than_three, 1, "initial value set (${class})");
like(
exception { $obj->less_than_three(4) },
qr/isa check for "less_than_three" failed: 4 is not less than three/,
"exception thrown on bad set (${class})"
);
is($obj->less_than_three, 1, "initial value remains after bad set (${class})");
my $ret;
is(
exception { $ret = $obj->less_than_three(2) },
undef, "no exception on correct set (${class})"
);
is($ret, 2, "correct setter return (${class})");
is($obj->less_than_three, 2, "correct getter return (${class})");
is(exception { $class->new }, undef, "no exception with no value (${class})");
like(
exception { $class->new(less_than_three => 12) },
qr/isa check for "less_than_three" failed: 12 is not less than three/,
"exception thrown on bad constructor arg (${class})"
);
}
{
package Foo;
use Moo;
has less_than_three => (
is => 'rw',
isa => sub { die "$_[0] is not less than three" unless $_[0] < 3 }
);
}
run_for 'Foo';
{
package Bar;
use Sub::Quote;
use Moo;
has less_than_three => (
is => 'rw',
isa => quote_sub q{
my ($x) = @_;
die "$x is not less than three" unless $x < 3
}
);
}
run_for 'Bar';
{
package Baz;
use Sub::Quote;
use Moo;
has less_than_three => (
is => 'rw',
isa => quote_sub(
q{
my ($value) = @_;
die "$value is not less than ${word}" unless $value < $limit
},
{ '$limit' => \3, '$word' => \'three' }
)
);
}
run_for 'Baz';
my $lt3;
{
package LazyFoo;
use Sub::Quote;
use Moo;
has less_than_three => (
is => 'lazy',
isa => quote_sub(q{ die "$_[0] is not less than three" unless $_[0] < 3 })
);
sub _build_less_than_three { $lt3 }
}
$lt3 = 4;
my $lazyfoo = LazyFoo->new;
like(
exception { $lazyfoo->less_than_three },
qr/isa check for "less_than_three" failed: 4 is not less than three/,
"exception thrown on bad builder return value (LazyFoo)"
);
$lt3 = 2;
is(
exception { $lazyfoo->less_than_three },
undef,
'Corrected builder value on existing object returned ok'
);
is(LazyFoo->new->less_than_three, 2, 'Correct builder value returned ok');
{
package Fizz;
use Moo;
has attr1 => (
is => 'ro',
isa => sub {
no warnings 'once';
my $attr = $Method::Generate::Accessor::CurrentAttribute;
die bless [@$attr{'name', 'init_arg', 'step'}], 'MyException';
},
init_arg => 'attr_1',
);
}
my $e = exception { Fizz->new(attr_1 => 5) };
is(
ref($e),
'MyException',
'Exception objects passed though correctly',
);
is($e->[0], 'attr1', 'attribute name available in isa check');
is($e->[1], 'attr_1', 'attribute init_arg available in isa check');
is($e->[2], 'isa check', 'step available in isa check');
{
my $called;
local $SIG{__DIE__} = sub { $called++; die $_[0] };
my $e = exception { Fizz->new(attr_1 => 5) };
ok($called, '__DIE__ handler called if set')
}
{
package ClassWithDeadlyIsa;
use Moo;
has foo => (is => 'ro', isa => sub { die "nope" });
package ClassUsingDeadlyIsa;
use Moo;
has bar => (is => 'ro', coerce => sub { ClassWithDeadlyIsa->new(foo => $_[0]) });
}
like exception { ClassUsingDeadlyIsa->new(bar => 1) },
qr/isa check for "foo" failed: nope/,
'isa check within isa check produces correct exception';
{
package IsaWriter;
use Moo;
has attr => (
is => 'rwp',
isa => sub { die 'triggered' },
);
}
like exception { IsaWriter->new->_set_attr( 4 ) },
qr/triggered/, "isa triggered via writer";
{
package ClassWithEvilDestroy;
sub new { bless {}, $_[0] }
sub DESTROY {
eval {
1; # nop
};
}
package ClassWithEvilException;
use Moo;
has foo => (is => 'rw', isa => sub {
local $@;
die "welp";
});
has bar => (is => 'rw', isa => sub {
my $o = ClassWithEvilDestroy->new;
die "welp";
});
my $error;
has baz => (is => 'rw', isa => sub {
::is $@, $error, '$@ unchanged inside isa';
1;
});
my $o = ClassWithEvilException->new;
::like ::exception { $o->foo(1) }, qr/isa check for "foo" failed:/,
'got proper exception with localized $@';
::like ::exception { $o->bar(1) }, qr/isa check for "bar" failed:/,
'got proper exception with eval in DESTROY';
eval { die "blah\n" };
$error = $@;
$o->baz(1);
::is $@, $error, '$@ unchanged after successful isa';
}
done_testing;
|