Skip to content

Commit

Permalink
use Test2 in run()
Browse files Browse the repository at this point in the history
- Replaces cmp_deeply() with is().

- Replaces the diagnostic with what I hope is a better diagnostic aid; a table
  showing what was expected and how it differed from what we wanted.

- This update probably makes lancew#10 a moot point.
  • Loading branch information
Joshua Keroes committed Apr 1, 2016
1 parent 8233838 commit 6e5c9be
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
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 Types::Standard
script: PERL5OPT=-MDevel::Cover prove -lv
after_success: cover -report coveralls
17 changes: 13 additions & 4 deletions lib/Scientist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ 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::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 @@ -90,15 +91,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 Down
5 changes: 4 additions & 1 deletion lib/Scientist.pod
Original file line number Diff line number Diff line change
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 inclides macthed/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
14 changes: 11 additions & 3 deletions t/scientist.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ is $experiment->result->{'mismatched'}, 1,
is $experiment->result->{observation}{candidate}, 20, 'Observation Candidate data correct';
is $experiment->result->{observation}{control}, 10, 'Observation Control data correct';

like $experiment->result->{observation}{diagnostic},
qr/got : '20'/,
'Observation diagnostic correct';
my $expected_diag = q{
+------+-----+----+-------+
| PATH | GOT | OP | CHECK |
+------+-----+----+-------+
| [0] | 20 | eq | 10 |
+------+-----+----+-------+};
$expected_diag =~ s/^\s+//mg;

is $experiment->result->{observation}{diagnostic},
$expected_diag,
'Observation diagnostic correct';

$experiment->use( \&old_code );
$experiment->try( \&old_code );
Expand Down

0 comments on commit 6e5c9be

Please sign in to comment.