blib/lib/Test2/Plugin/TodoFailOnSuccess.pm

filter: Statementstmt 100% 35 / 35 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 84.8% 56 / 66 CC 11 SCAR 17.2
CC
11
Cov
89%
__ANON__
9.2
import
2.0
on_context_release
2.0
CRAP 5.6
↑ Summary
1 package Test2::Plugin::TodoFailOnSuccess;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Report failure if a TODO test unexpectedly passes
7 3 our $VERSION = '0.0.2'; # VERSION
8  
9 3 our $AUTHORITY = 'cpan:GSG';
10  
11 #pod =encoding utf8
12 #pod
13 #pod =head1 SYNOPSIS
14 #pod
15 #pod package My::Tests;
16 #pod
17 #pod use Test2::V0;
18 #pod
19 #pod use Test2::Plugin::TodoFailOnSuccess; # report unexpected TODO success
20 #pod
21 #pod use Test2::Tools::Basic; # for "todo" sub
22 #pod use Test2::Todo; # for "todo" object
23 #pod
24 #pod sub test_something
25 #pod {
26 #pod # Lexical scope TODO:
27 #pod #
28 #pod {
29 #pod my $todo = todo 'Not expected to pass';
30 #pod is $value, $expected_value, "Got expected value";
31 #pod }
32 #pod
33 #pod # Coderef TODO:
34 #pod #
35 #pod todo 'Not expected to pass either' => sub {
36 #pod is $value, $expected_value, "Got expected value";
37 #pod };
38 #pod
39 #pod # Object-oriented TODO:
40 #pod #
41 #pod my $todo = Test2::Todo->new( reason => 'Still not expected to pass' );
42 #pod is $value, $expected_value, "Got expected value";
43 #pod $todo->end;
44 #pod }
45 #pod
46 #pod =head1 DESCRIPTION
47 #pod
48 #pod Wrapping a test with TODO is a conventient way to avoid being tripped
49 #pod up by test failures until you have a chance to get the code working.
50 #pod It normally won't hurt to leave the TODO in place after the tests
51 #pod start passing, but if you forget to remove the TODO at that point,
52 #pod a subsequent code change could start causing new test failures which
53 #pod would then go unreported and possibly unnoticed.
54 #pod
55 #pod This module provides a mechanism to trigger explicit test failures
56 #pod when TODO tests unexpectedly pass, so that you have an opportunity
57 #pod to remove the TODO.
58 #pod
59 #pod If a TODO test passes, a failure will be reported with a message
60 #pod containing the test description, equivalent to doing:
61 #pod
62 #pod fail "TODO passed unexpectedly: $test_description";
63 #pod
64 #pod which might appear in your TAP output along with the TODO reason as
65 #pod something like:
66 #pod
67 #pod not ok 3 - TODO passed unexpectedly: Got expected value # TODO Not expected to pass
68 #pod
69 #pod Note that due to the additional C<fail> being reported, you may
70 #pod see messages about your planned number of tests being exceeded,
71 #pod for example:
72 #pod
73 #pod # Did not follow plan: expected 5, ran 6.
74 #pod
75 #pod There are no options or arguments, just C<use Test2::Plugin::TodoFailOnSuccess>
76 #pod in your test file.
77 #pod
78 #pod =cut
79  
80 3 ▶use Test2::API qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
81 test2_add_callback_context_init
82 test2_add_callback_context_release
83 3 );
84  
85 3 my $PLUGIN_LOADED = 0;
86  
87 sub import
88 2 ▶{
Subroutine import: called CC=2 SCAR=7.1
89 2 ▶ return if $PLUGIN_LOADED++;
Branch: 1 branch
BranchTrueFalseTotal
if $PLUGIN_LOADED++ 0 2 2
90  
91 2 test2_add_callback_context_init ( \&on_context_init );
92 2 test2_add_callback_context_release( \&on_context_release );
93 }
94  
95 sub on_context_init
96 11 ▶{
Subroutine on_context_init: called CC=1 SCAR=0.0
Pod undocumented
97 11 my ($ctx) = @_;
98  
99 # Set up a listener on the hub to watch events going by,
100 # looking for the ones that indicate a TODO test which passed:
101 #
102 $ctx->{_TodoFailOnSuccess_hub_listener} = $ctx->hub->listen(
103 12 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
104 12 my ($hub, $event, $number) = @_;
105  
106 12 my $facet_data = $event->facet_data;
107  
108 # Events inside a TODO will have amnesty (although will
109 # need to verify the type of amnesty later):
110 #
111 12 my $amnesty_list = $facet_data->{amnesty};
112 12 ▶ return unless $amnesty_list && @$amnesty_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $amnesty_list and @$amnesty_list 9 3 12
Condition66%2/3
$amnesty_list and @$amnesty_list
!ll&&!rl&&r
903
MC/DC50%1/2
$amnesty_list and @$amnesty_list
$amnesty_list @$amnesty_list
Truth table66%2/3
A and B
A= $amnesty_listB= @$amnesty_list
ABresult
0X0
100
111
113  
114 # Only interested if the event made an assertion which passed:
115 #
116 3 my $assert = $facet_data->{assert};
117 3 ▶ return unless $assert && $assert->{pass};
Branch: 1 branch
BranchTrueFalseTotal
unless $assert and $assert->{"pass"} 2 1 3
Condition66%2/3
$assert and $assert->{"pass"}
!ll&&!rl&&r
021
MC/DC50%1/2
$assert and $assert->{"pass"}
$assert $assert->{"pass"}
Truth table66%2/3
A and B
A= $assertB= $assert->{"pass"}
ABresult
0X0
100
111
118  
119 # Make sure at least one of the amnesty reasons
120 # is because of TODO:
121 #
122 1 my %todo_reasons;
123 1 foreach my $amnesty (@$amnesty_list) {
124 1 ▶ next unless $amnesty->{tag} eq 'TODO';
Branch: 1 branch
BranchTrueFalseTotal
unless $amnesty->{"tag"} eq "TODO" 0 1 1
125 1 $todo_reasons{ $amnesty->{details} } = 1;
126 }
127 1 ▶ return unless keys %todo_reasons;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %todo_reasons 0 1 1
128  
129 1 my $details = $assert->{details};
130  
131 1 foreach my $todo_reason (sort keys %todo_reasons) {
132 1 $ctx->fail(
133 qq{TODO passed unexpectedly: $details}
134 );
135 }
136 },
137 11 inherit => 1,
138 );
139 }
140  
141 sub on_context_release
142 11 ▶{
Subroutine on_context_release: called CC=2 SCAR=7.1
Pod undocumented
143 11 my ($ctx) = @_;
144  
145 11 my $hub_listener = delete $ctx->{_TodoFailOnSuccess_hub_listener};
146 11 ▶ $ctx->hub->unlisten($hub_listener) if $hub_listener;
Branch: 1 branch
BranchTrueFalseTotal
if $hub_listener 11 0 11
147 }
148  
149 3 1;
150  
151 __END__
↑ Summary