From: Yuval Kogman Date: Thu, 26 Jun 2008 08:58:54 +0000 (+0000) Subject: test for BUILDARGS X-Git-Tag: 0_55~91^2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a136705d777111a3b66bef78b3a026049e68d9eb;p=gitmo%2FMoose.git test for BUILDARGS --- diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index 1012d08..6c37c14 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -125,7 +125,7 @@ sub _generate_BUILDARGS { my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS"); - if ( !$buildargs || $buildargs->body == \&Moose::Object::BUILDARGS and $args eq '@_') { + if ( $args eq '@_' and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS ) ) { return join("\n", 'do {', 'confess "Single parameters to new() must be a HASH ref"', diff --git a/t/300_immutable/009_buildargs.t b/t/300_immutable/009_buildargs.t new file mode 100644 index 0000000..e1a3016 --- /dev/null +++ b/t/300_immutable/009_buildargs.t @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More 'no_plan'; + +{ + package Foo; + use Moose; + + has bar => ( is => "rw" ); + + sub BUILDARGS { + my ( $self, @args ) = @_; + unshift @args, "bar" if @args % 2 == 1; + return {@args}; + } + + package Bar; + use Moose; + + extends qw(Foo); + + __PACKAGE__->meta->make_immutable; +} + +foreach my $class qw(Foo Bar) { + is( $class->new->bar, undef, "no args" ); + is( $class->new( bar => 42 )->bar, 42, "normal args" ); + is( $class->new( 37 )->bar, 37, "single arg" ); +}