From 6ae500e6df83081fbc14caadd1ca342265fcd573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 8 Feb 2013 19:37:58 -0800 Subject: [PATCH] Make equals() for unit tests more clever. Supporting tolerance for numbers, and calling of #equals() on expected value. --- test/lib/helpers.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/lib/helpers.js b/test/lib/helpers.js index 5ef1a77c..52dc1d00 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -11,7 +11,7 @@ */ // Override equals to convert functions to message and execute them as tests() -function equals(actual, expected, message) { +function equals(actual, expected, message, tolerance) { if (typeof actual === 'function') { if (!message) { message = actual.toString().match( @@ -27,7 +27,14 @@ function equals(actual, expected, message) { actual = actual(); } // Let's be strict - return strictEqual(actual, expected, message); + if (tolerance !== undefined) { + var ok = Math.abs(actual - expected) <= tolerance; + return QUnit.push(ok, ok ? expected : actual, expected, message); + } else if (expected && expected.equals) { + return QUnit.push(expected.equals(actual), actual + '', expected + '', message); + } else { + return strictEqual(actual, expected, message); + } } function test(testName, expected) {