File Coverage

blib/lib/Plack/App/DAIA/Test/Suite.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1 1     1   30709 use strict;
  1         2  
  1         37  
2 1     1   5 use warnings;
  1         1  
  1         52  
3             package Plack::App::DAIA::Test::Suite;
4             #ABSTRACT: Test DAIA Servers via a test scripting language
5             our $VERSION = '0.55'; #VERSION
6 1     1   4 use base 'Test::Builder::Module';
  1         2  
  1         247  
7             our @EXPORT = qw(provedaia);
8              
9 1     1   11 use Test::More;
  1         1  
  1         5  
10 1     1   678 use Plack::App::DAIA::Test;
  0            
  0            
11             use Scalar::Util qw(reftype blessed);
12             use Test::JSON::Entails;
13             use JSON;
14             use Carp;
15              
16             sub provedaia {
17             my ($suite, %args) = @_;
18              
19             my $test = __PACKAGE__->builder;
20             my @lines;
21              
22             if ( ref($suite) ) {
23             croak 'usage: provedaia( $file | $glob | $string )'
24             unless reftype($suite) eq 'GLOB' or blessed($suite) and $suite->isa('IO::File');
25             @lines = <$suite>;
26             } elsif ( $suite !~ qr{^https?://} and $suite !~ /[\r\n]/ ) {
27             open (my $fh, '<', $suite) or croak "failed to open daia test suite $suite";
28             @lines = <$fh>;
29             close $fh;
30             } else {
31             @lines = split /\n/, $suite;
32             }
33              
34             my $line = 0;
35             my $comment = '';
36             my $json = undef;
37             my %vars = ( server => $args{server} );
38             my @ids;
39             @ids = @{$args{ids}} if $args{ids};
40              
41             my $run = sub {
42             my $server = $vars{server} or return;
43             $json ||= '{ }';
44             my $server_name = $server;
45             if ( $server !~ qr{^https?://}) {
46             no warnings 'redefine'; # we may load the same twice
47             $_ = Plack::Util::load_psgi($server);
48             if ( ref($_) ) {
49             diag("loaded PSGI from $server");
50             $server = $_;
51             } else {
52             fail("failed to load PSGI from $server");
53             return;
54             }
55             }
56             foreach my $id (@ids) {
57             my $test_name = "$server_name?id=$id";
58             $comment =~ s/^\s+|\s+$//g;
59             $test_name .= " ($comment)" if $comment ne '';
60             local $Test::Builder::Level = $Test::Builder::Level + 2; # called 2 levels above
61             my $test_json = $json;
62             $vars{id} = $id;
63             $test_json =~ s/\$([a-z]+)/defined $vars{$1} ? $vars{$1} : "\$$1"/emg;
64             $test_json = decode_json($test_json);
65             if (ref($server)) {
66             test_daia_psgi $server, $id => $test_json, $test_name;
67             } else {
68             test_daia $server, $id => $test_json, $test_name;
69             }
70             }
71             };
72              
73             foreach (@lines) {
74             if ($args{end}) {
75             $args{end} = 0 if /__END__/;
76             next;
77             }
78             chomp;
79             $comment = $1 if /^#(.*)/;
80             s/^(#.*|\s+)$//; # empty line or comment
81             $line++;
82              
83             if (defined $json) {
84             $json .= $_;
85             if ($_ eq '') {
86             $run->();
87             $json = undef;
88             $comment = '';
89             }
90             } elsif ( $_ eq '' ) {
91             next;
92             } elsif( $_ =~ qr{^([a-z]+)\s*=\s*(.*)}i ) {
93             $comment = '';
94             my ($key, $value) = ($1,$2);
95             if ($1 =~ /^id[s]?/) {
96             @ids = $value eq '' ? () : ($value);
97             } else {
98             $vars{$key} = $value;
99             }
100             diag( "$key = $value" ) if $args{verbose};
101             } elsif( $_ =~ qr/^\s*{/ ) {
102             $json = $_;
103             } else { # identifier
104             $comment = '';
105             push @ids, $_;
106             }
107             }
108             $run->();
109             }
110              
111             1;
112              
113             __END__