merge in kberg changes
[dygraphs.git] / auto_tests / tests / MoreAsserts.js
CommitLineData
718ad8e2 1// Copyright (c) 2011 Google, Inc.
80d29b32
RK
2//
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:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
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
19// THE SOFTWARE.
20
21
22/**
23 * @fileoverview Additional assertions.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
27
28/**
29 * Asserts that two doubles (or two arrays of doubles) are equal
30 * to within a positive delta.
31 */
32function assertEqualsDelta(msg, expected, actual, epsilon) {
33 var args = this.argsWithOptionalMsg_(arguments, 4);
34 jstestdriver.assertCount++;
35 msg = args[0];
36 expected = args[1];
37 actual = args[2];
38 epsilon = args[3];
39
40 if (!compareDelta_(expected, actual, epsilon)) {
41 this.fail(msg + 'expected ' + epsilon + ' within ' +
42 this.prettyPrintEntity_(expected) +
43 ' but was ' + this.prettyPrintEntity_(actual) + '');
44 }
45 return true;
46};
47
48
49function compareDelta_(expected, actual, epsilon) {
50 var compareDouble = function(e,a,d) {
51 return Math.abs(e - a) <= d;
52 }
53 if (expected === actual) {
54 return true;
55 }
56
57 if (typeof expected == "number" ||
58 typeof actual == "number" ||
59 !expected || !actual) {
60 return compareDouble(expected, actual, epsilon);
61 }
62
63 if (isElement_(expected) || isElement_(actual)) {
64 return false;
65 }
66
67 var key = null;
68 var actualLength = 0;
69 var expectedLength = 0;
70
71 try {
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;
76 } else {
77 // In case it is an object it is a little bit more complicated to
78 // get the length.
79 for (key in actual) {
80 if (actual.hasOwnProperty(key)) {
81 ++actualLength;
82 }
83 }
84 }
85
86 // Arguments object
87 if (actualLength == 0 && typeof actual.length == "number") {
88 actualLength = actual.length;
89
90 for (var i = 0, l = actualLength; i < l; i++) {
91 if (!(i in actual)) {
92 actualLength = 0;
93 break;
94 }
95 }
96 }
97
98 for (key in expected) {
99 if (expected.hasOwnProperty(key)) {
100 if (!compareDelta_(expected[key], actual[key], epsilon)) {
101 return false;
102 }
103
104 ++expectedLength;
105 }
106 }
107
108 if (expectedLength != actualLength) {
109 return false;
110 }
111
112 return expectedLength == 0 ? expected.toString() == actual.toString() : true;
113 } catch (e) {
114 return false;
115 }
116};
117