blib/lib/Test2/Plugin/SRand.pm

filter: Statementstmt 100% 40 / 40 Branchbran 93.7% 15 / 16 Conditioncond 46.6% 7 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 81.1% 73 / 90 CC 14 SCAR 19.6
CC
14
Cov
87%
import
10.5
__ANON__
8.9
BEGIN
1.0
CRAP 7.1
« blib/lib/Test2/Mock.pm ↑ Summary blib/lib/Test2/Compare/Base.pm »
1 package Test2::Plugin::SRand;
2 163 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 163 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 163 our $VERSION = '1.302226';
6  
7 163 ▶use Carp qw/carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 163 ▶use Test2::API qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 context
11 test2_add_callback_post_load
12 test2_add_callback_exit
13 test2_stack
14 163 };
15  
16 163 my $ADDED_HOOK = 0;
17 163 my $SEED;
18 my $FROM;
19  
20 10 ▶sub seed { $SEED }
Subroutine seed: called CC=1 SCAR=0.0
Pod undocumented
21 5 ▶sub from { $FROM }
Subroutine from: called CC=1 SCAR=0.0
Pod undocumented
22  
23 169 ▶sub import {
Subroutine import: called CC=10 SCAR=23.5
24 169 my $class = shift;
25  
26 169 ▶ carp "SRand loaded multiple times, re-seeding rand"
Branch: 1 branch
BranchTrueFalseTotal
if defined $SEED 6 163 169
27 if defined $SEED;
28  
29 169 ▶ if (@_ == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 2 167 169
elsif (@_ == 2 and $_[0] eq 'seed') { } 1 166 167
elsif (exists $ENV{'T2_RAND_SEED'}) { } 1 165 166
30 2 ($SEED) = @_;
31 2 $FROM = 'import arg';
32 }
33 ▶ elsif (@_ == 2 and $_[0] eq 'seed') {
Condition66%2/3
@_ == 2 and $_[0] eq "seed"
!ll&&!rl&&r
16601
MC/DC50%1/2
@_ == 2 and $_[0] eq "seed"
@_ == 2 $_[0] eq "seed"
Truth table66%2/3
A and B
A= @_ == 2B= $_[0] eq "seed"
ABresult
0X0
100
111
34 1 $SEED = $_[1];
35 1 $FROM = 'import arg';
36 }
37 elsif(exists $ENV{T2_RAND_SEED}) {
38 1 $SEED = $ENV{T2_RAND_SEED};
39 1 $FROM = 'environment variable';
40 }
41 else {
42 165 my @ltime = localtime;
43 # Yes, this would be an awful seed if you actually wanted randomness.
44 # The idea here is that we want "random" behavior to be predictable
45 # within a given day. This allows you to reproduce failures that may or
46 # may not happen due to randomness.
47 165 $SEED = sprintf('%04d%02d%02d', 1900 + $ltime[5], 1 + $ltime[4], $ltime[3]);
48 165 $FROM = 'local date';
49 }
50  
51 169 ▶ $SEED = 0 unless $SEED;
Branch: 1 branch
BranchTrueFalseTotal
unless $SEED 1 168 169
52 169 srand($SEED);
53  
54 169 ▶ if ($ENV{HARNESS_IS_VERBOSE} || !$ENV{HARNESS_ACTIVE}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'HARNESS_IS_VERBOSE'} or not $ENV{'HARNESS_ACTIVE'}) { } 4 165 169
elsif (not $ADDED_HOOK++) { } 163 2 165
Condition66%2/3
$ENV{"HARNESS_IS_VERBOSE"} or not $ENV{"HARNESS_ACTIVE"}
l!l&&r!l&&!r
40165
MC/DC50%1/2
$ENV{"HARNESS_IS_VERBOSE"} or not $ENV{"HARNESS_ACTIVE"}
$ENV{"HARNESS_IS_VERBOSE"} not $ENV{"HARNESS_ACTIVE"}
Truth table66%2/3
A or not(B)
A= $ENV{"HARNESS_IS_VERBOSE"}B= not $ENV{"HARNESS_ACTIVE"}
ABresult
1X1
011
000
55 # If the harness is verbose then just display the message for all to
56 # see. It is nice info and they already asked for noisy output.
57  
58 4 ▶ test2_add_callback_post_load(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 4 test2_stack()->top; # Ensure we have at least 1 hub.
60 4 my ($hub) = test2_stack()->all;
61 4 $hub->send(
62 Test2::Event::Note->new(
63 trace => Test2::Util::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__, 'SRAND']),
64 message => "Seeded srand with seed '$SEED' from $FROM.",
65 )
66 );
67 4 });
68 }
69 elsif (!$ADDED_HOOK++) {
70 # The seed can be important for debugging, so if anything is wrong we
71 # should output the seed message as a diagnostics message. This must be
72 # done at the very end, even later than a hub hook.
73 test2_add_callback_exit(
74 117 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=21.9
75 117 my ($ctx, $real, $new) = @_;
76  
77 117 ▶ $ctx->diag("Seeded srand with seed '$SEED' from $FROM.")
Branch: 1 branch
BranchTrueFalseTotal
if $real or $new and $$new or not $ctx->hub->is_passing 0 117 117
Condition33%3/9
$real or $new and $$new or not $ctx->hub->is_passing
l!l&&r!l&&!r
00117
$real or $new and $$new
l!l&&r!l&&!r
00117
$new and $$new
!ll&&!rl&&r
01170
MC/DC0%0/4
$real or $new and $$new or not $ctx->hub->is_passing
$real $new $$new not $ctx->hub->is_passing
Truth table16%1/6
A or B and C or not(D)
A= $realB= $newC= $$newD= not $ctx->hub->is_passing
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
78 if $real
79 || ($new && $$new)
80 || !$ctx->hub->is_passing;
81 }
82 163 );
83 }
84 }
85  
86 163 1;
87  
88 __END__
« blib/lib/Test2/Mock.pm ↑ Summary blib/lib/Test2/Compare/Base.pm »