7.13. External test file

Separating the test cases from the code.

Example 7-6. examples/bc/bc5a.pl

#!/usr/bin/perl
use strict;
use warnings;

use Expect;
use Test::More qw(no_plan);
$Expect::Log_Stdout = 0;


open my $fh, "<", "bc_input.txt" or die "Could not open bc_input.txt";

my @sets;
while (my $line = <$fh>) {
    chomp $line;
    push @sets, [split /\s*,\s*/, $line];
}

my $e = Expect->new;
$e->spawn("bc") or die "Could not start bc\n";
$e->expect(undef, "warranty") or die "no warranty\n";

foreach my $set (@sets) {
    $e->send("$$set[0]\n");
    ok($e->expect(1, $$set[1]), $$set[0]);
}
$e->send("quit\n");