File Coverage

blib/lib/Assert/Refute/Driver/More.pm
Criterion Covered Total %
statement 49 50 98.0
branch 10 14 71.4
condition 4 8 50.0
subroutine 15 15 100.0
pod 8 8 100.0
total 86 95 90.5


line stmt bran cond sub pod time code
1             package Assert::Refute::Driver::More;
2              
3 15     15   296 use 5.006;
  15         51  
4 15     15   88 use strict;
  15         31  
  15         373  
5 15     15   72 use warnings;
  15         31  
  15         689  
6             our $VERSION = '0.16';
7              
8             =head1 NAME
9              
10             Assert::Refute::Driver::More - Test::More compatibility layer for Asser::Refute suite
11              
12             =head1 SYNOPSIS
13              
14             In your test script:
15              
16             use Test::More;
17             use Assert::Refute qw(:all); # in that order
18              
19             my $def = contract {
20             # don't use is/ok/etc here
21             my ($c, @args) = @_;
22             $c->is (...);
23             $c->like (...);
24             };
25              
26             is foo(), $bar, "Normal test";
27             subcontract "Repeated test block 1", $def, $value1;
28             like $string, qr/.../, "Another normal test";
29             subcontract "Repeated test block 2", $def, $value2;
30              
31             done_testing;
32              
33             =head1 DESCRIPTION
34              
35             This class is useless in and of itself.
36             It is auto-loaded as a bridge between L and L,
37             B Test::More has been loaded B Assert::Refute.
38              
39             =head1 METHODS
40              
41             We override some methods of L below so that
42             test results are fed to the more backend.
43              
44             =cut
45              
46 15     15   104 use Carp;
  15         46  
  15         1034  
47              
48 15     15   104 use parent qw(Assert::Refute::Report);
  15         32  
  15         96  
49 15     15   1125 use Assert::Refute::Build qw(to_scalar);
  15         32  
  15         10700  
50              
51             =head2 new
52              
53             Will automatically load L instance,
54             which is assumed to be a singleton as of this writing.
55              
56             =cut
57              
58             sub new {
59 16     16 1 51 my ($class, %opt) = @_;
60              
61 16 50       174 confess "Test::Builder not initialised, refusing toi proceed"
62             unless Test::Builder->can("new");
63              
64 16         131 my $self = $class->SUPER::new(%opt);
65 16         97 $self->{builder} = Test::Builder->new; # singletone this far
66 16         353 $self;
67             };
68              
69             =head2 refute( $condition, $message )
70              
71             The allmighty refute() boils down to
72              
73             ok !$condition, $message
74             or diag $condition;
75              
76             =cut
77              
78             sub refute {
79 30     30 1 88 my ($self, $reason, $mess) = @_;
80              
81             # TODO bug - if refute() is called directly as $contract->refute,
82             # it will report the wrong file & line
83 30         67 local $Test::Builder::Level = $Test::Builder::Level + 1;
84              
85 30         122 $self->{count} = $self->{builder}->current_test;
86 30         4819 $self->{builder}->ok(!$reason, $mess);
87              
88             # see Assert::Refute::Report->get_result_detail
89 30 50 66     15383 if (ref $reason eq 'ARRAY') {
    100          
90 0         0 $self->{builder}->diag(to_scalar($_)) for @$reason;
91             } elsif ($reason and $reason ne 1) {
92 4         28 $self->{builder}->diag(to_scalar($reason));
93             };
94              
95             # Do we even need to track it here?
96 30         1412 $self->SUPER::refute($reason, $mess);
97             };
98              
99             =head2 subcontract
100              
101             Proxy to L's subtest.
102              
103             =cut
104              
105             sub subcontract {
106 1     1 1 3 my ($self, $mess, $todo, @args) = @_;
107              
108             $self->{builder}->subtest( $mess => sub {
109 1     1   1105 my $rep = (ref $self)->new( builder => $self->{builder} )->do_run(
110             $todo, @args
111             );
112             # TODO also save $rep result in $self
113 1         7 } );
114             };
115              
116             =head2 done_testing
117              
118             Proxy for C in L.
119              
120             =cut
121              
122             sub done_testing {
123 1     1 1 3 my $self = shift;
124              
125 1         6 $self->{builder}->done_testing;
126 1         720 $self->SUPER::done_testing;
127             };
128              
129             =head2 do_log( $indent, $level, $message )
130              
131             Just fall back to diag/note.
132             Indentation is ignored.
133              
134             =cut
135              
136             sub do_log {
137 2     2 1 7 my ($self, $indent, $level, @mess) = @_;
138              
139 2 100       10 if ($level == -1) {
    50          
140 1         5 $self->{builder}->diag($_) for @mess;
141             } elsif ($level > 0) {
142 1         6 $self->{builder}->note($_) for @mess;
143             };
144              
145 2         826 $self->SUPER::do_log( $indent, $level, @mess );
146             };
147              
148             =head2 get_count
149              
150             Current test number.
151              
152             =cut
153              
154             sub get_count {
155 1     1 1 2 my $self = shift;
156 1         3 return $self->{builder}->current_test;
157             };
158              
159             =head2 is_passing
160              
161             Tell if the whole set is passing.
162              
163             =cut
164              
165             sub is_passing {
166 2     2 1 4 my $self = shift;
167 2         8 return $self->{builder}->is_passing;
168             };
169              
170             =head2 get_result
171              
172             Fetch result of n-th test.
173              
174             0 is for passing tests, a true value is for failing ones.
175              
176             =cut
177              
178             sub get_result {
179 2     2 1 6 my ($self, $n) = @_;
180              
181             return $self->{fail}{$n} || 0
182 2 100 50     13 if exists $self->{fail}{$n};
183              
184 1         4 my @t = $self->{builder}->summary;
185 1 50 33     172 $self->_croak( "Test $n has never been performed" )
186             unless $n =~ /^[1-9]\d*$/ and $n <= @t;
187              
188             # Alas, no reason here
189 1         8 return !$t[$n];
190             };
191              
192             =head1 LICENSE AND COPYRIGHT
193              
194             This module is part of L suite.
195              
196             Copyright 2017-2018 Konstantin S. Uvarin. C<< >>
197              
198             This program is free software; you can redistribute it and/or modify it
199             under the terms of the the Artistic License (2.0). You may obtain a
200             copy of the full license at:
201              
202             L
203              
204             =cut
205              
206             1;