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'
=item I<required =E<gt> (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<undef> with
-an accessor.
+This marks the attribute as being required. This means a I<defined> value must be
+supplied during class construction, and the attribute may never be set to
+C<undef> with an accessor.
=item I<weak_ref =E<gt> (1|0)>
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
# 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) {
use strict;
use warnings;
-use Test::More tests => 14;
+use Test::More tests => 16;
use Test::Exception;
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);
}
}
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';