-
Notifications
You must be signed in to change notification settings - Fork 29
/
createWrappedExpectProto.js
256 lines (242 loc) · 10.4 KB
/
createWrappedExpectProto.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
var createStandardErrorMessage = require('./createStandardErrorMessage');
var makePromise = require('./makePromise');
var isPendingPromise = require('./isPendingPromise');
var oathbreaker = require('./oathbreaker');
var UnexpectedError = require('./UnexpectedError');
var addAdditionalPromiseMethods = require('./addAdditionalPromiseMethods');
var utils = require('./utils');
function isAssertionArg(arg) {
return arg.type.is('assertion');
}
function lookupAssertionsInParentChain(assertionString, unexpected) {
var assertions = [];
for (var instance = unexpected ; instance ; instance = instance.parent) {
if (instance.assertions[assertionString]) {
Array.prototype.push.apply(assertions, instance.assertions[assertionString]);
}
}
return assertions;
}
function findSuffixAssertions(assertionString, unexpected) {
if (typeof assertionString !== 'string') {
return null;
}
var straightforwardAssertions = lookupAssertionsInParentChain(assertionString, unexpected);
if (straightforwardAssertions.length > 0) {
return straightforwardAssertions;
}
var tokens = assertionString.split(' ');
for (var n = tokens.length - 1 ; n > 0 ; n -= 1) {
var suffix = tokens.slice(n).join(' ');
var suffixAssertions = lookupAssertionsInParentChain(suffix, unexpected);
if (findSuffixAssertions(tokens.slice(0, n).join(' '), unexpected) && suffixAssertions.length > 0) {
return suffixAssertions;
}
}
return null;
}
module.exports = function createWrappedExpectProto(unexpected) {
var wrappedExpectProto = {
promise: makePromise,
errorMode: 'default',
equal: unexpected.equal,
inspect: unexpected.inspect,
createOutput: unexpected.createOutput.bind(unexpected),
findTypeOf: unexpected.findTypeOf.bind(unexpected),
findTypeOfWithParentType: unexpected.findTypeOfWithParentType.bind(unexpected),
findCommonType: unexpected.findCommonType.bind(unexpected),
it: function () { // ...
var length = arguments.length;
var args = new Array(length);
for (var i = 0 ; i < length ; i += 1) {
args[i] = arguments[i];
}
if (typeof args[0] === 'string') {
args[0] = utils.forwardFlags(args[0], this.flags);
}
return unexpected.it.apply(unexpected, args);
},
diff: unexpected.diff,
getType: unexpected.getType,
output: unexpected.output,
outputFormat: unexpected.outputFormat.bind(unexpected),
format: unexpected.format,
withError: unexpected.withError,
fail: function () {
var args = arguments;
var expect = this.context.expect;
this.callInNestedContext(function () {
expect.fail.apply(expect, args);
});
},
standardErrorMessage: function (output, options) {
var that = this;
options = typeof options === 'object' ? options : {};
if ('omitSubject' in output) {
options.subject = this.subject;
}
if (options && options.compact) {
options.compactSubject = function (output) {
output.jsFunctionName(that.subjectType.name);
};
}
return createStandardErrorMessage(output, that.subjectOutput, that.testDescription, that.argsOutput, options);
},
callInNestedContext: function (callback) {
var that = this;
var context = that.context;
var expect = context.expect;
context.level++;
try {
var result = oathbreaker(callback());
if (isPendingPromise(result)) {
result = result.then(undefined, function (e) {
context.level--;
if (e && e._isUnexpected) {
var wrappedError = new UnexpectedError(that, e);
wrappedError.originalError = e.originalError;
throw wrappedError;
}
throw e;
});
} else if (!result || typeof result.then !== 'function') {
context.level--;
result = makePromise.resolve(result);
} else {
context.level--;
}
return addAdditionalPromiseMethods(result, that.execute, that.subject);
} catch (e) {
context.level--;
if (e && e._isUnexpected) {
var wrappedError = new UnexpectedError(that, e);
wrappedError.originalError = e.originalError;
if (that.context.level === 0) {
expect.setErrorMessage(wrappedError);
}
throw wrappedError;
}
throw e;
}
},
shift: function (subject, assertionIndex) {
if (arguments.length <= 1) {
if (arguments.length === 0) {
subject = this.subject;
}
assertionIndex = -1;
for (var i = 0 ; i < this.assertionRule.args.length ; i += 1) {
var type = this.assertionRule.args[i].type;
if (type.is('assertion') || type.is('expect.it')) {
assertionIndex = i;
break;
}
}
} else if (arguments.length === 3) {
// The 3-argument syntax for wrappedExpect.shift is deprecated, please omit the first (expect) arg
subject = arguments[1];
assertionIndex = arguments[2];
}
if (assertionIndex !== -1) {
var args = this.args.slice(0, assertionIndex);
var rest = this.args.slice(assertionIndex);
var nextArgumentType = this.findTypeOf(rest[0]);
if (arguments.length > 1) {
// Legacy
this.argsOutput = function (output) {
args.forEach(function (arg, index) {
if (0 < index) {
output.text(', ');
}
output.appendInspected(arg);
});
if (args.length > 0) {
output.sp();
}
if (nextArgumentType.is('string')) {
output.error(rest[0]);
} else if (rest.length > 0) {
output.appendInspected(rest[0]);
}
if (rest.length > 1) {
output.sp();
}
rest.slice(1).forEach(function (arg, index) {
if (0 < index) {
output.text(', ');
}
output.appendInspected(arg);
});
};
}
if (nextArgumentType.is('expect.it')) {
var that = this;
return this.withError(function () {
return rest[0](subject);
}, function (err) {
that.fail(err);
});
} else if (nextArgumentType.is('string')) {
return this.execute.apply(this.execute, [subject].concat(rest));
} else {
return subject;
}
} else {
// No assertion to delegate to. Provide the new subject as the fulfillment value:
return subject;
}
},
_getSubjectType: function () {
return this.findTypeOfWithParentType(this.subject, this.assertionRule.subject.type);
},
_getArgTypes: function (index) {
var lastIndex = this.assertionRule.args.length - 1;
return this.args.map(function (arg, index) {
return this.findTypeOfWithParentType(arg, this.assertionRule.args[Math.min(index, lastIndex)].type);
}, this);
},
_getAssertionIndices: function () {
if (!this._assertionIndices) {
var assertionIndices = [];
var args = this.args;
var currentAssertionRule = this.assertionRule;
var offset = 0;
OUTER: while (true) {
if (currentAssertionRule.args.length > 1 && isAssertionArg(currentAssertionRule.args[currentAssertionRule.args.length - 2])) {
assertionIndices.push(offset + currentAssertionRule.args.length - 2);
var suffixAssertions = findSuffixAssertions(args[offset + currentAssertionRule.args.length - 2], unexpected);
if (suffixAssertions) {
for (var i = 0 ; i < suffixAssertions.length ; i += 1) {
if (suffixAssertions[i].args.some(isAssertionArg)) {
offset += currentAssertionRule.args.length - 1;
currentAssertionRule = suffixAssertions[i];
continue OUTER;
}
}
}
}
// No further assertions found,
break;
}
this._assertionIndices = assertionIndices;
}
return this._assertionIndices;
}
};
if (Object.__defineGetter__) {
Object.defineProperty(wrappedExpectProto, 'subjectType', {
enumerable: true,
get: function () {
return this.assertionRule && this._getSubjectType();
}
});
Object.defineProperty(wrappedExpectProto, 'argTypes', {
enumerable: true,
get: function () {
return this.assertionRule && this._getArgTypes();
}
});
}
utils.setPrototypeOfOrExtend(wrappedExpectProto, Function.prototype);
return wrappedExpectProto;
};