View source

class thx.Assert

Available on all platforms

This class contains only static members used to perform assertions. Its use is straight forward:

Assert.equals(1, 0); // fails
Assert.isFalse(1 == 1, "guess what?"); // fails and log the passed message
Assert.isTrue(true); // successfull

The default behavior for Assert static methods is determined by the implementation of Assert.behavior and defaults to thx.DefaultAssertBehavior.

If you want to entirely remove assertions from your generated output, just add the directive -D no-asserts (useful for production builds).

Class Fields

static function contains<T>(possibilities:Array<T>, value:T, ?msg:String, ?pos:PosInfos):Void

Checks that the test value matches at least one of the possibilities.

Parameters:
possibility:

An array of possible matches

value:

The value to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function equals(expected:Dynamic, value:Dynamic, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the value parameter is equal to the expected one.

Assert.equals(10, age);
Parameters:
expected:

The expected value to check against

value:

The value to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function excludes<T>(match:T, values:Array<T>, ?msg:String, ?pos:PosInfos):Void

Checks that the test array does not contain the match parameter.

Parameters:
match:

The element that must NOT be included in the tested array

values:

The values to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function fail(msg:String = "failure expected", ?pos:PosInfos):Void

Forces a failure.

Parameters:
msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function is(value:Dynamic, type:Dynamic, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the 'value' parameter is of the of the passed type.

Parameters:
value:

The value to test

type:

The type to test against

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function isContainedIn<T>(match:T, values:Array<T>, ?msg:String, ?pos:PosInfos):Void

Checks that the test array contains the match parameter.

Parameters:
match:

The element that must be included in the tested array

values:

The values to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function isFalse(value:Bool, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the condition is false.

Parameters:
cond:

The condition to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function isNull(value:Dynamic, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the value is null.

Parameters:
value:

The value to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function isTrue(cond:Bool, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the condition is true.

Parameters:
cond:

The condition to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function matches(pattern:EReg, value:Dynamic, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the value parameter does match against the passed EReg instance.

Assert.match(~/x/i, "haXe");
Parameters:
pattern:

The pattern to match against

value:

The value to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function nearEquals(expected:Float, value:Float, ?approx:Float, ?msg:String, ?pos:PosInfos):Void

Same as Assert.equals but accounting for an approximation error.

Assert.nearEquals(Math.PI, value);
Parameters:
expected:

The expected value to check against

value:

The value to test

approx:

The approximation tollerance. Default is 1e-5

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function notEquals(expected:Dynamic, value:Dynamic, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the value parameter is not the same as the expected one.

Assert.notEquals(10, age);
Parameters:
expected:

The expected value to check against

value:

The value to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function notNull(value:Dynamic, ?msg:String, ?pos:PosInfos):Void

Asserts successfully when the value is not null.

Parameters:
value:

The value to test

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function pass(msg:String = "pass expected", ?pos:PosInfos):Void

Adds a successful assertion for cases where there are no values to assert.

Parameters:
msg:

An optional success message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function raises(method:Void -> Void, ?type:Dynamic, ?msgNotThrown:String, ?msgWrongType:String, ?pos:PosInfos):Void

It is used to test an application that under certain circumstances must react throwing an error. This assert guarantees that the error is of the correct type (or Dynamic if non is specified).

Assert.raises(function() { throw "Error!"; }, String);
Parameters:
method:

A method that generates the exception.

type:

The type of the expected error. Defaults to Dynamic (catch all).

msgNotThrown:

An optional error message used when the function fails to raise the expected exception. If not passed a default one will be used

msgWrongType:

An optional error message used when the function raises the exception but it is of a different type than the one expected. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function same(expected:Dynamic, value:Dynamic, recursive:Bool = true, ?msg:String, ?pos:PosInfos):Void

Check that value is an object with the same fields and values found in expected. The default behavior is to check nested objects in fields recursively.

Assert.same({ name : "utest"}, ob);
Parameters:
expected:

The expected value to check against

value:

The value to test

recursive:

States whether or not the test will apply also to sub-objects. Defaults to true

msg:

An optional error message. If not passed a default one will be used

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

static function stringContains(match:String, value:String, ?msg:String, ?pos:PosInfos):Void

Checks that the expected values is contained in value.

Parameters:
match:

the string value that must be contained in value

value:

the value to test

msg:

An optional error message. If not passed a default one is be used

pos:

Code position where the Assert call has been executed.

static function stringSequence(sequence:Array<String>, value:String, ?msg:String, ?pos:PosInfos):Void

Checks that the test string contains all the values in sequence in the order they are defined.

Parameters:
sequence:

the values to match in the string

value:

the value to test

msg:

An optional error message. If not passed a default one is be used

pos:

Code position where the Assert call has been executed.

static function warn(msg:String, ?pos:PosInfos):Void

Creates a warning message.

Parameters:
msg:

A mandatory message that justifies the warning.

pos:

Code position where the Assert call has been executed. Don't fill it unless you know what you are doing.

Toggle inherited fields