This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
assertion-error.js
92 lines (79 loc) · 2.36 KB
/
assertion-error.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* should.js - assertion library
* Copyright(c) 2010-2013 TJ Holowaychuk <tj@vision-media.ca>
* Copyright(c) 2013-2017 Denis Bardadym <bardadymchik@gmail.com>
* MIT Licensed
*/
import { merge } from 'should-util';
import { functionName } from './util';
import { format } from './format';
/**
* should AssertionError
* @param {Object} options
* @constructor
* @memberOf should
* @static
*/
export default function AssertionError(options) {
merge(this, options);
if (!options.message) {
Object.defineProperty(this, 'message', {
get: function() {
if (!this._message) {
this._message = this.generateMessage();
this.generatedMessage = true;
}
return this._message;
},
configurable: true,
enumerable: false
}
);
}
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.stackStartFunction);
} else {
// non v8 browsers so we can have a stacktrace
var err = new Error();
if (err.stack) {
var out = err.stack;
if (this.stackStartFunction) {
// try to strip useless frames
var fn_name = functionName(this.stackStartFunction);
var idx = out.indexOf('\n' + fn_name);
if (idx >= 0) {
// once we have located the function frame
// we need to strip out everything before it (and its line)
var next_line = out.indexOf('\n', idx + 1);
out = out.substring(next_line + 1);
}
}
this.stack = out;
}
}
}
var indent = ' ';
function prependIndent(line) {
return indent + line;
}
function indentLines(text) {
return text.split('\n').map(prependIndent).join('\n');
}
// assert.AssertionError instanceof Error
AssertionError.prototype = Object.create(Error.prototype, {
name: {
value: 'AssertionError'
},
generateMessage: {
value: function() {
if (!this.operator && this.previous) {
return this.previous.message;
}
var actual = format(this.actual);
var expected = 'expected' in this ? ' ' + format(this.expected) : '';
var details = 'details' in this && this.details ? ' (' + this.details + ')' : '';
var previous = this.previous ? '\n' + indentLines(this.previous.message) : '';
return 'expected ' + actual + (this.negate ? ' not ' : ' ') + this.operator + expected + details + previous;
}
}
});