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:
[legend]

## Public Member Functions

| addTestCase (string name, code call, *softlist args)
| adds a test case to run More...
| addTestCase (QUnit::TestCase tc)
| adds a test case to run More...
public | assertEq (any expected, any actual, *string name)
| Tests a value for equality to an expected value with hard comparisons (types and values must be identical) More...
public | assertEqSoft (any expected, any actual, *string name)
| Tests a value for equality to an expected value with soft comparisons (types may differ) More...
public | assertFalse (any actual, *string name)
| Tests a boolean value. More...
public | assertFloatEq (float expected, float actual, float epsilon=0.0000000001, *string name)
| Tests a float value for equality to an expected value with an allowed error. More...
public | assertNumberEq (number expected, number actual, number epsilon=0.0000000001, *string name)
| Tests a number value for equality to an expected value with an allowed error. More...
public | assertSkip (*string name)
| Skips assertion on purpose. More...
public | assertThrows (string expectedErr, *string expectedDesc, code theCode, *softlist args, *string name)
| Tests that a piece of code throws an exception with given description. More...
public | assertThrows (string expectedErr, code theCode, *softlist args, *string name)
| Tests that a piece of code throws an exception. More...
public | assertTrue (any actual, *string name)
| Tests a boolean value. More...
| constructor (string name, string version, *reference p_argv, *hash opts)
| creates the object and sets the name of the test
bool | equals (any a, any 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...
public | 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 (any a, any b)
| Compare two values for inequality. More...
private string | printUnexpectedData (any exp, any act, *bool neg, *bool soft_comparisons)
| 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.
public any | testAssertion (string name, code condition, *softlist args, int expectedResultValue)
| Tests for a single assertion for a call returning an integer value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, float expectedResultValue)
| Tests for a single assertion for a call returning a floating-point value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, number expectedResultValue)
| Tests for a single assertion for a call returning an arbitrary-precision numeric value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, bool expectedResultValue)
| Tests for a single assertion for a call returning a boolean value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, string expectedResultValue)
| Tests for a single assertion for a call returning a string value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, date expectedResultValue)
| Tests for a single assertion for a call returning a date value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, binary expectedResultValue)
| Tests for a single assertion for a call returning a binary value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, hash expectedResultValue)
| Tests for a single assertion for a call returning a hash value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, list expectedResultValue)
| Tests for a single assertion for a call returning a list value and returns the value generated. More...
public any | testAssertion (string name, code condition, *softlist args, QUnit::AbstractTestResult expectedResult=new QUnit::TestResultSuccess())
| Tests for a single assertion and returns the value generated. More...
public any | testAssertionValue (*string name, any actual, any expected)
| Tests a value for equality to an expected value. More...
public | testNullAssertion (string name, code condition, *softlist 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...
public | testSkip (string reason)
| Skips a given test, eg. because it may be missing some dependencies. More...
Public Member Functions inherited from QUnit::TestReporter
| addTestResult (TestCase tc, int success, *string error, *string pos, *string detail)
| adds a test result
| constructor (string name, string version, *reference p_argv, hash opts=Opts)
| creates the object from the arguments More...
int | errors ()
| returns the number of errors encountered during test execution
int | skipped ()
| returns the number of tests skipped
int | testCount ()
| returns the total number of test results

## Static Public Member Functions

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

## Private Attributes

list | testCases = ()
| list of test cases

## Additional Inherited Members

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 | args
| ) |  |

adds a test case to run

Example:
  addTestCase("MyTest", \myTest());

Parameters

  name | the name of the test case
  call | the code to call that executes the test case
  args | any optional arguments to the test case call

QUnit::Test::addTestCase | ( | QUnit::TestCase | tc | ) |

adds a test case to run

Example:
  addTestCase(obj);

Parameters

  tc | the test case object

public QUnit::Test::assertEq | ( | any | expected,
|  | any | actual,
|  | *string | name
| ) |  |

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

Example:
  assertEq(5, functionThatShouldReturnFive());

Parameters

  expected | the expected value
  actual | the value generated by the test
  name | the name or description of the assertion

public QUnit::Test::assertEqSoft | ( | any | expected,
|  | any | actual,
|  | *string | name
| ) |  |

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

Example:
  assertEqSoft("5", functionThatShouldReturnFive());

Parameters

  expected | the expected value
  actual | the value generated by the test
  name | the name or description of the assertion

public QUnit::Test::assertFalse | ( | any | actual,
|  | *string | name
| ) |  |

Tests a boolean value.

Example:
  assertFalse(functionThatShouldReturnFalse());

Parameters

  actual | the value generated by the test
  name | the name or description of the assertion

