External test file


Separating the test cases from the code.


examples/bc/bc5a.pl
#!/usr/bin/perl
use strict;
use warnings;

use FindBin;

use Expect;
use Test::More;

$Expect::Log_Stdout = 0;

my @sets = read_file();

plan tests => 2 * scalar @sets;

my $e = Expect->new;
$e->raw_pty(1);
$e->spawn("bc") or die "Could not start bc\n";
$e->expect(1, [qr/warranty'./]) or die "no warranty\n";

foreach my $set (@sets) {
    $e->send("$set->[0]\n");
    ok($e->expect(1, [qr/\d+/]), 'numbers received');
    is($e->match, $set->[1], "expected value of " . $set->[0]);
}


$e->send("quit\n");


sub read_file {
    open my $fh, "<", "$FindBin::Bin/bc_input.txt"
         or die "Could not open bc_input.txt";

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

1..8
ok 1 - numbers received
ok 2 - expected value of 23+7
ok 3 - numbers received
ok 4 - expected value of 23+7
ok 5 - numbers received
not ok 6 - expected value of 11+1
#   Failed test 'expected value of 11+1'
#   at examples/bc/bc5a.pl line 30.
#          got: '12'
#     expected: '10'
ok 7 - numbers received
ok 8 - expected value of 2*21
# Looks like you failed 1 test of 8.