0
#!/usr/bin/env perl
use warnings;
no warnings 'redefine';
use 5.10.1;
use Term::ReadKey;
use lib 'lib';
use Term::Choose qw(choose);

my @test_keys = ( 
    Term::Choose::KEY_SPACE, Term::Choose::KEY_RIGHT, Term::Choose::KEY_RIGHT, Term::Choose::KEY_RIGHT,    
    Term::Choose::KEY_RIGHT, Term::Choose::KEY_SPACE, Term::Choose::KEY_DOWN,  Term::Choose::KEY_DOWN,
    Term::Choose::KEY_DOWN,  Term::Choose::KEY_DOWN,  Term::Choose::KEY_SPACE, Term::Choose::KEY_BTAB,
    Term::Choose::KEY_SPACE, Term::Choose::KEY_LEFT,  Term::Choose::KEY_UP,    Term::Choose::KEY_SPACE,
    Term::Choose::KEY_DOWN,  Term::Choose::KEY_DOWN,  Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,  
    Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,  
    Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,  
    Term::Choose::KEY_TAB,   Term::Choose::KEY_SPACE, Term::Choose::KEY_BSPACE,Term::Choose::KEY_BSPACE,
    Term::Choose::KEY_BSPACE,Term::Choose::KEY_BSPACE,Term::Choose::KEY_ENTER
);

SetTerminalSize( 80, 25 ,0 , 0, \*STDOUT );

sub Term::Choose::_getch { return shift @test_keys };

my @expected = ( 1, 23, 26, 33, 38, 42, 88 );
my @result = choose( [ 1 .. 100 ] );

say "expected: <@expected>";
say "result  : <@result>";

返回

expected: <1 23 26 33 38 42 88>
result  : <1 23 26 33 38 42 88>

为什么SetTerminalSizefrom Term::ReadKey在此测试中不起作用:

#!/usr/bin/env perl
use warnings;
no warnings 'redefine';
use strict;
use 5.10.1;
use Term::ReadKey;
use Term::Choose qw(choose);
use Test::More tests => 1;

my @test_keys = ( 
    Term::Choose::KEY_SPACE, Term::Choose::KEY_RIGHT, Term::Choose::KEY_RIGHT, Term::Choose::KEY_RIGHT,    
    Term::Choose::KEY_RIGHT, Term::Choose::KEY_SPACE, Term::Choose::KEY_DOWN,  Term::Choose::KEY_DOWN,
    Term::Choose::KEY_DOWN,  Term::Choose::KEY_DOWN,  Term::Choose::KEY_SPACE, Term::Choose::KEY_BTAB,
    Term::Choose::KEY_SPACE, Term::Choose::KEY_LEFT,  Term::Choose::KEY_UP,    Term::Choose::KEY_SPACE,
    Term::Choose::KEY_DOWN,  Term::Choose::KEY_DOWN,  Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,  
    Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,  
    Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,   Term::Choose::KEY_TAB,  
    Term::Choose::KEY_TAB,   Term::Choose::KEY_SPACE, Term::Choose::KEY_BSPACE,Term::Choose::KEY_BSPACE,
    Term::Choose::KEY_BSPACE,Term::Choose::KEY_BSPACE,Term::Choose::KEY_ENTER
);

SetTerminalSize( 80, 25 ,0 , 0, \*STDOUT );

sub Term::Choose::_getch { return shift @test_keys };

my @expected = ( 1, 23, 26, 33, 38, 42, 88 );
my @result = choose( [ 1 .. 100 ] );

is( "@result", "@expected", 'returnvalues "choose"' ); 

输出:

t/choose.t .. TIOCSWINSZ ioctl call to set terminal size failed: The argument is invalid at t/choose.t line 22, <DATA> line 261.
# Looks like your test exited with 22 before it could output anything.
t/choose.t .. Dubious, test returned 22 (wstat 5632, 0x1600)
Failed 1/1 subtests 

Test Summary Report
-------------------
t/choose.t (Wstat: 5632 Tests: 0 Failed: 0)
  Non-zero exit status: 22
  Parse errors: Bad plan.  You planned 1 tests but ran 0.
Files=1, Tests=0,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.04 cusr  0.01 csys =  0.07 CPU)
Result: FAIL
Failed 1/1 test programs. 0/0 subtests failed.
4

1 回答 1

1

这可能是因为脚本试图调用句柄TIOCSWINSZ ioctl()上的STDOUT,并且在通过单元测试运行的情况下,脚本STDOUT没有连接到终端,而是连接到来自prove. 作为管道而不是 TTY,它不会对此做出响应ioctl()并产生所示的错误。

于 2012-05-22T21:20:15.117 回答