Skip to content

Commit 33a3861

Browse files
Kai CataldoMylesBorins
authored andcommitted
assert: update comments
Remove the numbers from the comments to make it clear that assert does not follow the [CJS spec](http://wiki.commonjs.org/wiki/Unit_Testing/1.0). Additionally, clean up the existing comments for consistent formatting/language and ease of reading. PR-URL: #10579 Fixes: #9063 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 0db73fd commit 33a3861

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

lib/assert.js

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ const util = require('util');
3030
const Buffer = require('buffer').Buffer;
3131
const pToString = (obj) => Object.prototype.toString.call(obj);
3232

33-
// 1. The assert module provides functions that throw
33+
// The assert module provides functions that throw
3434
// AssertionError's when particular conditions are not met. The
3535
// assert module must conform to the following interface.
3636

3737
const assert = module.exports = ok;
3838

39-
// 2. The AssertionError is defined in assert.
39+
// The AssertionError is defined in assert.
4040
// new assert.AssertionError({ message: message,
4141
// actual: actual,
42-
// expected: expected })
42+
// expected: expected });
4343

4444
assert.AssertionError = function AssertionError(options) {
4545
this.name = 'AssertionError';
@@ -75,7 +75,7 @@ function getMessage(self) {
7575
// other keys to the AssertionError's constructor - they will be
7676
// ignored.
7777

78-
// 3. All of the following functions must throw an AssertionError
78+
// All of the following functions must throw an AssertionError
7979
// when a corresponding condition is not met, with a message that
8080
// may be undefined if not provided. All assertion methods provide
8181
// both the actual and expected values to the assertion error for
@@ -94,7 +94,7 @@ function fail(actual, expected, message, operator, stackStartFunction) {
9494
// EXTENSION! allows for well behaved errors defined elsewhere.
9595
assert.fail = fail;
9696

97-
// 4. Pure assertion tests whether a value is truthy, as determined
97+
// Pure assertion tests whether a value is truthy, as determined
9898
// by !!guard.
9999
// assert.ok(guard, message_opt);
100100
// This statement is equivalent to assert.equal(true, !!guard,
@@ -106,24 +106,25 @@ function ok(value, message) {
106106
}
107107
assert.ok = ok;
108108

109-
// 5. The equality assertion tests shallow, coercive equality with
109+
// The equality assertion tests shallow, coercive equality with
110110
// ==.
111111
// assert.equal(actual, expected, message_opt);
112112

113113
assert.equal = function equal(actual, expected, message) {
114114
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
115115
};
116116

117-
// 6. The non-equality assertion tests for whether two objects are not equal
118-
// with != assert.notEqual(actual, expected, message_opt);
117+
// The non-equality assertion tests for whether two objects are not
118+
// equal with !=.
119+
// assert.notEqual(actual, expected, message_opt);
119120

120121
assert.notEqual = function notEqual(actual, expected, message) {
121122
if (actual == expected) {
122123
fail(actual, expected, message, '!=', assert.notEqual);
123124
}
124125
};
125126

126-
// 7. The equivalence assertion tests a deep equality relation.
127+
// The equivalence assertion tests a deep equality relation.
127128
// assert.deepEqual(actual, expected, message_opt);
128129

129130
assert.deepEqual = function deepEqual(actual, expected, message) {
@@ -139,18 +140,22 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
139140
};
140141

141142
function _deepEqual(actual, expected, strict, memos) {
142-
// 7.1. All identical values are equivalent, as determined by ===.
143+
// All identical values are equivalent, as determined by ===.
143144
if (actual === expected) {
144145
return true;
146+
147+
// If both values are instances of buffers, equivalence is
148+
// determined by comparing the values and ensuring the result
149+
// === 0.
145150
} else if (actual instanceof Buffer && expected instanceof Buffer) {
146151
return compare(actual, expected) === 0;
147152

148-
// 7.2. If the expected value is a Date object, the actual value is
153+
// If the expected value is a Date object, the actual value is
149154
// equivalent if it is also a Date object that refers to the same time.
150155
} else if (util.isDate(actual) && util.isDate(expected)) {
151156
return actual.getTime() === expected.getTime();
152157

153-
// 7.3 If the expected value is a RegExp object, the actual value is
158+
// If the expected value is a RegExp object, the actual value is
154159
// equivalent if it is also a RegExp object with the same source and
155160
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
156161
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
@@ -160,18 +165,18 @@ function _deepEqual(actual, expected, strict, memos) {
160165
actual.lastIndex === expected.lastIndex &&
161166
actual.ignoreCase === expected.ignoreCase;
162167

163-
// 7.4. Other pairs that do not both pass typeof value == 'object',
164-
// equivalence is determined by ==.
168+
// If both values are primitives, equivalence is determined by
169+
// == or, if checking for strict equivalence, ===.
165170
} else if ((actual === null || typeof actual !== 'object') &&
166171
(expected === null || typeof expected !== 'object')) {
167172
return strict ? actual === expected : actual == expected;
168173

169174
// If both values are instances of typed arrays, wrap their underlying
170-
// ArrayBuffers in a Buffer each to increase performance
175+
// ArrayBuffers in a Buffer to increase performance.
171176
// This optimization requires the arrays to have the same type as checked by
172-
// Object.prototype.toString (aka pToString). Never perform binary
173-
// comparisons for Float*Arrays, though, since e.g. +0 === -0 but their
174-
// bit patterns are not identical.
177+
// Object.prototype.toString (pToString). Never perform binary
178+
// comparisons for Float*Arrays, though, since +0 === -0 is true despite the
179+
// two values' bit patterns not being identical.
175180
} else if (ArrayBuffer.isView(actual) && ArrayBuffer.isView(expected) &&
176181
pToString(actual) === pToString(expected) &&
177182
!(actual instanceof Float32Array ||
@@ -184,7 +189,7 @@ function _deepEqual(actual, expected, strict, memos) {
184189
expected.byteOffset +
185190
expected.byteLength)) === 0;
186191

187-
// 7.5 For all other Object pairs, including Array objects, equivalence is
192+
// For all other Object pairs, including Array objects, equivalence is
188193
// determined by having the same number of owned properties (as verified
189194
// with Object.prototype.hasOwnProperty.call), the same set of keys
190195
// (although not necessarily the same order), equivalent values for every
@@ -214,7 +219,8 @@ function isArguments(object) {
214219
function objEquiv(a, b, strict, actualVisitedObjects) {
215220
if (a === null || a === undefined || b === null || b === undefined)
216221
return false;
217-
// if one is a primitive, the other must be same
222+
223+
// If one is a primitive, the other must be the same.
218224
if (util.isPrimitive(a) || util.isPrimitive(b))
219225
return a === b;
220226
if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
@@ -226,20 +232,23 @@ function objEquiv(a, b, strict, actualVisitedObjects) {
226232
const ka = Object.keys(a);
227233
const kb = Object.keys(b);
228234
var key, i;
229-
// having the same number of owned properties (keys incorporates
230-
// hasOwnProperty)
235+
236+
// The pair must have the same number of owned properties (keys
237+
// incorporates hasOwnProperty).
231238
if (ka.length !== kb.length)
232239
return false;
233-
//the same set of keys (although not necessarily the same order),
240+
241+
// The pair must have the same set of keys (although not
242+
// necessarily in the same order).
234243
ka.sort();
235244
kb.sort();
236-
//~~~cheap key test
245+
// Cheap key test:
237246
for (i = ka.length - 1; i >= 0; i--) {
238247
if (ka[i] !== kb[i])
239248
return false;
240249
}
241-
//equivalent values for every corresponding key, and
242-
//~~~possibly expensive deep test
250+
// The pair must have equivalent values for every corresponding key.
251+
// Possibly expensive deep test:
243252
for (i = ka.length - 1; i >= 0; i--) {
244253
key = ka[i];
245254
if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects))
@@ -248,7 +257,7 @@ function objEquiv(a, b, strict, actualVisitedObjects) {
248257
return true;
249258
}
250259

251-
// 8. The non-equivalence assertion tests for any deep inequality.
260+
// The non-equivalence assertion tests for any deep inequality.
252261
// assert.notDeepEqual(actual, expected, message_opt);
253262

254263
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
@@ -265,7 +274,7 @@ function notDeepStrictEqual(actual, expected, message) {
265274
}
266275

267276

268-
// 9. The strict equality assertion tests strict equality, as determined by ===.
277+
// The strict equality assertion tests strict equality, as determined by ===.
269278
// assert.strictEqual(actual, expected, message_opt);
270279

271280
assert.strictEqual = function strictEqual(actual, expected, message) {
@@ -274,8 +283,9 @@ assert.strictEqual = function strictEqual(actual, expected, message) {
274283
}
275284
};
276285

277-
// 10. The strict non-equality assertion tests for strict inequality, as
278-
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
286+
// The strict non-equality assertion tests for strict inequality, as
287+
// determined by !==.
288+
// assert.notStrictEqual(actual, expected, message_opt);
279289

280290
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
281291
if (actual === expected) {
@@ -297,7 +307,7 @@ function expectedException(actual, expected) {
297307
return true;
298308
}
299309
} catch (e) {
300-
// Ignore. The instanceof check doesn't work for arrow functions.
310+
// Ignore. The instanceof check doesn't work for arrow functions.
301311
}
302312

303313
if (Error.isPrototypeOf(expected)) {
@@ -355,7 +365,7 @@ function _throws(shouldThrow, block, expected, message) {
355365
}
356366
}
357367

358-
// 11. Expected to throw an error:
368+
// Expected to throw an error.
359369
// assert.throws(block, Error_opt, message_opt);
360370

361371
assert.throws = function(block, /*optional*/error, /*optional*/message) {

0 commit comments

Comments
 (0)