($code, delete $self->{captures});
}
+sub _attr_desc {
+ my ($name, $init_arg) = @_;
+ return perlstring($name) if !defined($init_arg) or $init_arg eq $name;
+ return perlstring($name).' (constructor argument: '.perlstring($init_arg).')';
+}
+
sub _generate_coerce {
- my ($self, $name, $value, $coerce) = @_;
+ my ($self, $name, $value, $coerce, $init_arg) = @_;
$self->_generate_die_prefix(
- "coercion for ${\perlstring($name)} failed: ",
+ "coercion for ${\_attr_desc($name, $init_arg)} failed: ",
$self->_generate_call_code($name, 'coerce', "${value}", $coerce)
);
}
}
sub _generate_isa_check {
- my ($self, $name, $value, $check) = @_;
+ my ($self, $name, $value, $check, $init_arg) = @_;
$self->_generate_die_prefix(
- "isa check for ${\perlstring($name)} failed: ",
+ "isa check for ${\_attr_desc($name, $init_arg)} failed: ",
$self->_generate_call_code($name, 'isa_check', $value, $check)
);
}
}
sub _generate_populate_set {
- my ($self, $me, $name, $spec, $source, $test) = @_;
+ my ($self, $me, $name, $spec, $source, $test, $init_arg) = @_;
if ($self->has_eager_default($name, $spec)) {
my $get_indent = ' ' x ($spec->{isa} ? 6 : 4);
my $get_default = $self->_generate_get_default(
if ($spec->{coerce}) {
$get_value = $self->_generate_coerce(
$name, $get_value,
- $spec->{coerce}
+ $spec->{coerce}, $init_arg
)
}
($spec->{isa}
? " {\n my \$value = ".$get_value.";\n "
.$self->_generate_isa_check(
- $name, '$value', $spec->{isa}
+ $name, '$value', $spec->{isa}, $init_arg
).";\n"
.' '.$self->_generate_simple_set($me, $name, $spec, '$value').";\n"
." }\n"
? " $source = "
.$self->_generate_coerce(
$name, $source,
- $spec->{coerce}
+ $spec->{coerce}, $init_arg
).";\n"
: ""
)
.($spec->{isa}
? " "
.$self->_generate_isa_check(
- $name, $source, $spec->{isa}
+ $name, $source, $spec->{isa}, $init_arg
).";\n"
: ""
)
--- /dev/null
+use strictures 1;
+use Test::More;
+use Test::Fatal;
+
+{
+ package Foo;
+
+ use Moo;
+
+ has optional => (
+ is => 'rw',
+ init_arg => 'might_have',
+ isa => sub { die "isa" if $_[0] % 2 },
+ default => sub { 7 },
+ );
+
+ has lazy => (
+ is => 'rw',
+ init_arg => 'workshy',
+ isa => sub { die "aieee" if $_[0] % 2 },
+ default => sub { 7 },
+ lazy => 1,
+ );
+}
+
+like(
+ exception { Foo->new },
+ qr/\Aisa check for "optional" \(constructor argument: "might_have"\) failed:/,
+ "isa default"
+);
+
+like(
+ exception { Foo->new(might_have => 3) },
+ qr/\Aisa check for "optional" \(constructor argument: "might_have"\) failed:/,
+ "isa init_arg",
+);
+
+is(
+ exception { Foo->new(might_have => 2) },
+ undef, "isa init_arg ok"
+);
+
+my $foo = Foo->new(might_have => 2);
+
+like(
+ exception { $foo->optional(3) },
+ qr/\Aisa check for "optional" failed:/,
+ "isa accessor",
+);
+
+like(
+ exception { $foo->lazy },
+ qr/\Aisa check for "lazy" failed:/,
+ "lazy accessor",
+);
+
+like(
+ exception { $foo->lazy(3) },
+ qr/\Aisa check for "lazy" failed:/,
+ "lazy set isa fail",
+);
+
+is(
+ exception { $foo->lazy(4) },
+ undef,
+ "lazy set isa ok",
+);
+
+like(
+ exception { Foo->new(might_have => 2, workshy => 3) },
+ qr/\Aisa check for "lazy" \(constructor argument: "workshy"\) failed:/,
+ "lazy init_arg",
+);
+
+done_testing;