Section Assertions

Assertion functions, that extend Lua assert(...) standard function.

Assertions protect your code. Here is an unprotected code:

function Foo(str)
	return strlower(str)
end
Here you assume that str is a string, but what happen if str is a table, a Symbol?

Here is the protected version in Lua standard:

function Foo(str)
	assert(type(str) == "string", "Function Foo: str argument #1: string expected, got "..type(str))
	-- ... assert str is not empty
	assert(str ~= "", "... must not be empty")
	-- ... assert str contains a "W", why not?
	assert(strfind(str, "W", 1, true), "... must contains a 'W'")
	return strlower(str)
end

And now here is my recommanded code, that prints a stack strace to help you locate where the error happened:

function Foo(str)
	local stackName = "Foo(" .. tostring(str) .. ")"
	SMStack(stackName)
	assertNotEmptyString(str, "Foo, 'str' argument #1")
	assertExpression(strfind(str, "W", 1, true), "Foo, 'str' argument #1: must contain a 'W'")
	SMUnstack(stackName)
	return strlower(str)
end

Summary

Return typeFunction and summary
booleanassertBoolean(b, string errorMessage, boolean visual)
Assert b is a boolean (true or false, not nil), else throw errorMessage.
booleanassertDialog(dialog, string errorMessage, boolean visual)
Assert dialog is a Dialog, else throw errorMessage.
booleanassertEquals(expr, expected, string Text, boolean visual)
Assertion an expression or function call equals an expected result.
booleanassertExpression(expr, string errorMessage, boolean visual)
Assert an expression result is true.
booleanassertFalse(b, string errorMessage, boolean visual)
Assert b is false, else throw errorMessage.
booleanassertFunction(f, string errorMessage, boolean visual)
Assert f is a function, else throw errorMessage.
booleanassertInArray(value, table t, string text, boolean visual)
Assert a value is found in a table.
booleanassertNegativeNumber(n, string errorMessage, boolean visual)
Assert n is a negative number, else throw errorMessage.
booleanassertNil(n, string errorMessage, boolean visual)
Assert n is nil, else throw errorMessage.
booleanassertNotEmptyString(s, string errorMessage, boolean visual)
Assert s is a not-nil and not empty string, else throw errorMessage.
booleanassertNotNil(n, string errorMessage, boolean visual)
Assert n is not nil, else throw errorMessage.
booleanassertNotZero(n, string errorMessage, boolean visual)
Assert n is a number but not 0, else throw errorMessage.
booleanassertNumber(n, string errorMessage, boolean visual)
Assert n is a not-nil number, else throw errorMessage.
booleanassertPositiveNumber(n, string errorMessage, boolean visual)
Assert n is a positive number, else throw errorMessage.
booleanassertScore(s, string errorMessage, boolean visual)
Assert s is a Score, else throw errorMessage.
booleanassertString(s, string errorMessage, boolean visual)
Assert s is a not-nil string (possibly empty), else throw errorMessage.
booleanassertTable(t, string errorMessage, boolean visual)
Assert t is a table, else throw errorMessage.
booleanassertTrue(b, string errorMessage, boolean visual)
Assert b is true, else throw errorMessage.
booleanassertType(t, string expectedType, string errorMessage, boolean visual)
Assert type(t) is a expectedType, else throw errorMessage.

Functions

assertNumber(n, string errorMessage, boolean visual)

Assert n is a not-nil number, else throw errorMessage.

ParameterTypeDefaultDescription
n  n
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected number, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will stop with a console error message.
See
assertPositiveNumber
assertNegativeNumber
assertNotZero

assertPositiveNumber(n, string errorMessage, boolean visual)

Assert n is a positive number, else throw errorMessage.

ParameterTypeDefaultDescription
n  n
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected positive number, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertNumber
assertNegativeNumber
assertNotZero

assertNegativeNumber(n, string errorMessage, boolean visual)

Assert n is a negative number, else throw errorMessage.

ParameterTypeDefaultDescription
n  n
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected negative number, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertNumber
assertPositiveNumber
assertNotZero

assertNotZero(n, string errorMessage, boolean visual)

