From 80d29b32199185c9135805bfd676ee09116e0fb2 Mon Sep 17 00:00:00 2001 From: Robert Konigsberg Date: Wed, 27 Apr 2011 17:01:15 -0400 Subject: [PATCH] Moving the delta tests to MoreAsserts. MoreAsserts is actually going to go away when this code goes in to jstestdriver. I don't suggest you ... don't look too hard at MoreAsserts, but don't look too hard. A proper review will be done on the jstestdriver side. --- auto_tests/tests/MoreAsserts.js | 117 ++++++++++++++++++++++++++++++++++++++++ auto_tests/tests/sanity.js | 20 +------ 2 files changed, 118 insertions(+), 19 deletions(-) create mode 100644 auto_tests/tests/MoreAsserts.js diff --git a/auto_tests/tests/MoreAsserts.js b/auto_tests/tests/MoreAsserts.js new file mode 100644 index 0000000..b2a690a --- /dev/null +++ b/auto_tests/tests/MoreAsserts.js @@ -0,0 +1,117 @@ +// 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; + } +}; + diff --git a/auto_tests/tests/sanity.js b/auto_tests/tests/sanity.js index cbe07b4..1a56c2d 100644 --- a/auto_tests/tests/sanity.js +++ b/auto_tests/tests/sanity.js @@ -83,24 +83,6 @@ SanityTestCase.prototype.testYAxisRange_custom = function() { }; /** - * 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 @@ -115,6 +97,6 @@ SanityTestCase.prototype.testToDomYCoord = function() { 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); } }; -- 2.7.4