1 // Copyright (c) 2011 Google, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * @fileoverview Additional assertions.
25 * @author konigsberg@google.com (Robert Konigsberg)
29 * Asserts that two doubles (or two arrays of doubles) are equal
30 * to within a positive delta.
32 function assertEqualsDelta(msg
, expected
, actual
, epsilon
) {
33 var args
= this.argsWithOptionalMsg_(arguments
, 4);
34 jstestdriver
.assertCount
++;
40 if (!compareDelta_(expected
, actual
, epsilon
)) {
41 this.fail(msg
+ 'expected ' + epsilon
+ ' within ' +
42 this.prettyPrintEntity_(expected
) +
43 ' but was ' + this.prettyPrintEntity_(actual
) + '');
49 function compareDelta_(expected
, actual
, epsilon
) {
50 var compareDouble
= function(e
,a
,d
) {
51 return Math
.abs(e
- a
) <= d
;
53 if (expected
=== actual
) {
57 if (typeof expected
== "number" ||
58 typeof actual
== "number" ||
59 !expected
|| !actual
) {
60 return compareDouble(expected
, actual
, epsilon
);
63 if (isElement_(expected
) || isElement_(actual
)) {
69 var expectedLength
= 0;
72 // If an array is expected the length of actual should be simple to
73 // determine. If it is not it is undefined.
74 if (jstestdriver
.jQuery
.isArray(actual
)) {
75 actualLength
= actual
.length
;
77 // In case it is an object it is a little bit more complicated to
80 if (actual
.hasOwnProperty(key
)) {
87 if (actualLength
== 0 && typeof actual
.length
== "number") {
88 actualLength
= actual
.length
;
90 for (var i
= 0, l
= actualLength
; i
< l
; i
++) {
98 for (key
in expected
) {
99 if (expected
.hasOwnProperty(key
)) {
100 if (!compareDelta_(expected
[key
], actual
[key
], epsilon
)) {
108 if (expectedLength
!= actualLength
) {
112 return expectedLength
== 0 ? expected
.toString() == actual
.toString() : true;