Qore QUnit Module Reference  0.3.3
 All Classes Namespaces Functions Variables Groups Pages
QUnit::Test Class Reference

Base class representing a simple test, implements an implicit main() function and all utility functions for testing. More...

Inheritance diagram for QUnit::Test:

Private Member Functions

 addTestCase (string name, code call, *softlist< auto > args)
 adds a test case to run More...
 
 addTestCase (QUnit::TestCase tc)
 adds a test case to run More...
 
 assertEq (auto expected, auto actual, *string name)
 Tests a value for equality to an expected value with hard comparisons (types and values must be identical) More...
 
 assertEqSoft (auto expected, auto actual, *string name)
 Tests a value for equality to an expected value with soft comparisons (types may differ) More...
 
 assertFalse (auto actual, *string name)
 Tests a boolean value. More...
 
 assertFloatEq (float expected, float actual, float epsilon=DEFAULT_EPSILON, *string name)
 Tests a float value for equality to an expected value with an allowed error. More...
 
 assertGe (auto expected, auto actual, *string name)
 Tests that a value is greater than or equal to an expected value with hard comparisons (types may not differ) More...
 
 assertGeSoft (auto expected, auto actual, *string name)
 Tests that a value is greater than or equal to than an expected value with soft comparisons (types may differ) More...
 
 assertGt (auto expected, auto actual, *string name)
 Tests that a value is greater than an expected value with hard comparisons (types may not differ) More...
 
 assertGtSoft (auto expected, auto actual, *string name)
 Tests that a value is greater than an expected value with soft comparisons (types may differ) More...
 
 assertionOk (*string name)
 process an ok assertion
 
 assertLe (auto expected, auto actual, *string name)
 Tests that a value is less than or equal to an expected value with hard comparisons (types may not differ) More...
 
 assertLeSoft (auto expected, auto actual, *string name)
 Tests that a value is less than or equal to than an expected value with soft comparisons (types may differ) More...
 
 assertLt (auto expected, auto actual, *string name)
 Tests that a value is less than an expected value with hard comparisons (types may not differ) More...
 
 assertLtSoft (auto expected, auto actual, *string name)
 Tests that a value is less than an expected value with soft comparisons (types may differ) More...
 
 assertNeq (auto expected, auto actual, *string name)
 Tests a value for inequality to an expected value with hard comparisons (types and values must be identical) More...
 
 assertNeqSoft (auto expected, auto actual, *string name)
 Tests a value for inequality to an expected value with soft comparisons (types may differ) More...
 
 assertNothing (auto actual, *string name)
 Tests for no vlaue. More...
 
 assertNRegex (string regex_pattern, string actual, *string name)
 Tests that a test value does not pass a regular expression match. More...
 
 assertNumberEq (number expected, number actual, number epsilon=DEFAULT_EPSILON, *string name)
 Tests a number value for equality to an expected value with an allowed error. More...
 
 assertRegex (string regex_pattern, string actual, *string name)
 Tests that a test value passes a regular expression match. More...
 
 assertSkip (*string name)
 Skips assertion on purpose. More...
 
 assertThrows (string expectedErr, *string expectedDesc, code theCode, *softlist< auto > args, *string name)
 Tests that a piece of code throws an exception with given description. More...
 
 assertThrows (string expectedErr, code theCode, *softlist< auto > args, *string name)
 Tests that a piece of code throws an exception. More...
 
 assertTrue (auto actual, *string name)
 Tests a boolean value. More...
 
 constructor (string name, string version, *reference< list< string >> p_argv, *hash opts)
 creates the object and sets the name of the test
 
bool equals (auto a, auto b)
 Compare two values for equality. More...
 
bool equalsIterated (AbstractIterator a, AbstractIterator b)
 Compare two iterables, item by item, for equality of each index. More...
 
 fail (*string msg)
 Fails the test unconditionally. More...
 
*string getEnv (string key, *string def)
 facade for the system environment variables
 
TestCase getTestCase (string meth)
 returns the current test case
 
 globalSetUp ()
 global setup; will be called once before tests are run
 
 globalTearDown ()
 global tear down; will be called once after all tests are run
 
int main ()
 Run the whole suite, report results.
 
bool notEquals (auto a, auto b)
 Compare two values for inequality. More...
 
string printUnexpectedData (auto exp, auto act, *bool neg, *bool soft_comparisons, *string comparitor)
 Helper function for printing out human-readable comparison of two values.
 
