加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Perl中伪造STDIN?

发布时间:2020-12-15 21:20:53 所属栏目:大数据 来源:网络整理
导读:我单元测试需要用户输入的组件。如何告诉 Test::More 使用我预定义的一些输入,以便我不需要手动输入? 这就是我现在所说的: use strict;use warnings;use Test::More;use TestClass; *STDIN = "1n"; foreach my $file (@files) {#this constructor asks f
我单元测试需要用户输入的组件。如何告诉 Test::More使用我预定义的一些输入,以便我不需要手动输入?

这就是我现在所说的:

use strict;
use warnings;
use Test::More;
use TestClass;

    *STDIN = "1n";
    foreach my $file (@files)
    {

#this constructor asks for user input if it cannot find the file (1 is ignore);
    my $test = TestClass->new( file=> @files );

    isa_ok( $test,'TestClass');
    }


done_testing;

此代码按enter键,但该函数正在检索0不为1;

解决方法

以下最小脚本似乎有效:
#!/usr/bin/perl

package TestClass;
use strict;
use warnings;

sub new {
    my $class = shift;
    return unless <STDIN> eq "1n";
    bless {} => $class;
}

package main;

use strict;
use warnings;

use Test::More tests => 1;

{
    open my $stdin,'<', "1n"
        or die "Cannot open STDIN to read from string: $!";
    local *STDIN = $stdin;
    my $test = TestClass->new;
    isa_ok( $test,'TestClass');
}

输出:

C:Temp> t
1..1
ok 1 - The object isa TestClass

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读