[Moose-commits] r7784 - in Moose/trunk: . lib/Moose
lib/Moose/Meta/Method t/010_basics t/100_bugs t/300_immutable
autarch at code2.0beta.co.uk
autarch at code2.0beta.co.uk
Mon Feb 23 21:22:26 GMT 2009
Author: autarch
Date: 2009-02-23 13:22:26 -0800 (Mon, 23 Feb 2009)
New Revision: 7784
Removed:
Moose/trunk/t/100_bugs/008_new_w_undef.t
Modified:
Moose/trunk/Changes
Moose/trunk/lib/Moose/Meta/Method/Constructor.pm
Moose/trunk/lib/Moose/Object.pm
Moose/trunk/t/010_basics/017_error_handling.t
Moose/trunk/t/100_bugs/011_DEMOLISH_eats_exceptions.t
Moose/trunk/t/300_immutable/008_immutable_constructor_error.t
Log:
Handle Foo->new(undef) consistently, with an error saying a single param to new() must be a hashref
Modified: Moose/trunk/Changes
===================================================================
--- Moose/trunk/Changes 2009-02-23 19:58:00 UTC (rev 7783)
+++ Moose/trunk/Changes 2009-02-23 21:22:26 UTC (rev 7784)
@@ -1,5 +1,13 @@
Revision history for Perl extension Moose
+0.72
+ * Moose::Object
+ * Moose::Meta::Method::Constructor
+ - A mutable class accepted Foo->new(undef) without complaint,
+ while an immutable class would blow up with an unhelpful
+ error. Now, in both cases we throw a helpful error
+ instead. Reported by doy.
+
0.71_01 Sun, February 22, 2009
* Moose::Cookbook
- Hopefully fixed some POD errors in a few recipes that caused
Modified: Moose/trunk/lib/Moose/Meta/Method/Constructor.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/Method/Constructor.pm 2009-02-23 19:58:00 UTC (rev 7783)
+++ Moose/trunk/lib/Moose/Meta/Method/Constructor.pm 2009-02-23 21:22:26 UTC (rev 7784)
@@ -177,7 +177,7 @@
'@type_constraint_bodies' => \@type_constraint_bodies,
},
) or $self->throw_error("Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source );
-
+
$self->{'body'} = $code;
}
@@ -190,7 +190,7 @@
return join("\n",
'do {',
$self->_inline_throw_error('"Single parameters to new() must be a HASH ref"', 'data => $_[0]'),
- ' if scalar @_ == 1 && defined $_[0] && ref($_[0]) ne q{HASH};',
+ ' if scalar @_ == 1 && !( defined $_[0] && ref $_[0] eq q{HASH} );',
'(scalar @_ == 1) ? {%{$_[0]}} : {@_};',
'}',
);
Modified: Moose/trunk/lib/Moose/Object.pm
===================================================================
--- Moose/trunk/lib/Moose/Object.pm 2009-02-23 19:58:00 UTC (rev 7783)
+++ Moose/trunk/lib/Moose/Object.pm 2009-02-23 21:22:26 UTC (rev 7784)
@@ -21,16 +21,14 @@
sub BUILDARGS {
my $class = shift;
- if (scalar @_ == 1) {
- if (defined $_[0]) {
- (ref($_[0]) eq 'HASH')
- || $class->meta->throw_error("Single parameters to new() must be a HASH ref", data => $_[0]);
- return {%{$_[0]}};
- }
- else {
- return {}; # FIXME this is compat behavior, but is it correct?
+ if ( scalar @_ == 1 ) {
+ unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
+ $class->meta->throw_error(
+ "Single parameters to new() must be a HASH ref",
+ data => $_[0] );
}
- }
+ return { %{ $_[0] } };
+ }
else {
return {@_};
}
Modified: Moose/trunk/t/010_basics/017_error_handling.t
===================================================================
--- Moose/trunk/t/010_basics/017_error_handling.t 2009-02-23 19:58:00 UTC (rev 7783)
+++ Moose/trunk/t/010_basics/017_error_handling.t 2009-02-23 21:22:26 UTC (rev 7784)
@@ -3,7 +3,7 @@
use strict;
use warnings;
-use Test::More tests => 2;
+use Test::More tests => 3;
use Test::Exception;
# This tests the error handling in Moose::Object only
@@ -15,6 +15,8 @@
throws_ok { Foo->new('bad') } qr/^\QSingle parameters to new() must be a HASH ref/,
'A single non-hashref arg to a constructor throws an error';
+throws_ok { Foo->new(undef) } qr/^\QSingle parameters to new() must be a HASH ref/,
+ 'A single non-hashref arg to a constructor throws an error';
throws_ok { Foo->does() } qr/^\QYou much supply a role name to does()/,
'Cannot call does() without a role name';
Deleted: Moose/trunk/t/100_bugs/008_new_w_undef.t
===================================================================
--- Moose/trunk/t/100_bugs/008_new_w_undef.t 2009-02-23 19:58:00 UTC (rev 7783)
+++ Moose/trunk/t/100_bugs/008_new_w_undef.t 2009-02-23 21:22:26 UTC (rev 7784)
@@ -1,21 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-
-use Test::More tests => 1;
-use Test::Exception;
-
-{
- package Foo;
- use Moose;
- has 'foo' => ( is => 'ro' );
-}
-
-lives_ok {
- Foo->new(undef);
-} '... passing in undef just gets ignored';
-
-
-
-
Modified: Moose/trunk/t/100_bugs/011_DEMOLISH_eats_exceptions.t
===================================================================
--- Moose/trunk/t/100_bugs/011_DEMOLISH_eats_exceptions.t 2009-02-23 19:58:00 UTC (rev 7783)
+++ Moose/trunk/t/100_bugs/011_DEMOLISH_eats_exceptions.t 2009-02-23 21:22:26 UTC (rev 7784)
@@ -116,7 +116,7 @@
}
{
local $@;
- my $obj = eval { $pkg->new ( undef ); };
+ my $obj = eval { $pkg->new ( notanattr => 1 ); };
::like( $@, qr/is required/, "... $pkg undef" );
::is( $obj, undef, "... the object is undef" );
}
Modified: Moose/trunk/t/300_immutable/008_immutable_constructor_error.t
===================================================================
--- Moose/trunk/t/300_immutable/008_immutable_constructor_error.t 2009-02-23 19:58:00 UTC (rev 7783)
+++ Moose/trunk/t/300_immutable/008_immutable_constructor_error.t 2009-02-23 21:22:26 UTC (rev 7784)
@@ -3,7 +3,7 @@
use strict;
use warnings;
-use Test::More tests => 2;
+use Test::More tests => 3;
use Test::Exception;
@@ -30,4 +30,6 @@
'Non-ref provided to immutable constructor gives useful error message';
throws_ok { Foo->new(\$scalar) } qr/\QSingle parameters to new() must be a HASH ref/,
'Scalar ref provided to immutable constructor gives useful error message';
+throws_ok { Foo->new(undef) } qr/\QSingle parameters to new() must be a HASH ref/,
+ 'undef provided to immutable constructor gives useful error message';
More information about the Moose-commits
mailing list