bool regexpMatches (string s, string regexp)
 Compare a string for match against a regexp. More...
 
 setUp ()
 Prototype function for setting up test environment. It will be called for each test individually.
 
 tearDown ()
 Prototype function for cleaning up test environemnt. It will be called after each test has executed.
 
auto testAssertion (string name, code condition, *softlist< auto > args, int expectedResultValue)
 Tests for a single assertion for a call returning an integer value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, float expectedResultValue)
 Tests for a single assertion for a call returning a floating-point value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, number expectedResultValue)
 Tests for a single assertion for a call returning an arbitrary-precision numeric value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, bool expectedResultValue)
 Tests for a single assertion for a call returning a boolean value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, string expectedResultValue)
 Tests for a single assertion for a call returning a string value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, date expectedResultValue)
 Tests for a single assertion for a call returning a date value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, binary expectedResultValue)
 Tests for a single assertion for a call returning a binary value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, hash expectedResultValue)
 Tests for a single assertion for a call returning a hash value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, list expectedResultValue)
 Tests for a single assertion for a call returning a list value and returns the value generated. More...
 
auto testAssertion (string name, code condition, *softlist< auto > args, QUnit::AbstractTestResult expectedResult=new QUnit::TestResultSuccess())
 Tests for a single assertion and returns the value generated. More...
 
auto testAssertionValue (*string name, auto actual, auto expected)
 Tests a value for equality to an expected value. More...
 
auto testAssertionValue (*string name, number actual, number expected, number epsilon=DEFAULT_EPSILON)
 Tests a value for equality to an expected value of number type. More...
 
auto testAssertionValue (*string name, float actual, float expected, float epsilon=DEFAULT_EPSILON)
 Tests a value for equality to an expected value of float type. More...
 
list< TestCasetestCases ()
 list of test cases
 
 testNullAssertion (string name, code condition, *softlist< auto > args)
 Tests for a single assertion for a call returning no value (for example, to ensure that the call does not throw an exception) More...
 
 testSkip (string reason)
 Skips a given test, eg. because it may be missing some dependencies. More...
 

Static Private Member Functions

static string getAssertionName (*string name)
 returns the assertion name for display purposes
 

Private Attributes

const DEFAULT_EPSILON = 0.0000000001
 default epsilon for number/float comparison
 
const QUC_GE = "ge"
 greater than or equals comparitor
 
const QUC_GT = "gt"
 greater than comparitor
 
const QUC_LE = "le"
 greater than or equals comparitor
 
const QUC_LT = "lt"
 greater than comparitor
 
const QUC_Map
 comparitor info
 
const QUC_NEQ = "neq"
 inequality comparitor
 
const QUC_NRE = "nre"
 negative regex comparitor
 
const QUC_RE = "re"
 regex comparitor
 

Additional Inherited Members

- Public Member Functions inherited from QUnit::TestReporter
 constructor (string name, string version, *reference< list< string >> p_argv, hash opts=Opts)
 creates the object from the arguments More...
 
- Public Attributes inherited from QUnit::TestReporter
const Opts
 default options for Qore::GetOpt::constructor()
 

Detailed Description

Base class representing a simple test, implements an implicit main() function and all utility functions for testing.

Member Function Documentation

QUnit::Test::addTestCase ( string  name,
code  call,
*softlist< auto >  args 
)
private

adds a test case to run

Example:
1 addTestCase("MyTest", \myTest());
Parameters
namethe name of the test case
callthe code to call that executes the test case
argsany optional arguments to the test case call
QUnit::Test::addTestCase ( QUnit::TestCase  tc)
private

adds a test case to run

Example:
1 addTestCase(obj);
Parameters
tcthe test case object
QUnit::Test::assertEq ( auto  expected,
auto  actual,
*string  name 
)
private

Tests a value for equality to an expected value with hard comparisons (types and values must be identical)

Example:
1 assertEq(5, function_that_should_return_five());
Parameters
expectedthe expected value
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertEqSoft ( auto  expected,
auto  actual,
*string  name 
)
private

Tests a value for equality to an expected value with soft comparisons (types may differ)

Example:
1 assertEqSoft("5", function_that_should_return_five());
Parameters
expectedthe expected value
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertFalse ( auto  actual,
*string  name 
)
private

Tests a boolean value.

Example:
1 assertFalse(functionThatShouldReturnFalse());
Parameters
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertFloatEq ( float  expected,
float  actual,
float  epsilon = DEFAULT_EPSILON,
*string  name 
)
private

Tests a float value for equality to an expected value with an allowed error.

