swat/story.pm

filter: Statementstmt 14.2% 15 / 105 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 11.4% 4 / 35 Podpod 0.0% 0 / 30 totaltotal 9.1% 19 / 208 CC 20 SCAR 26.1
CC
20
Cov
11%
run_response_processor
56.0
apply_module_variables
30.0
run_swat_module
12.0
CRAP 13.6
« swat.pm ↑ Summary
1 package swat::story;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Outthentic::DSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Temp qw/ tempfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our @EXPORT = qw{
9  
10 new_story end_of_story
11  
12 get_prop set_prop
13  
14 debug_mod1 debug_mod2 debug_mod12
15  
16 set_response
17  
18 dsl captures capture stream match_lines
19  
20 run_swat_module apply_module_variables module_variable
21  
22 do_perl_file
23  
24 modify_resource
25  
26 set_response_processor
27 run_response_processor
28  
29 hostname ignore_http_err
30  
31 project_root_dir
32  
33 test_root_dir
34  
35 resource resource_dir
36  
37 http_method
38  
39 body
40  
41 headers
42  
43 };
44  
45 1 our @stories = ();
46  
47 0 ▶sub new_story {
Subroutine new_story: not called CC=1 SCAR=6.9
Pod undocumented
48
49 0 push @stories, {
50 template_variables => {},
51 props => { dsl => Outthentic::DSL->new() , response => [] },
52 };
53  
54 }
55  
56 0 ▶sub end_of_story {
Subroutine end_of_story: not called CC=2 SCAR=17.9
Pod undocumented
57  
58 0 ▶ if (debug_mod2()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod2()) 0 0 0
59 0 Test::More::ok(1,'end of story: '.(get_prop('check_list')));
60 }
61 0 delete $stories[-1];
62  
63 }
64  
65 0 ▶sub _story {
Subroutine _story: not called CC=1 SCAR=6.9
66 0 @stories[-1];
67 }
68  
69 0 ▶sub get_prop {
Subroutine get_prop: not called CC=1 SCAR=6.9
Pod undocumented
70  
71 0 my $name = shift;
72  
73 0 _story()->{props}->{$name};
74
75 }
76  
77 0 ▶sub set_prop {
Subroutine set_prop: not called CC=1 SCAR=6.9
Pod undocumented
78  
79 0 my $name = shift;
80 0 my $value = shift;
81  
82 0 _story()->{props}->{$name} = $value;
83
84 }
85  
86 0 ▶sub project_root_dir {
Subroutine project_root_dir: not called CC=1 SCAR=6.9
Pod undocumented
87 0 get_prop('project');
88 }
89  
90 0 ▶sub test_root_dir {
Subroutine test_root_dir: not called CC=1 SCAR=6.9
Pod undocumented
91 0 get_prop('test_root_dir');
92 }
93  
94 0 ▶sub hostname {
Subroutine hostname: not called CC=1 SCAR=6.9
Pod undocumented
95 0 get_prop('hostname');
96 }
97  
98 0 ▶sub ignore_http_err {
Subroutine ignore_http_err: not called CC=1 SCAR=6.9
Pod undocumented
99 0 get_prop('ignore_http_err');
100 }
101  
102 0 ▶sub resource {
Subroutine resource: not called CC=1 SCAR=6.9
Pod undocumented
103 0 get_prop('resource');
104 }
105  
106 0 ▶sub resource_dir {
Subroutine resource_dir: not called CC=1 SCAR=6.9
Pod undocumented
107 0 get_prop('resource_dir');
108 }
109  
110 0 ▶sub http_method {
Subroutine http_method: not called CC=1 SCAR=6.9
Pod undocumented
111 0 get_prop('http_method');
112 }
113  
114 0 ▶sub body {
Subroutine body: not called CC=1 SCAR=6.9
Pod undocumented
115 0 get_prop('body');
116 }
117  
118 0 ▶sub headers {
Subroutine headers: not called CC=1 SCAR=6.9
Pod undocumented
119 0 get_prop('headers');
120 }
121  
122  
123  
124 0 ▶sub context_populated {
Subroutine context_populated: not called CC=1 SCAR=6.9
Pod undocumented
125 0 get_prop('context_populated')
126 }
127  
128 0 ▶sub debug_mod1 {
Subroutine debug_mod1: not called CC=1 SCAR=6.9
Pod undocumented
129  
130 0 get_prop('debug') == 1
131 }
132  
133 0 ▶sub debug_mod2 {
Subroutine debug_mod2: not called CC=1 SCAR=6.9
Pod undocumented
134  
135 0 get_prop('debug') == 2
136 }
137  
138 0 ▶sub debug_mod12 {
Subroutine debug_mod12: not called CC=2 SCAR=17.9
Pod undocumented
139  
140 0 ▶ debug_mod1() or debug_mod2()
Branch: 1 branch
BranchTrueFalseTotal
debug_mod1() or debug_mod2() 0 0 0
141 }
142  
143 0 ▶sub set_response {
Subroutine set_response: not called CC=1 SCAR=6.9
Pod undocumented
144 0 my $cv = get_prop('response');
145 0 push @$cv, shift();
146 0 set_prop('response', $cv);
147 }
148  
149 0 ▶sub dsl {
Subroutine dsl: not called CC=1 SCAR=6.9
Pod undocumented
150 0 get_prop('dsl')
151 }
152  
153 0 ▶sub captures {
Subroutine captures: not called CC=1 SCAR=6.9
Pod undocumented
154  
155 dsl()->{captures}
156 0 }
157  
158 0 ▶sub stream {
Subroutine stream: not called CC=1 SCAR=6.9
Pod undocumented
159  
160 dsl()->{stream}
161 0 }
162  
163 0 ▶sub match_lines {
Subroutine match_lines: not called CC=1 SCAR=6.9
Pod undocumented
164  
165 dsl()->{match_lines}
166 0 }
167  
168 0 ▶sub capture {
Subroutine capture: not called CC=1 SCAR=6.9
Pod undocumented
169 0 dsl()->{captures}->[0]
170 }
171  
172 0 ▶sub run_swat_module {
Subroutine run_swat_module: not called CC=3 SCAR=24.8
Pod undocumented
173  
174 0 my $http_method = uc(shift());
175 0 my $resource = shift;
176 0 ▶ my $module_variables = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
177  
178 0 $main::module_variables = $module_variables;
179  
180 0 my $test_root_dir = get_prop('test_root_dir');
181  
182 0 my $http_method_path = uc($http_method);
183 0 my $module_file = "$test_root_dir/$resource/$http_method_path/request.mod";
184  
185 0 ▶ if (debug_mod2()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod2()) 0 0 0
186 0 Test::More::ok(1,"run swat module: $http_method => $resource");
187 0 Test::More::ok(1,"load module file: $module_file");
188 }
189  
190 0 my $test_root_dir = get_prop('test_root_dir');
191  
192 0 do_perl_file($module_file);
193
194 }
195  
196 0 ▶sub do_perl_file {
Subroutine do_perl_file: not called CC=3 SCAR=24.8
Pod undocumented
197  
198 0 my $file = shift;
199  
200 {
201 0 package main;
202 0 do $file;
203 0 ▶ our $STATUS = -1 and die "couldn't parse $file: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
Condition0%0/5
$@ && ((our $STATUS = -1) && die("couldn't parse ${file}: $@"))
!ll&&!rl&&r
000
our $STATUS = -1 and die "couldn't parse ${file}: $@"
!ll
00
MC/DC0%0/2
$@ && ((our $STATUS = -1) && die("couldn't parse ${file}: $@"))
$@ our $STATUS = -1
Truth table0%0/3
A && B
A= $@B= our $STATUS = -1
ABresult
0X0
100
111
204 };
205  
206 0 return 1;
207 }
208  
209  
210 0 ▶sub apply_module_variables {
Subroutine apply_module_variables: not called CC=5 SCAR=34.0
Pod undocumented
211  
212 0 set_prop( module_variables => $main::module_variables );
213  
214 0 for my $name ( keys %{ get_prop( 'module_variables' ) } ){
215  
216 0 my $v = module_variable($name);
217  
218 0 my $re = "%".$name."%";
219  
220 0 my $curl_cmd = get_prop('curl_cmd');
221 0 my $resource = get_prop('resource');
222  
223 0 s{$re}[$v]g for $curl_cmd;
224 0 s{$re}[$v]g for $resource;
225  
226 0 ▶ if (debug_mod2()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod2()) 0 0 0
227 0 Test::More::ok(1,"apply module variable: $name => $v");
228 }
229  
230 0 set_prop( curl_cmd => $curl_cmd );
231 0 set_prop( resource => $resource );
232 }
233
234 }
235  
236 0 ▶sub module_variable {
Subroutine module_variable: not called CC=1 SCAR=6.9
Pod undocumented
237  
238 0 my $name = shift;
239  
240 0 get_prop( 'module_variables' )->{$name};
241  
242 }
243  
244 0 ▶sub modify_resource {
Subroutine modify_resource: not called CC=3 SCAR=24.8
Pod undocumented
245  
246 0 my $sub = shift;
247  
248 0 my $resource = get_prop('resource');
249  
250 0 ▶ Test::More::ok(1,"try to modify_resource: $resource ") if debug_mod12();
Branch: 1 branch
BranchTrueFalseTotal
if debug_mod12() 0 0 0
251  
252 0 my $new_resource = $sub->($resource);
253  
254 0 ▶ Test::More::ok(1,"modify_resource ok: $resource => $new_resource") if debug_mod12();
Branch: 1 branch
BranchTrueFalseTotal
if debug_mod12() 0 0 0
255  
256 0 set_prop( resource => $new_resource );
257  
258 }
259  
260 0 ▶sub set_response_processor {
Subroutine set_response_processor: not called CC=2 SCAR=17.9
Pod undocumented
261  
262 0 my $sub = shift;
263  
264 0 ▶ Test::More::ok(1,"setting response processor") if debug_mod12();
Branch: 1 branch
BranchTrueFalseTotal
if debug_mod12() 0 0 0
265  
266 0 set_prop( response_processor => $sub );
267  
268 }
269  
270 0 ▶sub run_response_processor {
Subroutine run_response_processor: not called CC=7 SCAR=40.3
Pod undocumented
271  
272 0 ▶ return unless get_prop( 'response_processor' );
Branch: 1 branch
BranchTrueFalseTotal
unless get_prop("response_processor") 0 0 0
273  
274 0 my $body = body();
275  
276 0 my $headers = headers();
277  
278 0 ▶ Test::More::ok(1,"running response processor") if debug_mod12();
Branch: 1 branch
BranchTrueFalseTotal
if debug_mod12() 0 0 0
279  
280 0 my $sub = get_prop( 'response_processor' );
281  
282 0 my $retval = $sub->($headers,$body);
283  
284 0 my ($fh, $content_file) = tempfile( DIR => get_prop('test_root_dir') );
285  
286 0 ▶ open F, ">", $content_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open F, ">", $content_file or die $! 0 0 0
287 0 print F $retval;
288 0 close F;
289  
290 0 ▶ Test::More::diag("modified response saved to $content_file") if debug_mod12();
Branch: 1 branch
BranchTrueFalseTotal
if debug_mod12() 0 0 0
291  
292 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
293 0 my $debug_bytes = get_prop('debug_bytes');
294 0 my $rshort = substr( $retval, 0, $debug_bytes );
295 0 ▶ if (length($rshort) < length($retval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rshort < length $retval) { } 0 0 0
296 0 Test::More::diag("modified response:\n$rshort ... ( output truncated to $debug_bytes bytes )");
297 } else{
298 0 Test::More::diag("modified response:\n$retval");
299 }
300 }
301  
302 0 dsl()->{output} = $retval;
303 }
304  
305  
306 1 1;
307  
308 __END__
« swat.pm ↑ Summary