TestAssertions

Declared In:

Introduction

The script defining all the test assertions.



Member Functions

assert

Succeeds when the given expression is true.

assertCocoaReference

Succeeds when the given argument is a reference to a Cocoa object. Fails otherwise.

assertEqual

Succeeds when the two given expressions have the same value.

assertEqualAbsError

Fails unless e1 and e2 are within delta from each other.

assertEqualRelError

Fails unless e1 and e2 have a relative error less than eps.

assertInheritsFrom

Tests whether an object inherits from another object.

assertInstanceOf

Tests whether the given expression belongs to the given class.

assertKindOf

Tests whether the given object or any of its ancestors belongs to the given class.

assertMissing

A shortcut for assertEqual(missing value, expr).

assertNil

Deprecated.

assertNotEqual

Succeeds when the two given expressions are not equal.

assertNotReference

Fails when a variable is a reference.

assertNull

A shortcut for assertEqual(null, expr).

assertReference

Tests whether a variable is a reference.

asText

Utility handler to coerce an object to text.

fail

Raises a TEST_FAILED error.

failIf

Fails when the given assertion succeeds.

formatInheritanceChain

Helper handler that returns a textual representation of an inheritance chain.

isCocoaRef

Utility handler to check whether a given expression is a reference to a Cocoa object.

notOk

Succeeds when its argument is false.

ok

Succeeds when its argument is true.

pp

Returns a textual representation of an object.

refute

Succeeds when the given expression is false.

refuteCocoaReference

Succeeds when the given argument is not a reference to a Cocoa object. Fails otherwise.

refuteEqual

A synonym for assertNotEqual().

refuteInheritsFrom

Succeeds when anotherObj does not inherit (directly on indirectly) from obj.

refuteInstanceOf

Succeeds when the given expression is not of the given class.

refuteKindOf

Verifies that neither the given object nor any of its ancestors belong to the given class.

refuteMissing

A shortcut for refuteEqual(missing value, expr).

refuteNil

Deprecated.

refuteNull

A shortcut for refuteEqual(null, expr).

should

A synonym for assert().

shouldBeCocoaReference

A synonym for assertCocoaReference().

shouldBeReference

A synonym for assertReference().

shouldEqual

A synonym for assertEqual().

shouldNotBeCocoaReference

A synonym for refuteCocoaReference().

shouldNotBeReference

A synonym for assertNotReference().

shouldNotEqual

A synonym for assertNotEqual().

shouldNotRaise

Fails if expectedErrorNumber is raised by executing object.

shouldnt

A synonym for refute().

shouldntRaise

A synonym for shouldNotRaise(). Deprecated.

skip

Raises a TEST_SKIPPED error.


assert


Succeeds when the given expression is true.

on assert(
    expr,
    message)
Parameters
expr

[boolean] An expression that evaluates to true or false.

message

[text] A message that is printed when the test fails.

Throws

A TEST_FAILED error if the assertion fails.


assertCocoaReference


Succeeds when the given argument is a reference to a Cocoa object. Fails otherwise.

on assertCocoaReference(
    anObject)
Parameters
anObject

[anything] An expression.

Throws

A TEST_FAILED error if the assertion fails.


assertEqual


Succeeds when the two given expressions have the same value.

on assertEqual(
    expected,
    value)
Parameters
expected

[anything] The expected value.

value

[anything] Some other value.

Throws

A TEST_FAILED error if the assertion fails.


assertEqualAbsError


Fails unless e1 and e2 are within delta from each other.

on assertEqualAbsError(
    e1,
    e2,
    delta)
Parameters
e1

[number] A number.

e2

[number] A number.

delta

[number] The absolute error.

Discussion

This assertion succeeds if and only if |e1-e2| <= delta.

Throws

A TEST_FAILED error if the assertion fails.


assertEqualRelError


Fails unless e1 and e2 have a relative error less than eps.

