blib/lib/Test2/Tools/URL.pm

filter: Statementstmt 99.1% 114 / 115 Branchbran 86.3% 38 / 44 Conditioncond 80.9% 17 / 21 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 31 / 31 Podpod 100% 8 / 8 totaltotal 93.5% 217 / 232 CC 31 SCAR 16.3
CC
31
Cov
94%
deltas
12.2
verify
9.0
url_component
5.1
CRAP 5.1
↑ Summary
1 package Test2::Tools::URL;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Test2::Compare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Test2::Compare::Hash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Test2::Compare::String ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Test2::Compare::Custom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our @EXPORT = qw( url url_base url_component url_scheme url_host url_secure url_insecure url_mail_to );
14  
15 # ABSTRACT: Compare a URL in your Test2 test
16 2 our $VERSION = '0.07'; # VERSION
17  
18  
19 sub url (&)
20 46 ▶{
Subroutine url: called CC=1 SCAR=0.0
Pod documented
21 46 Test2::Compare::build('Test2::Tools::URL::Check', @_);
22 }
23  
24  
25 sub url_base ($)
26 3 ▶{
Subroutine url_base: called CC=2 SCAR=6.9
Pod documented
27 3 my($base) = @_;
28  
29 3 my $build = Test2::Compare::get_build();
30 3 ▶ if($build)
Branch: 1 branch
BranchTrueFalseTotal
if ($build) { } 1 2 3
31 1 { $build->set_base($base) }
32 else
33 2 { Test2::Tools::URL::Check->set_global_base($base) }
34 }
35  
36  
37 sub url_component ($$)
38 52 ▶{
Subroutine url_component: called CC=5 SCAR=16.2
Pod documented
39 52 my($name, $expect, $lc, $check_name) = @_;
40  
41 52 ▶ $check_name = 1 unless defined $check_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $check_name 45 7 52
42 52 ▶ if($check_name)
Branch: 1 branch
BranchTrueFalseTotal
if ($check_name) 45 7 52
43 {
44 45 ▶ Carp::croak("$name is not a valid URL component")
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^(?:scheme|authority|userinfo|hostport|host|port|path|query|fragment|user|password|media_type|data)$/ 0 45 45
45 unless $name =~ /^(?:scheme|authority|userinfo|hostport|host|port|path|query|fragment|user|password|media_type|data)$/;
46 }
47  
48 52 ▶ my $build = Test2::Compare::get_build()or Carp::croak("No current build!");
Branch: 1 branch
BranchTrueFalseTotal
my $build = Test2::Compare::get_build() or Carp::croak("No current build!") 52 0 52
49 52 $build->add_component($name, $expect, $lc);
50 }
51  
52  
53 sub url_scheme ($)
54 2 ▶{
Subroutine url_scheme: called CC=1 SCAR=0.0
Pod documented
55 2 unshift @_, 'scheme';
56 2 goto &url_component;
57 }
58  
59  
60 sub url_host ($)
61 2 ▶{
Subroutine url_host: called CC=1 SCAR=0.0
Pod documented
62 2 @_ = ('host', $_[0], 1);
63 2 goto &url_component;
64 }
65  
66  
67 sub url_secure ()
68 2 ▶{
Subroutine url_secure: called CC=1 SCAR=0.0
Pod documented
69 2 my @caller = caller;
70 my $test = Test2::Compare::Custom->new(
71 2 ▶ code => sub { defined $_ && ( ref $_ || $_ ) ? 1 : 0 },
Subroutine __ANON__: called CC=4 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && (ref $_ || $_) ? : 1 1 2
Condition66%4/6
defined $_ and ref $_ || $_
!ll&&!rl&&r
011
ref $_ or $_
l!l&&r!l&&!r
011
MC/DC33%1/3
defined $_ and ref $_ || $_
defined $_ ref $_ $_
Truth table50%2/4
A and B or C
A= defined $_B= ref $_C= $_
ABCresult
0XX0
1000
11X1
1011
72 2 name => 'TRUE',
73 operator => 'TRUE()',
74 file => $caller[1],
75 lines => [$caller[2]],
76 );
77 2 @_ = ('secure', $test, undef, 0);
78 2 goto &url_component;
79 }
80  
81  
82 sub url_insecure ()
83 2 ▶{
Subroutine url_insecure: called CC=1 SCAR=0.0
Pod documented
84 2 my @caller = caller;
85 my $test = Test2::Compare::Custom->new(
86 2 ▶ code => sub { my %p = @_; $p{got} ? 0 : $p{exists} },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$p{'got'} ? : 1 1 2
87 2 name => 'FALSE',
88 operator => 'FALSE()',
89 file => $caller[1],
90 lines => [$caller[2]],
91 );
92 2 @_ = ('secure', $test, undef, 0);
93 2 goto &url_component;
94 }
95  
96  
97 sub url_mail_to ($)
98 3 ▶{
Subroutine url_mail_to: called CC=1 SCAR=0.0
Pod documented
99 3 @_ = ('to', $_[0], undef, 0);
100 3 goto &url_component;
101 }
102  
103 package Test2::Tools::URL::Check;
104  
105 2 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
106 2 ▶use URI 1.61;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶use base qw( Test2::Compare::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 30 ▶sub name { '<URL>' }
Subroutine name: called CC=1 SCAR=0.0
112  
113 2 my $global_base;
114  
115 sub _uri
116 78 ▶{
Subroutine _uri: called CC=3 SCAR=11.0
117 78 my($self, $url) = @_;
118 $self->{base}
119 ? URI->new_abs("$url", $self->{base})
120 78 ▶ : $global_base
Branch: 2 branches
BranchTrueFalseTotal
$self->{'base'} ? : 2 76 78
$global_base ? : 2 74 76
121 ? URI->new_abs("$url", $global_base)
122 : URI->new("$url");
123 }
124  
125 sub verify
126 46 ▶{
Subroutine verify: called CC=9 SCAR=22.0
127 46 my($self, %params) = @_;
128 46 my($got, $exists) = @params{qw/ got exists /};
129  
130 46 ▶ return 0 unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 0 46 46
131 46 ▶ return 0 unless $got;
Branch: 1 branch
BranchTrueFalseTotal
unless $got 1 45 46
132 45 ▶ return 0 if ref($got) && !blessed($got);
Branch: 1 branch
BranchTrueFalseTotal
if ref $got and not builtin::blessed($got) 4 41 45
Condition100%3/3
ref $got and not builtin::blessed($got)
!ll&&!rl&&r
3924
MC/DC100%2/2
ref $got and not builtin::blessed($got)
ref $got not builtin::blessed($got)
Truth table100%3/3
A and not(B)
A= ref $gotB= not builtin::blessed($got)
ABresult
0X0
100
111
133 41 ▶ return 0 if ref($got) && !overload::Method($got, '""');
Branch: 1 branch
BranchTrueFalseTotal
if ref $got and not overload::Method($got, "\"\"") 1 40 41
Condition100%3/3
ref $got and not overload::Method($got, "\"\"")
!ll&&!rl&&r
3911
MC/DC100%2/2
ref $got and not overload::Method($got, "\"\"")
ref $got not overload::Method($got, "\"\"")
Truth table100%3/3
A and not(B)
A= ref $gotB= not overload::Method($got, "\"\"")
ABresult
0X0
100
111
134  
135 40 my $url = eval { $self->_uri($got) };
136 40 ▶ return 0 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 40 40
137 40 ▶ return 0 if ! $url->has_recognized_scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $url->has_recognized_scheme 2 38 40
138  
139 38 return 1;
140 }
141  
142 sub set_base
143 1 ▶{
Subroutine set_base: called CC=1 SCAR=0.0
144 1 my($self, $base) = @_;
145 1 $self->{base} = $base;
146 }
147  
148 sub set_global_base
149 2 ▶{
Subroutine set_global_base: called CC=1 SCAR=0.0
150 2 my($self, $base) = @_;
151 2 $global_base = $base;
152 }
153  
154 sub add_component
155 52 ▶{
Subroutine add_component: called CC=1 SCAR=0.0
156 52 my($self, $name, $expect, $lc) = @_;
157 52 push @{ $self->{component} }, [ $name, $expect, $lc ];
158 }
159  
160 sub deltas
161 38 ▶{
Subroutine deltas: called CC=12 SCAR=25.0
162 38 my($self, %args) = @_;
163 38 my($got, $convert, $seen) = @args{'got', 'convert', 'seen'};
164  
165 38 my $uri = $self->_uri($got);
166  
167 38 my @deltas;
168  
169 38 foreach my $comp (@{ $self->{component} })
170 {
171 52 my($name, $expect, $lc) = @$comp;
172  
173 52 my $method = $name;
174 52 ▶ $method = 'host_port' if $method eq 'hostport';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "hostport" 2 50 52
175 52 ▶ my $value = $uri->can($method) ? $uri->$method : undef;
Branch: 1 branch
BranchTrueFalseTotal
$uri->can($method) ? : 47 5 52
176 52 ▶ $value = lc $value if $lc && defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if $lc and defined $value 1 51 52
Condition100%3/3
$lc and defined $value
!ll&&!rl&&r
5011
MC/DC100%2/2
$lc and defined $value
$lc defined $value
Truth table100%3/3
A and B
A= $lcB= defined $value
ABresult
0X0
100
111
177 52 my $check = $convert->($expect);
178  
179 52 ▶ if($^O eq 'MSWin32' && $method eq 'path')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and $method eq "path") 0 52 52
Condition33%1/3
$^O eq "MSWin32" and $method eq "path"
!ll&&!rl&&r
5200
MC/DC0%0/2
$^O eq "MSWin32" and $method eq "path"
$^O eq "MSWin32" $method eq "path"
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= $method eq "path"
ABresult
0X0
100
111
180 {
181 0 $value =~ s{/([A-Z]:)}{$1};
182 }
183  
184 52 ▶ if($method eq 'query' && !$check->isa('Test2::Compare::String'))
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "query" and not $check->isa("Test2::Compare::String")) 6 46 52
Condition100%3/3
$method eq "query" and not $check->isa("Test2::Compare::String")
!ll&&!rl&&r
4516
MC/DC100%2/2
$method eq "query" and not $check->isa("Test2::Compare::String")
$method eq "query" not $check->isa("Test2::Compare::String")
Truth table100%3/3
A and not(B)
A= $method eq "query"B= not $check->isa("Test2::Compare::String")
ABresult
0X0
100
111
185 {
186 6 ▶ if($check->isa('Test2::Compare::Hash'))
Branch: 2 branches
BranchTrueFalseTotal
if ($check->isa('Test2::Compare::Hash')) { } 4 2 6
elsif ($check->isa('Test2::Compare::Array')) { } 2 0 2
187 {
188 4 $value = $uri->query_form_hash;
189 }
190 elsif($check->isa('Test2::Compare::Array'))
191 {
192 2 $value = [ $uri->query_form ];
193 }
194 }
195  
196  
197 52 push @deltas => $check->run(
198 id => [ HASH => $name ],
199 convert => $convert,
200 seen => $seen,
201 exists => defined $value,
202 got => $value,
203 );
204 }
205  
206 38 @deltas;
207 }
208  
209 2 1;
210  
211 __END__
↑ Summary