Enhancing the Test

The passing test case does not currently run any meaningful tests on the source code, and so you are going to enhance the test so that it does. You will add code that calculates the distance between two airports, and then check that the correct distance is returned.
  1. In COBOL Explorer view, double-click DistanceTest.cbl.

    The program appears in the editor.

  2. The test case code will use a variable called errormessage, and so add the following to the working-storage section:
           01 errormessage pic x(200).
  3. Jump to the MFU-TC-SETUP-PREFIX & TEST-TESTAIRCODE entry point, and then after the comment line that reads *> Add any other test setup code here, add the following code:
               move 4 to lnk-function
               call "aircode" using
                           by value lnk-function
                           by value lnk-airport1
                           by value lnk-airport2
                           by value lnk-prefix-text
                           by reference lnk-rec
                           by reference lnk-distance-result
                           by reference lnk-matched-codes-array                       
               end-call

    This code makes a call to the code under test, which attempts to open the data file.

  4. Now jump to the MFU-TC-PREFIX & TEST-TESTAIRCODE entry point, and add the following lines of code before the first call statement:
             move 2 to lnk-function
             move "LHR" to lnk-airport1 *> London Heathrow
             move "SEA" to lnk-airport2 *> Seattle
  5. Within the same section, after the comment line that reads *> Verify the outputs here, add the following code:
              *> Assertions to check that the correct distance is returned
               if function numval(distance-miles) not equal 4787 
                   string "Incorrect distance in miles returned - " 
                           distance-miles delimited by size
                           x"0" delimited by size
                           into errormessage
                   end-string
                   call MFU-ASSERT-FAIL-Z using errormessage
               end-if
    
               if function numval(distance-km) not equal 4787
                   string "Incorrect distance in kilometers returned - " 
                           distance-km delimited by size
                           x"0" delimited by size
                           into errormessage
                   end-string
                   call MFU-ASSERT-FAIL-Z using errormessage
               end-if

    These are the assertions that check that the correct values are calculated for the distance (in miles and km) between the two airports, and if the calculation is incorrect, the test fails.

  6. Finally, position the cursor immediately after the test setup section (just before the InitializeLinkageData section), type testteardown, press Ctrl+Space, press Enter, then select TEST-TESTAIRCODE from the list of level-78 items offered.

    An MFU-TC-TEARDOWN-PREFIX & TEST-TESTAIRCODE entry point is added to the program.

  7. Within this section, add the following code:
               move 5 to lnk-function
               call "aircode" using
                           by value lnk-function
                           by value lnk-airport1
                           by value lnk-airport2
                           by value lnk-prefix-text
                           by reference lnk-rec
                           by reference lnk-distance-result
                           by reference lnk-matched-codes-array
               end-call

    This closes the data file after the test case has run.

  8. To run the test case, in the Micro Focus Unit Testing view, click (Rerun Tests From Last Run).

    The test case reruns, but should fail with the following output: