Permalink
Browse files

Add TB2::History->last_result and last_event

I'm finding a number of modules which use summary and details just to get at
the last result.  Test::Most is one example.
  • Loading branch information...
1 parent cf08639 commit 6986d13c2eeee7d09411a0bb5a54163d06eb1e57 @schwern schwern committed Aug 23, 2012
Showing with 52 additions and 3 deletions.
  1. +5 −0 Changes
  2. +34 −0 lib/TB2/History.pm
  3. +13 −3 t/History/HistoryStats.t
View
@@ -1,6 +1,11 @@
See README and version control log for Test::Builder2 changes.
1.005000_006
+ New Features
+ * Memory usage will not grow as more tests are run.
+ * Added TB2::History->last_result and last_event to retrieve the
+ last result and event seen.
+
Bug Fixes
* Patched TB2::Mouse to not blow over tied $_ (a local $_ bug).
(Father Chrysostomos) [github 293]
View
@@ -109,8 +109,22 @@ Unless otherwise stated, these are all accessor methods of the form:
An array ref of all events seen.
+By default, no events are stored and this will throw an exception
+unless C<< $history->store_events >> is true.
+
+=head3 last_event
+
+ my $event = $history->last_event;
+
+Returns the last event seen.
+
=cut
+has last_event => (
+ is => 'rw',
+ isa => 'TB2::Event',
+);
+
sub event_storage_class {
return $_[0]->store_events ? "TB2::History::EventStorage" : "TB2::History::NoEventStorage";
}
@@ -141,6 +155,7 @@ sub handle_event {
$self->event_storage->events_push($event);
$self->event_count( $self->event_count + 1 );
+ $self->last_event($event);
return;
}
@@ -233,12 +248,18 @@ sub has_events { shift->event_count > 0 }
# The result of test #4.
my $result = $history->results->[3];
+Returns a list of all L<TB2::Result> objects seen in this test.
+
+By default, no results are stored and this will throw an exception
+unless C<< $history->store_events >> is true.
+
=cut
sub handle_result {
my $self = shift;
my $result = shift;
+ $self->last_result($result);
$self->_update_result_statistics($result);
$self->handle_event($result);
@@ -255,6 +276,19 @@ Returns true if we have stored results, false otherwise.
sub has_results { shift->result_count > 0 }
+=head3 last_result
+
+ my $result = $history->last_result;
+
+Returns the last result seen.
+
+=cut
+
+has last_result => (
+ is => 'rw',
+ isa => 'TB2::Result::Base',
+);
+
=head2 Statistics
=cut
@@ -31,15 +31,25 @@ note "basic history stats"; {
history => $history
);
+ $ec->post_event( TB2::Event::TestStart->new );
+
+ is $history->last_event->object_id, $history->test_start->object_id;
+
ok!$history->has_results, q{we no not yet have results};
$ec->post_event( Pass() );
$ec->post_event( Fail() );
$ec->post_event($_) for Pass(), Fail();
- $ec->post_event( TB2::Result->new_result(
+
+ my $todo_fail = TB2::Result->new_result(
pass => 0,
directives => ['todo'],
- ));
+ );
+ $ec->post_event( $todo_fail );
+
+ is $history->last_result->object_id, $todo_fail->object_id;
+ is $history->last_event->object_id, $todo_fail->object_id;
+
$ec->post_event( TB2::Result->new_result(
pass => 1,
directives => ['todo'],
@@ -50,7 +60,7 @@ note "basic history stats"; {
));
ok $history->has_results, q{we have results};
- is $history->event_count, 7, q{event_count};
+ is $history->event_count, 8, q{event_count};
is $history->result_count, 7, q{result_count};
is $history->pass_count, 5, q{pass_count};
is $history->fail_count, 2, q{fail_count};

0 comments on commit 6986d13

Please sign in to comment.