Chinaunix

标题: Test::Expect中expect—run的参数问题 [打印本页]

作者: lilyhit    时间: 2009-04-13 17:32
标题: Test::Expect中expect—run的参数问题
刚学习perl ,Test::Expect中expect—run的prompt到底是什么意思啊?参数该如何设置啊?高手帮帮忙,谢了!
作者: lilyhit    时间: 2009-04-14 10:31
Think back to your early programming days, when the canonical example of accepting user input was building a calculator. In Perl, you may have written something like simplecalc.pl:


    #!perl

   

    use strict;

    use warnings;

   

    print "> ";

   

    while (<>)

    {

        chomp;

        last unless $_;

   

        my ($command, @args) = split( /\s+/, $_ );

   

        my $sub;

        unless ($sub = _ _PACKAGE_ _->can( $command ))

        {

            print "Unknown command '$command'\n> ";

            next;

        }

   

        $sub->(@args);

        print "> ";

    }

   

    sub add

    {

        my $result = 0;

   

        $result += $_ for @_;

        print join(" + " , @_ ), " = $result\n";

    }

   

    sub subtract

    {

        my $result = shift;

   

        print join(" - " , $result, @_ );

   

        $result -= $_ for
       print " = $result\n";

    }



Save the following test file as testcalc.t:


    #!perl

   

    use strict;

    use Test::More tests => 7;

    use Test::Expect;

   

    expect_run(

        command => "$^X simplecalc.pl",

        prompt  => '> ',

        quit    => "\n",

    );

   

    expect(    'add 1 2 3',    '1 + 2 + 3 = 6',  'adding three numbers'       );

    expect_send('subtract 1 2 3',                'subtract should work'       );

    expect_is(  '1 - 2 - 3 = -4',                '.. producing good results'  );

    expect_send('weird magic',                   'not dying on bad input'     );

    expect_like(qr/Unknown command 'weird/,      '... but giving an error'    );




Run it from the directory containing simplecalc.pl:


    $ prove testcalc.t

    testcalc....ok

    All tests successful.

    Files=1, Tests=7,  0 wallclock secs ( 0.27 cusr +  0.02 csys =  0.29 CPU)
作者: lilyhit    时间: 2009-04-14 10:33
没人回答我 ~~~ 我自己找了点资料搞清楚了 ~~~ 传上来,希望对和我有同样疑惑的人一点帮助吧!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2