2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
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 = {
|
2017-09-13 22:02:26 +00:00
|
|
|
assertSimilar: function(type, val1, val2, errorMessage, depth) {
|
|
|
|
depth = depth || 0;
|
2017-09-11 21:57:31 +00:00
|
|
|
this.assertDefined(type, depth + 1);
|
|
|
|
type = type.replace('?', '');
|
|
|
|
if (val2 === null) {
|
|
|
|
this.assertNull(val1, errorMessage, depth + 1);
|
2017-09-13 22:02:26 +00:00
|
|
|
}
|
2017-09-11 21:57:31 +00:00
|
|
|
else if (type === 'float' || type === 'double') {
|
|
|
|
this.assertEqualWithTolerance(val1, val2, 0.000001, errorMessage, depth + 1);
|
|
|
|
}
|
|
|
|
else if (type === 'data') {
|
2017-09-13 22:02:26 +00:00
|
|
|
this.assertArraysEqual(new Uint8Array(val1), val2, errorMessage, depth + 1);
|
|
|
|
}
|
2017-09-11 21:57:31 +00:00
|
|
|
else if (type === 'date') {
|
|
|
|
this.assertEqual(val1 && val1.getTime(), val2.getTime(), errorMessage, depth + 1);
|
|
|
|
}
|
|
|
|
else if (type === 'object') {
|
|
|
|
for (const key of Object.keys(val1)) {
|
|
|
|
this.assertEqual(val1[key], val2[key], errorMessage, depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type === 'list') {
|
|
|
|
this.assertArraysEqual(val1, val2, errorMessage, depth + 1);
|
2017-09-13 22:02:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.assertEqual(val1, val2, errorMessage, depth + 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertEqual: function(val1, val2, errorMessage, depth) {
|
2015-09-28 23:05:57 +00:00
|
|
|
if (val1 !== val2) {
|
2017-09-13 21:52:01 +00:00
|
|
|
let message = `'${val1}' does not equal expected value '${val2}'`;
|
2015-09-28 23:05:57 +00:00
|
|
|
if (errorMessage) {
|
2017-09-13 21:52:01 +00:00
|
|
|
message = `${errorMessage} - ${message}`;
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(message, depth);
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertNotEqual: function(val1, val2, errorMessage, depth) {
|
2015-09-28 23:05:57 +00:00
|
|
|
if (val1 === val2) {
|
2017-09-13 21:52:01 +00:00
|
|
|
let message = `'${val1}' equals '${val2}'`;
|
2015-09-28 23:05:57 +00:00
|
|
|
if (errorMessage) {
|
2017-09-13 21:52:01 +00:00
|
|
|
message = `${errorMessage} - ${message}`;
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(message, depth);
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertEqualWithTolerance: function(val1, val2, tolerance, errorMessage, depth) {
|
2015-08-13 16:12:48 +00:00
|
|
|
if (val1 < val2 - tolerance || val1 > val2 + tolerance) {
|
2017-09-13 21:52:01 +00:00
|
|
|
let message = `'${val1}' does not equal '${val2}' with tolerance '${tolerance}'`;
|
2015-09-28 23:05:57 +00:00
|
|
|
if (errorMessage) {
|
2017-09-13 21:52:01 +00:00
|
|
|
message = `${errorMessage} - ${message}`;
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(message, depth);
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertArray: function(value, length, errorMessage, depth) {
|
2016-11-10 20:53:36 +00:00
|
|
|
if (!Array.isArray(value)) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(errorMessage || `Value ${value} is not an array`, depth);
|
2016-11-10 20:53:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertArrayLength: function(value, length, errorMessage, depth) {
|
|
|
|
this.assertArray(value, 1 + depth || 0);
|
2016-11-10 20:53:36 +00:00
|
|
|
if (value.length !== length) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(errorMessage || `Value ${value} is not an array of length ${length}`, depth);
|
2016-11-10 20:53:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertArraysEqual: function(val1, val2, errorMessage, depth) {
|
2017-09-11 21:57:31 +00:00
|
|
|
this.assertDefined(val1, `val1 should be non-null but is ${val1}`, 1 + (depth || 0));
|
|
|
|
this.assertDefined(val2, `val2 should be non-null but is ${val2}`, 1 + (depth || 0));
|
2017-09-13 21:52:01 +00:00
|
|
|
const len1 = val1.length;
|
|
|
|
const len2 = val2.length;
|
2015-11-16 23:49:04 +00:00
|
|
|
|
|
|
|
if (len1 !== len2) {
|
2017-09-13 21:52:01 +00:00
|
|
|
let message = `Arrays (${val1}) and (${val2}) have different lengths (${len1} != ${len2})`;
|
2015-11-16 23:49:04 +00:00
|
|
|
if (errorMessage) {
|
2017-09-13 21:52:01 +00:00
|
|
|
message = `${errorMessage} - ${message}`;
|
2015-11-16 23:49:04 +00:00
|
|
|
}
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(message, depth);
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
let compare;
|
|
|
|
if (val1.type === "data") {
|
|
|
|
compare = (i, a, b) => a === b || this.assertArraysEqual(new Uint8Array(a), b, `Data elements at index ${i}`, 1) || true;
|
|
|
|
}
|
|
|
|
else if (val1.type === "date") {
|
|
|
|
compare = (i, a, b) => (a && a.getTime()) === (b && b.getTime());
|
|
|
|
}
|
|
|
|
else if (val1.type === "float" || val1.type === "double") {
|
|
|
|
compare = (i, a, b) => a >= b - 0.000001 && a <= b + 0.000001;
|
|
|
|
}
|
|
|
|
else if (val1.type === 'object') {
|
|
|
|
compare = (i, a, b) => Object.keys(a).every(key => a[key] === b[key]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
compare = (i, a, b) => a === b;
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
for (let i = 0; i < len1; i++) {
|
2017-09-11 21:57:31 +00:00
|
|
|
if (!compare(i, val1[i], val2[i])) {
|
2017-09-13 21:52:01 +00:00
|
|
|
let message = `Array contents not equal at index ${i} (${val1[i]} != ${val2[i]})`;
|
2015-11-16 23:49:04 +00:00
|
|
|
if (errorMessage) {
|
2017-09-13 21:52:01 +00:00
|
|
|
message = `${errorMessage} - ${message}`;
|
2015-11-16 23:49:04 +00:00
|
|
|
}
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(message, depth);
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertThrows: function(func, errorMessage, depth) {
|
|
|
|
let caught = false;
|
2015-08-13 16:12:48 +00:00
|
|
|
try {
|
|
|
|
func();
|
|
|
|
}
|
2015-09-28 23:05:57 +00:00
|
|
|
catch (e) {
|
2015-08-13 16:12:48 +00:00
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!caught) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(errorMessage || 'Expected exception not thrown', depth);
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2016-04-18 22:24:58 +00:00
|
|
|
assertThrowsException: function(func, expectedException) {
|
2017-09-13 21:52:01 +00:00
|
|
|
let caught = false;
|
2016-04-18 22:24:58 +00:00
|
|
|
try {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
caught = true;
|
2017-01-10 12:41:56 +00:00
|
|
|
if (e.name !== expectedException.name) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(`Expected a ${expectedException.name} exception but caught a ${e.name} instead. Message was: ${e.message}`);
|
2017-01-10 12:41:56 +00:00
|
|
|
}
|
2016-04-29 20:01:01 +00:00
|
|
|
if (e.message != expectedException.message) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(`Expected exception "${expectedException}" not thrown - instead caught: "${e}"`);
|
2016-04-18 22:24:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!caught) {
|
|
|
|
throw new TestFailureError('Expected exception not thrown');
|
|
|
|
}
|
|
|
|
},
|
2017-09-14 22:50:11 +00:00
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertThrowsContaining: function(func, expectedMessage, depth) {
|
2017-09-11 21:57:31 +00:00
|
|
|
let caught = false;
|
2017-09-14 22:50:11 +00:00
|
|
|
try {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
caught = true;
|
|
|
|
if (!e.message.includes(expectedMessage)) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown - instead caught: "${e}"`, depth);
|
2017-09-14 22:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!caught) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown`, depth);
|
2017-09-14 22:50:11 +00:00
|
|
|
}
|
|
|
|
},
|
2016-04-18 22:24:58 +00:00
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertTrue: function(condition, errorMessage, depth) {
|
2015-08-13 16:12:48 +00:00
|
|
|
if (!condition) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(errorMessage || `Condition ${condition} expected to be true`, depth);
|
2015-11-16 11:26:33 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2016-05-10 18:34:33 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
assertFalse: function(condition, errorMessage, depth) {
|
|
|
|
if (condition) {
|
|
|
|
throw new TestFailureError(errorMessage || `Condition ${condition} expected to be false`, depth);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertInstanceOf: function(object, type, errorMessage, depth) {
|
2016-11-10 18:34:39 +00:00
|
|
|
if (!(object instanceof type)) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(errorMessage || `Object ${object} expected to be of type ${type}`, depth);
|
2016-11-10 18:34:39 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertType: function(value, type, depth) {
|
|
|
|
this.assertEqual(typeof value, type, `Value ${value} expected to be of type ${type}`, 1 + depth || 0);
|
2016-11-10 18:34:39 +00:00
|
|
|
},
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
assertDefined: function(value, errorMessage, depth) {
|
|
|
|
if (value === undefined || value === null) {
|
|
|
|
throw new TestFailureError(errorMessage || `Value ${value} expected to be non-null`, depth);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertUndefined: function(value, errorMessage, depth) {
|
2016-11-11 02:06:06 +00:00
|
|
|
if (value !== undefined) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(errorMessage || `Value ${value} expected to be undefined`, depth);
|
2016-11-11 02:06:06 +00:00
|
|
|
}
|
2016-11-10 18:34:39 +00:00
|
|
|
},
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
assertNull: function(value, errorMessage, depth) {
|
2017-02-01 13:18:59 +00:00
|
|
|
if (value !== null) {
|
2017-09-13 21:52:01 +00:00
|
|
|
throw new TestFailureError(errorMessage || `Value ${value} expected to be null`, depth);
|
2017-02-01 13:18:59 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-10 18:34:33 +00:00
|
|
|
isNode: function() {
|
2016-05-16 21:15:56 +00:00
|
|
|
// eslint-disable-next-line no-undef
|
2016-05-10 18:34:33 +00:00
|
|
|
return typeof process == 'object' && Object.prototype.toString.call(process) == '[object process]';
|
|
|
|
},
|
|
|
|
|
|
|
|
isNode6: function() {
|
2016-05-16 21:15:56 +00:00
|
|
|
// eslint-disable-next-line no-undef
|
2016-05-10 18:34:33 +00:00
|
|
|
return this.isNode() && process.version.indexOf('v6.') == 0;
|
|
|
|
},
|
2015-10-06 07:57:35 +00:00
|
|
|
};
|
2015-09-28 23:05:57 +00:00
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
function TestFailureError(message, depth) {
|
|
|
|
let error;
|
2015-09-28 23:05:57 +00:00
|
|
|
try {
|
|
|
|
throw new Error(message);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:52:01 +00:00
|
|
|
depth = 2 + (depth || 0);
|
|
|
|
|
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
|
2017-09-13 21:52:01 +00:00
|
|
|
const 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.
|
2017-09-13 21:52:01 +00:00
|
|
|
const stack = error.stack && error.stack.split('\n');
|
|
|
|
const match = stack[depth] && stack[depth].match(regex);
|
2015-09-28 23:05:57 +00:00
|
|
|
if (match) {
|
|
|
|
this.sourceURL = match[1];
|
|
|
|
this.line = +match[2];
|
|
|
|
this.column = +match[3];
|
2017-09-13 21:52:01 +00:00
|
|
|
this.stack = stack.slice(depth).join('\n');
|
2015-09-28 23:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.__proto__ = error;
|
|
|
|
}
|