--- /dev/null
+// Copyright (c) 2011 Google, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+
+/**
+ * @fileoverview Additional assertions.
+ *
+ * @author konigsberg@google.com (Robert Konigsberg)
+ */
+
+/**
+ * Asserts that two doubles (or two arrays of doubles) are equal
+ * to within a positive delta.
+ */
+function assertEqualsDelta(msg, expected, actual, epsilon) {
+ var args = this.argsWithOptionalMsg_(arguments, 4);
+ jstestdriver.assertCount++;
+ msg = args[0];
+ expected = args[1];
+ actual = args[2];
+ epsilon = args[3];
+
+ if (!compareDelta_(expected, actual, epsilon)) {
+ this.fail(msg + 'expected ' + epsilon + ' within ' +
+ this.prettyPrintEntity_(expected) +
+ ' but was ' + this.prettyPrintEntity_(actual) + '');
+ }
+ return true;
+};
+
+
+function compareDelta_(expected, actual, epsilon) {
+ var compareDouble = function(e,a,d) {
+ return Math.abs(e - a) <= d;
+ }
+ if (expected === actual) {
+ return true;
+ }
+
+ if (typeof expected == "number" ||
+ typeof actual == "number" ||
+ !expected || !actual) {
+ return compareDouble(expected, actual, epsilon);
+ }
+
+ if (isElement_(expected) || isElement_(actual)) {
+ return false;
+ }
+
+ var key = null;
+ var actualLength = 0;
+ var expectedLength = 0;
+
+ try {
+ // If an array is expected the length of actual should be simple to
+ // determine. If it is not it is undefined.
+ if (jstestdriver.jQuery.isArray(actual)) {
+ actualLength = actual.length;
+ } else {
+ // In case it is an object it is a little bit more complicated to
+ // get the length.
+ for (key in actual) {
+ if (actual.hasOwnProperty(key)) {
+ ++actualLength;
+ }
+ }
+ }
+
+ // Arguments object
+ if (actualLength == 0 && typeof actual.length == "number") {
+ actualLength = actual.length;
+
+ for (var i = 0, l = actualLength; i < l; i++) {
+ if (!(i in actual)) {
+ actualLength = 0;
+ break;
+ }
+ }
+ }
+
+ for (key in expected) {
+ if (expected.hasOwnProperty(key)) {
+ if (!compareDelta_(expected[key], actual[key], epsilon)) {
+ return false;
+ }
+
+ ++expectedLength;
+ }
+ }
+
+ if (expectedLength != actualLength) {
+ return false;
+ }
+
+ return expectedLength == 0 ? expected.toString() == actual.toString() : true;
+ } catch (e) {
+ return false;
+ }
+};
+
};
/**
- * Test when the expected and actual values are within a certain
- * range (delta). If they're exactly apart by delta, that is considered
- * acceptable.
- */
-function assertEqualsDelta(msg, expected, actual, delta) {
- var args = this.argsWithOptionalMsg_(arguments, 4);
-
- var message = args[0];
- var exp = args[1];
- var act = args[2];
- var d = args[3];
- if (Math.abs(exp - act) > d) {
- fail(message +
- " Expected to be within " + d + " of " + exp + ", got " + act);
- }
-}
-
-/**
* Test that valueRange matches the y-axis range specifically.
*
* This is based on the assumption that 20 pixels are dedicated to the
assertEquals(0, g.toDomYCoord(50));
for (var x = 0; x <= 50; x++) {
- assertEqualsDelta(50 - x, g.toDomYCoord(x), 0.00001);
+ MoreAsserts.assertEqualsDelta(50 - x, g.toDomYCoord(x), 0.00001);
}
};