Skip to content

Commit

Permalink
Fix TB2::OnlyOnePlan so it doesn't store state.
Browse files Browse the repository at this point in the history
This lets use the same handler in multiple test states.

Needed by TBT which copies all the handlers to emulate the test behavior.

For #242
  • Loading branch information
schwern committed Dec 2, 2011
1 parent 1118690 commit 38181d9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 23 deletions.
18 changes: 5 additions & 13 deletions lib/TB2/OnlyOnePlan.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,20 @@ This specificially allows redundant planning...
=cut

has existing_plan =>
is => 'rw',
isa => 'Object',
;

sub handle_set_plan {
my $self = shift;
my $event = shift;

$self->already_saw_plan($event) if $self->existing_plan;
my($plan, $ec) = @_;

$self->existing_plan($event);
my $existing_plan = $ec->history->plan;
$self->_already_saw_plan($existing_plan, $plan) if $existing_plan;

return;
}


sub already_saw_plan {
sub _already_saw_plan {
my $self = shift;
my $new_plan = shift;

my $existing_plan = $self->existing_plan;
my($existing_plan, $new_plan) = @_;

return if $existing_plan->no_plan &&
( $new_plan->no_plan || $new_plan->asserts_expected );
Expand Down
4 changes: 4 additions & 0 deletions lib/Test/Builder/Tester.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ sub _start_testing {
# Make a detached TestState
my $state = $original_state->create(
formatters => [$formatter],

# Preserve existing handlers
early_handlers => $original_state->early_handlers,
late_handlers => $original_state->late_handlers,
);

# remember that we're testing
Expand Down
37 changes: 27 additions & 10 deletions t/OnlyOnePlan/basics.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ use warnings;
BEGIN { require "t/test.pl" }

use TB2::Events;
use TB2::EventCoordinator;

my $CLASS = 'TB2::OnlyOnePlan';
use_ok $CLASS;


note "Two different plans are not ok"; {
my $onlyone = $CLASS->new;
my $ec = TB2::EventCoordinator->new(
formatters => [],
early_handlers => [$onlyone]
);

ok eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(
asserts_expected => 3,
file => "bar.t",
Expand All @@ -26,7 +31,7 @@ note "Two different plans are not ok"; {
}, "one plan is ok";

ok !eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(
asserts_expected => 1,
file => "foo.t",
Expand All @@ -41,9 +46,13 @@ note "Two different plans are not ok"; {

note "The same plan is ok"; {
my $onlyone = $CLASS->new;
my $ec = TB2::EventCoordinator->new(
formatters => [],
early_handlers => [$onlyone]
);

ok eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(
asserts_expected => 3,
file => "bar.t",
Expand All @@ -54,7 +63,7 @@ note "The same plan is ok"; {
}, "one plan is ok";

ok eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(
asserts_expected => 3,
file => "foo.t",
Expand All @@ -68,30 +77,34 @@ note "The same plan is ok"; {

note "Multiple no_plans are ok"; {
my $onlyone = $CLASS->new;
my $ec = TB2::EventCoordinator->new(
formatters => [],
early_handlers => [$onlyone]
);

ok eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(no_plan => 1)
);
1;
}, "one no_plan is ok";

ok eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(no_plan => 1)
);
1;
}, "two no_plans are ok";

ok eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(asserts_expected => 3)
);
1;
}, "a fixed number of tests after a no_plan is ok";

ok !eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(asserts_expected => 23)
);
1;
Expand All @@ -101,16 +114,20 @@ note "Multiple no_plans are ok"; {

note "Error message with no file/line"; {
my $onlyone = $CLASS->new;
my $ec = TB2::EventCoordinator->new(
formatters => [],
early_handlers => [$onlyone]
);

ok eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(asserts_expected => 2)
);
1;
}, "one plan is ok";

ok !eval {
$onlyone->accept_event(
$ec->post_event(
TB2::Event::SetPlan->new(
asserts_expected => 3,
)
Expand Down
55 changes: 55 additions & 0 deletions t/OnlyOnePlan/no_state.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env perl -w

use strict;
use warnings;

BEGIN { require "t/test.pl" }

use TB2::Events;
use TB2::EventCoordinator;

my $CLASS = 'TB2::OnlyOnePlan';
use_ok $CLASS;


note "Two plans from two different coordinators to one handler is ok"; {
my $onlyone = $CLASS->new;

# Deliberately put the same handler into two coordinators
my $ec1 = TB2::EventCoordinator->new(
formatters => [],
early_handlers => [$onlyone]
);
my $ec2 = TB2::EventCoordinator->new(
formatters => [],
early_handlers => [$onlyone]
);

# start testing in both coordinators
my $start = TB2::Event::TestStart->new;
$_->post_event($start) for $ec1, $ec2;

ok eval {
$ec1->post_event(
TB2::Event::SetPlan->new(
asserts_expected => 3,
file => "bar.t",
line => 42,
)
);
1;
}, "one plan is ok" or diag $@;

ok eval {
$ec2->post_event(
TB2::Event::SetPlan->new(
asserts_expected => 4,
file => "foo.t",
line => 23,
)
);
1;
}, "a plan in another coordinator is ok" or diag $@;
}

done_testing;

0 comments on commit 38181d9

Please sign in to comment.