Skip to content

Commit

Permalink
Merge pull request #23 from jkeroes/test2-uber-alles
Browse files Browse the repository at this point in the history
Test2 uber alles
  • Loading branch information
lancew committed Jul 6, 2016
2 parents 4c9cc31 + 2ad7747 commit f9ac102
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -9,6 +9,6 @@ perl:
- "5.12"
- "5.10"
- "5.8"
install: cpanm -nq Devel::Cover::Report::Coveralls Moo Test2::Suite Test::Deep::NoTest Types::Standard
install: cpanm -nq Devel::Cover::Report::Coveralls Moo Test2::Suite~0.000025 Types::Standard
script: PERL5OPT=-MDevel::Cover prove -lv
after_success: cover -report coveralls
24 changes: 20 additions & 4 deletions lib/Scientist.pm
Expand Up @@ -5,8 +5,10 @@ use warnings;
# VERSION

use Moo;
use Test::Deep::NoTest qw/deep_diag cmp_details/;
use Time::HiRes 'time';
use Test2::API qw/intercept/;
use Test2::Compare::Delta 0.000025 qw//;
use Test2::Tools::Compare qw/is/;
use Time::HiRes qw/time/;
use Types::Standard qw/Bool Str CodeRef HashRef/;

# ABSTRACT: Perl module inspired by https://github.com/github/scientist
Expand Down Expand Up @@ -92,15 +94,23 @@ sub run {
$run_control->();
}

my ($ok, $stack) = cmp_details( \@candidate, \@control );
# Capture the events generated by is().
my $events = intercept {
is(\@candidate, \@control);
};

my ($ok_ev, undef, $why_ev) = @$events;

my $ok = $ok_ev->pass;
$result{matched} = $ok ? 1 : 0;
$result{mismatched} = $ok ? 0 : 1;

$result{observation}{candidate} = $wantarray ? @candidate : $candidate[0];
$result{observation}{control} = $wantarray ? @control : $control[0];

if ($result{mismatched}){
$result{observation}{diagnostic} = deep_diag($stack);
# $why_ev has a table displaying why the test failed.
$result{observation}{diagnostic} = $why_ev->message;
}

$self->result( \%result );
Expand All @@ -109,6 +119,12 @@ sub run {
return $wantarray ? @control : $control[0];
}

# Use better column header names in the observation diagnostic table.
sub BUILD {
Test2::Compare::Delta->set_column_alias(GOT => 'CONTROL');
Test2::Compare::Delta->set_column_alias(CHECK => 'CANDIDATE');
}

1;

=head1 LICENSE
Expand Down
5 changes: 4 additions & 1 deletion lib/Scientist.pod
Expand Up @@ -99,7 +99,10 @@ Result contains the result of the experiment after it is run.

Will contain data only AFTER C<-E<gt>run()> has been called.

Observation is included in the result, this includes matched/mismatched. If mismatched present will also have diagnostic present.
Observation is included in the result, this includes matched/mismatched. If
there was a mismatch, then a diagnostic will be made available. This will
display what was expected (from the control) and what we got instead (from the
candidate).

=item run()

Expand Down
8 changes: 5 additions & 3 deletions t/result.t
Expand Up @@ -74,9 +74,11 @@ subtest result_observation => sub {
'Observation control data correct';

is $experiment->result->{observation}{diagnostic},
( "Compared \$data->[0]\n"
. " got : '20'\n"
. "expect : '10'\n" ),
( "+------+---------+----+-----------+\n"
. "| PATH | CONTROL | OP | CANDIDATE |\n"
. "+------+---------+----+-----------+\n"
. "| [0] | 20 | eq | 10 |\n"
. "+------+---------+----+-----------+" ),
'Observation diagnostic correct';
};

Expand Down

0 comments on commit f9ac102

Please sign in to comment.