From: Dave Rolsky Date: Fri, 23 Jan 2009 18:19:26 +0000 (+0000) Subject: Add tests and fix for implicit object construction skipping BUILD & X-Git-Tag: 0.15~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Singleton.git;a=commitdiff_plain;h=e2119ce999a1d02eb970f750abe8866cf64c5552 Add tests and fix for implicit object construction skipping BUILD & BUILDARGS. --- diff --git a/ChangeLog b/ChangeLog index be1a3d4..db4a905 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ Revision history for Perl extension MooseX-Singleton +0.15 + - When an object was implicitly constructed by calling + ClassName->attribute, it skipped the BUILD and BUILDARGS defined + for a class. Report and test from Josh in RT #42690. (Dave + Rolsky) + 0.14 2009-01-22 - Converted to use new method generation helpers in the most recent Moose (0.65) and Class::MOP (Dave Rolsky) diff --git a/lib/MooseX/Singleton/Meta/Instance.pm b/lib/MooseX/Singleton/Meta/Instance.pm index c6a0a3c..a56db58 100644 --- a/lib/MooseX/Singleton/Meta/Instance.pm +++ b/lib/MooseX/Singleton/Meta/Instance.pm @@ -16,7 +16,9 @@ sub get_singleton_instance { no strict 'refs'; return ${"$instance\::singleton"} if defined ${"$instance\::singleton"}; - return $instance->meta->construct_instance; + # We need to go through ->new in order to make sure BUILD and + # BUILDARGS get called. + return $instance->meta->name->new; } sub clone_instance { diff --git a/t/004-build_bug.t b/t/004-build_bug.t new file mode 100644 index 0000000..a46c803 --- /dev/null +++ b/t/004-build_bug.t @@ -0,0 +1,36 @@ +use strict; +use warnings; + +use Test::More 'no_plan'; + +{ + package MySingleton; + use MooseX::Singleton; + + has 'attrib' => + is => 'rw', + isa => 'Str', + default => 'foo'; + + sub hello {'world'} + + sub BUILDARGS { + my ( $class, %opts ) = @_; + + { attrib => 'bar', %opts }; + } +} + +is( + MySingleton->attrib, 'bar', + 'BUILDARGS changed value of attrib when instance was auto-instantiated' +); + +MySingleton->meta->remove_package_glob('singleton'); + +MySingleton->instance; + +is( + MySingleton->attrib, 'bar', + 'BUILDARGS changed value of attrib when instance was explicitly instantiated' +); diff --git a/t/005-build_bug-immutable.t b/t/005-build_bug-immutable.t new file mode 100644 index 0000000..4b6c1d3 --- /dev/null +++ b/t/005-build_bug-immutable.t @@ -0,0 +1,38 @@ +use strict; +use warnings; + +use Test::More 'no_plan'; + +{ + package MySingleton; + use MooseX::Singleton; + + has 'attrib' => + is => 'rw', + isa => 'Str', + default => 'foo'; + + sub hello {'world'} + + sub BUILDARGS { + my ( $class, %opts ) = @_; + + { attrib => 'bar', %opts }; + } + + __PACKAGE__->meta->make_immutable; +} + +is( + MySingleton->attrib, 'bar', + 'BUILDARGS changed value of attrib when instance was auto-instantiated' +); + +MySingleton->meta->remove_package_glob('singleton'); + +MySingleton->instance; + +is( + MySingleton->attrib, 'bar', + 'BUILDARGS changed value of attrib when instance was explicitly instantiated' +);