Chapter 8

Additional files

In addition to library units the following files are provided. Use them by including the file in your code with the include special form.

8.0.1  srfi-13-syntax.scm

This file provides the let-string-start+end syntax defined in SRFI-13. See: 5.12.

8.0.2  test-infrastructure.scm

This file provides a macro based unit testing facility based upon expectations concerning evaluations of expressions. These functions return tagged lists which contain the results of the test package, test case, or expectations evaluated(there are a few other types of results dealing with the gloss, todo, and skip macros detailed below). These result lists are wired together during evaluation to form a large hierarchical tree in memory. This result tree is then passed to either user defined functions which traverse the tree manipulating it in any way desired, or passed to a supplied (read: defined already in test-infrastructure.scm) function which manipulates it in a simple way usually producing human readable or html generated output. API functions to deal with the result types are supplied and the representation of the result is black boxed to the user. It is a violation of encapsulation to inspect the representation directly, and it may change unpredictably in the future.

8.0.2.1  The Test Package Macro API

This macro will evaluate in a left to right fashion the clauses inside it. Clauses can only be certain things, detailed below. All of the clauses are executed, except of course if you bail out of the test package with the escape procedure mechanism. Test packages may nest indefinitely.

8.0.2.2  The Test Case Macro API

This macro will evaluate in a left to right fashion the clauses inside it stopping at the first failed expectation. Clauses can only be certain things as detailed below. You may also stop the execution of expectations if you bail out of the test case with the escape procedure mechanism. Test cases may NOT nest.

8.0.2.3  The Expectation Macro API

An expectation at its core simply evaluates its arguments and check to see if it matches the expectation. The positive or negative result is encapsulated, along with other things such as the unevaluated expressions being checked and some messages supplied with each expectation into a particular type of black box object that one can query with the appropriate API calls(detailed below).

Expectations all have a descriptive message that can be bound to them, along with an optional warning syntax detailed below. A design decision was made to supply expectation macros for the usual types of expectations a user needs because this reduced the abstractness of an expectation into something more manageable. In a future release however, I will supply a special expectation macro where you may supply any predicate you wish along with a ``type tag'' of the predicate.

8.0.2.4  Result Object API

Expectations, test cases, test packages, and helper macros(gloss, todo, etc) all return an object that contains the results and other various aspects of the action performed which ultimately get wired together to form the result tree. This collection of functions forming the rest of the test infrastructure API allows manipulation of these results in an abstracted way as to allow changing of the representation in the future.

8.0.2.5  Test Package Result Object API

If any of these API functions, except test-package-result?, are passed something that isn't a test package result object, they will return 'not-a-test-package-result.

8.0.2.6  Test Case Result Object API

If any of these API functions, except test-case-result?, are passed something that isn't a test case result object, they will return 'not-a-test-case-result.

8.0.2.7  Expect Result Object API: Single Clause Style Expectation

These expectations all take the form of passing a single expression to them to see if they match some a priori expectation. If any of these API functions, except expect-result?, are passed something that isn't a single clause style expectation result object, they will return 'not-an-expect-result.

8.0.2.8  Expect Result Object API: Equivalence Style Expectation

These expectations all take the form of passing a two expressions, the ``left hand side'' and the ``right hand side'' to them to see if they match some a priori equivalence. The left hand side is that which you expect the right hand side to be equivalent. If any of these API functions, except expect-equivalence-result?, are passed something that isn't a single clause style expectation result object, they will return 'not-an-expect-equivalence-result.

8.0.2.9  Expect Result Object API: Tolerance Style Expectation

This is a specialized expectation which accepts three expressions and checks to see if the ``right hand side'' is within a ``tolerance'' of the ``left hand side''. There is only one expectation in the tolerance style currently. If any of these API functions, except expect-tolerance-result?, are passed something that isn't a tolerance style expectation result object, they will return 'not-an-expect-tolerance-result.

8.0.2.10  Various Helper API

These upcoming macros and functions allow the author of the test suite to better control both the execution flow of the test suite and ``decoration'' of the test suite with important information like things yet to do, or just plain documentation.

8.0.2.11  Termination API

When executing in a test package or a test case, one might discover some catastrophic failure of such proportions that it is utterly impossible to continue executing the test case or test package. When that happens you can use the termination facility to exit the test case or test package. Of course, no more expressions will be evaluated in the scope of the termination. It is recommended that you use this method of terminating the test case or test package evaluation since it wraps some contextual information up into the termination result so you can figure out what happened(and where) later when analyzing the result tree.

When using the manipulation API for a terminate result, if you pass a result to one of these function that is not a terminate result, it will return 'not-a-terminate-result.

8.0.2.12  Destructor Object API

The destructor object allows for you to create helper functions which clean up for you usually in case of aborting of a test case or package. For example, suppose you are testing whether or not file writing to a file works correctly in a test case, so, you'd perform an expectation to open the file, and then queue a function in the destructor to remove the file, and then perform the expectation of the write. If the write(or subsequent) expectation fails, then the test case will automatically invoke the helper cleanup function specified in the destructor object that removes the file.