on assertEqualRelError(
    e1,
    e2,
    eps)
Parameters
e1

[number] A number.

e2

[number] A number.

eps

[number] The relative error.

Discussion

This assertion succeeds if and only if |e1-e2| <= min(|e1|,|e2|) * eps.

Throws

A TEST_FAILED error if the assertion fails.


assertInheritsFrom


Tests whether an object inherits from another object.

on assertInheritsFrom(
    ancestor,
    descendant)
Parameters
ancestor

[anything] A value.

descendant

[anything] A value.

Discussion

This test walks up the inheritance chain of descendantObject until it finds obj, reaches the end of the inheritance chain, or detects a cycle in the inheritance chain.

Throws

A TEST_FAILED error if the assertion fails.


assertInstanceOf


Tests whether the given expression belongs to the given class.

on assertInstanceOf(
    klass,
    expr)
Parameters
klass

[class] A class name.

expr

[anything] A value.

Throws

A TEST_FAILED error if the assertion fails.


assertKindOf


Tests whether the given object or any of its ancestors belongs to the given class.

on assertKindOf(
    klass,
    expr)
Parameters
klass

[class] A class name.

expr

[anything] A value.

Discussion

This is mainly useful for user-defined scripts and user-defined inheritance hierarchies. For built-in types, it is almost equivalent to assertInstanceOf(). The main difference is that it can be used to test whether an expression is a number, but it does not matter if it is an integer or real (you cannot do that with assertInstanceOf()).

Throws

A TEST_FAILED error if the assertion fails.


assertMissing


A shortcut for assertEqual(missing value, expr).

on assertMissing(
    expr)

assertNil


Deprecated.

on assertNil(
    expr)

See


assertNotEqual


Succeeds when the two given expressions are not equal.

on assertNotEqual(
    unexpected,
    value)
Parameters
unexpected

[anything] The unexpected value.

value

[anything] Some other value.

Throws

A TEST_FAILED error if the assertion fails.


assertNotReference


Fails when a variable is a reference.

on assertNotReference(
    anObject)
Parameters
anObject

[anything] An expression.

Throws

A TEST_FAILED error if the assertion fails.


assertNull


A shortcut for assertEqual(null, expr).

on assertNull(
    expr)

assertReference


Tests whether a variable is a reference.

on assertReference(
    anObject)
Parameters
anObject

[anything] An expression.

Throws

A TEST_FAILED error if the assertion fails.


asText


Utility handler to coerce an object to text.

on asText(
    s)

fail


Raises a TEST_FAILED error.

on fail(
    why)
Parameters
why

[text] A message.

Throws

A TEST_FAILED error.


failIf


Fails when the given assertion succeeds.

on failIf(
    assertion,
    args,
    msg)
Parameters
assertion

[handler] An assertion handler.

args

[list] A list of arguments to be passed to the handler. The length of the list must match the number of arguments of the assertion.

msg

[text] A message to print when this test fails.

Discussion

This is mostly a convenience for testing ASUnit itself, since for every positive assertion (assert..., should...), ASUnit already defines a corresponding negative assertion (refute..., shouldnt...).

Throws

A TEST_FAILED error when assertion(args) succeeds.


formatInheritanceChain


Helper handler that returns a textual representation of an inheritance chain.


isCocoaRef


Utility handler to check whether a given expression is a reference to a Cocoa object.

on isCocoaRef(
    x)
Discussion

See this MacScripter's thread.


notOk


Succeeds when its argument is false.

on notOk(
    expr)
Parameters
expr

[boolean] An expression that evaluates to true or false.

Throws

A TEST_FAILED error if the assertion fails.


ok


Succeeds when its argument is true.

on ok(
    expr)
Parameters
expr

[boolean] An expression that evaluates to true or false.

Throws

A TEST_FAILED error if the assertion fails.


pp


Returns a textual representation of an object.

on pp(
    anObject)
Parameters
anObject

[anything] An expression.


