2015-10-27 20:59:15 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
2015-08-14 15:18:49 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 07:57:35 +00:00
|
|
|
module.exports = {
|
2015-09-28 23:05:57 +00:00
|
|
|
assertEqual: function(val1, val2, errorMessage) {
|
|
|
|
if (val1 !== val2) {
|
|
|
|
var message = "'" + val1 + "' does not equal expected value '" + val2 + "'";
|
|
|
|
if (errorMessage) {
|
2015-11-16 23:49:04 +00:00
|
|
|
message = errorMessage + ' - ' + message;
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
2015-09-28 23:05:57 +00:00
|
|
|
throw new TestFailureError(message);
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
assertNotEqual: function(val1, val2, errorMessage) {
|
|
|
|
if (val1 === val2) {
|
|
|
|
var message = "'" + val1 + "' equals '" + val2 + "'";
|
|
|
|
if (errorMessage) {
|
2015-11-16 23:49:04 +00:00
|
|
|
message = errorMessage + ' - ' + message;
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
2015-09-28 23:05:57 +00:00
|
|
|
throw new TestFailureError(message);
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
assertEqualWithTolerance: function(val1, val2, tolerance, errorMessage) {
|
|
|
|
if (val1 < val2 - tolerance || val1 > val2 + tolerance) {
|
|
|
|
var message = "'" + val1 + "' does not equal '" + val2 + "' with tolerance '" + tolerance + "'";
|
2015-09-28 23:05:57 +00:00
|
|
|
if (errorMessage) {
|
2015-11-16 23:49:04 +00:00
|
|
|
message = errorMessage + ' - ' + message;
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
2015-09-28 23:05:57 +00:00
|
|
|
throw new TestFailureError(message);
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-16 11:26:33 +00:00
|
|
|
assertArraysEqual: function(val1, val2, errorMessage) {
|
2015-11-16 23:49:04 +00:00
|
|
|
var len1 = val1.length;
|
|
|
|
var len2 = val2.length;
|
|
|
|
var message;
|
|
|
|
|
|
|
|
if (len1 !== len2) {
|
|
|
|
message = 'Arrays have different lengths (' + len1 + ' != ' + len2 + ')';
|
|
|
|
if (errorMessage) {
|
|
|
|
message = errorMessage + ' - ' + message;
|
|
|
|
}
|
|
|
|
throw new TestFailureError(message);
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 23:49:04 +00:00
|
|
|
for (var i = 0; i < len1; i++) {
|
2015-11-16 11:26:33 +00:00
|
|
|
if (val1[i] !== val2[i]) {
|
2015-11-16 23:49:04 +00:00
|
|
|
message = 'Array contents not equal at index ' + i + ' (' + val1[i] + ' != ' + val2[i] + ')';
|
|
|
|
if (errorMessage) {
|
|
|
|
message = errorMessage + ' - ' + message;
|
|
|
|
}
|
|
|
|
throw new TestFailureError(message);
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
assertThrows: function(func, errorMessage) {
|
|
|
|
var caught = false;
|
|
|
|
try {
|
|
|
|
func();
|
|
|
|
}
|
2015-09-28 23:05:57 +00:00
|
|
|
catch (e) {
|
2015-08-13 16:12:48 +00:00
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!caught) {
|
2015-09-28 23:05:57 +00:00
|
|
|
throw new TestFailureError(errorMessage || 'Expected exception not thrown');
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2015-08-27 20:33:20 +00:00
|
|
|
assertTrue: function(condition, errorMessage) {
|
2015-08-13 16:12:48 +00:00
|
|
|
if (!condition) {
|
2015-09-28 23:05:57 +00:00
|
|
|
throw new TestFailureError(errorMessage || 'Condition expected to be true');
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2015-10-06 07:57:35 +00:00
|
|
|
};
|
2015-09-28 23:05:57 +00:00
|
|
|
|
|
|
|
function TestFailureError(message) {
|
|
|
|
var error;
|
|
|
|
try {
|
|
|
|
throw new Error(message);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
2015-09-29 01:09:59 +00:00
|
|
|
// This regular expression will match stack trace lines provided by JavaScriptCore.
|
|
|
|
// Example: someMethod@file:///path/to/file.js:10:24
|
2015-10-15 18:34:24 +00:00
|
|
|
var regex = /^(?:.*?@)?([^\[\(].+?):(\d+)(?::(\d+))?\s*$/;
|
2015-09-29 01:09:59 +00:00
|
|
|
|
|
|
|
// Remove the top two stack frames and use information from the third, if possible.
|
2015-09-28 23:05:57 +00:00
|
|
|
var stack = error.stack && error.stack.split('\n');
|
2015-09-29 01:09:59 +00:00
|
|
|
var match = stack[2] && stack[2].match(regex);
|
2015-09-28 23:05:57 +00:00
|
|
|
if (match) {
|
|
|
|
this.sourceURL = match[1];
|
|
|
|
this.line = +match[2];
|
|
|
|
this.column = +match[3];
|
|
|
|
this.stack = stack.slice(2).join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.__proto__ = error;
|
|
|
|
}
|