Foundations.Assert
Provides a set of Assert functions, for checking code pre-conditions and post-conditions.
Using Foundations.Assert
Assert and Require Versions
For each function, there are two versions. For example:
- Assert.assertEqual(obj1, obj2, msg, params)
- Assert.requireEqual(obj1, obj2, msg, params)
If the assertion evaluates to false, the "assert" function logs an error message and returns false while the "require" one throws an exception.
Messages are logged to "/var/log/messages." You can use the "tail" utility to view messages in real-time.
To monitor logged messages in real-time:
- Open a shell on the device and run:
tail -f /var/log/messages
The "-f" option causes tail to display the last 10 lines of messages
and append new lines to the display as they are added.
To show output for just your app:
- Open a shell on the device and run:
tail -f /var/log/messages | grep packageid
Note that using "tail" like this works until the log file is rotated; then you will need to reissue the command.
Using the "msg" and "params" arguments
Each function can take optional "msg" and "params" arguments. The msg parameter is used when logging an error message or throwing an error. It can also contain "#{...}" placeholders for values from the params object. For example:
msg = "Expected a sum greater than #{count}, but it was #{amount}";
The params argument can then be used to situationally fill the placeholders. For example:
params = {count: 3, amount: 0};
Assert Functions
Function | Description |
---|---|
Assert.assert | Logs an error if the expression is not true. |
Assert.assertArray | Logs an error if the expression is not an array. |
Assert.assertClass | Logs an error if an object does not have a given constructor. |
Assert.assertDefined | Logs an error if the argument is not defined. |
Assert.assertEqual | Logs an error if the expressions are not equal (==). |
Assert.assertError | Logs an error if a function does not throw the expected error. |
Assert.assertFalse | Logs an error if the expression is not false. |
Assert.assertFunction | Logs an error if the argument is not a function. |
Assert.assertIdentical | Logs an error if the expressions are not identical (===), that is, if they do not have the same type and value. |
Assert.assertJSONObject | Logs an error if the argument does not evaluate (when run through Object.prototype.toString()) to '[object Object]' or '[object Array]' (an object of an array). |
Assert.assertMatch | Logs an error if a regular expression pattern has no matches in a given string. |
Assert.assertNumber | Logs an error if the argument is not a number. |
Assert.assertObject | Logs an error if the argument is not an object. |
Assert.assertProperty | Logs an error if the object does not have a particular property or properties. |
Assert.assertString | Logs an error if the argument is not a string. |
Assert.require | Throws an error if the expression is not true |
Assert.requireArray | Throws an error if the expression is not an array. |
Assert.requireClass | Throws an error if an object does not have a given constructor. |
Assert.requireDefined | Throws an error if the argument is not defined. |
Assert.requireEqual | Throws an error if the expressions are not equal (==). |
Assert.requireError | Throws an error if a function does not throw the expected error. |
Assert.requireFalse | Throws an error if the expression is not false. |
Assert.requireFunction | Throws an error if the argument is not a function. |
Assert.requireIdentical | Throws an error if the expressions are not identical (===), that is, if they do not have the same type and value. |
Assert.requireJSONObject | Throws an error if the argument does not evaluate (when run through Object.prototype.toString()) to '[object Object]' or '[object Array]' (an object of an array). |
Assert.requireMatch | Throws an error if the pattern does not match. |
Assert.requireNumber | Throws an error if the argument is not a number. |
Assert.requireObject | Throws an error if the argument is not an object. |
Assert.requireProperty | Throws an error if the object does not have a particular property or properties. |
Assert.requireString | Throws an error if the argument is not a string. |
Assert.assert
Logs an error if the expression is not true.
Syntax
Assert.assert(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | any | Expression to test |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Returns the expression passed.
Logs an error if the expression does not evaluate to true.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var exp=""; var msg = "Does not evaluate to true"; Mojo.Log.info("assert = "+ AssertUtils.assert(exp, msg));
Example Output
Error: Does not evaluate to true assert = ""
Assert.assertArray
Logs an error if the expression is not an array.
Syntax
boolean Assert.assertArray(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | any | Expression to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the expression is not an array.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var ary = [{"a":"b"}]; var notAry = "this is a string"; var msg = "Not an array"; Mojo.Log.info("assert = "+ AssertUtils.assertArray(ary, msg)); Mojo.Log.info("assert = "+ AssertUtils.assertArray(notAry, msg));
Example Output
assert = true Error: Not an array assert = false
Assert.assertClass
Logs an error if an object does not have a given constructor.
Syntax
boolean Assert.assertClass(obj, constructor, msg, params)
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | any | Object to test for constructor. |
constructor | Yes | any | Asserted constructor. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the constructor is not the passed object's constructor.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; function cat(name) { this.name = name; this.talk = function() { alert( this.name + " say meeow!" ); } } cat1 = new cat("felix"); Mojo.Log.info("assert = "+ AssertUtils.assertClass(cat1, cat));
Example Output
assert = true
Assert.assertDefined
Logs an error if the argument is not defined.
Syntax
boolean Assert.assertDefined(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | any | Expression to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the argument is not defined.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var undefObj; Mojo.Log.info("assert = "+ AssertUtils.assertDefined(undefObj, "Not defined"));
Example Output
Error: Not defined assert = false
Assert.assertEqual
Logs an error if the expressions are not equal (==).
Syntax
boolean Assert.assertEqual(exp1, exp2, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp1 | Yes | any | Expression to test. |
exp2 | Yes | any | Expression to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the expressions are not equal (==).
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var str1 = "Separate but equal"; var str2 = "Separate but equal not"; Mojo.Log.info("assert = "+ AssertUtils.assertEqual(str1, str2));
Example Output
Error: Assert.assertEqual: Separate but equal != Separate but equal not assert = false
Assert.assertError
Logs an error if the function does not throw the expected error.
Syntax
boolean Assert.assertError(context, func, args, error, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
context | Yes | any |
The "this" value for function. This is the first argument to an apply method. The apply() method invokes the passed function and passes context as the first parameter. The runtime uses this parameter for the this reference.
|
func | Yes | any | Function to run. |
args | Yes | any object |
Array of arguments. Second argument to apply() method.
|
error | Yes | any object | Expected error. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the function does not throw the expected error.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var context = { errmsg1: "Err1", errmsg2: "Err2" }; var args = ["User message 1", "User message 2" ]; function f(message) { throw this.errmsg1; } Mojo.Log.info("assert = "+ AssertUtils.assertError(context, f, args, "Err1" )); Mojo.Log.info("assert = "+ AssertUtils.assertError(context, f, args, "UnknownErr" ));
Example Output
assert = true Error: Assert.assertError: error thrown was 'Err1', instead of 'UnknownErr' assert = false
Assert.assertFalse
Logs an error if the expression is not false.
Syntax
boolean Assert.assertFalse(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | string | Expression to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the expression is not false.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var expFalse = ""; var expTrue = "something"; var msg = "Does not evaluate to false"; Mojo.Log.info("assert = "+ AssertUtils.assertFalse(expFalse, msg)); Mojo.Log.info("assert = "+ AssertUtils.assertFalse(expTrue, msg));
Example Output
assert = true Error: Does not evaluate to false assert = false
Assert.assertFunction
Logs an error if the argument is not a function.
Syntax
boolean Assert.assertFunction(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | Function | Object to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the argument is not a function.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var myFunc = new Function("5+2"); var myStr = "not a function"; var msg = "Is not a function"; Mojo.Log.info("assert = "+ AssertUtils.assertFunction(myFunc, msg)); Mojo.Log.info("assert = "+ AssertUtils.assertFunction(myStr, msg));
Example Output
assert = true Error: Is not a function asset = false
Assert.assertIdentical
Logs an error if the expressions are not identical (===). Both type and value have to be the same.
Syntax
boolean Assert.assertIdentical(exp1, exp2, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp1 | Yes | any | Expression to test. |
exp2 | Yes | any | Expression to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the expressions are not identical (===).
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var a = "3"; var b = 3; var c = "3"; var d = new String("3"); Mojo.Log.info("assert = "+ AssertUtils.assertIdentical(a,c,"Not identical")); Mojo.Log.info("assert = "+ AssertUtils.assertIdentical(a,b,"Not identical")); Mojo.Log.info("assert = "+ AssertUtils.assertIdentical(a,d,"Not identical"));
Example Output
assert = true Error: Not identical assert = false Error: Not identical assert = false
Assert.assertJSONObject
Logs an error if the argument does not evaluate (when run through Object.prototype.toString()) to '[object Object]' or '[object Array]' (an object of an array).
Syntax
boolean Assert.assertJSONObject(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | string | Object to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the argument does not evaluate to '[object Object]' or '[object Array]' (an object of an array).
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var jobj = [{ "fname": "Mabel", "lname":"Syrup", "isMember":true}]; Mojo.Log.info("assert = "+ AssertUtils.assertJSONObject(jobj, "Not an object or object in an array")); var notObj = "Not a valid object"; Mojo.Log.info("assert = "+ AssertUtils.assertJSONObject(notObj, "Not an object or object in an array"));
Example Output
assert = true Error: Not an object or object in an array assert = false
Assert.assertMatch
Logs an error if a regular expression pattern has no matches in a given string.
Syntax
string Assert.assertMatch(str, pattern, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
str | Yes | string | String to match against. |
pattern | Yes | string | Pattern to match (regular expression). |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
String with comma-separated matches or null.
Logs an error if the pattern does not match.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var str="The rain in SPAIN stays mainly in the plain"; var patt1=/ain/gi; // Search entire string for "ain", ignore case Mojo.Log.info("assert = "+ AssertUtils.assertMatch(str, patt1, "Not a match"));
Example Output
assert = ain,AIN,ain,ain
Assert.assertNumber
Logs an error if the argument is not a number.
Syntax
boolean Assert.assertNumber(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | any | Object to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the argument is not a number.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var num=32; Mojo.Log.info("assert = "+ AssertUtils.assertNumber(num, "Not a number")); var str="this is a string"; Mojo.Log.info("assert = "+ AssertUtils.assertNumber(str, "Not a number"));
Example Output
assert = true Error: Not a number assert = false
Assert.assertObject
Logs an error if the argument is not an object.
Syntax
boolean Assert.assertObject(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | any | Object to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the argument is not an object.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var txt = new String("this is an object"); Mojo.Log.info("assert = "+ AssertUtils.assertObject(txt, "Not an object")); var str="this is not an object"; Mojo.Log.info("assert = "+ AssertUtils.assertObject(str, "Not an object"));
Example Output
assert = true Error: Not an object assert = false
Assert.assertProperty
Logs an error if the object does not have a particular property or properties.
Syntax
boolean Assert.assertProperty(obj, props, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | any object | Object to test. |
props | Yes | any object | Object containing properties to validate: {property1:value1, property2:value2}. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the object does not have a particular property or properties.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var home = new Object; home.type= "colonial"; home.status = "foreclosed"; var props = {"status":"foreclosed"}; Mojo.Log.info("assert = "+ AssertUtils.assertProperty(home, props, "Not a property")); props = "foreclosed"; Mojo.Log.info("assert = "+ AssertUtils.assertProperty(home, props, "Not a property"));
Example Output
assert = true Error: Not a property assert = false
Assert.assertString
Logs an error if the argument is not a string.
Syntax
boolean Assert.assertString(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | string | Object to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
true or false
Logs an error if the argument is not a string.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var num=32; Mojo.Log.info("assert = "+ AssertUtils.assertString(num, "Not a string")); var str="this is a string"; Mojo.Log.info("assert = "+ AssertUtils.assertString(str, "Not a string"));
Example Output
Error: Not a string assert = false assert = true
Assert.require
Syntax
Assert.require(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | any | Expression to test. |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the expression is not true.
Example
var exp = ""; try { AssertUtils.require(exp, "Does not evaluate to true"); } catch (err) { Mojo.Log.info("Err = " + err); }
Example Output
Err = Error: Does not evaluate to true
Assert.requireArray
Throws an error if the expression is not an array.
Syntax
Assert.requireArray(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | any | Expression to test. |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the argument is not an array.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var ary = [{"a":"b"}]; var notAry = "this is a string"; var msg = "Not an array"; try { AssertUtils.requireArray(ary, msg); Mojo.Log.info("ary is Array"); } catch (err) { Mojo.Log.info("Err = " + err); } try { AssertUtils.requireArray(notAry, msg); Mojo.Log.info("notAry is Array"); } catch (err) { Mojo.Log.info("Err = " + err); }
Example Output
ary is Array Err = Error: Not an array
Assert.requireClass
Syntax
Assert.requireClass(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | any | Object to test. |
constructor | Yes | any | Required constructor. |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the constructor is not the passed object's constructor.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; function cat(name) { this.name = name; this.talk = function() { alert( this.name + " say meeow!" ); } } cat1 = new cat("felix"); try { AssertUtils.requireClass(cat1, cat); Mojo.Log.info("cat1 has cat constructor"); } catch (err) { Mojo.Log.info("requireClass throws " + err); }
Example Output
cat1 has cat constructor
Assert.requireDefined
Throws an error if the passed argument is not defined.
Syntax
Assert.requireDefined(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | any | Expression to test. |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the argument is not defined.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var undefObj; try { AssertUtils.requireDefined(indefObj, "Not defined"); Mojo.Log.info("Object is defined"); } catch (err) { Mojo.Log.info("requireDefined throws " + err); }
Example Output
requireDefined throws ReferenceError: indefObj is not defined
Assert.requireEqual
Throws an error if two arguments are not equal (==).
Syntax
Assert.requireEqual(exp1, exp2, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp1 | Yes | any | Expression to test. |
exp2 | Yes | any | Expression to test. |
msg | No | string |
message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the expressions are not equal (==).
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var str1 = "Separate but equal"; var str2 = "Separate but equal not"; try { AssertUtils.requireEqual(str1, str2, "Not equal"); Mojo.Log.info("Objects are equal"); } catch (err) { Mojo.Log.info("requireEqual throws " + err); }
Example Output
requireEqual throws Error: Not equal
Assert.requireError
Throws an error if a function does not throw an expected error.
Syntax
Assert.requireError(context, func, args, error, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
context | Yes | string |
The "this" value for function. This is the first argument to an apply method. The apply() method invokes the passed function and passes context as the first parameter. The runtime uses this parameter for the this reference.
|
func | Yes | string | Function to run. |
args | Yes | any | Array of arguments |
error | Yes | string | Expected error |
msg | No | string |
message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the function does not throw an expected error.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var context = { errmsg1: "Err1", errmsg2: "Err2" }; var args = ["User message 1", "User message 2" ]; function f(message) { throw this.errmsg1; } try { AssertUtils.requireError(context, f, args, "Err1", "Does not throw expected error"); Mojo.Log.info("Function throws expected error"); } catch (err) { Mojo.Log.info("requireError throws " + err); }
Example Output
Function throws expected error
Assert.requireFalse
Throws an error if the expression does not evaluate to false.
Syntax
Assert.requireFalse(exp, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp | Yes | any | Expression to test |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the expression is not false.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var expFalse = ""; var expTrue = "something"; var msg = "Does not evaluate to false"; try { AssertUtils.requireFalse(expFalse, msg); Mojo.Log.info("Evaluates to false"); } catch (err) { Mojo.Log.info("requireFalse throws " + err); } try { AssertUtils.requireFalse(expTrue, msg); Mojo.Log.info("Evaluates to false"); } catch (err) { Mojo.Log.info("requireFalse throws " + err); }
Example Output
Evaluates to false requireFalse throws Error: Does not evaluate to false
Assert.requireFunction
Syntax
Assert.requireFunction(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | any | Object to test. |
msg | Yes | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | Yes | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the argument is not a function.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var myFunc = new Function("5+2"); var myStr = "not a function"; var msg = "Is not a function"; try { AssertUtils.requireFunction(myFunc, msg); Mojo.Log.info("Is a function."); } catch (err) { Mojo.Log.info("requireFunction throws " + err); } try { AssertUtils.requireFalse(myStr, msg); Mojo.Log.info("Is a function."); } catch (err) { Mojo.Log.info("requireFunction throws " + err); }
Example Output
Is a function. requireFunction throws Error: Is not a function
Assert.requireIdentical
Throws an error if two expressions do not have the same value and type (===).
Syntax
Assert.requireIdentical(exp1, exp2, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
exp1 | Yes | string | Expression to test. |
exp2 | Yes | string | Expression to test. |
msg | Yes | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | Yes | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the expressions are not identical (===).
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var a = "3"; var b = 3; var c = "3"; var d = new String("3"); try { AssertUtils.requireIdentical(a,c,"Not identical"); Mojo.Log.info("Identical"); } catch (err) { Mojo.Log.info("requireIdentical throws " + err); } try { AssertUtils.requireIdentical(a,b,"Not identical"); Mojo.Log.info("Identical"); } catch (err) { Mojo.Log.info("requireIdentical throws " + err); } try { AssertUtils.requireIdentical(a,d,"Not identical"); Mojo.Log.info("Identical"); } catch (err) { Mojo.Log.info("requireIdentical throws " + err); }
Example Output
Identical requireIdentical throws Error: Not identical requireIdentical throws Error: Not identical
Assert.requireJSONObject
Throws an error if the passed object, when run through Object.prototype.toString(), does not evaluate to '[object Object]' or '[object Array]'.
Syntax
Assert.requireJSONObject(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | string | Object to test. |
msg | No | string |
Message to log. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the argument does not evaluate to '[object Object]' or '[object Array]' (an object of an array).
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; Mojo.Log.info("assert = "+ JSON.stringify(AssertUtils.assertJSONObject(jobj, "Not an object or object in an array"))); try { AssertUtils.requireJSONObject(jobj, "Not an object or object in an array"); Mojo.Log.info("Is an object or object in an array"); } catch (err) { Mojo.Log.info("requireJSONObject throws " + err); } var notObj = "Not a valid object"; try { AssertUtils.requireJSONObject(notObj, "Not an object or object in an array"); Mojo.Log.info("Is an object or object in an array"); } catch (err) { Mojo.Log.info("requireJSONObject throws " + err); }
Example Output
Is an object or object in an array requireJSONObject throws Error: Not an object or object in an array
Assert.requireMatch
Throws an error if there not a match between a regular expression and a string.
Syntax
Assert.requireMatch(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | string | Object to test. |
obj | Yes | string | Object to test. |
pattern | Yes | string | Pattern to match (regular expression). |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the pattern does not match.
Example
var AssertUtils = libraries["foundations"].Assert; var str = "The rain in SPAIN stays mainly in the plain"; var patt1 = /ain/gi; //... Search entire string for "ain", ignore case try { AssertUtils.requireMatch(str, patt1, "Not match"); Mojo.Log.info("Match"); } catch (err) { Mojo.Log.info("requireMatch throws " + err); }
Example Output
Match
Assert.requireNumber
Throws an error if the argument is not a number.
Syntax
Assert.requireNumber(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | any | Object to test. |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the argument is not a number.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var num=32; try { AssertUtils.requireNumber(num, "Not a number"); Mojo.Log.info("It is a number."); } catch (err) { Mojo.Log.info("requireNumber throws " + err); } var str="this is a string"; try { AssertUtils.requireNumber(str, "Not a number"); Mojo.Log.info("It is a number.") } catch (err) { Mojo.Log.info("requireNumber throws " + err); }
Example Output
It is a number. requireNumber throws Error: Not a number
Assert.requireObject
Throws an error if a passed argument is not an object.
Syntax
Assert.requireObject(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | string | Object to test. |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the argument is not an object.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var txt = new String("this is an object"); var str="this is not an object"; try { AssertUtils.requireObject(txt, "Not an object"); Mojo.Log.info("It is an object."); } catch (err) { Mojo.Log.info("requireObject throws " + err); } try { AssertUtils.requireObject(str, "Not an object"); Mojo.Log.info("It is an object."); } catch (err) { Mojo.Log.info("requireObject throws " + err); }
Example Output
It is an object. requireObject throws Error: Not an object
Assert.requireProperty
Syntax
Assert.requireProperty(obj, props, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | string | Object to test. |
props | Yes | string |
An object containing properties to validate: {property1:value1, property2:value2} .
|
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the object does not have a particular property set as expected.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var home = new Object(); home.type= "colonial"; home.status = "foreclosed"; var props = {"status":"foreclosed"}; try { AssertUtils.requireProperty(home, props, "Not a property"); Mojo.Log.info("Property is there"); } catch (err) { Mojo.Log.info("requireProperty throws " + err); } props = "foreclosed"; try { AssertUtils.requireProperty(home, props, "Not a property"); Mojo.Log.info("Property is there"); } catch (err) { Mojo.Log.info("requireProperty throws " + err); }
Example Output
Property is there requireProperty throws ReferenceError: props is not defined requireProperty throws Error: Not a property
Assert.requireString
Throws an error if the argument is not a string.
Syntax
Assert.requireString(obj, msg, params);
Parameters
Argument | Required | Type | Description |
---|---|---|---|
obj | Yes | string | Object to test. |
msg | No | string |
Message to throw. This can contain "#{...}" placeholders for values from the params object, i.e., "Expected a sum greater than #{count}, but it was #{amount}".
|
params | No | any object | Parameters to fill in "msg" placeholders, i.e., {count: 3, amount: 0}. |
Returns
Throws an error if the argument is not a string.
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var AssertUtils = libraries["foundations"].Assert; var num=32; try { AssertUtils.requireString(num, "Not a string"); Mojo.Log.info("Is a string"); } catch (err) { Mojo.Log.info("requireString throws " + err); } var str="this is a string"; try { AssertUtils.requireString(str, "Not a string"); Mojo.Log.info("Is a string"); } catch (err) { Mojo.Log.info("requireString throws " + err); }
Example Output
requireString throws Error: Not a string Is a string