_Designing for Testability_
by Fred Wild


Listing One
/test
   /suite
      /bin
      /common
      /pre
      /post
   /input
   /actuals
   /ideals

Listing Two
$logSuffix         = ".log";
$batSuffix         = ".bat";
$scriptSuffix      = ".bat";
$batExecCmd        = "call";


Listing Three
$diffPgm           = "diff" ;
$echoPgm           = "echo" ;
$masterScriptName  = "master";
$compareScriptName = "compare";
$masterFile        = "$masterScriptName"  . "$scriptSuffix" ;
$compareFile       = "$compareScriptName" . "$scriptSuffix" ;
$compareLogFile    = "$compareScriptName" . "$logSuffix" ;


Listing Four
$testRoot        = "/test";
$testSuite       =     "$testRoot/suite";
$testSuiteBin    =         "$testSuite/bin";
$testSuiteCommon =         "$testSuite/common";
$testSuitePre    =         "$testSuite/pre";
$testSuitePost   =         "$testSuite/post";
$testInput       =     "$testRoot/input";
$testActuals     =     "$testRoot/actuals";
$testIdeals      =     "$testRoot/ideals";

-e $testRoot     || die "Aborting:  Missing directory $testRoot\n";
-e $testSuite    || die "Aborting:  Missing directory $testSuite\n";
-e $testSuiteBin || die "Aborting:  Missing directory $testSuiteBin\n";
-e $testActuals  || die "Aborting:  Missing directory $testActuals\n";
-e $testIdeals   || die "Aborting:  Missing directory $testIdeals\n";

Listing Five
$masterScriptPath  = "$testRoot/$masterFile" ;
$compareScriptPath = "$testRoot/$compareFile" ;
$compareLogPath    = "$testRoot/$compareLogFile" ;


Listing Six
$scriptEntryCmds   = "\@echo off\n"
                   . "$echoPgm Test run start...\n\n";
$scriptExitCmds    = "$echoPgm Test run end...\n";

$compareEntryCmds  = "$echoPgm Comparing log files...\n"
                   . "$echoPgm Compare log start... > $compareLogPath\n\n";
$compareExitCmds   = "$echoPgm Compare log end...   >> $compareLogPath\n";


Listing Seven
opendir (BIN_DIR, $testSuiteBin) 
    || die "Aborting:  Can't read dir $testSuiteBin\n";
while ( $filename = readdir(BIN_DIR) ) {
    if ( -f "$testSuiteBin/$filename" ) {
        ($prefix) = split( /\./, $filename ) ;
        $testFilePrefixes{$filename} = $prefix ;
    }
}
closedir (BIN_DIR);


Listing Eight
opendir (PRE_DIR, $testSuitePre) ;
while ( $filename = readdir(PRE_DIR) ) {
    if ( -f "$testSuitePre/$filename" ) {
        @testPreFiles = ( @testPreFiles, $filename ) ;
    }
}
closedir (PRE_DIR) ;

opendir (POST_DIR, $testSuitePost) ;
while ( $filename = readdir(POST_DIR) ) {
    if ( -f "$testSuitePost/$filename" ) {
        @testPostFiles = ( @testPostFiles, $filename ) ;
    }
}
closedir (POST_DIR) ;


Listing Nine
open (TMASTER, "> $masterScriptPath") 
    || die "Aborting:  Can't create $masterPath\n";
print TMASTER "$scriptEntryCmds";

open (TCOMPARE, "> $compareScriptPath") 
    || die "Aborting:  Can't create $comparePath\n";
print TCOMPARE "$compareEntryCmds";
 
foreach $test (keys(%testFilePrefixes)) {
    print "Adding drop-in test $test...\n";
    print TMASTER "$echoPgm $test...\n";

    # --------------------------------------------------------
    # Create preprocessing entries for this test.  If it's a 
    # batch file, invoke it using the $batExecCmd
    #
    foreach $preFile (@testPreFiles) {
        ($prefix) = split( /\./, $preFile ) ;
        if ($prefix eq $testFilePrefixes{$test}) {
            print TMASTER "$batExecCmd " if ($preFile =~ /.*$batSuffix$/ ) ;
            print TMASTER "$testSuitePre/$preFile\n";
        }
    }
    # -----------------------------------------------------
    # Create master script main entry for this test.
    #
    $logFile   = "$testFilePrefixes{$test}$logSuffix";
    $logActual = "$testActuals/$logFile";
    $logIdeal  = "$testIdeals/$logFile";
    print TMASTER "$batExecCmd " if ($test =~ /.*$batSuffix$/ ) ;
    print TMASTER "$testSuiteBin/$test > $logActual\n";

    # --------------------------------------------------------
    # Create postprocessing entries for this test.  If it's a 
    # batch file, invoke it using the $batExecCmd
    #
    foreach $postFile (@testPostFiles) {
        ($prefix) = split( /\./, $postFile ) ;
        if ($prefix eq $testFilePrefixes{$test}) {
            print TMASTER "$batExecCmd " if ($postFile =~ /.*$batSuffix$/ ) ;
            print TMASTER "$testSuitePre/$postFile\n";
        }
    }
    print TMASTER "\n";
    # -----------------------------------------------------
    # Create compare script entry for this test.  If the
    # test's ideal log file doesn't exist, we create a
    # placeholder for the ideal file contents that will
    # be exposed during the compare step.
    #
    print TCOMPARE "$echoPgm **** $logIdeal vs. $logActual ****" .
        " >> $compareLogPath\n"; 
    print TCOMPARE "$diffPgm $logIdeal $logActual >> $compareLogPath\n\n"; 

    if (! -e $logIdeal) {
        open (IDEAL,"> $logIdeal") || die "Aborting: Can't create $logIdeal\n";
        print IDEAL "--replace with ideal contents--\n";
        close (IDEAL);
    }
}
print TMASTER "$scriptExitCmds";
close (TMASTER);

print TCOMPARE "$compareExitCmds";
close (TCOMPARE);





