Question 6: Perl scripting [9]

Simplify the following perl script (a completed version of the perl testing script discussed in lectures) so that it only uses is to compare the expected output to the actual output, and thus the test cases will no longer contain the comparison type to be used in the test.

Be sure to remove all the code that is no longer required because of these changes.

(Note the script continues onto the next page.)
 #! /usr/bin/perl -w 
# run a collection of tests on a specified program from a file of test data 
# syntax is:    driver progname testfile 
#        or:    prove driver :: progname testfile 
# the expected format of the test data file is as follows 
#    each test case is on a line of its own , whitespace delimited, consisting of
#       the arguments to the program,
#       the comparison type to be used in the test 
#       the expected return value 
#       the expected output (cannot contain whitespace) 
#       the name of the test case 
 
use strict; 
use Switch; 
 
my $args;       # command line arguments to be sent to the program under test
my $progname;   # name of the program to test
my $testfile;   # name of the file containing the test cases
my @productTestSet; # array of test cases (to be filled from the testfile)
my $numTests;       # total number of tests (twice the array size)
 
# process the command line arguments sent to the test driver,
#    if valid then read the test cases from the file
BEGIN {  
   $args = scalar @ARGV; 
   if ($args != 2) { 
      print "Expected use $0 programName testFile\n"; 
      exit 0; 
   } 
   $progname = $ARGV[0]; 
   $testfile = $ARGV[1]; 
 
   open (my $fptr, "<", "$testfile") 
      or die "unable to open data file $testfile\n"; 
   while (my $line = <$fptr>) { 
      chomp $line; # drop the newline 
      # split the line into words (assumes space delimited), 
      my @tcase = split(/\s+/, $line); 
      # add the test case to the end of the array of test cases 
      push (@productTestSet, \@tcase); 
   } 
   close($fptr); 
 
   # compute the total number of test cases, two per array case
   $numTests = scalar @productTestSet; 
   $numTests = 2 * $numTests; 
} 
 
use Test::More tests => $numTests; 
print "$numTests tests\n"; 
 
# process each test case in turn 
foreach my $testCase (@productTestSet) { 
   my $testname = pop @$testCase; 
   my $exp = pop @$testCase; 
   my $expret = pop @$testCase; 
   my $ttype = pop @$testCase; 
   my $output = `$progname @$testCase`; 
   my $return = $?; 
   if ($return == -1) { 
      print "Diagnostic: failed to execute\n"; 
   } elsif (($return & 128) > 0) { 
      $return = $return & 127; 
      print "Diagnostic: core dumped, status $return\n"; 
   } else { 
      $return = ($return >> 8); 
   } 
   my $outp = chomp($output); # strip \n (if any) off the output 
   switch ($ttype) { 
      case "ok"     { ok($output, $testname); } 
      case "is"     { is($output, $exp, $testname); } 
      case "isnt"   { isnt($output, $exp, $testname); } 
      case "like"   { like($output, qr/$exp/, $testname); } 
      case "unlike" { unlike($output, qr/$exp/, $testname); } 
      else          { cmp_ok($output, $ttype, $exp, $testname); } 
   } 
   is($return, $expret, $testname); 
}