use Scalar::Util 'blessed', 'weaken', 'reftype';
use Carp 'confess';
-our $VERSION = '0.07';
+our $VERSION = '0.08';
use Moose::Util::TypeConstraints ();
sub _inline_check_lazy {
my $self = shift;
return '' unless $self->is_lazy;
+ if ($self->has_type_constraint) {
+ # NOTE:
+ # this could probably be cleaned
+ # up and streamlined a little more
+ return 'unless (exists $_[0]->{$attr_name}) {' .
+ ' if ($attr->has_default) {' .
+ ' my $default = $attr->default($_[0]);' .
+ ' (defined($attr->type_constraint->check($default)))' .
+ ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' .
+ ' . $attr->type_constraint->name . ") with " . (defined($default) ? "\'$default\'" : "undef")' .
+ ' if defined($default);' .
+ ' $_[0]->{$attr_name} = $default; ' .
+ ' }' .
+ ' else {' .
+ ' $_[0]->{$attr_name} = undef;' .
+ ' }' .
+ '}';
+ }
return '$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)'
. 'unless exists $_[0]->{$attr_name};';
}
use strict;
use warnings;
-use Test::More tests => 7;
+use Test::More tests => 10;
use Test::Exception;
BEGIN {
use_ok('Moose');
}
-## Roles
-
{
- package Test::TheDefaultFor::ArrayRef::and::HashRef;
- use Moose;
+ {
+ package Test::TheDefaultFor::ArrayRef::and::HashRef;
+ use Moose;
- has 'array_ref' => (is => 'rw', isa => 'ArrayRef');
- has 'hash_ref' => (is => 'rw', isa => 'HashRef');
+ has 'array_ref' => (is => 'rw', isa => 'ArrayRef');
+ has 'hash_ref' => (is => 'rw', isa => 'HashRef');
+
+ }
+
+ my $test = Test::TheDefaultFor::ArrayRef::and::HashRef->new;
+ isa_ok($test, 'Test::TheDefaultFor::ArrayRef::and::HashRef');
+
+ is_deeply($test->array_ref, [], '.... got the right default value');
+ is_deeply($test->hash_ref, {}, '.... got the right default value');
+
+ my $test2 = Test::TheDefaultFor::ArrayRef::and::HashRef->new(
+ array_ref => [ 1, 2, [] ],
+ hash_ref => { one => 1, two => 2, three => {} },
+ );
+ isa_ok($test2, 'Test::TheDefaultFor::ArrayRef::and::HashRef');
+ is_deeply($test2->array_ref, [ 1, 2, [] ], '.... got the right default value');
+ is_deeply($test2->hash_ref, { one => 1, two => 2, three => {} }, '.... got the right default value');
}
-my $test = Test::TheDefaultFor::ArrayRef::and::HashRef->new;
-isa_ok($test, 'Test::TheDefaultFor::ArrayRef::and::HashRef');
+{
+ {
+ package Test::For::Lazy::TypeConstraint;
+ use Moose;
+ use Moose::Util::TypeConstraints;
-is_deeply($test->array_ref, [], '.... got the right default value');
-is_deeply($test->hash_ref, {}, '.... got the right default value');
+ has 'bad_lazy_attr' => (
+ is => 'rw',
+ isa => 'ArrayRef',
+ lazy => 1,
+ default => sub { "test" },
+ );
+
+ has 'good_lazy_attr' => (
+ is => 'rw',
+ isa => 'ArrayRef',
+ lazy => 1,
+ default => sub { [] },
+ );
-my $test2 = Test::TheDefaultFor::ArrayRef::and::HashRef->new(
- array_ref => [ 1, 2, [] ],
- hash_ref => { one => 1, two => 2, three => {} },
-);
-isa_ok($test2, 'Test::TheDefaultFor::ArrayRef::and::HashRef');
+ }
-is_deeply($test2->array_ref, [ 1, 2, [] ], '.... got the right default value');
-is_deeply($test2->hash_ref, { one => 1, two => 2, three => {} }, '.... got the right default value');
\ No newline at end of file
+ my $test = Test::For::Lazy::TypeConstraint->new;
+ isa_ok($test, 'Test::For::Lazy::TypeConstraint');
+
+ dies_ok {
+ $test->bad_lazy_attr;
+ } '... this does not work';
+
+ lives_ok {
+ $test->good_lazy_attr;
+ } '... this does not work';
+}