var _assertNS = {
isArray : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isArray");
}
return Array.isArray(value);
},
isBoolean : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isBoolean");
}
return typeof value === "boolean";
},
isDate : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isDate");
}
return this.isObject(value) &&
value instanceof Date &&
!isNaN(Date.parse(value));
},
isError : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isError");
}
return this.isObject(value) && value instanceof Error;
},
isFunction : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isFunction");
}
return typeof value === 'function';
},
isInstanceOf : function(value, constructor) {
if (arguments.length < 2) {
throw new Error("Arguments missing at isInstanceOf");
}
try {
if (value instanceof constructor) {
return true;
} else {
return false;
}
} catch (exception) {
return false;
}
},
isNull : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isNull");
}
return value === null;
},
isNullOrUndefined : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isNullOrUndefined");
}
return this.isNull(value) || this.isUndefined(value);
},
isNumber : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isNumber");
}
return typeof value === "number";
},
isObject : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isObject");
}
return typeof value === "object" && value !== null;
},
isPlainObject : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isPlainObject");
}
return value.constructor === Object;
},
isRegExp : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isRegExp");
}
if (value.constructor) {
return value.constructor.name === "RegExp";
} else {
return false;
}
},
isString : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isString");
}
return typeof value === "string";
},
isTypeOf : function(value, type) {
if (arguments.length < 2) {
throw new Error("Arguments missing at isTypeOf");
}
return typeof value === type;
},
isUndefined : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isUndefined");
}
return value === undefined;
},
isUndefinedOrNull : function(value) {
if (arguments.length === 0) {
throw new Error("Argument missing at isUndefinedOrNull");
}
return this.isUndefined(value) || this.isNull(value);
},
deepEqual : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at deepEqual");
}
if (!this._isDeepEqual(actual, expected)) {
if (this.isUndefined(message)) {
this._failOut("deepEqual(" + JSON.stringify(actual) + " == " +
JSON.stringify(expected) +
")");
} else {
this._failOut(message);
}
} else {
this._passOut("deepEqual(" + JSON.stringify(actual) + " == " +
JSON.stringify(expected) + ")");
}
},
doesNotMatch : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at doesNotMatch");
}
this.notMatch(actual, expected, message);
},
equal : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at equal");
}
if (actual != expected) {
if (this.isUndefined(message)) {
this._failOut("equal(" + String(actual) + " == " +
String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("equal(" + String(actual) + " == " +
String(expected) + ")");
}
},
fail : function(message) {
if (this.isUndefined(message)) {
this._failOut();
throw new Error("AssertionFail");
} else {
this._failOut(message);
throw new Error("AssertionFail: " + message);
}
},
greaterThan : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at match");
}
if (actual <= expected) {
if (this.isUndefined(message)) {
this._failOut("greaterThan(" + String(actual) + " > " +
String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("greaterThan(" + String(actual) + " > " +
String(expected) + ")");
}
},
greaterThanOrEqual : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at match");
}
if (actual < expected) {
if (this.isUndefined(message)) {
this._failOut("greaterThanOrEqual(" + String(actual) +
" >= " + String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("greaterThanOrEqual(" + String(actual) +
" >= " + String(expected) + ")");
}
},
lessThan : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at match");
}
if (actual >= expected) {
if (this.isUndefined(message)) {
this._failOut("lessThan(" + String(actual) + " < " +
String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("lessThan(" + String(actual) + " < " +
String(expected) + ")");
}
},
lessThanOrEqual : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at match");
}
if (actual > expected) {
if (this.isUndefined(message)) {
this._failOut("lessThanOrEqual(" + String(actual) + " <= " +
String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("lessThanOrEqual(" + String(actual) + " <= " +
String(expected) + ")");
}
},
match : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at match");
}
if (!this.isRegExp(expected)) {
throw new Error("expected argument must be RegExp");
}
if (!expected.test(actual)) {
if (this.isUndefined(message)) {
this._failOut("The input '" + String(actual) +
"' did not match the regular expression " + String(expected));
} else {
this._failOut(message);
}
} else {
this._passOut("The input '" + actual +
"' match the regular expression " + String(expected));
}
},
notDeepEqual : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at notDeepEqual");
}
if (this._isDeepEqual(actual, expected)) {
if (this.isUndefined(message)) {
this._failOut("notDeepEqual(" + JSON.stringify(actual) +
" != " + JSON.stringify(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("notDeepEqual(" + JSON.stringify(actual) + " != " +
JSON.stringify(expected) + ")");
}
},
notEqual : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at notEqual");
}
if (actual == expected) {
if (this.isUndefined(message)) {
this._failOut("notEqual(" + String(actual) + " != " +
String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("notEqual(" + String(actual) + " != " +
String(expected) + ")");
}
},
notMatch : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at notMatch");
}
if (!this.isRegExp(expected)) {
throw new Error("expected argument must be RegExp");
}
if (expected.test(actual)) {
if (this.isUndefined(message)) {
this._failOut("The input '" + String(actual) +
"' did match the regular expression " + String(expected));
} else {
this._failOut(message);
}
} else {
this._passOut("The input '" + String(actual) +
"' did not match the regular expression " +
String(expected));
}
},
notOk : function(value, message) {
if (arguments.length === 0) {
throw new Error("Arguments missing at notOk");
}
if (value) {
if (this.isUndefined(message)) {
this._failOut("notOk(" + String(value) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("notOk(" + String(value) + ")");
}
},
notStrictEqual : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at notStrictEqual");
}
if (actual === expected) {
if (this.isUndefined(message)) {
this._failOut("notStrictEqual(" + String(actual) + " !== " +
String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("notStrictEqual(" + String(actual) + " !== " +
String(expected) + ")");
}
},
ok : function(value, message) {
if (arguments.length === 0) {
throw new Error("Arguments missing at ok");
}
if (!value) {
if (this.isUndefined(message)) {
this._failOut("ok(" + String(value) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("ok(" + String(value) + ")");
}
},
strictEqual : function(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at strictEqual");
}
if (actual !== expected) {
if (this.isUndefined(message)) {
this._failOut("strictEqual(" + String(actual) + " === " +
String(expected) + ")");
} else {
this._failOut(message);
}
} else {
this._passOut("strictEqual(" + String(actual) + " === " +
String(expected) + ")");
}
},
throws : function(fn, expectedError, message) {
if (arguments.length < 2) {
throw new Error("Arguments missing at throws");
}
var thrown = false;
if (this.isFunction(fn)) {
var thrownError;
try {
fn();
} catch (err) {
thrown = true;
thrownError = err;
}
if (thrown && expectedError) {
thrown = this._errorMatches(thrownError, expectedError);
}
if (!thrown) {
if (this.isUndefined(message)) {
if (!(this.isUndefined(thrownError))) {
this._failOut("throws get the unexpected error " +
thrownError.name + "(" + thrownError.message + ")");
} else {
this._failOut("throws get no error");
}
} else {
this._failOut(message);
}
} else {
this._passOut("throws get the expected error " +
thrownError.name + "(" + thrownError.message + ")");
}
}
return thrown;
},
describe : function(name, fn) {
if (arguments.length < 2) {
throw new Error("Arguments missing at describe");
}
if (this.isFunction(fn)) {
System.log("{ " + String(name));
try {
fn();
} catch (exception) {
System.error(exception);
}
System.log("} //" + String(name));
}
},
describeEach : function(table, name, fn) {
if (arguments.length < 3) {
throw new Error("Arguments missing at describeEach");
}
if (this.isArray(table) && this.isFunction(fn)) {
System.log("{ " + String(name));
try {
table.forEach( function(row) {
fn.apply(null, row);
});
} catch (exception) {
System.error(exception);
}
System.log("} //" + String(name));
}
},
describeEachSkip : function(table, name, fn) {
if (arguments.length < 3) {
throw new Error("Arguments missing at describeEachSkip");
}
if (this.isArray(table) && this.isFunction(fn)) {
System.log("Skipped: " + String(name));
}
},
describeSkip : function(name, fn) {
if (arguments.length < 2) {
throw new Error("Arguments missing at describeSkip");
}
if (this.isFunction(fn)) {
System.log("Skipped: " + String(name));
}
},
test : function(name, fn) {
if (arguments.length < 2) {
throw new Error("Arguments missing at test");
}
if (this.isFunction(fn)) {
System.log(String(name));
try {
fn();
} catch (exception) {
System.error(exception);
}
}
},
testEach : function(table, name, fn) {
if (arguments.length < 3) {
throw new Error("Arguments missing at testEach");
}
if (this.isArray(table) && this.isFunction(fn)) {
System.log("{ " + String(name));
try {
table.forEach( function(row) {
fn.apply(null, row);
});
} catch (exception) {
System.error(exception);
}
System.log("} //" + String(name));
}
},
testEachSkip : function(table, name, fn) {
if (arguments.length < 3) {
throw new Error("Arguments missing at testEach");
}
if (this.isArray(table) && this.isFunction(fn)) {
System.log("Skipped: " + String(name));
}
},
testSkip : function(name, fn) {
if (arguments.length < 2) {
throw new Error("Arguments missing at testSkip");
}
if (this.isFunction(fn)) {
System.log("Skipped: " + String(name));
}
},
testTodo : function(todo) {
if (arguments.length < 1) {
throw new Error("Arguments missing at testTodo");
}
System.log("Todo: " + String(todo));
},
getClassName : function() {
return "Assert";
},
_errorMatches : function(actual, expected) {
if (arguments.length < 2) {
throw new Error("Arguments missing at _errorMatches");
}
if (this.isError(actual)) {
if (this.isString(expected)) {
return actual.message === expected;
}
if (expected instanceof RegExp) {
return expected.test(actual.message);
}
if (this.isError(expected)) {
if (actual === expected) {
return true;
}
}
}
return false;
},
_getObjectKeys : function(object) {
if (arguments.length === 0) {
throw new Error("Argument missing at _getObjectKeys");
}
var keys = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
},
_isDeepEqual : function(actual, expected) {
if (arguments.length < 2) {
throw new Error("Arguments missing at _isDeepEqual");
}
if (actual === expected) {
return true;
}
if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();
}
if (actual instanceof RegExp && expected instanceof RegExp) {
return (
actual.source === expected.source &&
actual.global === expected.global &&
actual.multiline === expected.multiline &&
actual.lastIndex === expected.lastIndex &&
actual.ignoreCase === expected.ignoreCase
);
}
if (typeof actual !== 'object' && typeof expected !== 'object') {
return actual == expected;
}
return this._objectsEqual(actual, expected);
},
_objectsEqual : function(object1, object2) {
if (arguments.length < 2) {
throw new Error("Arguments missing at _objectsEqual");
}
if (this.isNullOrUndefined(object1) || this.isNullOrUndefined(object2)) {
return false;
}
if (object1.prototype !== object2.prototype) {
return false;
}
var object1Keys = this._getObjectKeys(object1);
var object2Keys = this._getObjectKeys(object2);
if (object1Keys.length !== object2Keys.length) {
return false;
}
object1Keys.sort();
object2Keys.sort();
for (var i = 0; i < object1Keys.length; i++) {
if (object1Keys[i] != object2Keys[i]) {
return false;
}
}
for (var j = 0; j < object1Keys.length; j++) {
var key = object1Keys[j];
if (!this._isDeepEqual(object1[key], object2[key])) {
return false;
}
}
return true;
},
_passOut : function(text) {
if (this.isUndefined(text)) {
System.log("Pass");
} else {
System.log("Pass: " + text);
}
},
_failOut : function(text) {
if (this.isUndefined(text)) {
System.error("Fail");
} else {
System.error("Fail: " + text);
}
},
_runtimeCategoryShort : "Duration Short",
getRuntimeCategoryShort : function() {
return this._runtimeCategoryShort;
},
_runtimeCategoryMedium : "Duration Medium",
getRuntimeCategoryMedium : function() {
return this._runtimeCategoryMedium;
},
_runtimeCategoryLong : "Duration Long",
getRuntimeCategoryLong : function() {
return this._runtimeCategoryLong;
},
_riskLevelCritical : "Risk level critical",
getRiskLevelCritical : function() {
return this._riskLevelCritical;
},
_riskLevelDangerous : "Risk level dangerous",
getRiskLevelDangerous : function() {
return this._riskLevelDangerous;
},
_riskLevelHarmless : "Risk level harmless",
getRiskLevelHarmless : function() {
return this._riskLevelHarmless;
},
_runtimeCategory : "Duration Short",
getRuntimeCategory : function() {
return this._runtimeCategory;
},
setRuntimeCategory : function(runtimeCategory) {
this._runtimeCategory = runtimeCategory;
},
_riskLevel : "Risk level harmless",
getRiskLevel : function() {
return this._riskLevel;
},
setRiskLevel : function(riskLevel) {
this._riskLevel = riskLevel;
}
};
return _assertNS;
|