Util/UUID.pm

filter: Statementstmt 95.6% 109 / 114 Branchbran 57.5% 23 / 40 Conditioncond 68.4% 13 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 26 / 26 Podpod 33.3% 1 / 3 totaltotal 82.1% 175 / 213 CC 30 SCAR 14.8
CC
30
Cov
84%
get_gen_uuid
9.5
import
8.2
_gen_uuid
4.2
CRAP 4.4
↑ Summary Plugin/UUID.pm »
1 package Test2::Util::UUID;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.002011';
6  
7 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 my %EXPORT = (
10 looks_like_uuid => 1,
11 gen_uuid => 1,
12 GEN_UUID_BACKEND => 1,
13 uuid2bin => 1,
14 bin2uuid => 1,
15 );
16  
17 6 ▶sub import {
Subroutine import: called CC=8 SCAR=21.0
18 6 my $class = shift;
19 6 my $caller = caller;
20  
21 6 my %gen_params;
22 my %import;
23  
24 6 ▶ while (my $arg = shift @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @_) 30 6 36
25 30 ▶ if ($EXPORT{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($EXPORT{$arg}) 22 8 30
26 22 $import{$arg}++;
27 22 next;
28 }
29  
30 8 ▶ if ($arg eq 'warn' || $arg eq 'backends') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq "warn" or $arg eq "backends") 8 0 8
Condition66%2/3
$arg eq "warn" or $arg eq "backends"
l!l&&r!l&&!r
440
MC/DC0%0/2
$arg eq "warn" or $arg eq "backends"
$arg eq "warn" $arg eq "backends"
Truth table66%2/3
A or B
A= $arg eq "warn"B= $arg eq "backends"
ABresult
1X1
011
000
31 8 $gen_params{$arg} = shift @_;
32 8 next;
33 }
34  
35 0 croak "Invalid argument '$arg'";
36 }
37  
38 6 my $subs = $class->get_gen_uuid(%gen_params);
39  
40 6 for my $name (keys %import) {
41 22 ▶ my $sub = $subs->{$name} || $class->can($name) or croak "'$name' is not available for import";
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $subs->{$name} || $class->can($name) or croak("'${name}' is not available for import") 22 0 22
Condition66%2/3
$subs->{$name} || $class->can($name)
l!l&&r!l&&!r
1660
MC/DC0%0/2
$subs->{$name} || $class->can($name)
$subs->{$name} $class->can($name)
Truth table66%2/3
A || B
A= $subs->{$name}B= $class->can($name)
ABresult
1X1
011
000
42  
43 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 22 *{"$caller\::$name"} = $sub;
45 }
46  
47 6 return;
48 }
49  
50 2 my %GEN_UUID_CACHE;
51  
52 13 ▶sub clear_cache { %GEN_UUID_CACHE = () }
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod undocumented
53  
54 19 ▶sub get_gen_uuid {
Subroutine get_gen_uuid: called CC=9 SCAR=22.5
Pod undocumented
55 19 my $class = shift;
56 19 my %params = @_;
57  
58 19 ▶ my $warn = $params{warn} // ($ENV{TEST2_UUID_NO_WARN} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TEST2_UUID_NO_WARN'} ? : 0 6 6
Condition100%2/2
$params{"warn"} // ($ENV{"TEST2_UUID_NO_WARN"} ? 0 : 1)
l!l
136
MC/DC100%1/1
$params{"warn"} // ($ENV{"TEST2_UUID_NO_WARN"} ? 0 : 1)
$params{"warn"}
Truth table100%2/2
A
A= $params{"warn"}
Aresult
11
00
59 19 ▶ my $backends = $params{backends} // ($ENV{TEST2_UUID_BACKEND} ? [split /\s*,\s*/, $ENV{TEST2_UUID_BACKEND}] : ['UUID', 'Data::UUID::MT', 'UUID::Tiny', 'Data::UUID']);
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TEST2_UUID_BACKEND'} ? : 0 2 2
Condition100%2/2
$params{"backends"} // ($ENV{"TEST2_UUID_BACKEND"} ? [split(/\s*,\s*/, $ENV{"TEST2_UUID_BACKEND"}, 0)] : ["UUID", "Data::UUID::MT", "UUID::Tiny", "Data::UUID"])
l!l
172
MC/DC100%1/1
$params{"backends"} // ($ENV{"TEST2_UUID_BACKEND"} ? [split(/\s*,\s*/, $ENV{"TEST2_UUID_BACKEND"}, 0)] : ["UUID", "Data::UUID::MT", "UUID::Tiny", "Data::UUID"])
$params{"backends"}
Truth table100%2/2
A
A= $params{"backends"}
Aresult
11
00
60  
61 19 for my $backend (@$backends) {
62 19 ▶ return $GEN_UUID_CACHE{$backend} if $GEN_UUID_CACHE{$backend};
Branch: 1 branch
BranchTrueFalseTotal
if $GEN_UUID_CACHE{$backend} 4 15 19
63  
64 15 my $meth = lc("_gen_$backend");
65 15 $meth =~ s/::/_/g;
66  
67 15 ▶ croak "'$backend' is not supported" unless $class->can($meth);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($meth) 0 15 15
68  
69 15 ▶ $GEN_UUID_CACHE{$backend} = $class->$meth($warn) or next;
Branch: 1 branch
BranchTrueFalseTotal
$GEN_UUID_CACHE{$backend} = $class->$meth($warn) or next 15 0 15
70 15 ▶ $GEN_UUID_CACHE{$backend}->{GEN_UUID_BACKEND} = sub() { $backend };
Subroutine __ANON__: called CC=1 SCAR=0.0
71 15 return $GEN_UUID_CACHE{$backend};
72 }
73  
74 0 croak "No UUID generator found, please install one of these: UUID, Data::UUID::MT, Data::UUID, or UUID::Tiny. ('UUID' is preferred over the others)\n";
75 }
76  
77 6 ▶sub _gen_uuid {
Subroutine _gen_uuid: called CC=4 SCAR=14.4
78 6 my $class = shift;
79 6 my ($warn) = @_;
80  
81 6 local $@;
82 6 ▶ return undef unless eval { require UUID; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require UUID; 1 } } 0 6 6
83  
84 6 ▶ unless (eval { UUID->VERSION('0.35'); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { "UUID"->VERSION("0.35"); 1 } }) 0 6 6
85 0 ▶ warn "UUID version is too old, need 0.35 or greater to avoid a fork related bug. Please upgrade the UUID module.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
86 if $warn;
87  
88 0 return;
89 }
90  
91 return {
92 60 ▶ gen_uuid => sub { uc(UUID::uuid7()) },
Subroutine __ANON__: called CC=1 SCAR=0.0
93 1 ▶ bin2uuid => sub { my $out; UUID::unparse($_[0], $out); uc($out) },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 1 ▶ uuid2bin => sub { my $out; UUID::parse($_[0], $out); $out },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 6 };
96 }
97  
98 3 ▶sub _gen_data_uuid_mt {
Subroutine _gen_data_uuid_mt: called CC=3 SCAR=11.0
99 3 my $class = shift;
100 3 my ($warn) = @_;
101  
102 3 local $@;
103 3 ▶ return undef unless eval { require Data::UUID::MT; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Data::UUID::MT; 1 } } 0 3 3
104  
105 3 my $ug = Data::UUID::MT->new(version => 4);
106 my $out = {
107 59 ▶ gen_uuid => sub { uc($ug->create_string()) },
Subroutine __ANON__: called CC=1 SCAR=0.0
108 3 };
109  
110 3 ▶ if (eval { require UUID::Tiny; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require UUID::Tiny; 1 } }) 3 0 3
111 1 ▶ $out->{uuid2bin} = sub { UUID::Tiny::string_to_uuid($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 1 ▶ $out->{bin2uuid} = sub { uc(UUID::Tiny::uuid_to_string($_[0])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
113 3 }
114  
115 3 return $out;
116 }
117  
118 3 ▶sub _gen_uuid_tiny {
Subroutine _gen_uuid_tiny: called CC=3 SCAR=11.1
119 3 my $class = shift;
120 3 my ($warn) = @_;
121  
122 3 local $@;
123  
124 3 ▶ return undef unless eval { require UUID::Tiny; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require UUID::Tiny; 1 } } 0 3 3
125  
126 3 ▶ warn "Using UUID::Tiny for uuid generation. UUID::Tiny is significantly slower than the 'UUID' or 'Data::UUID::MT' modules, please install 'UUID' or 'Data::UUID::MT' if possible. If you insist on using UUID::Tiny you can set the TEST2_UUID_NO_WARN environment variable.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 3 3
127 if $warn;
128  
129 return {
130 58 ▶ gen_uuid => sub { uc(UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V4())) },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 1 ▶ bin2uuid => sub { uc(UUID::Tiny::uuid_to_string($_[0])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
132 1 ▶ uuid2bin => sub { UUID::Tiny::string_to_uuid($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
133 3 };
134 }
135  
136 3 ▶sub _gen_data_uuid {
Subroutine _gen_data_uuid: called CC=3 SCAR=11.1
137 3 my $class = shift;
138 3 my ($warn) = @_;
139  
140 3 local $@;
141 3 ▶ return undef unless eval { require Data::UUID; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Data::UUID; 1 } } 0 3 3
142  
143 3 ▶ warn "Using Data::UUID to generate UUIDs, this works, but the UUIDs will not be suitible as database keys. Please install the 'UUID', 'Data::UUID::MT' or the slower but pure perl 'UUID::Tiny' cpan modules for better UUIDs. If you insist on using Data::UUID you can set the TEST2_UUID_NO_WARN environment variable.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 3 3
144 if $warn;
145  
146 3 my ($UG, $UG_PID);
147  
148 64 ▶ my $UG_INIT = sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
149 64 ▶ return $UG if $UG && $UG_PID && $UG_PID == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $UG and $UG_PID and $UG_PID == $$ 61 3 64
Condition66%4/6
$UG and $UG_PID and $UG_PID == $$
!ll&&!rl&&r
3061
$UG and $UG_PID
!ll&&!rl&&r
3061
MC/DC33%1/3
$UG and $UG_PID and $UG_PID == $$
$UG $UG_PID $UG_PID == $$
Truth table50%2/4
A and B and C
A= $UGB= $UG_PIDC= $UG_PID == $$
ABCresult
0XX0
10X0
1100
1111
150  
151 3 $UG_PID = $$;
152 3 return $UG = Data::UUID->new;
153 3 };
154  
155 # Initialize it here in this PID to start
156 3 $UG_INIT->();
157  
158 return {
159 59 ▶ gen_uuid => sub { uc($UG_INIT->()->create_str()) },
Subroutine __ANON__: called CC=1 SCAR=0.0
160 1 ▶ bin2uuid => sub { uc($UG_INIT->()->to_string($_[0])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
161 1 ▶ uuid2bin => sub { $UG_INIT->()->from_string($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
162 3 };
163 }
164  
165 41 ▶sub looks_like_uuid {
Subroutine looks_like_uuid: called CC=3 SCAR=14.2
Pod documented
166 41 my ($in) = @_;
167 41 ▶ return $in if $in && $in =~ m/^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $in and $in =~ /^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i 41 0 41
Condition33%1/3
$in and $in =~ /^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i
!ll&&!rl&&r
0041
MC/DC0%0/2
$in and $in =~ /^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i
$in $in =~ /^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i
Truth table33%1/3
A and B
A= $inB= $in =~ /^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/i
ABresult
0X0
100
111
168 0 return undef;
169 }
170  
171 2 1;
172  
173 __END__
↑ Summary Plugin/UUID.pm »