Example:
1 assertFloatEq(2.5, 2.50001, 0.001);
Parameters
expectedthe expected value
actualthe value generated by the test
epsilonthe allowed error
namethe name or description of the assertion
QUnit::Test::assertGe ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is greater than or equal to an expected value with hard comparisons (types may not differ)

Example:
1 assertGe(5, function_that_should_rturn_a_value_greater_than_or_equal_to_five());
Parameters
expecteda value that should be less than or equal to actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertGeSoft ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is greater than or equal to than an expected value with soft comparisons (types may differ)

Example:
1 assertGeSoft("5", function_that_should_rturn_a_value_greater_than_or_equal_to_five());
Parameters
expecteda value that should be less than or equal to actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertGt ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is greater than an expected value with hard comparisons (types may not differ)

Example:
1 assertGt(5, function_that_should_rturn_a_value_greater_than_five());
Parameters
expecteda value that should be less than actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertGtSoft ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is greater than an expected value with soft comparisons (types may differ)

Example:
1 assertGtSoft("5", function_that_should_rturn_a_value_greater_than_five());
Parameters
expecteda value that should be less than actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertLe ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is less than or equal to an expected value with hard comparisons (types may not differ)

Example:
1 assertLe(5, function_that_should_rturn_a_value_less_than_or_equal_to_five());
Parameters
expecteda value that should be greater than or equal to actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertLeSoft ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is less than or equal to than an expected value with soft comparisons (types may differ)

Example:
1 assertLeSoft("5", function_that_should_rturn_a_value_less_than_or_equal_to_five());
Parameters
expecteda value that should be greater than or equal to actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertLt ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is less than an expected value with hard comparisons (types may not differ)

Example:
1 assertLt(5, function_that_should_rturn_a_value_less_than_five());
Parameters
expecteda value that should be greater than actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertLtSoft ( auto  expected,
auto  actual,
*string  name 
)
private

Tests that a value is less than an expected value with soft comparisons (types may differ)

Example:
1 assertLtSoft("5", function_that_should_rturn_a_value_less_than_five());
Parameters
expecteda value that should be greater than actual
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertNeq ( auto  expected,
auto  actual,
*string  name 
)
private

Tests a value for inequality to an expected value with hard comparisons (types and values must be identical)

Example:
1 assertNeq(5, function_that_should_return_five());
Parameters
expectedthe value that should not be equal to the test value with a hard comparison
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertNeqSoft ( auto  expected,
auto  actual,
*string  name 
)
private

Tests a value for inequality to an expected value with soft comparisons (types may differ)

Example:
1 assertNeqSoft("5", function_that_should_not_return_five());
Parameters
expectedthe value that should not be equal to the test value with a soft comparison
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertNothing ( auto  actual,
*string  name 
)
private

Tests for no vlaue.

Example:
1 assertNothing(function_that_should_not_return_a value());
Parameters
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertNRegex ( string  regex_pattern,
string  actual,
*string  name 
)
private

Tests that a test value does not pass a regular expression match.

Example:
1 assertNRegex("regex string", string_function());
Parameters
regex_patternthe regular expression pattern string that the test value should not match
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertNumberEq ( number  expected,
number  actual,
number  epsilon = DEFAULT_EPSILON,
*string  name 
)
private

Tests a number value for equality to an expected value with an allowed error.

Example:
1 assertNumberEq(2.5n, 2.50001n, 0.001n);
Parameters
expectedthe expected value
actualthe value generated by the test
epsilonthe allowed error
namethe name or description of the assertion
QUnit::Test::assertRegex ( string  regex_pattern,
string  actual,
*string  name 
)
private

Tests that a test value passes a regular expression match.

Example:
1 assertRegex("regex string", string_function());
Parameters
regex_patternthe regular expression pattern string that the test value should match
actualthe value generated by the test
namethe name or description of the assertion
QUnit::Test::assertSkip ( *string  name)
private

Skips assertion on purpose.

Parameters
namethe name or description of the assertion
Example:
1 assertSkip("assertion name or reason to skip");
QUnit::Test::assertThrows ( string  expectedErr,
*string  expectedDesc,
code  theCode,
*softlist< auto >  args,
*string  name 
)
private

Tests that a piece of code throws an exception with given description.

Example:
1 assertThrows("DIVISION-BY-ZERO", "division by zero found in integer expression", sub(int a) {print(5/a);}, 0);
Parameters
expectedErrthe expected exception type
expectedDescthe expected exception detail (desc field), ignored if NOTHING
theCodethe code to execute
argsoptional arguments to the code
namean optional label for the assertion
QUnit::Test::assertThrows ( string  expectedErr,
code  theCode,
*softlist< auto >  args,
*string  name 
)
private

