5.13. Put the test cases in an external file

Example 5-19. examples/intro/t13_calc.t

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

use Test::Simple "no_plan";

open my $fh, "<", "calc.txt" or die $!;

while ( my $line = <$fh> ) {
    chomp $line;
    next if $line =~ /^\s*$/;
    next if $line =~ /^#/;

    my ( $exp, $res ) = split /\s*=\s*/, $line;

    ok( `./mycalc $exp` == $res, $exp );
}

Example 5-20. examples/intro/calc.txt

# +
1 + 1      = 2
2 + 2      = 4
2 + 2 + 2  = 6
1+1        = 2
0+ -1      = -1

# -
0-1        = -1

# mixed
-1+1       = 0

Example 5-21. examples/intro/t13_calc.out

ok 1 - 1 + 1
ok 2 - 2 + 2
not ok 3 - 2 + 2 + 2
#   Failed test '2 + 2 + 2'
#   in t13_calc.t at line 16.
ok 4 - 1+1
ok 5 - 0+ -1
ok 6 - 0-1
ok 7 - -1+1
1..7
# Looks like you failed 1 test of 7.