1 opyright
2009 Google Inc
.
3 * Licensed under the Apache License
, Version
2.0 (the
"License"); you may not
4 * use this file except
in compliance
with the License
. You may obtain a copy of
7 * http
://www.apache.org/licenses/LICENSE
-2.0
9 * Unless required by applicable law or agreed to
in writing
, software
10 * distributed under the License is distributed on an
"AS IS" BASIS
, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND
, either express or implied
. See the
12 * License
for the specific language governing permissions and limitations under
15 function expectAsserts(count
) {
16 jstestdriver
.expectedAssertCount
= count
;
20 var fail
= function fail(msg
) {
21 var err
= new Error(msg
);
22 err
.name
= 'AssertError';
32 function isBoolean_(bool
) {
33 if (typeof(bool
) != 'boolean') {
34 fail('Not a boolean: ' + prettyPrintEntity_(bool
));
39 var isElement_
= (function () {
40 var div
= document
.createElement('div');
42 function isNode(obj
) {
53 return function isElement(obj
) {
54 return obj
&& obj
.nodeType
=== 1 && isNode(obj
);
59 function formatElement_(el
) {
63 tagName
= el
.tagName
.toLowerCase();
64 var str
= '<' + tagName
;
65 var attrs
= el
.attributes
, attribute
;
67 for (var i
= 0, l
= attrs
.length
; i
< l
; i
++) {
68 attribute
= attrs
.item(i
);
70 if (!!attribute
.nodeValue
) {
71 str
+= ' ' + attribute
.nodeName
+ '=\"' + attribute
.nodeValue
+ '\"';
75 return str
+ '>...</' + tagName
+ '>';
77 return '[Element]' + (!!tagName
? ' ' + tagName
: '');
82 function prettyPrintEntity_(entity
) {
83 if (isElement_(entity
)) {
84 return formatElement_(entity
);
89 if (typeof entity
== 'function') {
91 str
= entity
.toString().match(/(function [^\(]+\(\))/)[1];
94 return str
|| '[function]';
98 str
= JSON
.stringify(entity
);
101 return str
|| '[' + typeof entity
+ ']';
105 function argsWithOptionalMsg_(args
, length
) {
107 // make copy because it's bad practice to change a passed in mutable
108 // And to ensure we aren't working with an arguments array. IE gets bitchy.
109 for(var i
= 0; i
< args
.length
; i
++) {
110 copyOfArgs
.push(args
[i
]);
112 var min
= length
- 1;
114 if (args
.length
< min
) {
115 fail('expected at least ' + min
+ ' arguments, got ' + args
.length
);
116 } else if (args
.length
== length
) {
117 copyOfArgs
[0] += ' ';
119 copyOfArgs
.unshift('');
125 function assertTrue(msg
, actual
) {
126 var args
= argsWithOptionalMsg_(arguments
, 2);
127 jstestdriver
.assertCount
++;
130 if (args
[1] != true) {
131 fail(args
[0] + 'expected true but was ' + prettyPrintEntity_(args
[1]));
137 function assertFalse(msg
, actual
) {
138 var args
= argsWithOptionalMsg_(arguments
, 2);
139 jstestdriver
.assertCount
++;
142 if (args
[1] != false) {
143 fail(args
[0] + 'expected false but was ' + prettyPrintEntity_(args
[1]));
149 function assertEquals(msg
, expected
, actual
) {
150 var args
= argsWithOptionalMsg_(arguments
, 3);
151 jstestdriver
.assertCount
++;
156 if (!compare_(expected
, actual
)) {
157 fail(msg
+ 'expected ' + prettyPrintEntity_(expected
) + ' but was ' +
158 prettyPrintEntity_(actual
) + '');
164 function compare_(expected
, actual
) {
165 if (expected
=== actual
) {
169 if (typeof expected
!= 'object' ||
170 typeof actual
!= 'object' ||
171 !expected
|| !actual
) {
172 return expected
== actual
;
175 if (isElement_(expected
) || isElement_(actual
)) {
180 var actualLength
= 0;
181 var expectedLength
= 0;
184 // If an array is expected the length of actual should be simple to
185 // determine. If it is not it is undefined.
186 if (jstestdriver
.jQuery
.isArray(actual
)) {
187 actualLength
= actual
.length
;
189 // In case it is an object it is a little bit more complicated to
191 for (key
in actual
) {
192 if (actual
.hasOwnProperty(key
)) {
199 if (actualLength
== 0 && typeof actual
.length
== 'number') {
200 actualLength
= actual
.length
;
202 for (var i
= 0, l
= actualLength
; i
< l
; i
++) {
203 if (!(i
in actual
)) {
210 for (key
in expected
) {
211 if (expected
.hasOwnProperty(key
)) {
212 if (!compare_(expected
[key
], actual
[key
])) {
220 if (expectedLength
!= actualLength
) {
224 return expectedLength
== 0 ? expected
.toString() == actual
.toString() : true;
231 function assertNotEquals(msg
, expected
, actual
) {
233 assertEquals
.apply(this, arguments
);
235 if (e
.name
== 'AssertError') {
242 var args
= argsWithOptionalMsg_(arguments
, 3);
244 fail(args
[0] + 'expected ' + prettyPrintEntity_(args
[1]) +
245 ' not to be equal to ' + prettyPrintEntity_(args
[2]));
249 function assertSame(msg
, expected
, actual
) {
250 var args
= argsWithOptionalMsg_(arguments
, 3);
251 jstestdriver
.assertCount
++;
253 if (!isSame_(args
[2], args
[1])) {
254 fail(args
[0] + 'expected ' + prettyPrintEntity_(args
[1]) + ' but was ' +
255 prettyPrintEntity_(args
[2]));
261 function assertNotSame(msg
, expected
, actual
) {
262 var args
= argsWithOptionalMsg_(arguments
, 3);
263 jstestdriver
.assertCount
++;
265 if (isSame_(args
[2], args
[1])) {
266 fail(args
[0] + 'expected not same as ' + prettyPrintEntity_(args
[1]) +
267 ' but was ' + prettyPrintEntity_(args
[2]));
273 function isSame_(expected
, actual
) {
274 return actual
=== expected
;
278 function assertNull(msg
, actual
) {
279 var args
= argsWithOptionalMsg_(arguments
, 2);
280 jstestdriver
.assertCount
++;
282 if (args
[1] !== null) {
283 fail(args
[0] + 'expected null but was ' + prettyPrintEntity_(args
[1]));
289 function assertNotNull(msg
, actual
) {
290 var args
= argsWithOptionalMsg_(arguments
, 2);
291 jstestdriver
.assertCount
++;
293 if (args
[1] === null) {
294 fail(args
[0] + 'expected not null but was null');
301 function assertUndefined(msg
, actual
) {
302 var args
= argsWithOptionalMsg_(arguments
, 2);
303 jstestdriver
.assertCount
++;
305 if (typeof args
[1] != 'undefined') {
306 fail(args
[2] + 'expected undefined but was ' + prettyPrintEntity_(args
[1]));
312 function assertNotUndefined(msg
, actual
) {
313 var args
= argsWithOptionalMsg_(arguments
, 2);
314 jstestdriver
.assertCount
++;
316 if (typeof args
[1] == 'undefined') {
317 fail(args
[0] + 'expected not undefined but was undefined');
323 function assertNaN(msg
, actual
) {
324 var args
= argsWithOptionalMsg_(arguments
, 2);
325 jstestdriver
.assertCount
++;
327 if (!isNaN(args
[1])) {
328 fail(args
[0] + 'expected to be NaN but was ' + args
[1]);
335 function assertNotNaN(msg
, actual
) {
336 var args
= argsWithOptionalMsg_(arguments
, 2);
337 jstestdriver
.assertCount
++;
339 if (isNaN(args
[1])) {
340 fail(args
[0] + 'expected not to be NaN');
347 function assertException(msg
, callback
, error
) {
348 if (arguments
.length
== 1) {
349 // assertThrows(callback)
352 } else if (arguments
.length
== 2) {
353 if (typeof callback
!= 'function') {
354 // assertThrows(callback, type)
359 // assertThrows(msg, callback)
363 // assertThrows(msg, callback, type)
367 jstestdriver
.assertCount
++;
372 if (e
.name
== 'AssertError') {
376 if (error
&& e
.name
!= error
) {
377 fail(msg
+ 'expected to throw ' + error
+ ' but threw ' + e
.name
);
383 fail(msg
+ 'expected to throw exception');
387 function assertNoException(msg
, callback
) {
388 var args
= argsWithOptionalMsg_(arguments
, 2);
389 jstestdriver
.assertCount
++;
394 fail(args
[0] + 'expected not to throw exception, but threw ' + e
.name
+
395 ' (' + e
.message
+ ')');
400 function assertArray(msg
, actual
) {
401 var args
= argsWithOptionalMsg_(arguments
, 2);
402 jstestdriver
.assertCount
++;
404 if (!jstestdriver
.jQuery
.isArray(args
[1])) {
405 fail(args
[0] + 'expected to be array, but was ' +
406 prettyPrintEntity_(args
[1]));
411 function assertTypeOf(msg
, expected
, value
) {
412 var args
= argsWithOptionalMsg_(arguments
, 3);
413 jstestdriver
.assertCount
++;
414 var actual
= typeof args
[2];
416 if (actual
!= args
[1]) {
417 fail(args
[0] + 'expected to be ' + args
[1] + ' but was ' + actual
);
424 function assertBoolean(msg
, actual
) {
425 var args
= argsWithOptionalMsg_(arguments
, 2);
426 return assertTypeOf(args
[0], 'boolean', args
[1]);
430 function assertFunction(msg
, actual
) {
431 var args
= argsWithOptionalMsg_(arguments
, 2);
432 return assertTypeOf(args
[0], 'function', args
[1]);
436 function assertObject(msg
, actual
) {
437 var args
= argsWithOptionalMsg_(arguments
, 2);
438 return assertTypeOf(args
[0], 'object', args
[1]);
442 function assertNumber(msg
, actual
) {
443 var args
= argsWithOptionalMsg_(arguments
, 2);
444 return assertTypeOf(args
[0], 'number', args
[1]);
448 function assertString(msg
, actual
) {
449 var args
= argsWithOptionalMsg_(arguments
, 2);
450 return assertTypeOf(args
[0], 'string', args
[1]);
454 function assertMatch(msg
, regexp
, actual
) {
455 var args
= argsWithOptionalMsg_(arguments
, 3);
456 var isUndef
= typeof args
[2] == 'undefined';
457 jstestdriver
.assertCount
++;
460 if (isUndef
|| !args
[1].test(args
[2])) {
461 actual
= (isUndef
? _undef
: prettyPrintEntity_(args
[2]));
462 fail(args
[0] + 'expected ' + actual
+ ' to match ' + args
[1]);
469 function assertNoMatch(msg
, regexp
, actual
) {
470 var args
= argsWithOptionalMsg_(arguments
, 3);
471 jstestdriver
.assertCount
++;
473 if (args
[1].test(args
[2])) {
474 fail(args
[0] + 'expected ' + prettyPrintEntity_(args
[2]) +
475 ' not to match ' + args
[1]);
482 function assertTagName(msg
, tagName
, element
) {
483 var args
= argsWithOptionalMsg_(arguments
, 3);
484 var actual
= args
[2] && args
[2].tagName
;
486 if (String(actual
).toUpperCase() != args
[1].toUpperCase()) {
487 fail(args
[0] + 'expected tagName to be ' + args
[1] + ' but was ' + actual
);
493 function assertClassName(msg
, className
, element
) {
494 var args
= argsWithOptionalMsg_(arguments
, 3);
495 var actual
= args
[2] && args
[2].className
;
496 var regexp
= new RegExp('(^|\\s)' + args
[1] + '(\\s|$)');
499 assertMatch(args
[0], regexp
, actual
);
501 actual
= prettyPrintEntity_(actual
);
502 fail(args
[0] + 'expected class name to include ' +
503 prettyPrintEntity_(args
[1]) + ' but was ' + actual
);
510 function assertElementId(msg
, id
, element
) {
511 var args
= argsWithOptionalMsg_(arguments
, 3);
512 var actual
= args
[2] && args
[2].id
;
513 jstestdriver
.assertCount
++;
515 if (actual
!== args
[1]) {
516 fail(args
[0] + 'expected id to be ' + args
[1] + ' but was ' + actual
);
523 function assertInstanceOf(msg
, constructor
, actual
) {
524 jstestdriver
.assertCount
++;
525 var args
= argsWithOptionalMsg_(arguments
, 3);
526 var pretty
= prettyPrintEntity_(args
[2]);
527 var expected
= args
[1] && args
[1].name
|| args
[1];
529 if (args
[2] == null) {
530 fail(args
[0] + 'expected ' + pretty
+ ' to be instance of ' + expected
);
533 if (!(Object(args
[2]) instanceof args
[1])) {
534 fail(args
[0] + 'expected ' + pretty
+ ' to be instance of ' + expected
);
541 function assertNotInstanceOf(msg
, constructor
, actual
) {
542 var args
= argsWithOptionalMsg_(arguments
, 3);
543 jstestdriver
.assertCount
++;
545 if (Object(args
[2]) instanceof args
[1]) {
546 var expected
= args
[1] && args
[1].name
|| args
[1];
547 var pretty
= prettyPrintEntity_(args
[2]);
548 fail(args
[0] + 'expected ' + pretty
+ ' not to be instance of ' + expected
);
555 * Asserts that two doubles, or the elements of two arrays of doubles,
556 * are equal to within a positive delta.
558 function assertEqualsDelta(msg
, expected
, actual
, epsilon
) {
559 var args
= this.argsWithOptionalMsg_(arguments
, 4);
560 jstestdriver
.assertCount
++;
566 if (!compareDelta_(expected
, actual
, epsilon
)) {
567 this.fail(msg
+ 'expected ' + epsilon
+ ' within ' +
568 this.prettyPrintEntity_(expected
) +
569 ' but was ' + this.prettyPrintEntity_(actual
) + '');
574 function compareDelta_(expected
, actual
, epsilon
) {
575 var compareDouble
= function(e
,a
,d
) {
576 return Math
.abs(e
- a
) <= d
;
578 if (expected
=== actual
) {
582 if (typeof expected
== "number" ||
583 typeof actual
== "number" ||
584 !expected
|| !actual
) {
585 return compareDouble(expected
, actual
, epsilon
);
588 if (isElement_(expected
) || isElement_(actual
)) {
593 var actualLength
= 0;
594 var expectedLength
= 0;
597 // If an array is expected the length of actual should be simple to
598 // determine. If it is not it is undefined.
599 if (jstestdriver
.jQuery
.isArray(actual
)) {
600 actualLength
= actual
.length
;
602 // In case it is an object it is a little bit more complicated to
604 for (key
in actual
) {
605 if (actual
.hasOwnProperty(key
)) {
612 if (actualLength
== 0 && typeof actual
.length
== "number") {
613 actualLength
= actual
.length
;
615 for (var i
= 0, l
= actualLength
; i
< l
; i
++) {
616 if (!(i
in actual
)) {
623 for (key
in expected
) {
624 if (expected
.hasOwnProperty(key
)) {
625 if (!compareDelta_(expected
[key
], actual
[key
], epsilon
)) {
633 if (expectedLength
!= actualLength
) {
637 return expectedLength
== 0 ? expected
.toString() == actual
.toString() : true;
643 var assert
= assertTrue
;