From: Stevan Little Date: Mon, 30 Jul 2007 19:21:33 +0000 (+0000) Subject: fixing required X-Git-Tag: 0_25~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ab859145de2d6d4a7c67f7281974bac0bc79bc68;p=gitmo%2FMoose.git fixing required --- diff --git a/Changes b/Changes index 6463b3f..717ae97 100644 --- a/Changes +++ b/Changes @@ -1,10 +1,15 @@ Revision history for Perl extension Moose 0.25 + * Moose::Meta::Attribute + - required attributes now will no longer accept undef + from the constructor, even if there is a default and lazy + - added tests for this + * Moose::Meta::Role - massive refactoring of this code - added serveral more tests - - tests for subtle conflict resolition bugs (thanks to kolibre) + - tests for subtle conflict resolition bugs (thanks to kolibre) * Moose - (Docs) referenced Moose::Util::TypeConstraints under 'isa' in 'has' diff --git a/lib/Moose.pm b/lib/Moose.pm index 0aa13d5..0201714 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -411,9 +411,9 @@ is expected to have consumed. =item I (1|0)> -This marks the attribute as being required. This means a value must be supplied -during class construction, and the attribute may never be set to C with -an accessor. +This marks the attribute as being required. This means a I value must be +supplied during class construction, and the attribute may never be set to +C with an accessor. =item I (1|0)> diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index c1613da..7bd0bbe 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -207,6 +207,10 @@ sub initialize_instance_slot { my $val; if (exists $params->{$init_arg}) { $val = $params->{$init_arg}; + + if (!defined $val && $self->is_required) { + confess "Attribute (" . $self->name . ") is required and cannot be undef"; + } } else { # skip it if it's lazy @@ -220,7 +224,7 @@ sub initialize_instance_slot { # attribute's default value (if it has one) if (!defined $val && $self->has_default) { $val = $self->default($instance); - } + } if (defined $val) { if ($self->has_type_constraint) { diff --git a/t/035_attribute_required.t b/t/035_attribute_required.t index 7248dce..8f098b2 100644 --- a/t/035_attribute_required.t +++ b/t/035_attribute_required.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 16; use Test::Exception; BEGIN { @@ -16,12 +16,6 @@ BEGIN { has 'bar' => (is => 'ro', required => 1); has 'baz' => (is => 'rw', default => 100, required => 1); - - # NOTE: - # this attribute is actually kind of silly - # since lazy requires default, then the - # required attribute becomes void in this - # case. But hey, best to test it :) has 'boo' => (is => 'rw', lazy => 1, default => 50, required => 1); } @@ -53,6 +47,14 @@ BEGIN { } throws_ok { + Foo->new(bar => 10, baz => undef); +} qr/^Attribute \(baz\) is required and cannot be undef/, '... must supply all the required attribute'; + +throws_ok { + Foo->new(bar => 10, boo => undef); +} qr/^Attribute \(boo\) is required and cannot be undef/, '... must supply all the required attribute'; + +throws_ok { Foo->new; } qr/^Attribute \(bar\) is required/, '... must supply all the required attribute';