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(
72 $self->tests( \%tests );
76 =head2 C<state_version>
78 Returns the current version of state storage.
82 sub state_version {STATE_VERSION}
86 Returns the name of the class used for tracking individual tests. This class
87 should either subclass from C<App::Prove::State::Result::Test> or provide an
93 return 'App::Prove::State::Result::Test';
97 generation => { method => 'generation', default => 0 },
98 last_run_time => { method => 'last_run_time', default => undef },
101 while ( my ( $key, $description ) = each %methods ) {
102 my $default = $description->{default};
104 *{ $description->{method} } = sub {
107 $self->{$key} = shift;
110 return $self->{$key} || $default;
116 Getter/setter for the "generation" of the test suite run. The first
117 generation is 1 (one) and subsequent generations are 2, 3, etc.
119 =head3 C<last_run_time>
121 Getter/setter for the time of the test suite run.
125 Returns the tests for a given generation. This is a hashref or a hash,
126 depending on context called. The keys to the hash are the individual
127 test names and the value is a hashref with various interesting values.
128 Each k/v pair might resemble something like this:
131 elapsed => '0.0428488254547119',
133 last_pass_time => '1219328376.07815',
135 last_run_time => '1219328376.07815',
137 mtime => '1191708862',
147 $self->{tests} = shift;
150 my %tests = %{ $self->{tests} };
151 my @tests = sort { $a->sequence <=> $b->sequence } values %tests;
152 return wantarray ? @tests : \@tests;
157 my $test = $result->test('t/customer/create.t');
159 Returns an individual C<App::Prove::State::Result::Test> instance for the
160 given test name (usually the filename). Will return a new
161 C<App::Prove::State::Result::Test> instance if the name is not found.
166 my ( $self, $name ) = @_;
167 croak("test() requires a test name") unless defined $name;
169 my $tests = $self->{tests} ||= {};
170 if ( my $test = $tests->{$name} ) {
174 my $test = $self->test_class->new( { name => $name } );
175 $self->{tests}->{$name} = $test;
182 Returns an list of test names, sorted by run order.
188 return map { $_->name } $self->tests;
193 $result->remove($test_name); # remove the test
194 my $test = $result->test($test_name); # fatal error
196 Removes a given test from results. This is a no-op if the test name is not
202 my ( $self, $name ) = @_;
203 delete $self->{tests}->{$name};
209 Returns the number of tests for a given test suite result.
213 sub num_tests { keys %{ shift->{tests} } }
217 Returns a hashref of raw results, suitable for serialization by YAML.
226 foreach my $test ( $self->tests ) {
227 $tests{ $test->name } = $test->raw;
229 $raw{tests} = \%tests;