Zapp/Task/Test.pm

filter: Statementstmt 28.0% 7 / 25 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 2 / 3 Podpod 100% 1 / 1 totaltotal 22.2% 10 / 45 CC 10 SCAR 45.2
CC
10
Cov
17%
run
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 91.8
« Zapp.pm ↑ Summary Zapp/Util.pm »
1 package Zapp::Task::Test;
2 # ABSTRACT: Test the output of previous tasks
3  
4 #pod =head1 DESCRIPTION
5 #pod
6 #pod This task allows testing of previous tasks' output. If any test fails,
7 #pod this task fails.
8 #pod
9 #pod =head2 Output
10 #pod
11 #pod tests - An array of test hashes with the following fields
12 #pod expr - An expression to evaluate
13 #pod op - An operator
14 #pod value - The expected value
15 #pod expr_value - The value of the evaluated expression
16 #pod pass - True if the test passed
17 #pod
18 #pod =head1 SEE ALSO
19 #pod
20 #pod =cut
21  
22 2 ▶use Mojo::Base 'Zapp::Task', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Yancy::Util qw( currym );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 ▶sub run( $self, $input ) {
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
26 0 my $fail = 0;
27 0 my @tests = $input->{tests}->@*;
28 0 for my $test ( @tests ) {
29 # Stringify whatever data we get because the value to test
30 # against can only ever be a string.
31 # XXX: Support deep comparisons
32 0 my $expr_value = $test->{ expr_value } = $self->app->formula->eval( $test->{expr}, currym( $self, 'context' ) );
33 # XXX: Add good, robust logging to help debug job problems
34 #; $self->app->log->debug( sprintf 'Test expr %s has value %s (%s %s)', $test->@{qw( expr expr_value op value )} );
35 0 my $pass;
36 0 ▶ if ( $test->{op} eq '==' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($test->{'op'} eq '==') { } 0 0 0
elsif ($test->{'op'} eq '!=') { } 0 0 0
elsif ($test->{'op'} eq '>') { } 0 0 0
elsif ($test->{'op'} eq '<') { } 0 0 0
elsif ($test->{'op'} eq '>=') { } 0 0 0
elsif ($test->{'op'} eq '<=') { } 0 0 0
37 0 $pass = ( $expr_value eq $test->{value} );
38 }
39 elsif ( $test->{op} eq '!=' ) {
40 0 $pass = ( $expr_value ne $test->{value} );
41 }
42 elsif ( $test->{op} eq '>' ) {
43 0 $pass = ( $expr_value gt $test->{value} );
44 }
45 elsif ( $test->{op} eq '<' ) {
46 0 $pass = ( $expr_value lt $test->{value} );
47 }
48 elsif ( $test->{op} eq '>=' ) {
49 0 $pass = ( $expr_value ge $test->{value} );
50 }
51 elsif ( $test->{op} eq '<=' ) {
52 0 $pass = ( $expr_value le $test->{value} );
53 }
54 0 $test->{pass} = $pass;
55 0 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 0 0 0
56 0 $fail = 1;
57 # XXX: Should be $self->log and logs should be added to job
58 # notes
59 $self->app->log->debug(
60 sprintf "Failed test %s %s %s with value %s",
61 0 $test->@{qw( expr op value expr_value )},
62 );
63 }
64 }
65 0 ▶ my $method = $fail ? 'fail' : 'finish';
Branch: 1 branch
BranchTrueFalseTotal
$fail ? : 0 0 0
66 0 return $self->$method( $input );
67 }
68  
69 2 1;
70  
71 =pod
72  
73 =head1 NAME
74  
75 Zapp::Task::Test - Test the output of previous tasks
76  
77 =head1 VERSION
78  
79 version 0.004
80  
81 =head1 DESCRIPTION
82  
83 This task allows testing of previous tasks' output. If any test fails,
84 this task fails.
85  
86 =head2 Output
87  
88 tests - An array of test hashes with the following fields
89 expr - An expression to evaluate
90 op - An operator
91 value - The expected value
92 expr_value - The value of the evaluated expression
93 pass - True if the test passed
94  
95 =head1 SEE ALSO
96  
97 =head1 AUTHOR
98  
99 Doug Bell <preaction@cpan.org>
100  
101 =head1 COPYRIGHT AND LICENSE
102  
103 This software is copyright (c) 2021 by Doug Bell.
104  
105 This is free software; you can redistribute it and/or modify it under
106 the same terms as the Perl 5 programming language system itself.
107  
108 =cut
109  
110 __DATA__
« Zapp.pm ↑ Summary Zapp/Util.pm »