if (($spec->{trigger}||0) eq 1) {
$spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)');
}
- if (exists $spec->{default}) {
- my $default = $spec->{default};
- my $invalid = "Invalid default '" . overload::StrVal($default)
+
+ for my $setting (qw( default coerce )) {
+ next if !exists $spec->{$setting};
+ my $value = $spec->{$setting};
+ my $invalid = "Invalid $setting '" . overload::StrVal($value)
. "' for $into->$name - not a coderef";
die "$invalid or code-convertible object"
- unless ref $default and (ref $default eq 'CODE' or blessed($default));
+ unless ref $value and (ref $value eq 'CODE' or blessed($value));
die "$invalid and could not be converted to a coderef: $@"
- if !eval { \&$default };
+ if !eval { \&$value };
}
my %methods;
use Moo;
}
+{
+ package WithOverload;
+ use overload '&{}' => sub { sub { 5 } };
+ sub new { bless {} }
+}
+
$gen->generate_method('Foo' => 'one' => { is => 'ro' });
$gen->generate_method('Foo' => 'two' => { is => 'rw' });
qr/Unknown is purple/, 'is purple rejected'
);
-like(
- exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', default => 5 }) },
- qr/Invalid default/, 'default - scalar rejected'
-);
-
-like(
- exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', default => [] }) },
- qr/Invalid default/, 'default - arrayref rejected'
-);
-
-like(
- exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', default => Foo->new }) },
- qr/Invalid default/, 'default - non-code-convertible object rejected'
-);
-
-is(
- exception { $gen->generate_method('Foo' => 'six' => { is => 'ro', default => sub { 5 } }) },
- undef, 'default - coderef accepted'
-);
-
-is(
- exception { $gen->generate_method('Foo' => 'seven' => { is => 'ro', default => bless sub { 5 } => 'Blah' }) },
- undef, 'default - blessed sub accepted'
-);
-
-{
- package WithOverload;
- use overload '&{}' => sub { sub { 5 } };
- sub new { bless {} }
+for my $setting (qw( default coerce )) {
+ like(
+ exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', $setting => 5 }) },
+ qr/Invalid $setting/, "$setting - scalar rejected"
+ );
+
+ like(
+ exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', $setting => [] }) },
+ qr/Invalid $setting/, "$setting - arrayref rejected"
+ );
+
+ like(
+ exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', $setting => Foo->new }) },
+ qr/Invalid $setting/, "$setting - non-code-convertible object rejected"
+ );
+
+ is(
+ exception { $gen->generate_method('Foo' => 'six' => { is => 'ro', $setting => sub { 5 } }) },
+ undef, "$setting - coderef accepted"
+ );
+
+ is(
+ exception { $gen->generate_method('Foo' => 'seven' => { is => 'ro', $setting => bless sub { 5 } => 'Blah' }) },
+ undef, "$setting - blessed sub accepted"
+ );
+
+ is(
+ exception { $gen->generate_method('Foo' => 'eight' => { is => 'ro', $setting => WithOverload->new }) },
+ undef, "$setting - object with overloaded ->() accepted"
+ );
+
+ like(
+ exception { $gen->generate_method('Foo' => 'nine' => { is => 'ro', $setting => bless {} => 'Blah' }) },
+ qr/Invalid $setting/, "$setting - object rejected"
+ );
}
-is(
- exception { $gen->generate_method('Foo' => 'eight' => { is => 'ro', default => WithOverload->new }) },
- undef, 'default - object with overloaded ->() accepted'
-);
-
-like(
- exception { $gen->generate_method('Foo' => 'nine' => { is => 'ro', default => bless {} => 'Blah' }) },
- qr/Invalid default/, 'default - object rejected'
-);
-
my $foo = Foo->new(one => 1);
is($foo->one, 1, 'ro reads');