Skip to content

Commit dd35637

Browse files
committed
Fix assert.js code style
1 parent 0d7e88a commit dd35637

2 files changed

Lines changed: 247 additions & 217 deletions

File tree

lib/assert.js

Lines changed: 156 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,38 @@ var assert = exports;
3636
// new assert.AssertionError({message: message, actual: actual, expected: expected})
3737

3838
assert.AssertionError = function AssertionError(options) {
39-
this.name = "AssertionError";
40-
this.message = options.message;
41-
this.actual = options.actual;
42-
this.expected = options.expected;
43-
this.operator = options.operator;
44-
//v8 specific
45-
if(Error.captureStackTrace){
46-
Error.captureStackTrace(this,fail);
47-
//node specific, removes the node machinery stack frames
48-
if(typeof(__filename) !== undefined){
49-
var stack = this.stack.split('\n');
50-
for (var i = stack.length - 1; i >= 0; i--) {
51-
if(stack[i].indexOf(__filename) != -1){
52-
this.stack = stack.slice(0,i+2).join('\n');
53-
break;
54-
}
55-
}
39+
this.name = "AssertionError";
40+
this.message = options.message;
41+
this.actual = options.actual;
42+
this.expected = options.expected;
43+
this.operator = options.operator;
44+
45+
//v8 specific
46+
if (Error.captureStackTrace) {
47+
Error.captureStackTrace(this, fail);
48+
//node specific, removes the node machinery stack frames
49+
if (typeof(__filename) !== undefined) {
50+
var stack = this.stack.split('\n');
51+
for (var i = stack.length - 1; i >= 0; i--) {
52+
if (stack[i].indexOf(__filename) != -1) {
53+
this.stack = stack.slice(0, i+2).join('\n');
54+
break;
5655
}
56+
}
5757
}
58+
}
5859
};
5960

60-
assert.AssertionError.prototype.toString = function(){
61-
if(this.message)
62-
return [this.name+":", this.message].join(' ');
63-
else
64-
return [this.name+":", JSON.stringify(this.expected ), this.operator , JSON.stringify(this.actual)].join(" ");
61+
assert.AssertionError.prototype.toString = function() {
62+
if (this.message) {
63+
return [this.name+":", this.message].join(' ');
64+
} else {
65+
return [ this.name+":"
66+
, JSON.stringify(this.expected )
67+
, this.operator
68+
, JSON.stringify(this.actual)
69+
].join(" ");
70+
}
6571
}
6672

6773
// assert.AssertionError instanceof Error
@@ -80,12 +86,12 @@ assert.AssertionError.__proto__ = Error.prototype;
8086
// display purposes.
8187

8288
function fail(actual, expected, message, operator) {
83-
throw new assert.AssertionError({
84-
message: message,
85-
actual: actual,
86-
expected: expected,
87-
operator: operator
88-
});
89+
throw new assert.AssertionError({
90+
message: message,
91+
actual: actual,
92+
expected: expected,
93+
operator: operator
94+
});
8995
}
9096

9197
// EXTENSION! allows for well behaved errors defined elsewhere.
@@ -99,188 +105,183 @@ assert.fail = fail;
99105
// assert.strictEqual(true, guard, message_opt);.
100106

101107
assert.ok = function ok(value, message) {
102-
if (!!!value)
103-
fail(value, true, message, "==");
108+
if (!!!value) fail(value, true, message, "==");
104109
};
105110

106111
// 5. The equality assertion tests shallow, coercive equality with
107112
// ==.
108113
// assert.equal(actual, expected, message_opt);
109114

110115
assert.equal = function equal(actual, expected, message) {
111-
if (actual != expected)
112-
fail(actual, expected, message, "==");
116+
if (actual != expected) fail(actual, expected, message, "==");
113117
};
114118

115-
116119
// 6. The non-equality assertion tests for whether two objects are not equal
117120
// with != assert.notEqual(actual, expected, message_opt);
118121

119122
assert.notEqual = function notEqual(actual, expected, message) {
120-
if (actual == expected)
121-
fail(actual, expected, message, "!=");
123+
if (actual == expected) fail(actual, expected, message, "!=");
122124
};
123125

124126
// 7. The equivalence assertion tests a deep equality relation.
125127
// assert.deepEqual(actual, expected, message_opt);
126128

127129
exports.deepEqual = function deepEqual(actual, expected, message) {
128-
if (!_deepEqual(actual, expected))
129-
fail(actual, expected, message, "deepEqual");
130+
if (!_deepEqual(actual, expected)) {
131+
fail(actual, expected, message, "deepEqual");
132+
}
130133
};
131134

132135
function _deepEqual(actual, expected) {
133-
// 7.1. All identical values are equivalent, as determined by ===.
134-
if (actual === expected) {
135-
return true;
136-
137-
// 7.2. If the expected value is a Date object, the actual value is
138-
// equivalent if it is also a Date object that refers to the same time.
139-
} else if (actual instanceof Date
140-
&& expected instanceof Date) {
141-
return actual.getTime() === expected.getTime();
142-
143-
// 7.3. Other pairs that do not both pass typeof value == "object",
144-
// equivalence is determined by ==.
145-
} else if (typeof actual != 'object'
146-
&& typeof expected != 'object') {
147-
return actual == expected;
148-
149-
// 7.4. For all other Object pairs, including Array objects, equivalence is
150-
// determined by having the same number of owned properties (as verified
151-
// with Object.prototype.hasOwnProperty.call), the same set of keys
152-
// (although not necessarily the same order), equivalent values for every
153-
// corresponding key, and an identical "prototype" property. Note: this
154-
// accounts for both named and indexed properties on Arrays.
155-
} else {
156-
return objEquiv(actual, expected);
157-
}
136+
// 7.1. All identical values are equivalent, as determined by ===.
137+
if (actual === expected) {
138+
return true;
139+
140+
// 7.2. If the expected value is a Date object, the actual value is
141+
// equivalent if it is also a Date object that refers to the same time.
142+
} else if (actual instanceof Date && expected instanceof Date) {
143+
return actual.getTime() === expected.getTime();
144+
145+
// 7.3. Other pairs that do not both pass typeof value == "object",
146+
// equivalence is determined by ==.
147+
} else if (typeof actual != 'object' && typeof expected != 'object') {
148+
return actual == expected;
149+
150+
// 7.4. For all other Object pairs, including Array objects, equivalence is
151+
// determined by having the same number of owned properties (as verified
152+
// with Object.prototype.hasOwnProperty.call), the same set of keys
153+
// (although not necessarily the same order), equivalent values for every
154+
// corresponding key, and an identical "prototype" property. Note: this
155+
// accounts for both named and indexed properties on Arrays.
156+
} else {
157+
return objEquiv(actual, expected);
158+
}
158159
}
159160

160161
function isUndefinedOrNull (value) {
161-
return value === null || value === undefined;
162+
return value === null || value === undefined;
162163
}
163164

164-
function isArguments (object){
165-
return Object.prototype.toString.call(object) == '[object Arguments]';
165+
function isArguments (object) {
166+
return Object.prototype.toString.call(object) == '[object Arguments]';
166167
}
167168

168169
function objEquiv (a, b) {
169-
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
170-
return false;
171-
// an identical "prototype" property.
172-
if (a.prototype !== b.prototype) return false;
173-
//~~~I've managed to break Object.keys through screwy arguments passing.
174-
// Converting to array solves the problem.
175-
if(isArguments(a)){
176-
if(!isArguments(b)){
177-
return false;
178-
}
179-
a = pSlice.call(a);
180-
b = pSlice.call(b);
181-
return _deepEqual(a,b);
182-
}
183-
try{
184-
var ka = Object.keys(a),
185-
kb = Object.keys(b),
186-
key, i;
187-
} catch (e) {//happens when one is a string literal and the other isn't
188-
return false;
170+
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
171+
return false;
172+
// an identical "prototype" property.
173+
if (a.prototype !== b.prototype) return false;
174+
//~~~I've managed to break Object.keys through screwy arguments passing.
175+
// Converting to array solves the problem.
176+
if (isArguments(a)) {
177+
if (!isArguments(b)) {
178+
return false;
189179
}
190-
// having the same number of owned properties (keys incorporates hasOwnProperty)
191-
if(ka.length != kb.length)
192-
return false;
193-
//the same set of keys (although not necessarily the same order),
194-
ka.sort();
195-
kb.sort();
196-
//~~~cheap key test
197-
for (i = ka.length - 1; i >= 0; i--) {
198-
if(ka[i] != kb[i])
199-
return false;
200-
}
201-
//equivalent values for every corresponding key, and
202-
//~~~possibly expensive deep test
203-
for (i = ka.length - 1; i >= 0; i--) {
204-
key = ka[i];
205-
if(!_deepEqual(a[key], b[key] ))
206-
return false;
207-
}
208-
return true;
180+
a = pSlice.call(a);
181+
b = pSlice.call(b);
182+
return _deepEqual(a, b);
183+
}
184+
try{
185+
var ka = Object.keys(a),
186+
kb = Object.keys(b),
187+
key, i;
188+
} catch (e) {//happens when one is a string literal and the other isn't
189+
return false;
190+
}
191+
// having the same number of owned properties (keys incorporates hasOwnProperty)
192+
if (ka.length != kb.length)
193+
return false;
194+
//the same set of keys (although not necessarily the same order),
195+
ka.sort();
196+
kb.sort();
197+
//~~~cheap key test
198+
for (i = ka.length - 1; i >= 0; i--) {
199+
if (ka[i] != kb[i])
200+
return false;
201+
}
202+
//equivalent values for every corresponding key, and
203+
//~~~possibly expensive deep test
204+
for (i = ka.length - 1; i >= 0; i--) {
205+
key = ka[i];
206+
if (!_deepEqual(a[key], b[key] ))
207+
return false;
208+
}
209+
return true;
209210
}
210211

211212
// 8. The non-equivalence assertion tests for any deep inequality.
212213
// assert.notDeepEqual(actual, expected, message_opt);
213214

214215
exports.notDeepEqual = function notDeepEqual(actual, expected, message) {
215-
if (_deepEqual(actual, expected))
216-
fail(actual, expected, message, "notDeepEqual");
216+
if (_deepEqual(actual, expected)) {
217+
fail(actual, expected, message, "notDeepEqual");
218+
}
217219
};
218220

219221
// 9. The strict equality assertion tests strict equality, as determined by ===.
220222
// assert.strictEqual(actual, expected, message_opt);
221223

222224
assert.strictEqual = function strictEqual(actual, expected, message) {
223-
if (actual !== expected)
224-
fail(actual, expected, message, "===");
225+
if (actual !== expected) fail(actual, expected, message, "===");
225226
};
226227

227228
// 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
228229
// assert.notStrictEqual(actual, expected, message_opt);
229230

230231
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
231-
if (actual === expected)
232-
fail(actual, expected, message, "!==");
232+
if (actual === expected) fail(actual, expected, message, "!==");
233233
};
234234

235-
// 11. Expected to throw an error:
236-
// assert.throws(block, Error_opt, message_opt);
235+
function _throws (shouldThrow, block, err, message) {
236+
var exception = null,
237+
threw = false,
238+
typematters = true;
237239

238-
assert.throws = function(block, /*optional*/error, /*optional*/message){
239-
var args = [true]
240-
_throws.apply(this, args.concat(pSlice.call(arguments)));
241-
}
240+
message = message || "";
242241

243-
// EXTENSION! This is annoying to write outside this module.
244-
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message){
245-
var args = [false]
246-
_throws.apply(this, args.concat(pSlice.call(arguments)));
242+
//handle optional arguments
243+
if (arguments.length == 3) {
244+
if (typeof(err) == "string") {
245+
message = err;
246+
typematters = false;
247+
}
248+
} else if (arguments.length == 2) {
249+
typematters = false;
250+
}
251+
252+
try {
253+
block();
254+
} catch (e) {
255+
threw = true;
256+
exception = e;
257+
}
258+
259+
if (shouldThrow && !threw) {
260+
fail( "Missing expected exception"
261+
+ (err && err.name ? " ("+err.name+")." : '.')
262+
+ (message ? " " + message : "")
263+
);
264+
}
265+
if (!shouldThrow && threw && typematters && exception instanceof err) {
266+
fail( "Got unwanted exception"
267+
+ (err && err.name ? " ("+err.name+")." : '.')
268+
+ (message ? " " + message : "")
269+
);
270+
}
271+
if ((shouldThrow && threw && typematters && !(exception instanceof err)) ||
272+
(!shouldThrow && threw)) {
273+
throw exception;
274+
}
247275
}
248276

249-
var _throws = function (shouldThrow, block, err, message) {
250-
var exception = null
251-
threw = false
252-
typematters = true;
253-
message = message || "";
254-
255-
//handle optional arguments
256-
if(arguments.length == 3){
257-
if(typeof(err) == "string"){
258-
message = err;
259-
typematters = false;
260-
}
261-
} else if(arguments.length == 2) {
262-
typematters = false;
263-
}
277+
// 11. Expected to throw an error:
278+
// assert.throws(block, Error_opt, message_opt);
264279

265-
try {
266-
block();
267-
} catch (e) {
268-
threw = true;
269-
exception = e;
270-
}
280+
assert.throws = function(block, /*optional*/error, /*optional*/message) {
281+
_throws.apply(this, [true].concat(pSlice.call(arguments)));
282+
};
271283

272-
if(shouldThrow && !threw){
273-
fail("Missing expected exception"+
274-
(err && err.name ? " ("+err.name+")." : '.') +
275-
(message ? " " + message : ""));
276-
}
277-
if(!shouldThrow && threw && typematters && exception instanceof err){
278-
fail("Got unwanted exception" +
279-
(err && err.name ? " ("+err.name+")." : '.') +
280-
(message ? " " + message : ""));
281-
}
282-
if((shouldThrow && threw && typematters && !(exception instanceof err)) ||
283-
(!shouldThrow && threw)){
284-
throw exception;
285-
}
284+
// EXTENSION! This is annoying to write outside this module.
285+
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
286+
_throws.apply(this, [false].concat(pSlice.call(arguments)));
286287
};

0 commit comments

Comments
 (0)