blib/lib/Test/DBGp.pm

filter: Statementstmt 23.9% 28 / 117 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 29.1% 7 / 24 Podpod 0.0% 0 / 15 totaltotal 16.1% 35 / 217 CC 28 SCAR 32.2
CC
28
Cov
16%
dbgp_listen_unix
56.0
_extract_command_data
42.0
dbgp_listen_tcp
42.0
CRAP 24.9
↑ Summary
1 package Test::DBGp;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Test::DBGp - Test helpers for debuggers using the DBGp protocol
9  
10 =head1 SYNOPSIS
11  
12 use Test::DBGp;
13  
14 dbgp_listen();
15  
16 # start program under debugger
17  
18 dbgp_wait_connection($EXPECTED_APPID);
19  
20 dbgp_command_is(['step_into'], {
21 reason => 'ok',
22 status => 'break',
23 command => 'step_into',
24 });
25  
26 =head1 DESCRIPTION
27  
28 Various helpers to write tests for modules dealing with the DBGp
29 debugger protocol.
30  
31 =cut
32  
33 1 our $VERSION = '0.07';
34  
35 1 ▶use Test::Differences;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 require Exporter; *import = \&Exporter::import;
42  
43 1 our @EXPORT = qw(
44 dbgp_response_cmp
45 dbgp_parsed_response_cmp
46 dbgp_init_is
47 dbgp_command_is
48  
49 dbgp_listen
50 dbgp_listening_port
51 dbgp_listening_path
52 dbgp_stop_listening
53 dbgp_wait_connection
54  
55 dbgp_send_command
56  
57 dbgp_reset_output
58 dbgp_stdout_is
59 dbgp_stderr_is
60 );
61  
62 1 my ($LISTEN, $CLIENT, $INIT, $SEQ, $PORT, $PATH);
63 1 my ($STDOUT, $STDERR) = ('', '');
64  
65 0 ▶sub dbgp_response_cmp {
Subroutine dbgp_response_cmp: not called CC=1 SCAR=6.9
Pod undocumented
66 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
67  
68 0 require DBGp::Client::Parser;
69  
70 0 my ($xml, $expected) = @_;
71 0 my $res = DBGp::Client::Parser::parse($xml);
72 0 my $cmp = _extract_command_data($res, $expected);
73  
74 0 eq_or_diff($cmp, $expected);
75 }
76  
77 0 ▶sub dbgp_parsed_response_cmp {
Subroutine dbgp_parsed_response_cmp: not called CC=1 SCAR=6.9
Pod undocumented
78 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
79  
80 0 my ($res, $expected) = @_;
81 0 my $cmp = _extract_command_data($res, $expected);
82  
83 0 eq_or_diff($cmp, $expected);
84 }
85  
86 0 ▶sub _extract_command_data {
Subroutine _extract_command_data: not called CC=6 SCAR=37.4
87 0 my ($res, $expected) = @_;
88  
89 0 ▶ if (!ref $expected) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $expected) { } 0 0 0
elsif (ref $expected eq 'HASH') { } 0 0 0
elsif (ref $expected eq 'ARRAY') { } 0 0 0
90 0 return $res;
91 } elsif (ref $expected eq 'HASH') {
92 0 ▶ return $res if !defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 0 0
93 return {
94 map {
95 0 $_ => _extract_command_data($res->$_, $expected->{$_})
96 } keys %$expected
97 };
98 } elsif (ref $expected eq 'ARRAY') {
99 0 ▶ return $res if ref $res ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $res ne "ARRAY" 0 0 0
100 return [
101 ( map {
102 0 _extract_command_data($res->[$_], $expected->[$_])
103 } 0 .. $#$expected ),
104 ( ("<unexpected item>") x ($#$res - $#$expected) ),
105 ];
106 } else {
107 0 die "Can't extract ", ref $expected, "value";
108 }
109 }
110  
111 0 ▶sub dbgp_listen {
Subroutine dbgp_listen: not called CC=2 SCAR=17.9
Pod undocumented
112 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
113 0 dbgp_listen_tcp();
114 } else {
115 0 dbgp_listen_unix();
116 }
117 }
118  
119 0 ▶sub dbgp_listen_tcp {
Subroutine dbgp_listen_tcp: not called CC=6 SCAR=37.4
Pod undocumented
120 0 ▶ return if $LISTEN;
Branch: 1 branch
BranchTrueFalseTotal
if $LISTEN 0 0 0
121  
122 0 ▶ for my $port (!$PORT ? (17000 .. 19000) : ($PORT)) {
Branch: 1 branch
BranchTrueFalseTotal
!$PORT ? : 0 0 0
123 0 $LISTEN = IO::Socket::INET->new(
124 Listen => 1,
125 LocalAddr => '127.0.0.1',
126 LocalPort => $port,
127 Proto => 'tcp',
128 Timeout => 2,
129 );
130 0 ▶ next unless $LISTEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $LISTEN 0 0 0
131  
132 0 $PORT = $port;
133 0 $PATH = undef;
134 0 last;
135 }
136  
137 0 ▶ die "Unable to open a listening socket in the 17000 - 19000 port range"
Branch: 1 branch
BranchTrueFalseTotal
unless $LISTEN 0 0 0
138 unless $LISTEN;
139 }
140  
141 0 ▶sub dbgp_listen_unix {
Subroutine dbgp_listen_unix: not called CC=7 SCAR=40.3
Pod undocumented
142 0 ▶ return if $LISTEN;
Branch: 1 branch
BranchTrueFalseTotal
if $LISTEN 0 0 0
143  
144 0 my $path = $PATH;
145 0 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 0 0
146 0 $path = File::Spec::Functions::rel2abs('dbgp.sock', Cwd::getcwd());
147  
148 0 ▶ if (length($path) >= 90) { # arbitrary, should be low enough
Branch: 1 branch
BranchTrueFalseTotal
if (length $path >= 90) 0 0 0
149 0 my $tempdir = File::Temp::tempdir(CLEANUP => 1);
150 0 $path = File::Spec::Functions::rel2abs('dbgp.sock', $tempdir);
151 }
152 }
153 0 ▶ unlink $path if -S $path;
Branch: 1 branch
BranchTrueFalseTotal
if -S $path 0 0 0
154 0 ▶ return if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 0 0 0
155  
156 0 $LISTEN = IO::Socket::UNIX->new(
157 Local => $path,
158 Listen => 1,
159 );
160 0 $PORT = undef;
161 0 $PATH = $path;
162  
163 0 ▶ die "Unable to open a listening socket on '$path'"
Branch: 1 branch
BranchTrueFalseTotal
unless $LISTEN 0 0 0
164 unless $LISTEN;
165 }
166  
167 0 ▶sub dbgp_stop_listening {
Subroutine dbgp_stop_listening: not called CC=1 SCAR=6.9
Pod undocumented
168 0 close $LISTEN;
169 0 $LISTEN = undef;
170 }
171  
172 0 ▶sub dbgp_listening_port { $PORT }
Subroutine dbgp_listening_port: not called CC=1 SCAR=6.9
Pod undocumented
173 0 ▶sub dbgp_listening_path { $PATH }
Subroutine dbgp_listening_path: not called CC=1 SCAR=6.9
Pod undocumented
174  
175 0 ▶sub _append_output {
Subroutine _append_output: not called CC=3 SCAR=24.8
176 0 my ($event) = @_;
177  
178 0 ▶ if ($event->type eq 'stdout') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->type eq 'stdout') { } 0 0 0
elsif ($event->type eq 'stderr') { } 0 0 0
179 0 $STDOUT .= $event->content;
180 } elsif ($event->type eq 'stderr') {
181 0 $STDERR .= $event->content;
182 } else {
183 0 die "Unknown event type ", $event->type
184 }
185 }
186  
187 0 ▶sub dbgp_wait_connection {
Subroutine dbgp_wait_connection: not called CC=5 SCAR=34.0
Pod undocumented
188 0 my ($pid, $reject) = @_;
189 0 my $conn = $LISTEN->accept;
190  
191 0 ▶ die "Did not receive any connection from the debugged program: ", $LISTEN->error
Branch: 1 branch
BranchTrueFalseTotal
unless $conn 0 0 0
192 unless $conn;
193  
194 0 ▶ if ($reject) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reject) 0 0 0
195 0 close $conn;
196 0 return;
197 }
198  
199 0 require DBGp::Client::Stream;
200 0 require DBGp::Client::Parser;
201  
202 0 $CLIENT = DBGp::Client::Stream->new(socket => $conn);
203  
204 # consume initialization line
205 0 $INIT = DBGp::Client::Parser::parse($CLIENT->get_line);
206  
207 0 ▶ die "We got connected with the wrong debugged program"
Branch: 1 branch
BranchTrueFalseTotal
if $INIT->appid != $pid or $INIT->language ne "Perl" 0 0 0
Condition0%0/3
$INIT->appid != $pid or $INIT->language ne "Perl"
l!l&&r!l&&!r
000
MC/DC0%0/2
$INIT->appid != $pid or $INIT->language ne "Perl"
$INIT->appid != $pid $INIT->language ne "Perl"
Truth table0%0/3
A or B
A= $INIT->appid != $pidB= $INIT->language ne "Perl"
ABresult
1X1
011
000
208 if $INIT->appid != $pid || $INIT->language ne 'Perl';
209 }
210  
211 0 ▶sub dbgp_send_command {
Subroutine dbgp_send_command: not called CC=5 SCAR=34.0
Pod undocumented
212 0 my ($command, @args) = @_;
213  
214 0 $CLIENT->put_line($command, '-i', ++$SEQ, @args);
215 0 for (;;) {
216 0 my $res = DBGp::Client::Parser::parse($CLIENT->get_line);
217  
218 0 ▶ if ($res && $res->is_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res and $res->is_stream) 0 0 0
Condition0%0/3
$res and $res->is_stream
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->is_stream
$res $res->is_stream
Truth table0%0/3
A and B
A= $resB= $res->is_stream
ABresult
0X0
100
111
219 0 _append_output($res);
220 0 next;
221 }
222  
223 0 ▶ die 'Mismatched transaction IDs: got ', $res->transaction_id,
Branch: 1 branch
BranchTrueFalseTotal
if $res and $res->transaction_id != $SEQ 0 0 0
Condition0%0/3
$res and $res->transaction_id != $SEQ
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->transaction_id != $SEQ
$res $res->transaction_id != $SEQ
Truth table0%0/3
A and B
A= $resB= $res->transaction_id != $SEQ
ABresult
0X0
100
111
224 ' expected ', $SEQ
225 if $res && $res->transaction_id != $SEQ;
226  
227 0 return $res;
228 }
229 }
230  
231 0 ▶sub dbgp_reset_output {
Subroutine dbgp_reset_output: not called CC=1 SCAR=6.9
Pod undocumented
232 0 $STDOUT = $STDERR = '';
233 }
234  
235 0 ▶sub dbgp_stdout_is {
Subroutine dbgp_stdout_is: not called CC=1 SCAR=6.9
Pod undocumented
236 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
237 0 my ($expected) = @_;
238  
239 0 eq_or_diff($STDOUT, $expected);
240 }
241  
242 0 ▶sub dbgp_stderr_is {
Subroutine dbgp_stderr_is: not called CC=1 SCAR=6.9
Pod undocumented
243 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
244 0 my ($expected) = @_;
245  
246 0 eq_or_diff($STDERR, $expected);
247 }
248  
249 0 ▶sub dbgp_init_is {
Subroutine dbgp_init_is: not called CC=1 SCAR=6.9
Pod undocumented
250 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
251  
252 0 my ($expected) = @_;
253 0 my $cmp = _extract_command_data($INIT, $expected);
254  
255 0 eq_or_diff($cmp, $expected);
256 }
257  
258 0 ▶sub dbgp_command_is {
Subroutine dbgp_command_is: not called CC=1 SCAR=6.9
Pod undocumented
259 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
260  
261 0 my ($command, $expected) = @_;
262 0 my $res = dbgp_send_command(@$command);
263 0 my $cmp = _extract_command_data($res, $expected);
264  
265 0 eq_or_diff($cmp, $expected);
266 }
267  
268 1 1;
269  
270 __END__
↑ Summary