Add support for animated zooms.
[dygraphs.git] / auto_tests / lib / Asserts.js
index b3ed441..aa907c1 100644 (file)
@@ -1,25 +1,24 @@
-/*
+/**
  * Copyright 2009 Google Inc.
- *
+ * 
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
- *
+ * 
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-
 function expectAsserts(count) {
   jstestdriver.expectedAssertCount = count;
 }
 
 
-this.fail = function fail(msg) {
+var fail = function fail(msg) {
   var err = new Error(msg);
   err.name = 'AssertError';
 
@@ -28,15 +27,16 @@ this.fail = function fail(msg) {
   }
 
   throw err;
-}
+};
 
 
 function isBoolean_(bool) {
   if (typeof(bool) != 'boolean') {
-    this.fail('Not a boolean: ' + this.prettyPrintEntity_(bool));
+    fail('Not a boolean: ' + prettyPrintEntity_(bool));
   }
 }
 
+
 var isElement_ = (function () {
   var div = document.createElement('div');
 
@@ -56,28 +56,30 @@ var isElement_ = (function () {
   };
 }());
 
+
 function formatElement_(el) {
   var tagName;
 
   try {
     tagName = el.tagName.toLowerCase();
-    var str = "<" + tagName;
+    var str = '<' + tagName;
     var attrs = el.attributes, attribute;
 
     for (var i = 0, l = attrs.length; i < l; i++) {
       attribute = attrs.item(i);
 
       if (!!attribute.nodeValue) {
-        str += " " + attribute.nodeName + "=\"" + attribute.nodeValue + "\"";
+        str += ' ' + attribute.nodeName + '=\"' + attribute.nodeValue + '\"';
       }
     }
 
-    return str + ">...</" + tagName + ">";
+    return str + '>...</' + tagName + '>';
   } catch (e) {
-    return "[Element]" + (!!tagName ? " " + tagName : "");
+    return '[Element]' + (!!tagName ? ' ' + tagName : '');
   }
 }
 
+
 function prettyPrintEntity_(entity) {
   if (isElement_(entity)) {
     return formatElement_(entity);
@@ -85,19 +87,19 @@ function prettyPrintEntity_(entity) {
 
   var str;
 
-  if (typeof entity == "function") {
+  if (typeof entity == 'function') {
     try {
       str = entity.toString().match(/(function [^\(]+\(\))/)[1];
     } catch (e) {}
 
-    return str || "[function]";
+    return str || '[function]';
   }
 
   try {
-    str = this.JSON.stringify(entity);
+    str = JSON.stringify(entity);
   } catch (e) {}
 
-  return str || "[" + typeof entity + "]";
+  return str || '[' + typeof entity + ']';
 }
 
 
@@ -111,56 +113,53 @@ function argsWithOptionalMsg_(args, length) {
   var min = length - 1;
 
   if (args.length < min) {
-    this.fail("expected at least " +
-              min + " arguments, got " + args.length);
+    fail('expected at least ' + min + ' arguments, got ' + args.length);
   } else if (args.length == length) {
-    copyOfArgs[0] += " ";
+    copyOfArgs[0] += ' ';
   } else {
-    copyOfArgs.unshift("");
+    copyOfArgs.unshift('');
   }
   return copyOfArgs;
 }
 
 
 function assertTrue(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   isBoolean_(args[1]);
   if (args[1] != true) {
-    this.fail(args[0] + 'expected true but was ' +
-              this.prettyPrintEntity_(args[1]));
+    fail(args[0] + 'expected true but was ' + prettyPrintEntity_(args[1]));
   }
   return true;
-};
+}
 
 
 function assertFalse(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   isBoolean_(args[1]);
   if (args[1] != false) {
-    this.fail(args[0] + 'expected false but was ' +
-              this.prettyPrintEntity_(args[1]));
+    fail(args[0] + 'expected false but was ' + prettyPrintEntity_(args[1]));
   }
   return true;
-};
+}
 
 
 function assertEquals(msg, expected, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   jstestdriver.assertCount++;
   msg = args[0];
   expected = args[1];
   actual = args[2];
 
   if (!compare_(expected, actual)) {
-    this.fail(msg + 'expected ' + this.prettyPrintEntity_(expected) +
-              ' but was ' + this.prettyPrintEntity_(actual) + '');
+    fail(msg + 'expected ' + prettyPrintEntity_(expected) + ' but was ' +
+        prettyPrintEntity_(actual) + '');
   }
   return true;
-};
+}
 
 
 function compare_(expected, actual) {
@@ -168,8 +167,8 @@ function compare_(expected, actual) {
     return true;
   }
 
-  if (typeof expected != "object" ||
-      typeof actual != "object" ||
+  if (typeof expected != 'object' ||
+      typeof actual != 'object' ||
       !expected || !actual) {
     return expected == actual;
   }
@@ -198,7 +197,7 @@ function compare_(expected, actual) {
     }
 
     // Arguments object
-    if (actualLength == 0 && typeof actual.length == "number") {
+    if (actualLength == 0 && typeof actual.length == 'number') {
       actualLength = actual.length;
 
       for (var i = 0, l = actualLength; i < l; i++) {
@@ -227,110 +226,107 @@ function compare_(expected, actual) {
   } catch (e) {
     return false;
   }
-};
+}
 
 
 function assertNotEquals(msg, expected, actual) {
   try {
     assertEquals.apply(this, arguments);
   } catch (e) {
-    if (e.name == "AssertError") {
+    if (e.name == 'AssertError') {
       return true;
     }
 
     throw e;
   }
 
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
 
-  this.fail(args[0] + "expected " + this.prettyPrintEntity_(args[1]) +
-            " not to be equal to " + this.prettyPrintEntity_(args[2]));
+  fail(args[0] + 'expected ' + prettyPrintEntity_(args[1]) +
+      ' not to be equal to ' + prettyPrintEntity_(args[2]));
 }
 
 
 function assertSame(msg, expected, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   jstestdriver.assertCount++;
 
   if (!isSame_(args[2], args[1])) {
-    this.fail(args[0] + 'expected ' + this.prettyPrintEntity_(args[1]) +
-              ' but was ' + this.prettyPrintEntity_(args[2]));
+    fail(args[0] + 'expected ' + prettyPrintEntity_(args[1]) + ' but was ' +
+        prettyPrintEntity_(args[2]));
   }
   return true;
-};
+}
 
 
 function assertNotSame(msg, expected, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   jstestdriver.assertCount++;
 
   if (isSame_(args[2], args[1])) {
-    this.fail(args[0] + 'expected not same as ' +
-              this.prettyPrintEntity_(args[1]) + ' but was ' +
-              this.prettyPrintEntity_(args[2]));
+    fail(args[0] + 'expected not same as ' + prettyPrintEntity_(args[1]) +
+        ' but was ' + prettyPrintEntity_(args[2]));
   }
   return true;
-};
+}
 
 
 function isSame_(expected, actual) {
   return actual === expected;
-};
+}
 
 
 function assertNull(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   if (args[1] !== null) {
-    this.fail(args[0] + 'expected null but was ' +
-              this.prettyPrintEntity_(args[1]));
+    fail(args[0] + 'expected null but was ' + prettyPrintEntity_(args[1]));
   }
   return true;
-};
+}
 
 
 function assertNotNull(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   if (args[1] === null) {
-    this.fail(args[0] + "expected not null but was null");
+    fail(args[0] + 'expected not null but was null');
   }
 
   return true;
-};
+}
 
 
 function assertUndefined(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
-  if (typeof args[1] != "undefined") {
-    this.fail(args[2] + "expected undefined but was " +
-              this.prettyPrintEntity_(args[1]));
+  if (typeof args[1] != 'undefined') {
+    fail(args[2] + 'expected undefined but was ' + prettyPrintEntity_(args[1]));
   }
   return true;
-};
+}
 
 
 function assertNotUndefined(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
-  if (typeof args[1] == "undefined") {
-    this.fail(args[0] + 'expected not undefined but was undefined');
+  if (typeof args[1] == 'undefined') {
+    fail(args[0] + 'expected not undefined but was undefined');
   }
   return true;
-};
+}
 
 
 function assertNaN(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   if (!isNaN(args[1])) {
-    this.fail(args[0] + "expected to be NaN but was " + args[1]);
+    fail(args[0] + 'expected to be NaN but was ' + args[1]);
   }
 
   return true;
@@ -338,11 +334,11 @@ function assertNaN(msg, actual) {
 
 
 function assertNotNaN(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   if (isNaN(args[1])) {
-    this.fail(args[0] + "expected not to be NaN");
+    fail(args[0] + 'expected not to be NaN');
   }
 
   return true;
@@ -353,20 +349,20 @@ function assertException(msg, callback, error) {
   if (arguments.length == 1) {
     // assertThrows(callback)
     callback = msg;
-    msg = "";
+    msg = '';
   } else if (arguments.length == 2) {
-    if (typeof callback != "function") {
+    if (typeof callback != 'function') {
       // assertThrows(callback, type)
       error = callback;
       callback = msg;
-      msg = "";
+      msg = '';
     } else {
       // assertThrows(msg, callback)
-      msg += " ";
+      msg += ' ';
     }
   } else {
     // assertThrows(msg, callback, type)
-    msg += " ";
+    msg += ' ';
   }
 
   jstestdriver.assertCount++;
@@ -374,53 +370,52 @@ function assertException(msg, callback, error) {
   try {
     callback();
   } catch(e) {
-    if (e.name == "AssertError") {
+    if (e.name == 'AssertError') {
       throw e;
     }
 
     if (error && e.name != error) {
-      this.fail(msg + "expected to throw " +
-                error + " but threw " + e.name);
+      fail(msg + 'expected to throw ' + error + ' but threw ' + e.name);
     }
 
     return true;
   }
 
-  this.fail(msg + "expected to throw exception");
+  fail(msg + 'expected to throw exception');
 }
 
 
 function assertNoException(msg, callback) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   try {
     args[1]();
   } catch(e) {
-    fail(args[0] + "expected not to throw exception, but threw " +
-         e.name + " (" + e.message + ")");
+    fail(args[0] + 'expected not to throw exception, but threw ' + e.name +
+        ' (' + e.message + ')');
   }
 }
 
 
 function assertArray(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
+  var args = argsWithOptionalMsg_(arguments, 2);
   jstestdriver.assertCount++;
 
   if (!jstestdriver.jQuery.isArray(args[1])) {
-    fail(args[0] + "expected to be array, but was " +
-         this.prettyPrintEntity_(args[1]));
+    fail(args[0] + 'expected to be array, but was ' +
+        prettyPrintEntity_(args[1]));
   }
 }
 
 
 function assertTypeOf(msg, expected, value) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   jstestdriver.assertCount++;
   var actual = typeof args[2];
 
   if (actual != args[1]) {
-    this.fail(args[0] + "expected to be " + args[1] + " but was " + actual);
+    fail(args[0] + 'expected to be ' + args[1] + ' but was ' + actual);
   }
 
   return true;
@@ -428,44 +423,44 @@ function assertTypeOf(msg, expected, value) {
 
 
 function assertBoolean(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
-  return assertTypeOf(args[0], "boolean", args[1]);
+  var args = argsWithOptionalMsg_(arguments, 2);
+  return assertTypeOf(args[0], 'boolean', args[1]);
 }
 
 
 function assertFunction(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
-  return assertTypeOf(args[0], "function", args[1]);
+  var args = argsWithOptionalMsg_(arguments, 2);
+  return assertTypeOf(args[0], 'function', args[1]);
 }
 
 
 function assertObject(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
-  return assertTypeOf(args[0], "object", args[1]);
+  var args = argsWithOptionalMsg_(arguments, 2);
+  return assertTypeOf(args[0], 'object', args[1]);
 }
 
 
 function assertNumber(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
-  return assertTypeOf(args[0], "number", args[1]);
+  var args = argsWithOptionalMsg_(arguments, 2);
+  return assertTypeOf(args[0], 'number', args[1]);
 }
 
 
 function assertString(msg, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 2);
-  return assertTypeOf(args[0], "string", args[1]);
+  var args = argsWithOptionalMsg_(arguments, 2);
+  return assertTypeOf(args[0], 'string', args[1]);
 }
 
 
 function assertMatch(msg, regexp, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
-  var isUndef = typeof args[2] == "undefined";
+  var args = argsWithOptionalMsg_(arguments, 3);
+  var isUndef = typeof args[2] == 'undefined';
   jstestdriver.assertCount++;
   var _undef;
 
   if (isUndef || !args[1].test(args[2])) {
-    actual = (isUndef ? _undef : this.prettyPrintEntity_(args[2]));
-    this.fail(args[0] + "expected " + actual + " to match " + args[1]);
+    actual = (isUndef ? _undef : prettyPrintEntity_(args[2]));
+    fail(args[0] + 'expected ' + actual + ' to match ' + args[1]);
   }
 
   return true;
@@ -473,12 +468,12 @@ function assertMatch(msg, regexp, actual) {
 
 
 function assertNoMatch(msg, regexp, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   jstestdriver.assertCount++;
 
   if (args[1].test(args[2])) {
-    this.fail(args[0] + "expected " + this.prettyPrintEntity_(args[2]) +
-              " not to match " + args[1]);
+    fail(args[0] + 'expected ' + prettyPrintEntity_(args[2]) +
+        ' not to match ' + args[1]);
   }
 
   return true;
@@ -486,28 +481,27 @@ function assertNoMatch(msg, regexp, actual) {
 
 
 function assertTagName(msg, tagName, element) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   var actual = args[2] && args[2].tagName;
 
   if (String(actual).toUpperCase() != args[1].toUpperCase()) {
-    this.fail(args[0] + "expected tagName to be " + args[1] + " but was " +
-              actual);
+    fail(args[0] + 'expected tagName to be ' + args[1] + ' but was ' + actual);
   }
   return true;
 }
 
 
 function assertClassName(msg, className, element) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   var actual = args[2] && args[2].className;
-  var regexp = new RegExp("(^|\\s)" + args[1] + "(\\s|$)");
+  var regexp = new RegExp('(^|\\s)' + args[1] + '(\\s|$)');
 
   try {
-    this.assertMatch(args[0], regexp, actual);
+    assertMatch(args[0], regexp, actual);
   } catch (e) {
-    actual = this.prettyPrintEntity_(actual);
-    this.fail(args[0] + "expected class name to include " +
-              this.prettyPrintEntity_(args[1]) + " but was " + actual);
+    actual = prettyPrintEntity_(actual);
+    fail(args[0] + 'expected class name to include ' +
+        prettyPrintEntity_(args[1]) + ' but was ' + actual);
   }
 
   return true;
@@ -515,13 +509,12 @@ function assertClassName(msg, className, element) {
 
 
 function assertElementId(msg, id, element) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   var actual = args[2] && args[2].id;
   jstestdriver.assertCount++;
 
   if (actual !== args[1]) {
-    this.fail(args[0] + "expected id to be " + args[1] + " but was " +
-              actual);
+    fail(args[0] + 'expected id to be ' + args[1] + ' but was ' + actual);
   }
 
   return true;
@@ -530,18 +523,16 @@ function assertElementId(msg, id, element) {
 
 function assertInstanceOf(msg, constructor, actual) {
   jstestdriver.assertCount++;
-  var args = this.argsWithOptionalMsg_(arguments, 3);
-  var pretty = this.prettyPrintEntity_(args[2]);
+  var args = argsWithOptionalMsg_(arguments, 3);
+  var pretty = prettyPrintEntity_(args[2]);
   var expected = args[1] && args[1].name || args[1];
 
   if (args[2] == null) {
-    this.fail(args[0] + "expected " + pretty + " to be instance of " +
-              expected);
+    fail(args[0] + 'expected ' + pretty + ' to be instance of ' + expected);
   }
 
   if (!(Object(args[2]) instanceof args[1])) {
-    this.fail(args[0] + "expected " + pretty + " to be instance of " +
-              expected);
+    fail(args[0] + 'expected ' + pretty + ' to be instance of ' + expected);
   }
 
   return true;
@@ -549,17 +540,105 @@ function assertInstanceOf(msg, constructor, actual) {
 
 
 function assertNotInstanceOf(msg, constructor, actual) {
-  var args = this.argsWithOptionalMsg_(arguments, 3);
+  var args = argsWithOptionalMsg_(arguments, 3);
   jstestdriver.assertCount++;
 
   if (Object(args[2]) instanceof args[1]) {
     var expected = args[1] && args[1].name || args[1];
-    var pretty = this.prettyPrintEntity_(args[2]);
-    this.fail(args[0] + "expected " + pretty + " not to be instance of " +
-              expected);
+    var pretty = prettyPrintEntity_(args[2]);
+    fail(args[0] + 'expected ' + pretty + ' not to be instance of ' + expected);
   }
 
   return true;
 }
 
+/**
+ * Asserts that two doubles, or the elements of 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;
+  }
+};
+
 var assert = assertTrue;