NOTE: This API is still a little experimental in the sense that eventually he destructor object should return a typed result that contains the success of the individual destructor calls. But for now, it is functional for what it does. Also, be VERY CAREFUL that you specify the arguments to these API calls correctly since due to lambda functions not being comparable, this API cannot garuantee that a true destructor object name had been passed to it. So if you call one of the following API calls incorrectly, the behavior will be undefined.

8.0.2.13  Todo API

The purpose of the todo API is to allow the author of a test suite the ability to record into the result tree for later analysis that something still needs to be done. This way you can count/manipulate this information at a later date. Todo macro invocations can occur inside of test cases or test packages.

8.0.2.14  Gloss API

The purpose of the gloss API is to allow the author of a test suite the ability to record messages into the result tree purely for documentation purposes. Gloss macro invocations can occur inside of test cases or test packages.

8.0.2.15  Skip API

The purpose of the skip API is to allow the author of a test suite to completely skip evaluation of a set of expresssions. Skip macro invocations can occur inside of test cases or test packages.

8.0.2.16  Side Effect API

This section of the API just contains a single macro currently since it is considered a little experimental for now. The side effecting evaluates all of its arguments as in a (begin ...) form, it returns a result that is completely ignored by the system and unavailable to the output analysis code.

8.0.2.17  Miscellaneous API

This section contains a few functions whose purpose is to simplify certain kinds of manipulations of result objects.

8.0.2.18  Analysis of the Result Tree

Once a result tree has been evaluated and constructed in memory, what do you do with it? Well, you can do anything you want with it if you choose to write the analysis or output generation functions and pass it the evaluated result tree, and this is, in fact, encouraged. However, usually you just want to print out the tree in a human readable format so you can check things before the serious analysis code gets written. So, to save work for the test suite designer, a tiny API has been written to produce human readable output given a tree of results rooted in a single test package. Of course the single test package may have many other test packages and test cases embedded within it.

8.0.2.19  Output Generation API

8.0.2.20  Example Usages of the Test Suite Infrastructure

This section contains some simple examples of how to author test suites.

Here is a simple example:

(let ((result
        ;; output-style-human function requires a single test package
        ;; to encapsulate everything.
        (test-package "Arithmetic Operators" pd pe

                (test-case "Testing '+'" d e
                        (expect-equal "Adding two positive numbers" 2 (+ 1 1))
                        (expect-equal "Adding two negative numbers" -2 (+ -1 -1))
                        (expect-zero "Adding positive and negative" (+ -1 1)))

                (test-case "Testing '-'" d e
                        (expect-zero "Subtracting two positive numbers" (- 1 1))
                        (expect-zero "Subtracting two negative numbers" (- -1 -1))
                        (expect-equal "Subtracting positive and negative" -2 (- -1 1))))))
        (output-style-human result))

The above example, when evaluated, will produce some human readable output and the a #t value as the result of the top level package. The result variable contains the tree of evaluated expectations, test cases, and the package arranged in a hierarchy extremely similar to the nesting of the above macros. If you desire to manipulate the result tree yourself, you may use the various APIs to manipulate the various results. Please see the implementation of (output-style-human ...) (it isn't large) in the test-infrastructure.scm file to see an example of this.

The variables: pe, e are the escape functions you may use with the (terminate ...) function call if you wish to abort the above code somewhere.

The variables: pd, d allow use of a "destructor" object which allows you to run side effecting functions at the "finishing" point of the evaluation of the test case or test package. These functions are run no matter if the code succeeded correctly or not, so be careful to manage the destructor object carefully so you don't perform unwanted side effects. The names of the destructor objects you supply are lexically scoped in the bodies of the test case or test package.

Now, here is some example output that (output-style-human ...) might generate with the above testing code:

Begin Package: Arithmetic Operators
  Begin Test Case: Testing '+'
    Begin Expectation: Adding two positive numbers
    Expect equal
      Expected Value: 
      2
      Unevaluated: 
      (+ 1 1)
      Evaluated: 
      2
    Result: #t
    End Expectation: Adding two positive numbers

    Begin Expectation: Adding two negative numbers
    Expect equal
      Expected Value: 
      -2
      Unevaluated: 
      (+ -1 -1)
      Evaluated: 
      -2
    Result: #t
    End Expectation: Adding two negative numbers

    Begin Expectation: Adding positive and negative
    Expect zero
      Unevaluated: 
      (+ -1 1)
      Evaluated: 
      0
    Result: #t
    End Expectation: Adding positive and negative

  Result: #t
  End Test Case: Testing '+'

  Begin Test Case: Testing '-'
    Begin Expectation: Subtracting two positive numbers
    Expect zero
      Unevaluated: 
      (- 1 1)
      Evaluated: 
      0
    Result: #t
    End Expectation: Subtracting two positive numbers

    Begin Expectation: Subtracting two negative numbers
    Expect zero
      Unevaluated: 
      (- -1 -1)
      Evaluated: 
      0
    Result: #t
    End Expectation: Subtracting two negative numbers

    Begin Expectation: Subtracting positive and negative
    Expect equal
      Expected Value: 
      -2
      Unevaluated: 
      (- -1 1)
      Evaluated: 
      -2
    Result: #t
    End Expectation: Subtracting positive and negative

  Result: #t
  End Test Case: Testing '-'

Result: #t
End Package: Arithmetic Operators

#t