blib/lib/POE/Test/Loops/wheel_readline.pm untested

filter: Statementstmt 0.0% 0 / 164 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 291 CC 20 SCAR 34.4
CC
20
Cov
0%
BEGIN
110.0
END
12.0
test_start
12.0
CRAP 31.3
1 #!/usr/bin/perl -w
2 # vim: ts=2 sw=2 expandtab
3  
4 # Exercises Wheel::ReadLine
5  
6 use strict;
7 use warnings;
8 use lib qw(./mylib ../mylib);
9  
10 sub DEBUG () { 0 }
11  
12 # Set the INPUTRC environment variable to a nonexistent file. This
13 # prevents users from overriding the behaviors of keystrokes we user
14 # here.
15  
16 BEGIN {
17 foreach my $candidate (qw(nonexistent moo deleteme please-dont-exist)) {
18 next if -f $candidate;
19 $ENV{INPUTRC} = $candidate;
20 last;
21 }
22 }
23  
24 ### Tests to run.
25 #
26 # Each test consists of a "name" for test reporting, a series of steps
27 # that contain text to "type" in a particular order, and a "done" line
28 # that should contain the final input from Wheel::ReadLine.
29  
30 my @tests = (
31 {
32 name => "plain typing",
33 step => [
34 "this is a test", # plain typing
35 "\cJ", # accept-line
36 ],
37 done => "this is a test",
38 },
39 {
40 name => "backspace",
41 step => [
42 "this is a test",
43 "\cH\cH\cH\cH", # backward-delete-char
44 "TEST",
45 "\cA", # beginning-of-line
46 "\cA", # beginning-of-line (fail bol)
47 "\cD", # delete-char
48 "T",
49 "\cJ",
50 ],
51 done => "This is a TEST",
52 },
53 {
54 name => "forward/backward",
55 step => [
56 "one three five",
57 "\cA", # beginning-of-line
58 "\cB", # backward-char (fail bol)
59 "\eb", # backward-word (fail bol)
60 "\cH", # backward-delete-char (fail bol)
61 "\cF\cF\cF two", # forward-char
62 "\cE", # end-of-line
63 "\cE", # end-of-line (fail eol)
64 "\cF", # forward-char (fail eol)
65 "\cD", # delete-char (fail eol)
66 "\ef", # forward-word (fail eol)
67 "\cB\cB\cB\cB", # backward-char
68 "four \cJ",
69 ],
70 done => "one two three four five",
71 },
72 {
73 name => "delete words",
74 step => [
75 "one two three",
76 "\ed", # kill-word (fail eol)
77 "\e\cH", # backward-kill-word
78 "four",
79 "\cA",
80 "\e\cH", # backward-kill-word (fail bol)
81 "\ed", # kill-word
82 "\cJ",
83 ],
84 done => "two four",
85 },
86 {
87 name => "case changes",
88 step => [
89 "LOWER upper other",
90 "\cA\cF\cF\cF",
91 "\el", # downcase-word
92 "\ef", # forward-word
93 "\eb", # backward-word
94 "\cF\cF\cF",
95 "\eu", # upcase-word
96 "\ec", # capitalize-word
97 "\cE",
98 "\el", # downcase-word (fail eol)
99 "\eu", # upcase-word (fail eol)
100 "\ec", # capitalize-word (fail eol)
101 "\cJ",
102 ],
103 done => "LOWer uppER Other",
104 },
105 {
106 name => "transpose",
107 step => [
108 "one two 12",
109 "\cb",
110 "\cT", # transpose-chars
111 "\eb\eb",
112 "\et", # transpose-words
113 "\cA\cT", # transpose-chars (fail bol)
114 "\cE12\cT", # transpose-chars (at eol
115 "\cJ",
116 ],
117 done => "two one 2121",
118 },
119 );
120  
121 sub POE::Kernel::CATCH_EXCEPTIONS () { 0 }
122 sub POE::Kernel::ASSERT_DEFAULT () { 1 }
123  
124 BEGIN {
125 package
126 POE::Kernel;
127 use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'});
128 }
129  
130 use Test::More;
131  
132 # There are some reasons not to run this test.
133  
134 BEGIN {
135 my $error;
136 if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) {
137 $error = "$^O cannot multiplex terminals";
138 }
139 elsif (!-t STDIN) {
140 $error = "not running in a terminal";
141 }
142 else {
143 eval "use Term::ReadKey";
144 if ($@) {
145 $error = "This test requires Term::ReadKey";
146 }
147 else {
148 eval "use IO::Pty";
149 if ($@) {
150 $error = "This test requires IO::Pty";
151 }
152 else {
153 eval "use Term::Cap";
154 if ($@) {
155 $error = "This test requires Term::Cap";
156 }
157 }
158 }
159 }
160  
161 unless ($error) {
162 use POSIX ();
163  
164 my $termios = POSIX::Termios->new();
165 $termios->getattr();
166 my $ospeed = $termios->getospeed() || eval { POSIX::B38400() } || 0;
167  
168 $ENV{TERM} = "vt100" if $^O eq "solaris" or not $ENV{TERM};
169  
170 my $termcap = eval {
171 Term::Cap->Tgetent( { TERM => $ENV{TERM}, OSPEED => $ospeed } )
172 };
173 unless ($termcap) {
174 $error = "Term::Cap failure: $@";
175 $error =~ s/ at \S+ line \d+.*//;
176 $error =~ s/\s+/ /g;
177 }
178 }
179  
180 if ($error) {
181 plan skip_all => $error;
182 CORE::exit();
183 }
184 }
185  
186 use Symbol qw(gensym);
187 use POSIX qw(
188 sysconf setsid _SC_OPEN_MAX ECHO ICANON IEXTEN ISIG BRKINT ICRNL
189 INPCK ISTRIP IXON CSIZE PARENB OPOST TCSANOW
190 );
191  
192 # Redirection must be done before POE::Wheel::ReadLine is loaded,
193 # otherwise it grabs copies of STDIN and STDOUT.
194  
195 my ($saved_stdin, $saved_stdout, $pty_master, $pty_slave);
196 BEGIN {
197 # Redirect STDIN and STDOUT to temporary handles for the duration of
198 # this test.
199  
200 $saved_stdin = gensym();
201 open($saved_stdin, "<&STDIN") or die "can't save stdin: $!";
202 $saved_stdout = gensym();
203 open($saved_stdout, ">&STDOUT") or die "can't save stdout: $!";
204  
205 # Create a couple one-way pipes for our new stdin and stdout.
206  
207 $pty_master = IO::Pty->new() or die "pty: $!";
208 select $pty_master; $| = 1;
209  
210 $pty_slave = $pty_master->slave();
211  
212 # Put the pty conduit (slave side) into "raw" or "cbreak" mode,
213 # per APITUE 19.4 and 11.10.
214  
215 my $tio = POSIX::Termios->new();
216 $tio->getattr(fileno($pty_slave));
217 my $lflag = $tio->getlflag;
218 $lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
219 $tio->setlflag($lflag);
220 my $iflag = $tio->getiflag;
221 $iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
222 $tio->setiflag($iflag);
223 my $cflag = $tio->getcflag;
224 $cflag &= ~(CSIZE | PARENB);
225 $tio->setcflag($cflag);
226 my $oflag = $tio->getoflag;
227 $oflag &= ~(OPOST);
228 $tio->setoflag($oflag);
229 $tio->setattr(fileno($pty_slave), TCSANOW);
230  
231 select $pty_slave; $| = 1;
232  
233 # Redirect our STDIN and STDOUT to the pipes.
234  
235 open(STDIN, "<&=" . fileno($pty_slave)) or die "stdin pipe redir: $!";
236 open(STDOUT, ">&=" . fileno($pty_slave)) or die "stdout pipe redir: $!";
237 select STDOUT; $| = 1;
238 }
239  
240 # Restore the original stdio at the end of the run.
241  
242 END {
243 if ($saved_stdin) {
244 open(STDIN, "<&=" . fileno($saved_stdin)) or die "stdin restore: $!";
245 $saved_stdin = undef;
246 }
247  
248 if ($saved_stdout) {
249 open(STDOUT, ">&=" . fileno($saved_stdout)) or die "stdout restore: $!";
250 $saved_stdout = undef;
251 }
252 }
253  
254 use POE qw(Filter::Stream Wheel::ReadWrite);
255  
256 eval "use POE::Wheel::ReadLine";
257 if ($@ and $@ =~ /(requires a termcap|failed termcap lookup|cannot run)/) {
258 my $error = $@;
259 $error =~ s/ at \S+ line \d+.*//s;
260 $error =~ s/\s+/ /g;
261 plan skip_all => $error;
262 }
263  
264 plan tests => scalar(@tests);
265  
266 ### Session to run the tests.
267  
268 POE::Session->create(
269 inline_states => {
270 _start => \&test_start,
271 got_readwrite_output => \&test_readwrite_output,
272 got_readline_input => \&test_readline_input,
273 start_next_test => \&test_start_next,
274 step_this_test => \&test_step,
275 _stop => sub { },
276 },
277 );
278  
279 ### Main loop.
280  
281 POE::Kernel->run();
282  
283 ### The rest of this code is event handlers.
284  
285 sub test_start {
286 my ($kernel, $heap) = @_[KERNEL, HEAP];
287  
288 # The ReadLine wheel to drive and test.
289  
290 eval {
291 $heap->{readline} = POE::Wheel::ReadLine->new(
292 InputEvent => "got_readline_input",
293 appname => "my_cli",
294 );
295 };
296  
297 if ($@) {
298 my $error = $@;
299 $error =~ s/ at \S+ line \d+.*//s;
300 $error =~ s/\s+/ /g;
301 plan skip_all => $error;
302 return;
303 }
304  
305 unless ($heap->{readline}) {
306 plan skip_all => "POE::Wheel::Readline->new failed (term=$ENV{TERM})";
307 return;
308 }
309  
310 # Create a Wheel::ReadWrite to work on the driving side of the
311 # pipes.
312  
313 $heap->{readwrite} = POE::Wheel::ReadWrite->new(
314 Handle => $pty_master,
315 Filter => POE::Filter::Stream->new(),
316 InputEvent => "got_readwrite_output",
317 );
318  
319  
320 # And start testing.
321  
322 $kernel->yield("start_next_test");
323 }
324  
325 sub test_readwrite_output {
326 my ($heap, $input) = @_[HEAP, ARG0];
327 if (DEBUG) {
328 $input =~ s/[\x0A\x0D]+/{ENTER}/g;
329 warn "$heap->{test}{name} - got output from child ($input)\n";
330 }
331 }
332  
333 sub test_readline_input {
334 my ($kernel, $heap, $input) = @_[KERNEL, HEAP, ARG0];
335  
336 my $test = $heap->{test};
337 my $name = $test->{name};
338  
339 DEBUG and warn "$name - got readline input ($input)\n";
340  
341 if (@{$test->{step}}) {
342 fail("$name - got test input prematurely");
343 }
344 else {
345 is( $input, $test->{done}, $name );
346 }
347  
348 $kernel->yield("start_next_test");
349 }
350  
351 sub test_start_next {
352 my ($kernel, $heap) = @_[KERNEL, HEAP];
353  
354 if (@tests) {
355 $heap->{test} = shift @tests;
356 $kernel->yield("step_this_test");
357 $heap->{readline}->get("next step");
358  
359 # Second get to instrument a test for this sort of thing.
360 $heap->{readline}->get("next step");
361 return;
362 }
363  
364 # Delete them first so that we can tell the relative order of
365 # destruction when DEBUG is turned on.
366 delete $heap->{readline};
367 delete $heap->{readwrite};
368 DEBUG and warn "Done with all tests.\n";
369 return;
370 }
371  
372 sub test_step {
373 my ($kernel, $heap) = @_[KERNEL, HEAP];
374  
375 my $next_step = shift @{$heap->{test}{step}};
376 unless ($next_step) {
377 DEBUG and warn "$heap->{test}{name} - done with test\n";
378 return;
379 }
380  
381 if (DEBUG) {
382 my $output_next_step = $next_step;
383 $output_next_step =~ s/[\x00-\x1F\x7F]/./g;
384 warn "$heap->{test}{name} - typing ($output_next_step)\n";
385 }
386  
387 $heap->{readwrite}->put($next_step);
388 $kernel->yield("step_this_test");
389 }
390  
391 1;
392  
393 __END__