[Moose-commits] r7384 - in MooseX-Singleton/trunk: . lib/MooseX/Singleton/Meta t

autarch at code2.0beta.co.uk autarch at code2.0beta.co.uk
Fri Jan 23 18:19:26 GMT 2009


Author: autarch
Date: 2009-01-23 10:19:26 -0800 (Fri, 23 Jan 2009)
New Revision: 7384

Added:
   MooseX-Singleton/trunk/t/004-build_bug.t
   MooseX-Singleton/trunk/t/005-build_bug-immutable.t
Modified:
   MooseX-Singleton/trunk/ChangeLog
   MooseX-Singleton/trunk/lib/MooseX/Singleton/Meta/Instance.pm
Log:
Add tests and fix for implicit object construction skipping BUILD &
BUILDARGS.

Modified: MooseX-Singleton/trunk/ChangeLog
===================================================================
--- MooseX-Singleton/trunk/ChangeLog	2009-01-23 17:59:38 UTC (rev 7383)
+++ MooseX-Singleton/trunk/ChangeLog	2009-01-23 18:19:26 UTC (rev 7384)
@@ -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)

Modified: MooseX-Singleton/trunk/lib/MooseX/Singleton/Meta/Instance.pm
===================================================================
--- MooseX-Singleton/trunk/lib/MooseX/Singleton/Meta/Instance.pm	2009-01-23 17:59:38 UTC (rev 7383)
+++ MooseX-Singleton/trunk/lib/MooseX/Singleton/Meta/Instance.pm	2009-01-23 18:19:26 UTC (rev 7384)
@@ -16,7 +16,9 @@
     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 {

Added: MooseX-Singleton/trunk/t/004-build_bug.t
===================================================================
--- MooseX-Singleton/trunk/t/004-build_bug.t	                        (rev 0)
+++ MooseX-Singleton/trunk/t/004-build_bug.t	2009-01-23 18:19:26 UTC (rev 7384)
@@ -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'
+);


Property changes on: MooseX-Singleton/trunk/t/004-build_bug.t
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Rev
Name: svn:eol-style
   + native

Added: MooseX-Singleton/trunk/t/005-build_bug-immutable.t
===================================================================
--- MooseX-Singleton/trunk/t/005-build_bug-immutable.t	                        (rev 0)
+++ MooseX-Singleton/trunk/t/005-build_bug-immutable.t	2009-01-23 18:19:26 UTC (rev 7384)
@@ -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'
+);


Property changes on: MooseX-Singleton/trunk/t/005-build_bug-immutable.t
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Rev
Name: svn:eol-style
   + native




More information about the Moose-commits mailing list