[Pdx-pm] Testing STDIN

Eric Wilhelm scratchcomputing at gmail.com
Fri Aug 3 18:55:17 PDT 2007


# from Joshua Keroes
# on Friday 03 August 2007 05:48 pm:

>Each state typically prints a prompt, e.g.
>
>    Username:
>
>and waits for the user to type something, e.g.
>
>    chomp( my $in = <STDIN> );
>
>And of course, that <STDIN> will block while it waits for user-input.
>Seems like I need to fork the router process off into one process and
>connect it to another process that will pretend to be a fake user.

You could either make $stdin = \*STDIN and have some way to override 
that (testing from within.)   Alternatively, without disturbing your 
code (testing from without) use Expect or just pipe it some input.

It seems like either way is going to require a driver process unless 
your prompts are known and invariant for the test condition (e.g. a 
pre-created list of expected prompts and answers.)

  my $answers = join("\n", @answers);
  open(my $in, '<', \$answers);
  $thing->set_stdin($in);

  my $prompts;
  open(my $out, '>', \$prompts);
  $thing->set_stdout($out);

  eval {$thing->run};
  ok(! $@, "yay");
  is($prompts, $expected);

Or thereabouts.  (You know, with bugs and syntax errors removed.)

--Eric
-- 
As an old bass player friend of mine used to say: throw money, don't 
clap.
--Tony Parisi
---------------------------------------------------
    http://scratchcomputing.com
---------------------------------------------------


More information about the Pdx-pm-list mailing list