public QUnit::Test::assertFloatEq | ( | float | expected,
|  | float | actual,
|  | float | epsilon = `0.0000000001`,
|  | *string | name
| ) |  |

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

Example:
  assertFloatEq(2.5, 2.50001, 0.001);

Parameters

  expected | the expected value
  actual | the value generated by the test
  epsilon | the allowed error
  name | the name or description of the assertion

public QUnit::Test::assertNumberEq | ( | number | expected,
|  | number | actual,
|  | number | epsilon = `0.0000000001`,
|  | *string | name
| ) |  |

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

Example:
  assertNumberEq(2.5n, 2.50001n, 0.001n);

Parameters

  expected | the expected value
  actual | the value generated by the test
  epsilon | the allowed error
  name | the name or description of the assertion

public QUnit::Test::assertSkip | ( | *string | name | ) |

Skips assertion on purpose.

Parameters

  name | the name or description of the assertion

Example:
  assertSkip("assertion name or reason to skip");

public QUnit::Test::assertThrows | ( | string | expectedErr,
|  | *string | expectedDesc,
|  | code | theCode,
|  | *softlist | args,
|  | *string | name
| ) |  |

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

Example:
  assertThrows("DIVISION-BY-ZERO", "division by zero found in integer expression", sub(int a) {print(5/a);}, 0);

Parameters

  expectedErr | the expected exception type
  expectedDesc | the expected exception detail (desc field), ignored if NOTHING
  theCode | the code to execute
  args | optional arguments to the code

public QUnit::Test::assertThrows | ( | string | expectedErr,
|  | code | theCode,
|  | *softlist | args,
|  | *string | name
| ) |  |

Tests that a piece of code throws an exception.

Example:
  assertThrows("DIVISION-BY-ZERO", sub(int a) {print(5/a);}, 0);

Parameters

  expectedErr | the expected exception type
  theCode | the code to execute
  args | optional arguments to the code

public QUnit::Test::assertTrue | ( | any | actual,
|  | *string | name
| ) |  |

Tests a boolean value.

Example:
  assertTrue(functionThatShouldReturnTrue());

Parameters

  actual | the value generated by the test
  name | the name or description of the assertion

bool QUnit::Test::equals | ( | any | a,
|  | any | b
| ) |  |

Compare two values for equality.

Parameters

  a | Argument 1
  b | Argument 2

Returns
  a == b

bool QUnit::Test::equalsIterated | ( | AbstractIterator | a,
|  | AbstractIterator | b
| ) |  |

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

Parameters

  a | Iterable 1
  b | Iterable 2

Returns
  a == b

public QUnit::Test::fail | ( | *string | msg | ) |

Fails the test unconditionally.

Example:
  fail("Unexpected code executed");

Parameters

  msg | the failure message

bool QUnit::Test::notEquals | ( | any | a,
|  | any | b
| ) |  |

Compare two values for inequality.

Parameters

  a | Argument 1
  b | Argument 2

Returns
  a != b

bool QUnit::Test::regexpMatches | ( | string | s,
|  | string | regexp
| ) |  |

Compare a string for match against a regexp.

Parameters

  s | String to match
  regexp | Regular expression to match against

Returns
  a == b

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | int | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | an integer value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | float | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | a floating-point value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | number | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | an arbitrary-precision numeric value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | bool | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | a boolean value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | string | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | a string value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | date | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | a date value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | binary | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | a binary value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | hash | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | a hash value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | list | expectedResultValue
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResultValue | a list value

Returns
  the result of the condition call, if the immediate value has any further use

public any QUnit::Test::testAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args,
|  | QUnit::AbstractTestResult | expectedResult = `new QUnit::TestResultSuccess()`
| ) |  |

Tests for a single assertion and returns the value generated.

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition
  expectedResult | A 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

public any QUnit::Test::testAssertionValue | ( | *string | name,
|  | any | actual,
|  | any | expected
| ) |  |

Tests a value for equality to an expected value.

Example:
  testAssertionValue("date > operator", now() > (now() - 1D), True);

Parameters

  name | the name or description of the assertion
  actual | the value generated by the test
  expected | the expected value

Returns
  the value argument

Note
  make sure and use testAssertion() for any calls that could throw an exception

public QUnit::Test::testNullAssertion | ( | string | name,
|  | code | condition,
|  | *softlist | args
| ) |  |

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

Parameters

  name | the name or description of the assertion
  condition | A test function whose result we are asserting
  args | Arguments passed to condition

Returns
  the result of the condition call, if the immediate value has any further use

public QUnit::Test::testSkip | ( | string | reason | ) |

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

Parameters

  reason | The reason for the test skip