为什么`Term :: ReadKey :: SetTerminalSize`在测试中表现不同?

时间:2012-05-19 09:01:29

标签: perl terminal automated-tests

#!/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>

为什么来自Term::ReadKeySetTerminalSize在此测试中不起作用:

#!/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.

1 个答案:

答案 0 :(得分:1)

可能这是因为脚本试图调用TIOCSWINSZ句柄上的ioctl() STDOUT,并且在通过单元测试运行的情况下,脚本的{{1} }没有附加到终端,而是附加到STDOUT的管道。作为管道而不是TTY,它不响应此prove并产生显示的错误。