1
- ( function ( ) {
2
- "use strict"
3
-
4
1
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
5
2
//
6
3
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
25
22
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
23
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
24
28
- // UTILITY
29
- var util = require ( 'util' ) ;
25
+ // when used in node, this will actually load the util module we depend on
26
+ // versus loading the builtin util module as happens otherwise
27
+ // this is a bug in node module loading as far as I am concerned
28
+ var util = require ( './node_modules/util' ) ;
29
+
30
30
var pSlice = Array . prototype . slice ;
31
31
32
32
// 1. The assert module provides functions that throw
33
33
// AssertionError's when particular conditions are not met. The
34
34
// assert module must conform to the following interface.
35
35
36
- var assert = exports ;
36
+ var assert = module . exports = ok ;
37
37
38
38
// 2. The AssertionError is defined in assert.
39
39
// new assert.AssertionError({ message: message,
@@ -42,32 +42,52 @@ var assert = exports;
42
42
43
43
assert . AssertionError = function AssertionError ( options ) {
44
44
this . name = 'AssertionError' ;
45
- this . message = options . message ;
46
45
this . actual = options . actual ;
47
46
this . expected = options . expected ;
48
47
this . operator = options . operator ;
48
+ if ( options . message ) {
49
+ this . message = options . message ;
50
+ this . generatedMessage = false ;
51
+ } else {
52
+ this . message = getMessage ( this ) ;
53
+ this . generatedMessage = true ;
54
+ }
49
55
var stackStartFunction = options . stackStartFunction || fail ;
50
56
51
57
if ( Error . captureStackTrace ) {
52
58
Error . captureStackTrace ( this , stackStartFunction ) ;
53
59
}
54
60
} ;
61
+
62
+ // assert.AssertionError instanceof Error
55
63
util . inherits ( assert . AssertionError , Error ) ;
56
64
57
- assert . AssertionError . prototype . toString = function ( ) {
58
- if ( this . message ) {
59
- return [ this . name + ':' , this . message ] . join ( ' ' ) ;
60
- } else {
61
- return [ this . name + ':' ,
62
- JSON . stringify ( this . expected ) ,
63
- this . operator ,
64
- JSON . stringify ( this . actual ) ] . join ( ' ' ) ;
65
+ function replacer ( key , value ) {
66
+ if ( util . isUndefined ( value ) ) {
67
+ return '' + value ;
65
68
}
66
- } ;
69
+ if ( util . isNumber ( value ) && ( isNaN ( value ) || ! isFinite ( value ) ) ) {
70
+ return value . toString ( ) ;
71
+ }
72
+ if ( util . isFunction ( value ) || util . isRegExp ( value ) ) {
73
+ return value . toString ( ) ;
74
+ }
75
+ return value ;
76
+ }
67
77
68
- // assert.AssertionError instanceof Error
78
+ function truncate ( s , n ) {
79
+ if ( util . isString ( s ) ) {
80
+ return s . length < n ? s : s . slice ( 0 , n ) ;
81
+ } else {
82
+ return s ;
83
+ }
84
+ }
69
85
70
- assert . AssertionError . __proto__ = Error . prototype ;
86
+ function getMessage ( self ) {
87
+ return truncate ( JSON . stringify ( self . actual , replacer ) , 128 ) + ' ' +
88
+ self . operator + ' ' +
89
+ truncate ( JSON . stringify ( self . expected , replacer ) , 128 ) ;
90
+ }
71
91
72
92
// At present only the three keys mentioned above are used and
73
93
// understood by the spec. Implementations or sub modules can pass
@@ -96,13 +116,14 @@ assert.fail = fail;
96
116
// 4. Pure assertion tests whether a value is truthy, as determined
97
117
// by !!guard.
98
118
// assert.ok(guard, message_opt);
99
- // This statement is equivalent to assert.equal(true, guard,
119
+ // This statement is equivalent to assert.equal(true, !! guard,
100
120
// message_opt);. To test strictly for the value true, use
101
121
// assert.strictEqual(true, guard, message_opt);.
102
122
103
- assert . ok = function ok ( value , message ) {
104
- if ( ! ! ! value ) fail ( value , true , message , '==' , assert . ok ) ;
105
- } ;
123
+ function ok ( value , message ) {
124
+ if ( ! value ) fail ( value , true , message , '==' , assert . ok ) ;
125
+ }
126
+ assert . ok = ok ;
106
127
107
128
// 5. The equality assertion tests shallow, coercive equality with
108
129
// ==.
@@ -135,7 +156,7 @@ function _deepEqual(actual, expected) {
135
156
if ( actual === expected ) {
136
157
return true ;
137
158
138
- } else if ( Buffer . isBuffer ( actual ) && Buffer . isBuffer ( expected ) ) {
159
+ } else if ( util . isBuffer ( actual ) && util . isBuffer ( expected ) ) {
139
160
if ( actual . length != expected . length ) return false ;
140
161
141
162
for ( var i = 0 ; i < actual . length ; i ++ ) {
@@ -146,15 +167,25 @@ function _deepEqual(actual, expected) {
146
167
147
168
// 7.2. If the expected value is a Date object, the actual value is
148
169
// equivalent if it is also a Date object that refers to the same time.
149
- } else if ( actual instanceof Date && expected instanceof Date ) {
170
+ } else if ( util . isDate ( actual ) && util . isDate ( expected ) ) {
150
171
return actual . getTime ( ) === expected . getTime ( ) ;
151
172
152
- // 7.3. Other pairs that do not both pass typeof value == 'object',
173
+ // 7.3 If the expected value is a RegExp object, the actual value is
174
+ // equivalent if it is also a RegExp object with the same source and
175
+ // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
176
+ } else if ( util . isRegExp ( actual ) && util . isRegExp ( expected ) ) {
177
+ return actual . source === expected . source &&
178
+ actual . global === expected . global &&
179
+ actual . multiline === expected . multiline &&
180
+ actual . lastIndex === expected . lastIndex &&
181
+ actual . ignoreCase === expected . ignoreCase ;
182
+
183
+ // 7.4. Other pairs that do not both pass typeof value == 'object',
153
184
// equivalence is determined by ==.
154
- } else if ( typeof actual != 'object' && typeof expected != 'object' ) {
185
+ } else if ( ! util . isObject ( actual ) && ! util . isObject ( expected ) ) {
155
186
return actual == expected ;
156
187
157
- // 7.4. For all other Object pairs, including Array objects, equivalence is
188
+ // 7.5 For all other Object pairs, including Array objects, equivalence is
158
189
// determined by having the same number of owned properties (as verified
159
190
// with Object.prototype.hasOwnProperty.call), the same set of keys
160
191
// (although not necessarily the same order), equivalent values for every
@@ -165,16 +196,12 @@ function _deepEqual(actual, expected) {
165
196
}
166
197
}
167
198
168
- function isUndefinedOrNull ( value ) {
169
- return value === null || value === undefined ;
170
- }
171
-
172
199
function isArguments ( object ) {
173
200
return Object . prototype . toString . call ( object ) == '[object Arguments]' ;
174
201
}
175
202
176
203
function objEquiv ( a , b ) {
177
- if ( isUndefinedOrNull ( a ) || isUndefinedOrNull ( b ) )
204
+ if ( util . isNullOrUndefined ( a ) || util . isNullOrUndefined ( b ) )
178
205
return false ;
179
206
// an identical 'prototype' property.
180
207
if ( a . prototype !== b . prototype ) return false ;
@@ -248,7 +275,7 @@ function expectedException(actual, expected) {
248
275
return false ;
249
276
}
250
277
251
- if ( expected instanceof RegExp ) {
278
+ if ( Object . prototype . toString . call ( expected ) == '[object RegExp]' ) {
252
279
return expected . test ( actual ) ;
253
280
} else if ( actual instanceof expected ) {
254
281
return true ;
@@ -262,7 +289,7 @@ function expectedException(actual, expected) {
262
289
function _throws ( shouldThrow , block , expected , message ) {
263
290
var actual ;
264
291
265
- if ( typeof expected === 'string' ) {
292
+ if ( util . isString ( expected ) ) {
266
293
message = expected ;
267
294
expected = null ;
268
295
}
@@ -277,11 +304,11 @@ function _throws(shouldThrow, block, expected, message) {
277
304
( message ? ' ' + message : '.' ) ;
278
305
279
306
if ( shouldThrow && ! actual ) {
280
- fail ( 'Missing expected exception' + message ) ;
307
+ fail ( actual , expected , 'Missing expected exception' + message ) ;
281
308
}
282
309
283
310
if ( ! shouldThrow && expectedException ( actual , expected ) ) {
284
- fail ( 'Got unwanted exception' + message ) ;
311
+ fail ( actual , expected , 'Got unwanted exception' + message ) ;
285
312
}
286
313
287
314
if ( ( shouldThrow && actual && expected &&
@@ -298,10 +325,8 @@ assert.throws = function(block, /*optional*/error, /*optional*/message) {
298
325
} ;
299
326
300
327
// EXTENSION! This is annoying to write outside this module.
301
- assert . doesNotThrow = function ( block , /*optional*/ error , /*optional*/ message ) {
328
+ assert . doesNotThrow = function ( block , /*optional*/ message ) {
302
329
_throws . apply ( this , [ false ] . concat ( pSlice . call ( arguments ) ) ) ;
303
330
} ;
304
331
305
332
assert . ifError = function ( err ) { if ( err ) { throw err ; } } ;
306
-
307
- } ( ) ) ;
0 commit comments