Tests that a piece of code throws an exception.

Example:
1 assertThrows("DIVISION-BY-ZERO", sub(int a) {print(5/a);}, 0);
Parameters
expectedErrthe expected exception type
theCodethe code to execute
argsoptional arguments to the code
namean optional label for the assertion
QUnit::Test::assertTrue ( auto  actual,
*string  name 
)
private

Tests a boolean value.

Example:
1 assertTrue(functionThatShouldReturnTrue());
Parameters
actualthe value generated by the test
namethe name or description of the assertion
bool QUnit::Test::equals ( auto  a,
auto  b 
)
private

Compare two values for equality.

Parameters
aArgument 1
bArgument 2
Returns
a == b
bool QUnit::Test::equalsIterated ( AbstractIterator  a,
AbstractIterator  b 
)
private

Compare two iterables, item by item, for equality of each index.

Parameters
aIterable 1
bIterable 2
Returns
a == b
QUnit::Test::fail ( *string  msg)
private

Fails the test unconditionally.

Example:
1 fail("Unexpected code executed");
Parameters
msgthe failure message
bool QUnit::Test::notEquals ( auto  a,
auto  b 
)
private

Compare two values for inequality.

Parameters
aArgument 1
bArgument 2
Returns
a != b
bool QUnit::Test::regexpMatches ( string  s,
string  regexp 
)
private

Compare a string for match against a regexp.

Parameters
sString to match
regexpRegular expression to match against
Returns
a == b
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
int  expectedResultValue 
)
private

Tests for a single assertion for a call returning an integer value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuean integer value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
float  expectedResultValue 
)
private

Tests for a single assertion for a call returning a floating-point value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuea floating-point value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
number  expectedResultValue 
)
private

Tests for a single assertion for a call returning an arbitrary-precision numeric value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuean arbitrary-precision numeric value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
bool  expectedResultValue 
)
private

Tests for a single assertion for a call returning a boolean value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuea boolean value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
string  expectedResultValue 
)
private

Tests for a single assertion for a call returning a string value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuea string value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
date  expectedResultValue 
)
private

Tests for a single assertion for a call returning a date value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuea date value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
binary  expectedResultValue 
)
private

Tests for a single assertion for a call returning a binary value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuea binary value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
hash  expectedResultValue 
)
private

Tests for a single assertion for a call returning a hash value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuea hash value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
list  expectedResultValue 
)
private

Tests for a single assertion for a call returning a list value and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultValuea list value
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertion ( string  name,
code  condition,
*softlist< auto >  args,
QUnit::AbstractTestResult  expectedResult = new QUnit::TestResultSuccess() 
)
private

Tests for a single assertion and returns the value generated.

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
expectedResultA class describing the expected result of condition; the default is QUnit::TestResultSuccess
Returns
the result of the condition call, if the immediate value has any further use
auto QUnit::Test::testAssertionValue ( *string  name,
auto  actual,
auto  expected 
)
private

Tests a value for equality to an expected value.

Example:
1 testAssertionValue("date > operator", now() > (now() - 1D), True);
Parameters
namethe name or description of the assertion
actualthe value generated by the test
expectedthe expected value
Returns
the value argument
Note
make sure and use testAssertion() for any calls that could throw an exception
auto QUnit::Test::testAssertionValue ( *string  name,
number  actual,
number  expected,
number  epsilon = DEFAULT_EPSILON 
)
private

Tests a value for equality to an expected value of number type.

Parameters
namethe name or description of the assertion
actualthe value generated by the test
expectedthe expected value
Returns
the value argument
Note
make sure and use testAssertion() for any calls that could throw an exception
auto QUnit::Test::testAssertionValue ( *string  name,
float  actual,
float  expected,
float  epsilon = DEFAULT_EPSILON 
)
private

Tests a value for equality to an expected value of float type.

Parameters
namethe name or description of the assertion
actualthe value generated by the test
expectedthe expected value
Returns
the value argument
Note
make sure and use testAssertion() for any calls that could throw an exception
QUnit::Test::testNullAssertion ( string  name,
code  condition,
*softlist< auto >  args 
)
private

Tests for a single assertion for a call returning no value (for example, to ensure that the call does not throw an exception)

Parameters
namethe name or description of the assertion
conditionA test function whose result we are asserting
argsArguments passed to condition
Returns
the result of the condition call, if the immediate value has any further use
QUnit::Test::testSkip ( string  reason)
private

Skips a given test, eg. because it may be missing some dependencies.

Parameters
reasonThe reason for the test skip