Assert n is a number but not 0, else throw errorMessage.

ParameterTypeDefaultDescription
n  n
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected not-zero number, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertNumber
assertPositiveNumber
assertNegativeNumber

assertString(s, string errorMessage, boolean visual)

Assert s is a not-nil string (possibly empty), else throw errorMessage.

ParameterTypeDefaultDescription
s  s
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected string, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertNotEmptyString

assertNotEmptyString(s, string errorMessage, boolean visual)

Assert s is a not-nil and not empty string, else throw errorMessage.

ParameterTypeDefaultDescription
s  s
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected not-empty string, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertString

assertBoolean(b, string errorMessage, boolean visual)

Assert b is a boolean (true or false, not nil), else throw errorMessage.

ParameterTypeDefaultDescription
b  b
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected boolean, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertTrue
assertFalse
assertExpression

assertTrue(b, string errorMessage, boolean visual)

Assert b is true, else throw errorMessage.

ParameterTypeDefaultDescription
b  b
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected true, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertFalse
assertNotNil
assertExpression

assertFalse(b, string errorMessage, boolean visual)

Assert b is false, else throw errorMessage.

Note: if you check a expression, e.g. x < y, Lua returns nil if the comparaison fails, instead of false.

ParameterTypeDefaultDescription
b  b
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected false, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertTrue
assertNil
assertExpression

assertTable(t, string errorMessage, boolean visual)

Assert t is a table, else throw errorMessage.

ParameterTypeDefaultDescription
t  t
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected table, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.

assertFunction(f, string errorMessage, boolean visual)

Assert f is a function, else throw errorMessage.

ParameterTypeDefaultDescription
f  f
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected function, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.

assertNil(n, string errorMessage, boolean visual)

Assert n is nil, else throw errorMessage.

ParameterTypeDefaultDescription
n  n
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected nil, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertNotNil
assertFalse
assertExpression

assertNotNil(n, string errorMessage, boolean visual)

Assert n is not nil, else throw errorMessage.

ParameterTypeDefaultDescription
n  n
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected not nil, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertNil
assertTrue
assertExpression

assertExpression(expr, string errorMessage, boolean visual)

Assert an expression result is true.

This is the equivalent of Lua assert function with more logging.

ParameterTypeDefaultDescription
expr  Result of an expression, e.g. x < y
errorMessagestring Nothing is concatenated, the message must be full and self-explainatory.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertTrue
assertNotNil

assertEquals(expr, expected, string Text, boolean visual)

Assertion an expression or function call equals an expected result.

This is useful for unit tests.

ParameterTypeDefaultDescription
expr  Result of an expression
expected  Expected result
Textstring to print on console and throw if expr ~= expected
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
Error
If expr ~= expected

assertInArray(value, table t, string text, boolean visual)

Assert a value is found in a table.

ParameterTypeDefaultDescription
value  number or string. nil always fails the assertion.
ttable List of values in which value is searched for.
textstring Text to print on console and throw if expr ~= expected
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
Error
If value not found in t

assertDialog(dialog, string errorMessage, boolean visual)

Assert dialog is a Dialog, else throw errorMessage.

ParameterTypeDefaultDescription
dialog  dialog
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected Dialog, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.

assertScore(s, string errorMessage, boolean visual)

Assert s is a Score, else throw errorMessage.

ParameterTypeDefaultDescription
s  s
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected Score, got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.

assertType(t, string expectedType, string errorMessage, boolean visual)

Assert type(t) is a expectedType, else throw errorMessage.

ParameterTypeDefaultDescription
t  t
expectedTypestring expectedType
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected 'expectedType', got ..." will be concatenated.
visualbooleanfalseVisual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails.
Your script must handle the returned value to stop gracefully.
Return
boolean: true if assertion is correct, nil (for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error.
See
assertDialog
assertFunction
assertNumber
assertScore
assertString
assertTable
Example

assertType(s, "Staff", "MyFunction, argument #1")
 --> if s is not a Staff, this print
 --> ERROR MyFunction, argument #1, expected Staff, got ...