Type Checking Assertions - dicksonlaw583/gmassert2 GitHub Wiki

Type Checking

These assertions from GMAssert_Typecheck checks whether the incoming value is or is not of a given type.

Hint: Assertions that check against a specific type (e.g. assert_is_string) have Feather parameter hints that explicitly state the received value's type. This will make Feather give early warnings in the IDE for potential mismatched types before the tests are actually run.

assert_is_array(got, [msg])

Assert that the gotten expression is an array.

Example

var array;
array[0] = 5;
array[1] = 8;
var str = "waahoo";
assert_is_array(array); //Assertion OK
assert_is_array(str); //Assertion failed

assert_is_defined(got, [msg])

Assert that the gotten expression is not undefined.

Example

var numA = 583,
    numB = undefined;
assert_is_defined(numA); //Assertion OK
assert_is_defined(numB); //Assertion failed

assert_is_instance_of(got, typeName, [msg])

Assert that the gotten expression is of the given type. If got is a struct, typeName can be "struct" to accept any struct or the string name of the constructor to accept only structs made with it. If got is any other type, typeName should be one of the strings returned by the built-in typeof() function.

Example

function Vector2(_x, _y) constructor {
	x = _x;
	y = _y;
	mag = function() { return sqrt(x*x+y*y); };
}
var myv2 = new Vector(369, 246);
assert_is_instance_of(243, "number"); //Assertion OK
assert_is_instance_of(243, "int64"); //Assertion failed
assert_is_instance_of({x: 294, y: 588}, "struct"); //Assertion OK
assert_is_instance_of({x: 294, y: 588), "Vector2"}); //Assertion failed
assert_is_instance_of(myv2, "struct"); //Assertion OK
assert_is_instance_of(myv2, "Vector2"); //Assertion OK

assert_is_method(got, [msg])

Assert that the gotten expression is a method (function).

Example

var helloMessage = function(name){ show_debug_message("Hello, " + name + "!"); };
var helloValue = "Hello, Dolly!";
assert_is_method(helloMessage); //Assertion OK
assert_is_method(helloValue); //Assertion failed

assert_is_real(got, [msg])

Assert that the gotten expression is a real number.

Example

var num = pi;
assert_is_real(num); //Assertion OK
assert_is_real(string(num)); //Assertion failed

assert_is_string(got, [msg])

Assert that the gotten expression is a string.

Example

var str = "waahoo",
    num = pi;
assert_is_string(str); //Assertion OK
assert_is_string(num); //Assertion failed

assert_is_struct(got, [msg])

Assert that the gotten expression is a struct.

Example

var myStruct = {x: 392, y: 784};
var myArray = ["foo", "bar", "baz"];
assert_is_struct(myStruct); //Assertion OK
assert_is_struct(myArray); //Assertion failed

assert_is_undefined(got, [msg])

Assert that the gotten expression is undefined.

Example

var numA = undefined,
    numB = 583;
assert_is_undefined(numA); //Assertion OK
assert_is_undefined(numB); //Assertion failed

assert_isnt_array(got, [msg])

Assert that the gotten expression is not an array.

Example

var array;
array[0] = 5;
array[1] = 8;
var str = "waahoo";
assert_isnt_array(str); //Assertion OK
assert_isnt_array(array); //Assertion failed

assert_isnt_defined(got, [msg])

Assert that the gotten expression is undefined.

Example

var numA = 583,
    numB = undefined;
assert_isnt_defined(numB); //Assertion OK
assert_isnt_defined(numA); //Assertion failed

assert_isnt_instance_of(got, typeName, [msg])

Assert that the gotten expression isn't the given type. If got is a struct, typeName can be "struct" to accept any struct or the string name of the constructor to accept only structs made with it. If got is any other type, typeName should be one of the strings returned by the built-in typeof() function.

Example

function Node(_value) constructor {
	value = _value;
	children = [];
	addChild = function(_childValue) {
		children[array_length(children)] = new Node(_childValue);
	}
}
var myNode = new Node("foo");
assert_isnt_instance_of(myNode.value, "number"); //Assertion OK
assert_isnt_instance_of(myNode.value, "string"); //Assertion failed
assert_isnt_instance_of(myNode.value, "struct"); //Assertion OK
assert_isnt_instance_of(myNode, "struct"); //Assertion failed
assert_isnt_instance_of(myNode.value, "Node"); //Assertion OK
assert_isnt_instance_of(myNode, "Node"); //Assertion failed

assert_isnt_method(got, [msg])

Assert that the gotten expression isn't a method.

Example

var likeMessage = function(nameA, nameB){ return nameA + " likes " + nameB + "."; };
var likeValue = "Alice likes Bob";
assert_isnt_method(likeValue); //Assertion OK
assert_isnt_method(likeMessage); //Assertion failed

assert_isnt_real(got, [msg])

Assert that the gotten expression is not a real number.

Example

var str = "3.14";
assert_isnt_real(str); //Assertion OK
assert_isnt_real(real(str)); //Assertion failed

assert_isnt_string(got, [msg])

Assert that the gotten expression is not a string.

Example

var str = "waahoo",
    num = pi;
assert_isnt_string(num); //Assertion OK
assert_isnt_string(str); //Assertion failed

assert_isnt_struct(got, [msg])

Assert that the gotten value is not a struct.

Example

assert_isnt_struct(583); //Assertion OK
assert_isnt_struct({foo: 583}); //Assertion failed

assert_isnt_undefined(got, [msg])

Assert that the gotten expression isn't undefined.

Example

var numA = 583,
    numB = undefined;
assert_isnt_undefined(numA); //Assertion OK
assert_isnt_undefined(numB); //Assertion failed