Skip to content

Commit 34aa0c8

Browse files
aduh95codebytere
authored andcommitted
assert: refactor to use more primordials
PR-URL: #35998 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 122797e commit 34aa0c8

2 files changed

Lines changed: 36 additions & 25 deletions

File tree

‎lib/internal/assert/assertion_error.js‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
const{
4+
ArrayPrototypeJoin,
5+
ArrayPrototypePop,
46
Error,
57
ErrorCaptureStackTrace,
68
MathMax,
@@ -9,6 +11,10 @@ const {
911
ObjectGetPrototypeOf,
1012
ObjectKeys,
1113
String,
14+
StringPrototypeEndsWith,
15+
StringPrototypeRepeat,
16+
StringPrototypeSlice,
17+
StringPrototypeSplit,
1218
}=primordials;
1319

1420
const{ inspect }=require('internal/util/inspect');
@@ -79,8 +85,8 @@ function createErrDiff(actual, expected, operator) {
7985
letend='';
8086
letskipped=false;
8187
constactualInspected=inspectValue(actual);
82-
constactualLines=actualInspected.split('\n');
83-
constexpectedLines=inspectValue(expected).split('\n');
88+
constactualLines=StringPrototypeSplit(actualInspected,'\n');
89+
constexpectedLines=StringPrototypeSplit(inspectValue(expected),'\n');
8490

8591
leti=0;
8692
letindicator='';
@@ -127,7 +133,7 @@ function createErrDiff(actual, expected, operator) {
127133
if(i>2){
128134
// Add position indicator for the first mismatch in case it is a
129135
// single line and the input length is less than the column length.
130-
indicator=`\n ${' '.repeat(i)}^`;
136+
indicator=`\n ${StringPrototypeRepeat(' ',i)}^`;
131137
i=0;
132138
}
133139
}
@@ -144,8 +150,8 @@ function createErrDiff(actual, expected, operator) {
144150
}else{
145151
other=a;
146152
}
147-
actualLines.pop();
148-
expectedLines.pop();
153+
ArrayPrototypePop(actualLines);
154+
ArrayPrototypePop(expectedLines);
149155
if(actualLines.length===0||expectedLines.length===0)
150156
break;
151157
a=actualLines[actualLines.length-1];
@@ -157,18 +163,19 @@ function createErrDiff(actual, expected, operator) {
157163
// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() })
158164
if(maxLines===0){
159165
// We have to get the result again. The lines were all removed before.
160-
constactualLines=actualInspected.split('\n');
166+
constactualLines=StringPrototypeSplit(actualInspected,'\n');
161167

162168
// Only remove lines in case it makes sense to collapse those.
163169
// TODO: Accept env to always show the full error.
164170
if(actualLines.length>50){
165171
actualLines[46]=`${blue}...${white}`;
166172
while(actualLines.length>47){
167-
actualLines.pop();
173+
ArrayPrototypePop(actualLines);
168174
}
169175
}
170176

171-
return`${kReadableOperator.notIdentical}\n\n${actualLines.join('\n')}\n`;
177+
return`${kReadableOperator.notIdentical}\n\n`+
178+
`${ArrayPrototypeJoin(actualLines,'\n')}\n`;
172179
}
173180

174181
// There were at least five identical lines at the end. Mark a couple of
@@ -235,9 +242,10 @@ function createErrDiff(actual, expected, operator) {
235242
// If the lines diverge, specifically check for lines that only diverge by
236243
// a trailing comma. In that case it is actually identical and we should
237244
// mark it as such.
238-
letdivergingLines=actualLine!==expectedLine&&
239-
(!actualLine.endsWith(',')||
240-
actualLine.slice(0,-1)!==expectedLine);
245+
letdivergingLines=
246+
actualLine!==expectedLine&&
247+
(!StringPrototypeEndsWith(actualLine,',')||
248+
StringPrototypeSlice(actualLine,0,-1)!==expectedLine);
241249
// If the expected line has a trailing comma but is otherwise identical,
242250
// add a comma at the end of the actual line. Otherwise the output could
243251
// look weird as in:
@@ -248,8 +256,8 @@ function createErrDiff(actual, expected, operator) {
248256
// ]
249257
//
250258
if(divergingLines&&
251-
expectedLine.endsWith(',')&&
252-
expectedLine.slice(0,-1)===actualLine){
259+
StringPrototypeEndsWith(expectedLine,',')&&
260+
StringPrototypeSlice(expectedLine,0,-1)===actualLine){
253261
divergingLines=false;
254262
actualLine+=',';
255263
}
@@ -362,7 +370,7 @@ class AssertionError extends Error {
362370
// In case the objects are equal but the operator requires unequal, show
363371
// the first object and say A equals B
364372
letbase=kReadableOperator[operator];
365-
constres=inspectValue(actual).split('\n');
373+
constres=StringPrototypeSplit(inspectValue(actual),'\n');
366374

367375
// In case "actual" is an object or a function, it should not be
368376
// reference equal.
@@ -377,15 +385,15 @@ class AssertionError extends Error {
377385
if(res.length>50){
378386
res[46]=`${blue}...${white}`;
379387
while(res.length>47){
380-
res.pop();
388+
ArrayPrototypePop(res);
381389
}
382390
}
383391

384392
// Only print a single input.
385393
if(res.length===1){
386394
super(`${base}${res[0].length>5 ? '\n\n' : ' '}${res[0]}`);
387395
}else{
388-
super(`${base}\n\n${res.join('\n')}\n`);
396+
super(`${base}\n\n${ArrayPrototypeJoin(res,'\n')}\n`);
389397
}
390398
}else{
391399
letres=inspectValue(actual);
@@ -394,15 +402,15 @@ class AssertionError extends Error {
394402
if(operator==='notDeepEqual'&&res===other){
395403
res=`${knownOperator}\n\n${res}`;
396404
if(res.length>1024){
397-
res=`${res.slice(0,1021)}...`;
405+
res=`${StringPrototypeSlice(res,0,1021)}...`;
398406
}
399407
super(res);
400408
}else{
401409
if(res.length>512){
402-
res=`${res.slice(0,509)}...`;
410+
res=`${StringPrototypeSlice(res,0,509)}...`;
403411
}
404412
if(other.length>512){
405-
other=`${other.slice(0,509)}...`;
413+
other=`${StringPrototypeSlice(other,0,509)}...`;
406414
}
407415
if(operator==='deepEqual'){
408416
res=`${knownOperator}\n\n${res}\n\nshould loosely deep-equal\n\n`;
@@ -463,12 +471,12 @@ class AssertionError extends Error {
463471

464472
for(constnameof['actual','expected']){
465473
if(typeofthis[name]==='string'){
466-
constlines=this[name].split('\n');
474+
constlines=StringPrototypeSplit(this[name],'\n');
467475
if(lines.length>10){
468476
lines.length=10;
469-
this[name]=`${lines.join('\n')}\n...`;
477+
this[name]=`${ArrayPrototypeJoin(lines,'\n')}\n...`;
470478
}elseif(this[name].length>512){
471-
this[name]=`${this[name].slice(512)}...`;
479+
this[name]=`${StringPrototypeSlice(this[name],512)}...`;
472480
}
473481
}
474482
}

‎lib/internal/assert/calltracker.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

33
const{
4+
ArrayPrototypePush,
45
Error,
6+
FunctionPrototype,
7+
ReflectApply,
58
SafeSet,
69
}=primordials;
710

@@ -15,7 +18,7 @@ const {
1518
validateUint32,
1619
}=require('internal/validators');
1720

18-
constnoop=()=>{};
21+
constnoop=FunctionPrototype;
1922

2023
classCallTracker{
2124

@@ -55,7 +58,7 @@ class CallTracker {
5558
if(context.actual===context.exact+1){
5659
callChecks.add(context);
5760
}
58-
returnfn.apply(this,arguments);
61+
returnReflectApply(fn,this,arguments);
5962
};
6063
}
6164

@@ -67,7 +70,7 @@ class CallTracker {
6770
constmessage=`Expected the ${context.name} function to be `+
6871
`executed ${context.exact} time(s) but was `+
6972
`executed ${context.actual} time(s).`;
70-
errors.push({
73+
ArrayPrototypePush(errors,{
7174
message,
7275
actual: context.actual,
7376
expected: context.exact,

0 commit comments

Comments
 (0)