1 package App::Prove::State::Result;
6 use App::Prove::State::Result::Test;
9 use constant STATE_VERSION => 1;
13 App::Prove::State::Result - Individual test suite results.
25 The C<prove> command supports a C<--state> option that instructs it to
26 store persistent state across runs. This module encapsulates the results for a
27 single test suite run.
32 $ prove --state=fail,save -rbv
42 my $result = App::Prove::State::Result->new({
43 generation => $generation,
47 Returns a new C<App::Prove::State::Result> instance.
52 my ( $class, $arg_for ) = @_;
54 my %instance_data = %$arg_for; # shallow copy
55 $instance_data{version} = $class->state_version;
56 my $tests = delete $instance_data{tests} || {};
57 my $self = bless \%instance_data => $class;
58 $self->_initialize($tests);
63 my ( $self, $tests ) = @_;
65 while ( my ( $name, $test ) = each %$tests ) {
66 $tests{$name} = $self->test_class->new({
71 $self->tests( \%tests );
75 =head2 C<state_version>
77 Returns the current version of state storage.
81 sub state_version {STATE_VERSION}
85 Returns the name of the class used for tracking individual tests. This class
86 should either subclass from C<App::Prove::State::Result::Test> or provide an
92 return 'App::Prove::State::Result::Test';
96 generation => { method => 'generation', default => 0 },
97 last_run_time => { method => 'last_run_time', default => undef },
100 while ( my ( $key, $description ) = each %methods ) {
101 my $default = $description->{default};
103 *{ $description->{method} } = sub {
106 $self->{$key} = shift;
109 return $self->{$key} || $default;
115 Getter/setter for the "generation" of the test suite run. The first
116 generation is 1 (one) and subsequent generations are 2, 3, etc.
118 =head3 C<last_run_time>
120 Getter/setter for the time of the test suite run.
124 Returns the tests for a given generation. This is a hashref or a hash,
125 depending on context called. The keys to the hash are the individual
126 test names and the value is a hashref with various interesting values.
127 Each k/v pair might resemble something like this:
130 elapsed => '0.0428488254547119',
132 last_pass_time => '1219328376.07815',
134 last_run_time => '1219328376.07815',
136 mtime => '1191708862',
146 $self->{tests} = shift;
149 my %tests = %{ $self->{tests} };
150 my @tests = sort { $a->sequence <=> $b->sequence } values %tests;
151 return wantarray ? @tests : \@tests;
156 my $test = $result->test('t/customer/create.t');
158 Returns an individual C<App::Prove::State::Result::Test> instance for the
159 given test name (usually the filename). Will return a new
160 C<App::Prove::State::Result::Test> instance if the name is not found.
165 my ( $self, $name ) = @_;
166 croak("test() requires a test name") unless defined $name;
168 my $tests = $self->{tests} ||= {};
169 if ( my $test = $tests->{$name} ) {
173 my $test = $self->test_class->new({name => $name});
174 $self->{tests}->{$name} = $test;
181 Returns an list of test names, sorted by run order.
187 return map { $_->name } $self->tests;
192 $result->remove($test_name); # remove the test
193 my $test = $result->test($test_name); # fatal error
195 Removes a given test from results. This is a no-op if the test name is not
201 my ( $self, $name ) = @_;
202 delete $self->{tests}->{$name};
208 Returns the number of tests for a given test suite result.
212 sub num_tests { keys %{ shift->{tests} } }
216 Returns a hashref of raw results, suitable for serialization by YAML.
225 foreach my $test ( $self->tests ) {
226 $tests{ $test->name } = $test->raw;
228 $raw{tests} = \%tests;