Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

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

Report information
The Basics
Id: 30471
Status: resolved
Priority: 0/
Queue: Test-MockRandom

People
Owner: Nobody in particular
Requestors: bitcard.org [...] perl.tgape.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in:
  • 0.94
  • 0.99
Fixed in: (no value)



Subject: Test::MockRandom::rand has no prototype
Perl's built-in rand function has a prototype of (;$). This allows programmers to do such things as $value = $array[rand(@array)]; Any code that does that will not currently work correctly with Test::MockRandom, despite being perfectly valid perl code; instead, it will take the contents of the first array element as the argument. Adding a prototype to this function fixes this issue. Note that prototypes are ignored for method calls, so it would not interfere with any call of the form: $MockRandomObject->rand($number); This solution does break t/09-import-override-reoverride, unless it's also given that prototype. Not that it's really pertinent, but just following directions... Tested with both Test-MockRandom 0.94 and 0.99, with perl 5.8.6, on my homegrown Linux dev box with kernel 2.6.10 (hardened).
Subject: Re: [rt.cpan.org #30471] Test::MockRandom::rand has no prototype
Date: Mon, 5 Nov 2007 07:24:23 -0500
To: bug-Test-MockRandom [...] rt.cpan.org
From: "David Golden" <dagolden [...] cpan.org>
On Nov 4, 2007 2:03 PM, Ed Grimm via RT <bug-Test-MockRandom@rt.cpan.org> wrote: Show quoted text
> Perl's built-in rand function has a prototype of (;$). This allows > programmers to do such things as > > $value = $array[rand(@array)];
Great point. Thanks for catching that. I'm putting it on the Todo list. David
From: INFINOID [...] cpan.org
On Mon Nov 05 07:24:47 2007, DAGOLDEN wrote: Show quoted text
> Great point. Thanks for catching that. I'm putting it on the Todo > list.
Hi, I just got bit by this same bug. I made up a patch to fix it, and then discovered someone had already submitted this issue. So I'll just post the patch here. :) This patch updates the rand signature, adds a test (and a test module) to test it, and fixes the failed test by updating SomeRandPackage.pm too. The new test passes a list of 0's to rand()... without the signature, rand() just uses the first 0 as its input, but with the signature, rand() gets the list size as its argument. So all the test has to do is check for a non-zero result. Please consider applying this - without it, I have to work around it by editing the code under test to do rand(scalar(@list)) instead of rand(@list). Thanks, Mark
diff -uNr Test-MockRandom-0.99.orig/lib/Test/MockRandom.pm Test-MockRandom-0.99/lib/Test/MockRandom.pm --- Test-MockRandom-0.99.orig/lib/Test/MockRandom.pm 2006-02-07 04:23:27.000000000 -0800 +++ Test-MockRandom-0.99/lib/Test/MockRandom.pm 2007-12-16 06:09:29.000000000 -0800 @@ -300,7 +300,7 @@ =cut -sub rand { +sub rand(;$) { my ($mult,$val); if (ref ($_[0]) eq __PACKAGE__) { # we're a MockRandom object $mult = $_[1]; diff -uNr Test-MockRandom-0.99.orig/t/17-list-context.t Test-MockRandom-0.99/t/17-list-context.t --- Test-MockRandom-0.99.orig/t/17-list-context.t 1969-12-31 16:00:00.000000000 -0800 +++ Test-MockRandom-0.99/t/17-list-context.t 2007-12-16 06:13:31.000000000 -0800 @@ -0,0 +1,26 @@ +# Test::MockRandom +use strict; + +use Test::More tests => 6 ; + +#--------------------------------------------------------------------------# +# Test package overriding via import to global +#--------------------------------------------------------------------------# + +use Test::MockRandom qw( CORE::GLOBAL ); +use lib qw( . ./t ); +use SomeListPackage; + +for ( __PACKAGE__, "SomeListPackage" ) { + is( UNIVERSAL::can( $_, 'rand'), undef, + "rand should not have been imported into $_" ); +} +for (qw ( srand oneish )) { + can_ok( __PACKAGE__, $_ ); +} + +my $obj = SomeListPackage->new; +isa_ok ( $obj, 'SomeListPackage'); +srand(.5); +# list_random(10) actually returns 5 +isnt($obj->list_random(10), 0, 'testing $obj->list_random(10) != 0'); diff -uNr Test-MockRandom-0.99.orig/t/SomeListPackage.pm Test-MockRandom-0.99/t/SomeListPackage.pm --- Test-MockRandom-0.99.orig/t/SomeListPackage.pm 1969-12-31 16:00:00.000000000 -0800 +++ Test-MockRandom-0.99/t/SomeListPackage.pm 2007-12-16 06:11:10.000000000 -0800 @@ -0,0 +1,18 @@ +package SomeListPackage; + +sub new { + my $class = shift; + return bless( {}, ref($class) || $class ); +} + +# calls rand() with a list of 0's. in list context, rand() will get 0. +# but in scalar context, rand() will get $limit. +sub list_random { + my ($self, $limit) = @_; + my @list; + push(@list, 0) for(1..$limit); + my $rnd = rand(@list); + return $rnd; +} + +1; diff -uNr Test-MockRandom-0.99.orig/t/SomeRandPackage.pm Test-MockRandom-0.99/t/SomeRandPackage.pm --- Test-MockRandom-0.99.orig/t/SomeRandPackage.pm 2006-02-07 04:23:27.000000000 -0800 +++ Test-MockRandom-0.99/t/SomeRandPackage.pm 2007-12-16 06:12:24.000000000 -0800 @@ -5,7 +5,7 @@ return bless( {}, ref($class) || $class ); } -sub rand { +sub rand(;$) { my $rnd = CORE::rand; return $rnd; }
Subject: Re: [rt.cpan.org #30471] Test::MockRandom::rand has no prototype
Date: Mon, 17 Dec 2007 11:44:14 -0500
To: bug-Test-MockRandom [...] rt.cpan.org
From: "David Golden" <dagolden [...] cpan.org>
On Dec 16, 2007 9:22 AM, Mark Glines via RT <bug-Test-MockRandom@rt.cpan.org> wrote: Show quoted text
> Please consider applying this - without it, I have to work around it by > editing the code under test to do rand(scalar(@list)) instead of > rand(@list).
Thank you very much for the reminder and the patch. I've just uploaded 1.00 to CPAN with your changes, though I did change up the tests a bit. Please let me know if it (a) passes tests for you and (b) meets your needs. Regards, David
Fixed in version 1.00