a single non-hashref arg as Moose::Object does.
provide its own implementation. See Recipe 11 for an example
of all this. (thanks Dave Rolsky)
- added tests for this
+ * Moose::Meta::Method::Constructor
+ - when a single argument that wasn't a hashref was provided to
+ an immutabilized constructor, the error message was very
+ unhelpful, as opposed to the non-immutable error. Reported by
+ dew. (Thanks to Dave Rolsky)
+ - added test for this (also Dave Rolsky)
0.50 Thurs. Jun 11, 2008
- Fixed a version number issue by bumping all modules
$source .= "\n" . 'return $class->Moose::Object::new(@_)';
$source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
+ $source .= "\n" . 'confess "Single parameters to new() must be a HASH ref"';
+ $source .= "\n" . ' if scalar @_ == 1 && defined $_[0] && ref($_[0]) ne q{HASH};';
+
$source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
$source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+use Test::Exception;
+
+BEGIN {
+ use_ok('Moose');
+}
+
+=pod
+
+This tests to make sure that we provide the same error messages from
+an immutable constructor as is provided by a non-immutable
+constructor.
+
+=cut
+
+{
+ package Foo;
+ use Moose;
+
+ has 'foo' => (is => 'rw', isa => 'Int');
+
+ Foo->meta->make_immutable(debug => 0);
+}
+
+my $scalar = 1;
+throws_ok { Foo->new($scalar) } qr/\QSingle parameters to new() must be a HASH ref/,
+ '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';
+