| 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
}
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
}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 26 | 5 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | $@ = "retrieve method returned a DAIA::Response" unless $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 43 | 10 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $@ = "application returned a DAIA::Response" unless $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
builtin::blessed($app) and $app->isa("Plack::App::DAIA")
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")
| |||||||||||||||||||||||||||||||||
| 65 | 1 | return $app; | |||||||||||||||||||||||||||||||
| 66 | } elsif ( $app =~ qr{^https?://} ) { | ||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | my $baseurl = $app . ($app =~ /\?/ ? '&id=' : '?id='); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $@ ||= ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ ||= ""
MC/DC0%0/1
$@ ||= ""
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $app and builtin::reftype($app) eq "CODE"
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"
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
builtin::blessed($daia) and $daia->isa("DAIA::Response")
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")
| |||||||||||||||||||||||||||||||||
| 95 | 16 | ▶ | if ( (reftype($expected)||'') eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
builtin::reftype($expected) || ''
MC/DC0%0/1
builtin::reftype($expected) || ''
builtin::reftype($expected)
Truth table50%1/2
A
A= builtin::reftype($expected)
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||