Exception Assertions - dicksonlaw583/gmassert2 GitHub Wiki

Exception Assertions

These assertions from GMAssert_Exceptions checks whether the given runnable payload throws or does not throw certain exceptional values when run.

A valid payload can be one of the following:

  • A function taking 0 arguments
  • A 2-entry array consisting of a function taking 1 argument and a value to pass into it

assert_doesnt_throw(payload, thrown, [msg])

Assert that running the payload would not throw a value equivalent to thrown.

Example

var nthPercentage = function(n) {
	if (n == 0) {
		throw "Division by Zero";
	}
	return 100/n;
};
assert_doesnt_throw([nthPercentage, 5], "Division by Zero"); //Assertion OK
assert_doesnt_throw([nthPercentage, 0], "Division by Zero"); //Assertion failed

assert_doesnt_throw_instance_of(payload, typeName, [msg])

Assert that running the payload would not throw a value of the given type. The type can be either a value that typeof() can return or the name of a constructor.

Example

var nthPercentage = function(n) {
	if (n == 0) {
		throw "Division by Zero";
	}
	return 100/n;
};
assert_doesnt_throw_instance_of([nthPercentage, 5], "string"); //Assertion OK
assert_doesnt_throw_instance_of([nthPercentage, 0], "string"); //Assertion failed

assert_not_throwless(payload, [msg])

Assert that running the given payload throws something.

Example

assert_not_throwless(function() { throw "foo"; }); //Assertion OK
assert_not_throwless(function() { return "foo"; }); //Assertion failed

assert_throwless(payload, [msg])

Assert that the payload runs smoothly without throwing anything.

Example

var nthPercentage = function(n) {
	if (n == 0) {
		throw "Division by Zero";
	}
	return 100/n;
};
assert_throwless([nthPercentage, 5]); //Assertion OK
assert_throwless([nthPercentage, 0]); //Assertion failed

assert_throws(payload, thrown, [msg])

Assert that running the payload throws a value equivalent to thrown.

Example

var nthPercentage = function(n) {
	if (n == 0) {
		throw "Division by Zero";
	}
	return 100/n;
};
assert_throws([nthPercentage, 0], "Division by Zero"); //Assertion OK
assert_throws([nthPercentage, 0], "division by zero"); //Assertion failed

assert_throws_instance_of(payload, typeName, [msg])

Assert that running the payload throws a value of the type typeName, which can be either a value that typeof() can return or the name of a constructor.

Example

var nthPercentage = function(n) {
	if (n == 0) {
		throw "Division by Zero";
	}
	return 100/n;
};
assert_throws_instance_of([nthPercentage, 0], "string"); //Assertion OK
assert_throws_instance_of([nthPercentage, 0], "undefined"); //Assertion failed