Subject: | Maybe[] breaks coerce=>1 |
Maybe[] breaks coerce=>1. The following is a test script that
demonstrates the problem:
===== BEGIN TEST CODE =====
BEGIN {
package Class;
use strict;
use warnings;
use Moose;
has val => (
is => 'rw',
isa => 'Num',
required => 1,
);
use Moose::Util::TypeConstraints;
coerce __PACKAGE__
=> from 'Num'
=> via { __PACKAGE__->new(val => $_) };
}
BEGIN {
package Container;
use strict;
use warnings;
use Moose;
has always => (
is => 'rw',
isa => 'Class',
coerce => 1,
default => sub { Class->new(val => 0) },
);
has maybe => (
is => 'rw',
isa => 'Maybe[Class]',
coerce => 1,
default => undef,
);
}
use strict;
use warnings;
use Test::More tests => 15;
my $always_fail = qr/^Attribute \(always\) does not pass the type
constraint/;
my $maybe_fail = qr/^Attribute \(maybe\) does not pass the type
constraint/;
my $o = Class->new(val => 123);
eval { Container->new() }; is($@, '');
eval { Container->new(always => $o) }; is($@, '');
eval { Container->new(always => 123) }; is($@, '');
eval { Container->new(always => "abc") }; like($@, $always_fail);
eval { Container->new(maybe => undef) }; is($@, '');
eval { Container->new(maybe => $o) }; is($@, '');
eval { Container->new(maybe => 123) }; is($@, '');
eval { Container->new(maybe => "abc") }; like($@, $maybe_fail);
my $c = Container->new();
eval { $c->always($o) }; is($@, '');
eval { $c->always(123) }; is($@, '');
eval { $c->always("abc") }; like($@, $always_fail);
eval { $c->maybe(undef) }; is($@, '');
eval { $c->maybe($o) }; is($@, '');
eval { $c->maybe(123) }; is($@, '');
eval { $c->maybe("abc") }; like($@, $maybe_fail);
===== END TEST CODE =====
(Workaround: http://www.perlmonks.org/?node_id=843554)