Skip to content

Commit 8e60dcb

Browse files
committed
[Robustness] cache and call-bind more prototype methods
1 parent b19da31 commit 8e60dcb

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

lib/results.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var inspect = require('object-inspect');
99
var callBound = require('call-bind/callBound');
1010
var has = require('has');
1111
var regexpTest = callBound('RegExp.prototype.test');
12+
var $split = callBound('String.prototype.split');
13+
var $replace = callBound('String.prototype.replace');
14+
var $shift = callBound('Array.prototype.shift');
15+
var $push = callBound('Array.prototype.push');
1216
var yamlIndicators = /:|-|\?/;
1317
var nextTick = typeof setImmediate !== 'undefined'
1418
? setImmediate
@@ -19,7 +23,7 @@ module.exports = Results;
1923
inherits(Results, EventEmitter);
2024

2125
function coalesceWhiteSpaces(str) {
22-
return String(str).replace(/\s+/g, ' ');
26+
return $replace(String(str), /\s+/g, ' ');
2327
}
2428

2529
function Results() {
@@ -94,7 +98,7 @@ Results.prototype.createStream = function (opts) {
9498

9599
Results.prototype.push = function (t) {
96100
var self = this;
97-
self.tests.push(t);
101+
$push(self.tests, t);
98102
self._watch(t);
99103
self.emit('_push', t);
100104
};
@@ -186,7 +190,7 @@ function encodeResult(res, count) {
186190
var errorStack = res.error && res.error.stack;
187191
var stack = defined(actualStack, errorStack);
188192
if (stack) {
189-
var lines = String(stack).split('\n');
193+
var lines = $split(String(stack), '\n');
190194
output += inner + 'stack: |-\n';
191195
for (var i = 0; i < lines.length; i++) {
192196
output += inner + ' ' + lines[i] + '\n';
@@ -199,11 +203,11 @@ function encodeResult(res, count) {
199203

200204
function getNextTest(results) {
201205
if (!results._only) {
202-
return results.tests.shift();
206+
return $shift(results.tests);
203207
}
204208

205209
do {
206-
var t = results.tests.shift();
210+
var t = $shift(results.tests);
207211
if (!t) continue;
208212
if (results._only === t) {
209213
return t;

lib/test.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ var toLowerCase = callBound('String.prototype.toLowerCase');
1717
var isProto = callBound('Object.prototype.isPrototypeOf');
1818
var $test = callBound('RegExp.prototype.test');
1919
var objectToString = callBound('Object.prototype.toString');
20+
var $split = callBound('String.prototype.split');
21+
var $replace = callBound('String.prototype.replace');
22+
var $strSlice = callBound('String.prototype.slice');
23+
var $push = callBound('Array.prototype.push');
24+
var $shift = callBound('Array.prototype.shift');
2025

2126
module.exports = Test;
2227

@@ -128,7 +133,7 @@ Test.prototype.run = function () {
128133
Test.prototype.test = function (name, opts, cb) {
129134
var self = this;
130135
var t = new Test(name, opts, cb);
131-
this._progeny.push(t);
136+
$push(this._progeny, t);
132137
this.pendingCount++;
133138
this.emit('test', t);
134139
t.on('prerun', function () {
@@ -150,8 +155,8 @@ Test.prototype.test = function (name, opts, cb) {
150155

151156
Test.prototype.comment = function (msg) {
152157
var that = this;
153-
forEach(trim(msg).split('\n'), function (aMsg) {
154-
that.emit('result', trim(aMsg).replace(/^#\s*/, ''));
158+
forEach($split(trim(msg), '\n'), function (aMsg) {
159+
that.emit('result', $replace(trim(aMsg), /^#\s*/, ''));
155160
});
156161
};
157162

@@ -191,7 +196,7 @@ Test.prototype._end = function (err) {
191196
if (!this._cb && !this._todo && !this._skip) this.fail('# TODO ' + this.name);
192197

193198
if (this._progeny.length) {
194-
var t = this._progeny.shift();
199+
var t = $shift(this._progeny);
195200
t.on('end', function () { self._end(); });
196201
t.run();
197202
return;
@@ -266,7 +271,7 @@ Test.prototype._assert = function assert(ok, opts) {
266271

267272
if (!ok) {
268273
var e = new Error('exception');
269-
var err = (e.stack || '').split('\n');
274+
var err = $split(e.stack || '', '\n');
270275
var dir = __dirname + path.sep;
271276

272277
for (var i = 0; i < err.length; i++) {
@@ -306,23 +311,23 @@ Test.prototype._assert = function assert(ok, opts) {
306311
/((?:\/|[a-zA-Z]:\\)[^:\)]+:(\d+)(?::(\d+))?)\)?/
307312
*/
308313
var re = /^(?:[^\s]*\s*\bat\s+)(?:(.*)\s+\()?((?:\/|[a-zA-Z]:\\)[^:)]+:(\d+)(?::(\d+))?)\)?$/;
309-
var lineWithTokens = err[i].replace(process.cwd(), '/$CWD').replace(__dirname, '/$TEST');
314+
var lineWithTokens = $replace($replace(err[i], process.cwd(), '/$CWD'), __dirname, '/$TEST');
310315
var m = re.exec(lineWithTokens);
311316

312317
if (!m) {
313318
continue;
314319
}
315320

316321
var callDescription = m[1] || '<anonymous>';
317-
var filePath = m[2].replace('/$CWD', process.cwd()).replace('/$TEST', __dirname);
322+
var filePath = $replace($replace(m[2], '/$CWD', process.cwd()), '/$TEST', __dirname);
318323

319-
if (filePath.slice(0, dir.length) === dir) {
324+
if ($strSlice(filePath, 0, dir.length) === dir) {
320325
continue;
321326
}
322327

323328
// Function call description may not (just) be a function name.
324329
// Try to extract function name by looking at first "word" only.
325-
res.functionName = callDescription.split(/\s+/)[0];
330+
res.functionName = $split(callDescription, /\s+/)[0];
326331
res.file = filePath;
327332
res.line = Number(m[3]);
328333
if (m[4]) res.column = Number(m[4]);
@@ -610,12 +615,12 @@ Test.prototype['throws'] = function (fn, expected, msg, extra) {
610615
var keys = Object.keys(expected);
611616
// Special handle errors to make sure the name and the message are compared as well.
612617
if (expected instanceof Error) {
613-
keys.push('name', 'message');
618+
$push(keys, 'name', 'message');
614619
} else if (keys.length === 0) {
615620
throw new TypeError('`throws` validation object must not be empty');
616621
}
617622
passed = keys.every(function (key) {
618-
if (typeof caught.error[key] === 'string' && isRegExp(expected[key]) && expected[key].test(caught.error[key])) {
623+
if (typeof caught.error[key] === 'string' && isRegExp(expected[key]) && $test(expected[key], caught.error[key])) {
619624
return true;
620625
}
621626
if (key in caught.error && deepEqual(caught.error[key], expected[key], { strict: true })) {

0 commit comments

Comments
 (0)