Section Syntax
Extend Lua language.
Summary
Return type | Function and summary |
---|---|
IncludeOnce(string file) Include file only and only if it hasn't been included before. | |
assertDialog(dialog, string errorMessage) Assert dialog is a Dialog, else throw errorMessage. | |
assertEquals(expr, expected, string Text) Assertion an expression or function call equals an expected result. | |
assertExpression(expr, string errorMessage) Assert an expression result is true. | |
assertFalse(b, string errorMessage) Assert b is false , else throw errorMessage. | |
assertFunction(f, string errorMessage) Assert f is a function, else throw errorMessage. | |
assertNegativeNumber(n, string errorMessage) Assert n is a negative number, else throw errorMessage. | |
assertNil(n, string errorMessage) Assert n is nil , else throw errorMessage. | |
assertNotEmptyString(s, string errorMessage) Assert s is a not-nil and not empty string, else throw errorMessage. | |
assertNotNil(n, string errorMessage) Assert n is not nil , else throw errorMessage. | |
assertNotZero(n, string errorMessage) Assert n is a number but not 0 , else throw errorMessage. | |
assertNumber(n, string errorMessage) Assert n is a not-nil number, else throw errorMessage. | |
assertPositiveNumber(n, string errorMessage) Assert n is a positive number, else throw errorMessage. | |
assertScore(s, string errorMessage) Assert s is a Score, else throw errorMessage. | |
assertString(s, string errorMessage) Assert s is a not-nil string (possibly empty), else throw errorMessage. | |
assertTable(t, string errorMessage) Assert t is a table, else throw errorMessage. | |
assertTrue(b, string errorMessage) Assert b is true , else throw errorMessage. | |
assertType(t, string expectedType, string errorMessage) Assert type(t) is a expectedType, else throw errorMessage. | |
table | case(string _type, _params, function _func, bool _break) Create a case for switch() function. |
table | default(function _func) Create a default case for switch() function. |
? | switch(term, table cases) switch..case. |
throw(string exception) Log (console and/or file) and stop the script with error() . |
Functions
IncludeOnce(string file)
Include file only and only if it hasn't been included before.
For that, you must always use IncludeOnce instead of Include. Include
is safe and fast for simple script that create constants and functions, but may load the CPU if included file performs heavy tasks.
Parameter | Type | Default | Description |
---|---|---|---|
file | string | Filename to include, file extension is not mandatory. |
- Usage
IncludeOnce("SMJavaCollections")
throw(string exception)
Log (console and/or file) and stop the script with error()
.
Inspired from Java's throw new Exception(...) but without 'catch' mechanism in Lua.
Parameter | Type | Default | Description |
---|---|---|---|
exception | string | Error message |
- See
- err
assertNumber(n, string errorMessage)
Assert n is a not-nil number, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
n | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected number, got ..." will be concatenated. |
assertPositiveNumber(n, string errorMessage)
Assert n is a positive number, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
n | |||
errorMessage | string | 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. |
assertNegativeNumber(n, string errorMessage)
Assert n is a negative number, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
n | |||
errorMessage | string | 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. |
assertNotZero(n, string errorMessage)
Assert n is a number but not 0
, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
n | |||
errorMessage | string | 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. |
assertString(s, string errorMessage)
Assert s is a not-nil string (possibly empty), else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
s | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected string, got ..." will be concatenated. |
assertNotEmptyString(s, string errorMessage)
Assert s is a not-nil and not empty string, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
s | |||
errorMessage | string | 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. |
- See
- assertString
assertTrue(b, string errorMessage)
Assert b is true
, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
b | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected true, got ..." will be concatenated. |
assertFalse(b, string errorMessage)
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
.
Parameter | Type | Default | Description |
---|---|---|---|
b | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected false, got ..." will be concatenated. |
assertTable(t, string errorMessage)
Assert t is a table, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
t | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected table, got ..." will be concatenated. |
assertFunction(f, string errorMessage)
Assert f is a function, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
f | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected function, got ..." will be concatenated. |
assertNil(n, string errorMessage)
Assert n is nil
, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
n | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected nil, got ..." will be concatenated. |
assertNotNil(n, string errorMessage)
Assert n is not nil
, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
n | |||
errorMessage | string | 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. |
assertExpression(expr, string errorMessage)
Assert an expression result is true.
This is the equivalent of Lua assert
function with more logging.
Parameter | Type | Default | Description |
---|---|---|---|
expr | Result of an expression, e.g. x < y | ||
errorMessage | string | Nothing is concatenated, the message must be full and self-explainatory. |
assertEquals(expr, expected, string Text)
Assertion an expression or function call equals an expected result.
This is useful for unit tests.
Parameter | Type | Default | Description |
---|---|---|---|
expr | Result of an expression | ||
expected | Expected result | ||
Text | string | to print on console and throw if expr ~= expected |
- Error
- If expr ~= expected
assertDialog(dialog, string errorMessage)
Assert dialog is a Dialog, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
dialog | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected Dialog, got ..." will be concatenated. |
assertScore(s, string errorMessage)
Assert s is a Score, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
s | |||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected Score, got ..." will be concatenated. |
assertType(t, string expectedType, string errorMessage)
Assert type(t)
is a expectedType, else throw errorMessage.
Parameter | Type | Default | Description |
---|---|---|---|
t | |||
expectedType | string | ||
errorMessage | string | Message to throw: by convention, the function where it happen, and the argument's or variable's name.": expected 'expectedType', got ..." will be concatenated. |
- 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 ...
switch(term, table cases)
switch..case.
Keyword switch doesn't exist in Lua? Here it is. Each case can test more things than C-like equals: equals, not equals, comparison (greater, lower), in a table, between two values (range). See SwitchCaseTest() for examples. Note that it creates tables, so switch is not best solution in huge loops where a set of if..elseif..elseif..end can do the job faster.
Parameter | Type | Default | Description |
---|---|---|---|
term | string or number, that can be compared | ||
cases | table | Table of cases created by case() and default() functions. |
- Return
- a value if cases' "body" functions return a value.
- Usage
switch(term, { case(...), case(...), default(...) }).
- Example
local result = switch(my_number, { case("<", 0, function(term) print(term.." is negative") return "negative" end), case("eq", 2, function(term) print(term.." = two") return "=2" end, false), case("in", {3,5,7,11,13,17}, function(term) print(term.." is a prime") return "prime" end), case("range", {20,29}, function(term) print("20 <= "..term.." <= 29") return "between 20 and 29" end), default( function(term) print(term.." doesn't match any case, this is default") return "The answer is... 42" end) })
case(string _type, _params, function _func, bool _break)
Create a case for switch() function.
Parameter | Type | Default | Description |
---|---|---|---|
_type | string | Accepted values are:
| |
_params | A number, a string, or a table:
| ||
_func | function | ||
_break | bool | true | true to stop after this case, false to continue and execute function of next case(s) until a case breaks. |
- Return
- table: a case for switch() function
- See
- switch