Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

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

Report information
The Basics
Id: 28836
Status: resolved
Priority: 0/
Queue: Test-Simple

People
Owner: Nobody in particular
Requestors: x.test-simple-bug-report-20070814 [...] chatterjee.net
Cc:
AdminCc:

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



Subject: Test::Builder::expected_tests should handle numbers that stringify to an integer
Running under 5.8.5 built for i686-linux, the following test script: use Test::More; my $num_tests = 100.0000000000001; plan tests => $num_tests; for (1..100) { ok(1); } causes the following error: # Looks like you planned 100 tests but only ran 100 If I change $num_tests to 99.99999999999999, I get the following error: # Looks like you planned 100 tests but ran 1.4210854715202e-14 extra. This is because in both cases, $num_tests stringifies to 100, but the full decimal value gets stored internally. Test::Builder::expected_tests (line 298) verifies that a test passed in matches /^\+?\d+$/ but it doesn't account for the possibility that a non-integer value could serialize to an integer. I encountered this bug while working on a set of tests where I was automatically generating $num_tests based on a set of mathematical calculations. I had no idea that the number of tests I was feeding to plan wasn't an integer. It would be optimal as an end-user if Test::Builder could ignore the incredibly minute non-stringifiable decimal difference. I recommend fixing this problem by changing line 300 of Test::Builder 0.70 from: $self->{Expected_Tests} = $max; to this: $self->{Expected_Tests} = "$max" + 0; A less end-user-friendly alternative would be to add something line this around line 299: if ($max != int $max) { $self->croak("Number of tests must be a positive integer. You gave it a number that stringifies to '$max', but is actually not a positive integer, possibly because of a tiny decimal expansion") } Another less user-friendly alternative would be to change line 298 to: unless $max =~ /^\+?\d+$/ and $max > 0 and $max == int $max; I think either of these would be poor choices to make; it's confusing to an end-user why a number that stringifies to an integer would secretly not be an integer.
Subject: Re: [rt.cpan.org #28836] Test::Builder::expected_tests should handle numbers that stringify to an integer
Date: Tue, 14 Aug 2007 17:56:46 -0700
To: bug-Test-Simple [...] rt.cpan.org
From: Michael G Schwern <schwern [...] pobox.com>
Thanks for your report. Sorry if it took you a bit to track down. via RT wrote: Show quoted text
> causes the following error: > > # Looks like you planned 100 tests but only ran 100 > > If I change $num_tests to 99.99999999999999, I get the following error: > > # Looks like you planned 100 tests but ran 1.4210854715202e-14 extra.
Cute! Show quoted text
> A less end-user-friendly alternative would be to add something line this > around line 299: > > if ($max != int $max) { > $self->croak("Number of tests must be a positive integer. You > gave it a number that stringifies to '$max', but is actually not a > positive integer, possibly because of a tiny decimal expansion") > }
I'm inclined to go with this, make it an error with a helpful message. Test::More likes to tend towards explicit behavior. Accepting a decimal number of tests is guessing where none need be. Your case aside, I'm willing to say if a user sets a plan to be a float its indicative of a mistake somewhere along the line. Getting bitten by floating point error is one thing, but what's 10.45 tests supposed to mean? For your case a simple work around exists, "plan tests => int $num_tests".
this is already fixed: $self->croak("Number of tests must be a positive integer. You gave it '$max'") unless $max =~ /^\+?\d+$/ and $max > 0;
added a test case for float plans failing correctly