Skip to content

Commit

Permalink
Move the logic to determine the Result of a subtest into SubtestEnd.
Browse files Browse the repository at this point in the history
That way a Formatter, or anything else, can just ask the SubtestEnd event.

This necessitated attaching the SubtestStart event to SubtestEnd so it could
have the necessary context to setup the Result.

For #250
  • Loading branch information
schwern committed Jan 24, 2012
1 parent c221931 commit bf96a6d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 43 deletions.
78 changes: 77 additions & 1 deletion lib/TB2/Event/SubtestEnd.pm
Expand Up @@ -2,7 +2,7 @@ package TB2::Event::SubtestEnd;

use TB2::Mouse;
use TB2::Types;
with 'TB2::Event';
with 'TB2::Event', 'TB2::CanThread', 'TB2::CanLoad';

our $VERSION = '1.005000_002';
$VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval)
Expand Down Expand Up @@ -46,6 +46,82 @@ has history =>
isa => 'TB2::History',
;


=head3 subtest_start
The matching L<TB2::Event::SubtestStart>.
Normally this will be filled in by L<TB2::TestState> during
posting.
=cut

has subtest_start =>
is => 'rw',
isa => 'TB2::Event::SubtestStart'
;


=head3 result
A Result summarizing the outcome of the subtest.
This will be created from the L<history> by default.
=cut

has result =>
is => 'rw',
isa => 'TB2::Result::Base',
lazy => 1,
trigger => sub { $_[0]->shared_clone($_[1]) },
default => sub {
return $_[0]->shared_clone( $_[0]->_build_result );
};

sub _build_result {
my $self = shift;

my $subtest_history = $self->history;

my %result_args;

# Inherit information from the subtest.
if( my $subtest_start = $self->subtest_start ) {
$result_args{name} = $subtest_start->name;

# If the subtest was started in a todo context, the subtest result
# will be todo.
$result_args{directives} = $subtest_start->directives;
$result_args{reason} = $subtest_start->reason;
}

# Did the subtest pass?
$result_args{pass} = $subtest_history->test_was_successful;

# Inherit the context.
for my $key (qw(file line)) {
my $val = $self->$key();
$result_args{$key} = $val if defined $val;
}

my $subtest_plan = $subtest_history->plan;
if( $subtest_plan && $subtest_plan->skip ) {
# If the subtest was a skip_all, make our result a skip.
$result_args{skip} = 1;
$result_args{reason} = $subtest_plan->skip_reason;
}
elsif( $subtest_history->test_count == 0 ) {
# The subtest didn't run any tests
my $name = $result_args{name};
$result_args{name} = "No tests run in subtest";
$result_args{name}.= qq[ "$name"] if defined $name;
}

$self->load("TB2::Result");
return TB2::Result->new_result( %result_args );
}

=head3 build_event_type
The event type is C<subtest_end>.
Expand Down
42 changes: 3 additions & 39 deletions lib/TB2/Formatter/TAP/Base.pm
Expand Up @@ -598,49 +598,13 @@ sub handle_subtest_end {
my $self = shift;
my($event, $ec) = @_;

my $subtest_history = $event->history;
my $subtest_start = $ec->history->subtest_start;

my %result_args;

# Did the subtest pass?
$result_args{pass} = $subtest_history->test_was_successful;

# Inherit the name from the subtest.
$result_args{name} = $subtest_start->name;

# If the subtest was started in a todo context, the subtest result
# will be todo.
$result_args{directives} = $subtest_start->directives;
$result_args{reason} = $subtest_start->reason;

# Inherit the context.
for my $key (qw(file line)) {
my $val = $event->$key();
$result_args{$key} = $val if defined $val;
}

# What was the result of the subtest?
if( my $abort = $subtest_history->abort ) {
if( my $abort = $event->history->abort ) {
# Subtest aborted, end the abort up to the top level
$ec->post_event($abort);
}
else {
my $subtest_plan = $subtest_history->plan;
if( $subtest_plan && $subtest_plan->skip ) {
# If the subtest was a skip_all, make our result a skip.
$result_args{skip} = 1;
$result_args{reason} = $subtest_plan->skip_reason;
}
elsif( $subtest_history->test_count == 0 ) {
# The subtest didn't run any tests
my $name = $result_args{name};
$result_args{name} = "No tests run in subtest";
$result_args{name}.= qq[ "$name"] if defined $name;
}

my $result = TB2::Result->new_result( %result_args );
$ec->post_event($result);
# Subtest ended normally, post a summary result
$ec->post_event($event->result);
}

return;
Expand Down
1 change: 1 addition & 0 deletions lib/TB2/TestState.pm
Expand Up @@ -362,6 +362,7 @@ sub handle_subtest_end {
# Attach the subtest history to the event. If somebody else already
# did so, honor that.
$event->history( $subtest_ec->history ) unless $event->history;
$event->subtest_start( $self->history->subtest_start ) unless $event->subtest_start;

# Post the event to the current level
$self->current_coordinator->post_event(@_);
Expand Down
47 changes: 44 additions & 3 deletions t/Event/SubtestEnd.t
Expand Up @@ -9,6 +9,8 @@ my $CLASS = 'TB2::Event::SubtestEnd';
use_ok $CLASS;

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

note "defaults"; {
my $history = TB2::History->new;
Expand All @@ -20,10 +22,49 @@ note "defaults"; {
is $event->history, $history;
is $event->event_type, "subtest_end";
is_deeply $event->as_hash, {
event_type => "subtest_end",
object_id => $event->object_id,
history => $history
coordinate_threads => $event->coordinate_threads,
event_type => "subtest_end",
object_id => $event->object_id,
history => $history,
result => $event->result,
};

is $event->result->name, "No tests run in subtest";
}


note "simple result"; {
my $ec = TB2::EventCoordinator->new;
$ec->clear_formatters;
$ec->post_event( $_ ) for
TB2::Event::TestStart->new,
TB2::Event::SetPlan->new( asserts_expected => 1 ),
TB2::Result->new_result( pass => 1 ),
TB2::Event::TestEnd->new,
;

my $event = $CLASS->new(
history => $ec->history,
subtest_start => TB2::Event::SubtestStart->new(
depth => 1,
name => 'some subtest',
directives => ['todo'],
reason => 'Because I said so'
)
);

# Just to make sure we set the conditions up right
ok $event->history->test_was_successful;

my $result = $event->result;
ok $result;
ok $result->is_todo;
ok !$result->is_skip;
ok $result->is_pass;
is $result->name, "some subtest";
is $result->reason, "Because I said so";
is $result->file, $event->file;
is $result->line, $event->line;
}

done_testing;

0 comments on commit bf96a6d

Please sign in to comment.