Subject: | Class::Null is not omnipotent: Class::Null->new->new fails |
Its all in the subject. I am attaching a patch containing a failing test
and a fix. Also the number of tests is doubled to verify that
subclassing works correctly. (I even bumped the version to .10 :)
Cheers!
Subject: | class_null.diff |
diff -ur Class-Null-1.09/lib/Class/Null.pm Class-Null-1.10/lib/Class/Null.pm
--- Class-Null-1.09/lib/Class/Null.pm 2007-11-24 18:22:54.000000000 +0100
+++ Class-Null-1.10/lib/Class/Null.pm 2008-03-19 18:47:48.715372419 +0100
@@ -3,7 +3,7 @@
use warnings;
use strict;
-our $VERSION = '1.09';
+our $VERSION = '1.10';
use overload
'bool' => sub { 0 },
@@ -11,9 +11,13 @@
'0+' => sub { 0 },
fallback => 1;
-sub new { our $singleton ||= bless {}, shift }
-sub AUTOLOAD { our $singleton }
+my $null;
+sub AUTOLOAD {
+ $null = bless (\do{'NULL'}, shift) unless defined $null;
+ return $null;
+}
+sub DESTROY {};
1;
diff -ur Class-Null-1.09/t/01_misc.t Class-Null-1.10/t/01_misc.t
--- Class-Null-1.09/t/01_misc.t 2007-11-05 14:37:43.000000000 +0100
+++ Class-Null-1.10/t/01_misc.t 2008-03-19 19:02:12.896372459 +0100
@@ -1,34 +1,48 @@
use strict;
use warnings;
-use Test::More tests => 46;
+use Test::More tests => 95;
use Class::Null;
-my $o = Class::Null->new;
-isa_ok($o, 'Class::Null');
-my @l = ('A'..'Z', 'a'..'z', '_');
-
-for (1..10) {
- my $method = join '' => map { $l[rand@l] } 1..10;
- ok(do { $o->$method, 1 }, "can $method()");
+for my $o (Class::Null->new, Class::Null::SubClass->new) {
+ isa_ok($o, 'Class::Null');
+ my @l = ('A'..'Z', 'a'..'z', '_');
+
+ for (1..10) {
+ my $method = join '' => map { $l[rand@l] } 1..10;
+ ok(do { $o->$method, 1 }, "can $method()");
+ }
+
+ for (1..10) {
+ my $method = join '' => map { $l[rand@l] } 1..10;
+ is($o->$method, Class::Null->new,
+ "$method() returns a Class::Null object");
+
+ # Now it will have installed the method via *{$AUTOLOAD}. Check
+ # it's still ok.
+
+ is($o->$method, Class::Null->new,
+ "$method() returns a Class::Null object");
+
+ is($o->$method->$method, Class::Null->new,
+ "$method() method chaining ok");
+ }
+
+ is($o+5, 5, 'adding null object 1');
+ is(3+$o, 3, 'adding null object 2');
+ is(-$o-7, -7, 'subtracting null object');
+ is("<<<$o>>>", '<<<>>>', 'stringifying null object');
+ is($o ? 'yes' : 'no', 'no', 'string object in boolean context');
+
+ my $o_clone;
+ eval { $o_clone = $o->new };
+ isa_ok($o_clone, 'Class::Null', 'Result of Class::Null->new->new');
}
-for (1..10) {
- my $method = join '' => map { $l[rand@l] } 1..10;
- is($o->$method, Class::Null->new,
- "$method() returns a Class::Null object");
+isa_ok(Class::Null::whatever(), 'Class::Null', 'Regular function call');
- # Now it will have installed the method via *{$AUTOLOAD}. Check
- # it's still ok.
- is($o->$method, Class::Null->new,
- "$method() returns a Class::Null object");
- is($o->$method->$method, Class::Null->new,
- "$method() method chaining ok");
-}
+package Class::Null::SubClass;
+use base 'Class::Null';
+1;
-is($o+5, 5, 'adding null object 1');
-is(3+$o, 3, 'adding null object 2');
-is(-$o-7, -7, 'subtracting null object');
-is("<<<$o>>>", '<<<>>>', 'stringifying null object');
-is($o ? 'yes' : 'no', 'no', 'string object in boolean context');