Skip Menu |

This queue is for tickets about the Test-MockObject CPAN distribution.

Report information
The Basics
Id: 69104
Status: open
Priority: 0/
Queue: Test-MockObject

People
Owner: Nobody in particular
Requestors: rmheule [...] yahoo.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.09
Fixed in: (no value)



In Test-MockObject-1.09, I have discovered a bug where the fake_module method in Test::MockObject incorrectly croaks that a module is already loaded. Tested on the following Perl versions / platforms: v5.12.3 built for x86_64-linux-thread-multi v5.10.0 built for aix I have not tried with more recent versions of Test::MockObject, but I have inspected the code for them and I believe it is still a bug in the most recent versions. I have attached sample code that triggers the bug. To fix the bug, the check_class_loaded method should ignore symbol table entries when looking through the symbol table. That is, if it is looking in %Foo:: it should ignore %Foo::Bar:: and only croak if there are other entries in the symbol table. It may be desirable to use the "loaded" method in Class::Inspector for this check, since it already incorporates that logic.
Subject: bug.pl
#!/usr/bin/perl use strict; use warnings; use Test::MockObject; # Create something in Foo::Bar. Test::MockObject will see this symbol table # entry and croak. This can also be done by a "use Foo::Bar" (assuming that is # a valid module). sub Foo::Bar::Test() { 1 } # Now try to mock Foo. This will croak: "No mocked subs for loaded module # 'Foo' at bug.pl line 14" Test::MockObject->fake_module('Foo');
Subject: [patch] Test::MockObject->fake_module croaks trying to mock packages with loaded children
From: madcityzen [...] gmail.com
On Mon Jun 27 13:55:19 2011, rmheule wrote: Show quoted text
> In Test-MockObject-1.09, I have discovered a bug where the fake_module > method in Test::MockObject incorrectly croaks that a module is already > loaded. > > To fix the bug, the check_class_loaded method should ignore symbol table > entries when looking through the symbol table. That is, if it is > looking in %Foo:: it should ignore %Foo::Bar:: and only croak if there > are other entries in the symbol table.
Here is a patch that does just that: --- MockObject.pm 2012-07-02 14:35:13.293011000 -0400 +++ MockObject.pm 2012-07-02 14:47:16.610133000 -0400 @@ -337,14 +337,17 @@ { unless (exists $symtable->{ $symbol . '::' }) { - $found = 0; - last; + return 0; } $symtable = $symtable->{ $symbol . '::' }; } - return $found; + # If all members are packages, this class probably + # hasn't been loaded + return 0 if keys %{ $symtable } == grep { /::$/ } keys %{ $symtable }; + + return 1; # Can't confirm the non-existence of the module } sub fake_new