Modules - test

The test module provides convenience predicates for writing unit tests in Plang. The following is a simple example for testing the sort/2 and sortd/2 predicates:

:- import(test).

test(sort)
{
    verify(sort([a, y, b, m], [a, b, m, y]));
    verify_error(sort(L, S), instantiation_error);
}

test(sortd)
{
    verify(sortd([a, y, b, m], [y, m, b, a]));
    verify_error(sortd(L, S), instantiation_error);
}

All test predicates should be declared as test(Name) where Name is an atom that uniquely identifies the test. Multiple test predicates can be declared in the same application, which will be executed when test::main/1 is called:

main(Args)
{
    test::main(Args);
}

As an alternative to providing a declaration for main/1, the -m option can be specified on the plang command-line:

plang -mtest::main test-sort.lp

The output of running the unit test will look something like this:

test-sort: sort: ok                                                     
test-sort: sortd: ok                                                    
test-sort: 2 passed, 0 failed                                           

compare/2, fail/1, fail/2, verify/1, verify_error/2, verify_fuzzy/1, verify_var_error/2, test::main/1


compare/2 - compares two values for equality.

Usage
compare(Actual, Expected)
Description
Compares Actual with Expected using (==)/2. Aborts the current unit test with an error if the comparison fails.
Examples
X is 2 + 2;
compare(X, 4);
See Also
fail/1, verify/1

fail/1 - fails the current unit test with a message.

Usage
fail(Message)
Description
Fails the current unit test and displays the string Message in the error log.
If the test executes fail/0, then the effect will be equivalent to calling fail/1 with a generic error message.
Examples
fail("resource not found");
See Also
fail/0, fail/2

fail/2 - fails the current unit test with two messages.

Usage
fail(Message1, Message2)
Description
Fails the current unit test and displays the strings Message and Message2 in the error log.
Examples
fail(2 + 2 =:= 4, "arithmetic failure");
See Also
fail/0, fail/1

verify/1 - verifies that a condition is true.

Usage
verify(Goal)
Description
Executes Goal and succeeds if it succeeds. If Goal fails or throws an error, then the current unit test will be aborted with an error.
Examples
verify(fail);
verify(2 + 2 =:= 4);
verify(abs(1.0 / 3.0 - 0.333333) <= 0.00001);
See Also
compare/2, fail/1, verify_error/2

verify_error/2 - verifies that a goal throws an expected error.

Usage
verify_error(Goal, ExpectedError)
Description
Executes Goal and succeeds if it throws the term error(ExpectedError, _). Otherwise the current unit test will be aborted with an error.
ExpectedError must be a ground term because it is compared to the actual error using (==)/2. If ExpectedError involves variables then the test will fail. Use verify_var_error/2 instead to check for errors that involve variables.
Examples
verify_error(X is 2 + "3", type_error(number, "3"));
See Also
fail/1, verify/1, verify_var_error/2

verify_fuzzy/1 - verifies that the current fuzzy confidence value is as expected.

Usage
verify_fuzzy(Expected)
Description
Evaluates Expected according to the rules of is/2 and compares it with the current fuzzy confidence value from fuzzy/1. Succeeds if the two values are within 0.0001 of each other. Otherwise the current unit test will be aborted with an error.
This predicate is intended for testing code that involves fuzzy logic.
Examples
verify_fuzzy(0.5);
See Also
verify/1, fuzzy/1

verify_var_error/2 - verifies that a goal throws an expected error that involves variables.

Usage
verify_var_error(Goal, ExpectedError)
Description
Executes Goal and succeeds if it throws a term that can be unified with error(ExpectedError, _). Otherwise the current unit test will be aborted with an error.
Examples
verify_var_error(fuzzy(1.0, [H]), type_error(interval_list, [H]));
See Also
fail/1, verify/1, verify_error/2

test::main/1 - runs all unit tests and reports their results.

Usage
test::main(Args)
Description
Runs all of the unit tests that have been registered with the dynamic test/1 predicate. The Args are the command-line arguments from the operating system. The first element of Args is stripped to its base name and then used as the name of the "suite" that is being run.
If one or more unit tests produce an error, then test::main/1 will fail. If all unit tests pass, then test::main/1 succeeds.
Examples
main(Args)
{
    test::main(Args);
}

Generated on 26 May 2011 for plang by  doxygen 1.6.1