blib/lib/Plack/App/DAIA/Test.pm

filter: Statementstmt 77.0% 74 / 96 Branchbran 61.7% 21 / 34 Conditioncond 43.7% 7 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 94.4% 17 / 18 Podpod 100% 3 / 3 totaltotal 70.0% 124 / 177 CC 24 SCAR 21.7
CC
24
Cov
70%
daia_app
23.8
__ANON__
20.0
__ANON__
6.8
CRAP 8.8
« blib/lib/Plack/App/DAIA.pm ↑ Summary app.psgi »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Plack::App::DAIA::Test;
5 #ABSTRACT: Test DAIA Servers
6 4 our $VERSION = '0.55'; #VERSION
7 4 ▶use base 'Test::Builder::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 our @EXPORT = qw(test_daia_psgi test_daia daia_app);
9  
10 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Plack::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Plack::App::DAIA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Test::JSON::Entails;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶sub test_daia {
Subroutine test_daia: called CC=6 SCAR=18.5
Pod documented
20 5 ▶ my $app = daia_app(shift) || do {
Condition33%1/3
daia_app(shift()) || do { __PACKAGE__->builder->ok(0, 'Could not construct DAIA application'); return }
l!l&&r!l&&!r
500
MC/DC0%0/2
daia_app(shift()) || do { __PACKAGE__->builder->ok(0, 'Could not construct DAIA application'); return }
daia_app(shift()) do { __PACKAGE__->builder->ok(0, 'Could not construct DAIA application'); return }
Truth table33%1/3
A || B
A= daia_app(shift())B= do { __PACKAGE__->builder->ok(0, 'Could not construct DAIA application'); return }
ABresult
1X1
011
000
21 0 __PACKAGE__->builder->ok(0,"Could not construct DAIA application");
22 0 return;
23 };
24 5 my $test_name = "test_daia";
25 5 ▶ $test_name = pop(@_) if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 4 5
26 5 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 5 11
27 6 my $id = shift;
28 6 my $expected = shift;
29 6 my $res = $app->retrieve($id);
30 6 ▶ if (!_if_daia_check( $res, $expected, $test_name )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_if_daia_check($res, $expected, $test_name)) 1 5 6
31 1 ▶ $@ = "retrieve method returned a DAIA::Response" unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 1 0 1
32 1 __PACKAGE__->builder->ok(0, $@);
33 }
34 }
35 }
36  
37 10 ▶sub test_daia_psgi {
Subroutine test_daia_psgi: called CC=3 SCAR=12.0
Pod documented
38 10 my $app = shift;
39  
40 # TODO: load psgi file if string given and allow for URL
41 10 my $test_name = "test_daia";
42 10 ▶ $test_name = pop(@_) if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 6 4 10
43 10 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 10 21
44 11 my $id = shift;
45 11 my $expected = shift;
46 11 ▶ test_psgi $app, sub {
Subroutine __ANON__: called CC=4 SCAR=19.2
47 11 my $req = shift->(GET "/?id=".uri_escape($id).'&format=xml');
48 11 my $res = eval { DAIA::parse( $req->content ); };
49 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
50 0 $@ =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\)://ig;
51 0 $@ =~ s/ at .* line.*//g;
52 0 $@ =~ s/\s*$//sg;
53 }
54 11 ▶ if (!_if_daia_check( $res, $expected, $test_name )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _if_daia_check($res, $expected, $test_name)) 0 11 11
55 0 ▶ $@ = "application returned a DAIA::Response" unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
56 0 __PACKAGE__->builder->ok(0, $@);
57 }
58 11 };
59 }
60 }
61  
62 5 ▶sub daia_app {
Subroutine daia_app: called CC=7 SCAR=31.7
Pod documented
63 5 my $app = shift;
64 5 ▶ if ( blessed($app) and $app->isa('Plack::App::DAIA') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($app) and $app->isa('Plack::App::DAIA')) { } 1 4 5
elsif ($app =~ qr"^https?://") { } 0 4 4
Condition66%2/3
builtin::blessed($app) and $app->isa("Plack::App::DAIA")
!ll&&!rl&&r
401
MC/DC50%1/2
builtin::blessed($app) and $app->isa("Plack::App::DAIA")
builtin::blessed($app) $app->isa("Plack::App::DAIA")
Truth table66%2/3
A and B
A= builtin::blessed($app)B= $app->isa("Plack::App::DAIA")
ABresult
0X0
100
111
65 1 return $app;
66 } elsif ( $app =~ qr{^https?://} ) {
67 0 ▶ my $baseurl = $app . ($app =~ /\?/ ? '&id=' : '?id=');
Branch: 1 branch
BranchTrueFalseTotal
$app =~ /\?/ ? : 0 0 0
68 0 ▶ $app = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
69 0 my $id = shift;
70 0 my $url = $baseurl.$id."&format=json";
71 0 my @daia = eval { DAIA->parse($url) };
72 0 ▶ if (!@daia) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@daia) 0 0 0
73 0 ▶ $@ ||= '';
Condition0%0/2
$@ ||= ""
l!l
00
MC/DC0%0/1
$@ ||= ""
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
74 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
75 0 $@ =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\)://ig;
76 0 $@ =~ s/ at .* line.*//g;
77 0 $@ =~ s/\s*$//sg;
78 }
79 0 $@ = "invalid DAIA from $url: $@";
80 }
81 0 return $daia[0];
82 0 };
83 }
84 4 ▶ if ( ref($app) and reftype($app) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app and builtin::reftype($app) eq "CODE") 4 0 4
Condition33%1/3
ref $app and builtin::reftype($app) eq "CODE"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $app and builtin::reftype($app) eq "CODE"
ref $app builtin::reftype($app) eq "CODE"
Truth table33%1/3
A and B
A= ref $appB= builtin::reftype($app) eq "CODE"
ABresult
0X0
100
111
85 4 return Plack::App::DAIA->new( code => $app );
86 }
87 0 return;
88 }
89  
90 # Call C<$code> with C<$daia> and set as C<$_>, if C<$daia> is a L<DAIA::Response>
91 # and return C<$daia> on success. Return C<undef> otherwise.
92 17 ▶sub _if_daia_check {
Subroutine _if_daia_check: called CC=5 SCAR=16.2
93 17 my ($daia, $expected, $test_name) = @_;
94 17 ▶ if ( blessed($daia) and $daia->isa('DAIA::Response') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($daia) and $daia->isa("DAIA::Response")) 16 1 17
Condition66%2/3
builtin::blessed($daia) and $daia->isa("DAIA::Response")
!ll&&!rl&&r
1016
MC/DC50%1/2
builtin::blessed($daia) and $daia->isa("DAIA::Response")
builtin::blessed($daia) $daia->isa("DAIA::Response")
Truth table66%2/3
A and B
A= builtin::blessed($daia)B= $daia->isa("DAIA::Response")
ABresult
0X0
100
111
95 16 ▶ if ( (reftype($expected)||'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($expected) || '') eq 'CODE') { } 9 7 16
Condition50%1/2
builtin::reftype($expected) || ''
l!l
160
MC/DC0%0/1
builtin::reftype($expected) || ''
builtin::reftype($expected)
Truth table50%1/2
A
A= builtin::reftype($expected)
Aresult
11
00
96 9 local $_ = $daia;
97 9 $expected->($daia);
98 } else {
99 7 local $Test::Builder::Level = $Test::Builder::Level + 2;
100 7 my $json = decode_json( $daia->json );
101 7 entails $json, $expected, $test_name;
102 }
103 16 return $daia;
104 }
105 }
106  
107 4 1;
108  
109 __END__
« blib/lib/Plack/App/DAIA.pm ↑ Summary app.psgi »