blob: 890862e7474c83a5ab76d7552c142628c35573c0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/**
* Custom error for failed assertions.
* @constructor
* @param {string} message Error message
* @returns Error
*/
function AssertionError(message) {
var instance = new Error(message);
instance.name = 'AssertionError';
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}
/**
* Throws an `AssertionError` if `value` is not truthy.
* @param {*} value Value to be tested
*/
function assert(value) {
if (!value)
throw new AssertionError("The assertion failed!");
}
/**
* Throws an `AssertionError` when called.
* @throws {AssertionError}
*/
function assertNotReached() {
throw new AssertionError("assertNotReached() was reached!");
}
/**
* Ensures the provided functions throws a specific error.
* @param {Function} testFunction Function executing the throwing code
* @param {object} [options]
* @param {Error} [options.error] Expected error type
* @param {string} [options.name] Expected error name
* @param {string} [options.message] Expected error message
*/
function assertThrowsError(testFunction, options) {
try {
testFunction();
assertNotReached();
} catch (e) {
if (options.error !== undefined)
assert(e instanceof options.error);
if (options.name !== undefined)
assert(e.name === options.name);
if (options.message !== undefined)
assert(e.message === options.message);
}
}
|