refute


Succeeds when the given expression is false.

on refute(
    expr,
    message)
Parameters
expr

[boolean] An expression that evaluates to true or false.

message

[text] A message that is printed when the test fails.

Throws

A TEST_FAILED error if the assertion fails.


refuteCocoaReference


Succeeds when the given argument is not a reference to a Cocoa object. Fails otherwise.

on refuteCocoaReference(
    anObject)
Parameters
anObject

[anything] An expression.

Throws

A TEST_FAILED error if the assertion fails.


refuteEqual


A synonym for assertNotEqual().

on refuteEqual(
    unexpected,
    value)

refuteInheritsFrom


Succeeds when anotherObj does not inherit (directly on indirectly) from obj.

on refuteInheritsFrom(
    obj,
    anotherObj)
Parameters
obj

[anything] A value.

anotherObj

[anything] A value.

Throws

A TEST_FAILED error if the assertion fails.


refuteInstanceOf


Succeeds when the given expression is not of the given class.

on refuteInstanceOf(
    klass,
    expr)
Parameters
klass

[class] A class name.

expr

[anything] A value.

Throws

A TEST_FAILED error if the assertion fails.


refuteKindOf


Verifies that neither the given object nor any of its ancestors belong to the given class.

on refuteKindOf(
    klass,
    expr)
Parameters
klass

[class] A class name.

expr

[anything] A value.

Discussion

See assertKindOf().

Throws

A TEST_FAILED error if the assertion fails.


refuteMissing


A shortcut for refuteEqual(missing value, expr).

on refuteMissing(
    expr)

refuteNil


Deprecated.

on refuteNil(
    expr)

See


refuteNull


A shortcut for refuteEqual(null, expr).

on refuteNull(
    expr)

should


A synonym for assert().

on should(
    expr,
    message)

shouldBeCocoaReference


A synonym for assertCocoaReference().

on shouldBeCocoaReference(
    anObject)

shouldBeReference


A synonym for assertReference().

on shouldBeReference(
    anObject)

shouldEqual


A synonym for assertEqual().

on shouldEqual(
    expected,
    value)

shouldNotBeCocoaReference


A synonym for refuteCocoaReference().


shouldNotBeReference


A synonym for assertNotReference().

on shouldNotBeReference(
    anObject)

shouldNotEqual


A synonym for assertNotEqual().

on shouldNotEqual(
    unexpected,
    value)

shouldNotRaise


Fails if expectedErrorNumber is raised by executing object.

on shouldNotRaise(
    expectedErrorNumber,
    object,
    message)
Parameters
expectedErrorNumber

[integer] or [list] An exception number or a list of exception numbers. To make this assertion succeed only when no exception is raised, pass an empty list.

object

[script] or [handler] A script or a handler without parameters.

message

[text] A message that is printed when this assertion fails.

Discussion

Fails if a certain error is raised.

Throws

A TEST_FAILED error if the assertion fails.


shouldnt


A synonym for refute().

on shouldnt(
    expr,
    message)

shouldntRaise


A synonym for shouldNotRaise(). Deprecated.

on shouldntRaise(
    expectedErrorNumber,
    object,
    message)

skip


Raises a TEST_SKIPPED error.

on skip(
    why)
Parameters
why

[text] A message.

Throws

A TEST_SKIPPED error.


Member Data

maxRecursionDepth

The maximum recursion depth for pp().

showInvisibles

Determines whether invisible characters should be made visible.


maxRecursionDepth


The maximum recursion depth for pp().

property maxRecursionDepth : 50 

showInvisibles


Determines whether invisible characters should be made visible.

property showInvisibles : true 
Discussion

When this property is set to true (which is the default), invisible characters (spaces, tabulations, linefeeds, and returns) are printed as visible characters. This is especially useful when a test like assertEqual() fails because the expected value and the actual value differ, say, just because the actual value has a trailing new line that the expected value does not have.