Perlito5X/Test2/Event/Generic.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 157 CC 12 SCAR 19.9
CC
12
Cov
0%
init
12.0
set_callback
12.0
set_terminate
12.0
CRAP 7.3
1 package Test2::Event::Generic;
2 use strict;
3 use warnings;
4  
5 use Carp qw/croak/;
6 use Scalar::Util qw/reftype/;
7  
8 our $VERSION = '1.302073';
9  
10 BEGIN { require Test2::Event; our @ISA = qw(Test2::Event) }
11 use Test2::Util::HashBase;
12  
13 my @FIELDS = qw{
14 causes_fail increments_count diagnostics no_display callback terminate
15 global sets_plan summary
16 };
17 my %DEFAULTS = (
18 causes_fail => 0,
19 increments_count => 0,
20 diagnostics => 0,
21 no_display => 0,
22 );
23  
24 sub init {
25 my $self = shift;
26  
27 for my $field (@FIELDS) {
28 my $val = defined $self->{$field} ? delete $self->{$field} : $DEFAULTS{$field};
29 next unless defined $val;
30  
31 my $set = "set_$field";
32 $self->$set($val);
33 }
34 }
35  
36 for my $field (@FIELDS) {
37 no strict 'refs';
38 my $stash = \%{__PACKAGE__ . "::"};
39  
40 *$field = sub { exists $_[0]->{$field} ? $_[0]->{$field} : () }
41 unless defined $stash->{$field}
42 && defined *{$stash->{$field}}{CODE};
43  
44 *{"set_$field"} = sub { $_[0]->{$field} = $_[1] }
45 unless defined $stash->{"set_$field"}
46 && defined *{$stash->{"set_$field"}}{CODE};
47 }
48  
49 sub summary {
50 my $self = shift;
51 return $self->{summary} if defined $self->{summary};
52 $self->SUPER::summary();
53 }
54  
55 sub sets_plan {
56 my $self = shift;
57 return unless $self->{sets_plan};
58 return @{$self->{sets_plan}};
59 }
60  
61 sub callback {
62 my $self = shift;
63 my $cb = $self->{callback} || return;
64 $self->$cb(@_);
65 }
66  
67 sub set_global {
68 my $self = shift;
69 my ($bool) = @_;
70  
71 if(!defined $bool) {
72 delete $self->{global};
73 return undef;
74 }
75  
76 $self->{global} = $bool;
77 }
78  
79 sub set_callback {
80 my $self = shift;
81 my ($cb) = @_;
82  
83 if(!defined $cb) {
84 delete $self->{callback};
85 return undef;
86 }
87  
88 croak "callback must be a code reference"
89 unless ref($cb) && reftype($cb) eq 'CODE';
90  
91 $self->{callback} = $cb;
92 }
93  
94 sub set_terminate {
95 my $self = shift;
96 my ($exit) = @_;
97  
98 if(!defined $exit) {
99 delete $self->{terminate};
100 return undef;
101 }
102  
103 croak "terminate must be a positive integer"
104 unless $exit =~ m/^\d+$/;
105  
106 $self->{terminate} = $exit;
107 }
108  
109 sub set_sets_plan {
110 my $self = shift;
111 my ($plan) = @_;
112  
113 if(!defined $plan) {
114 delete $self->{sets_plan};
115 return undef;
116 }
117  
118 croak "'sets_plan' must be an array reference"
119 unless ref($plan) && reftype($plan) eq 'ARRAY';
120  
121 $self->{sets_plan} = $plan;
122 }
123  
124 1;
125  
126 __END__