Skip to content

Commit

Permalink
First cut at breaking up plan().
Browse files Browse the repository at this point in the history
  • Loading branch information
schwern committed Feb 14, 2009
1 parent b989f45 commit f0fad55
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions lib/Test/Builder.pm
Expand Up @@ -215,49 +215,63 @@ If you call plan(), don't call any of the other methods below.
=cut

my %plan_cmds = (
no_plan => \&no_plan,
skip_all => \&skip_all,
tests => \&_plan_tests,
add => \&_plan_add,
);

my %call_once = map { $_ => 1 } qw(no_plan skip_all tests);

sub plan {
my( $self, $cmd, $arg ) = @_;

return unless $cmd;

local $Level = $Level + 1;

if( $cmd eq 'no_plan' ) {
$self->croak("You tried to plan twice") if $self->{Have_Plan};
$self->croak("You tried to plan twice") if $call_once{$cmd} and $self->{Have_Plan};

$self->carp("no_plan takes no arguments") if $arg;
$self->no_plan;
if( my $method = $plan_cmds{$cmd} ) {
local $Level = $Level + 1;
$self->$method($arg);
}
elsif( $cmd eq 'skip_all' ) {
$self->croak("You tried to plan twice") if $self->{Have_Plan};

return $self->skip_all($arg);
else {
my @args = grep { defined } ( $cmd, $arg );
$self->croak("plan() doesn't understand @args");
}
elsif( $cmd eq 'tests' ) {
$self->croak("You tried to plan twice") if $self->{Have_Plan};

if($arg) {
local $Level = $Level + 1;
return $self->expected_tests($arg);
}
elsif( !defined $arg ) {
$self->croak("Got an undefined number of tests");
}
else {
$self->croak("You said to run 0 tests");
}
return 1;
}


sub _plan_tests {
my($self, $arg) = @_;

if($arg) {
local $Level = $Level + 1;
return $self->expected_tests($arg);
}
elsif( $cmd eq 'add' ) {
$self->{Expected_Tests} = $self->{Expected_Tests} + $arg;
$self->{Have_Plan} = 1;
$self->no_plan;
elsif( !defined $arg ) {
$self->croak("Got an undefined number of tests");
}
else {
my @args = grep { defined } ( $cmd, $arg );
$self->croak("plan() doesn't understand @args");
$self->croak("You said to run 0 tests");
}

return 1;
return;
}


sub _plan_add {
my($self, $arg) = @_;

$self->{Expected_Tests} = $self->{Expected_Tests} + $arg;
$self->{Have_Plan} = 1;
$self->no_plan;

return;
}

=item B<expected_tests>
Expand Down Expand Up @@ -295,7 +309,9 @@ Declares that this test will run an indeterminate # of tests.
=cut

sub no_plan {
my $self = shift;
my($self, $arg) = @_;

$self->carp("no_plan takes no arguments") if $arg;

$self->{No_Plan} = 1;
$self->{Have_Plan} = 1;
Expand Down

0 comments on commit f0fad55

Please sign in to comment.