diff --git a/underscore-test-shim.js b/underscore-test-shim.js index 361780ff..f5bde303 100644 --- a/underscore-test-shim.js +++ b/underscore-test-shim.js @@ -1,5 +1,7 @@ /* * A shim for running underscore tests. + * + * See: http://api.qunitjs.com/ */ var skipErrors = true; @@ -8,50 +10,177 @@ var module = function(name) { print('MODULE', name); }; -var test = function(name, func) { +var assert_count; + +var test = function(name, arg1, arg2) { + var emsg; print('***', name); - func(); + + assert_count = 0; + if (typeof(arg1) === 'function') { + arg1(); + } else { + // deprecated + arg2(); + if (assert_count !== arg1) { + emsg = 'TEST CASE FAILED: assert count mismatch (' + assert_count + ' vs ' + arg1 + ')'; + if (skipErrors) { + print(emsg); + } else { + throw new Error(emsg); + } + } + } }; +var asyncTest = function(name, arg1, arg2) { + print('***', name, 'SKIPPED (asyncTest unimplemented)'); + return; + + assert_count = 0; + if (typeof(arg1) === 'function') { + throw new Error('unimplemented'); + } else { + // deprecated + throw new Error('unimplemented'); + if (assert_count !== arg1) { + emsg = 'TEST CASE FAILED: assert count mismatch (' + assert_count + ' vs ' + arg1 + ')'; + if (skipErrors) { + print(emsg); + } else { + throw new Error(emsg); + } + } + } +} + var handleResult = function(bool, msg) { + var emsg; + if (bool) { print('SUCCESS', msg); } else { + emsg = 'FAILURE ' + msg; if (skipErrors) { - print('FAILURE', msg); + print(emsg); } else { - throw new Error('FAILURE: ' + msg); + throw new Error(emsg); } } } var ok = function(bool, msg) { + assert_count++; handleResult(bool, msg); }; var equal = function(x, y, msg) { + assert_count++; handleResult(x == y, msg); }; +var notEqual = function(x, y, msg) { + assert_count++; + handleResult(x != y, msg); +}; + var strictEqual = function(x, y, msg) { + assert_count++; handleResult(x === y, msg); }; +var notStrictEqual = function(x, y, msg) { + assert_count++; + handleResult(x !== y, msg); +}; + var deepEqual = function(x, y, msg) { // FIXME: incorrect as key ordering matters, but close enough maybe? + assert_count++; handleResult(Duktape.jsonxEnc(x) === Duktape.jsonxEnc(y), msg); }; -// These are needed to transform the $(document).ready(fn); notation into -// a synchronous call. +var notDeepEqual = function(x, y, msg) { + // FIXME: incorrect as key ordering matters, but close enough maybe? + assert_count++; + handleResult(Duktape.jsonxEnc(x) !== Duktape.jsonxEnc(y), msg); +}; -var document = {}; +var raises = function(fn, expect_err, msg) { + var emsg; + var err_name = expect_err.name; + + assert_count++; + try { + fn(); + emsg = 'FAILURE ' + msg + ' -- did not throw ' + err_name + ' as expected'; + if (skipErrors) { + print(emsg); + } else { + throw new Error(emsg); + } + } catch (e) { + if (err_name === e.name) { + print('SUCCESS', msg); + } else { + emsg = 'FAILURE ' + msg + ' -- got unexpected error type: ' + e.name + ' (expected ' + err_name + ')'; + if (skipErrors) { + print(emsg); + } else { + throw new Error(emsg); + } + } + } +} + +// Minimal jQuery fakery. These are needed to transform the $(document).ready(fn); +// notation into a synchronous call, and to provide enough fake data and functions +// to satisfy the tests (while not breaking the test intent). + +var doc_images = []; +var document = { + images: doc_images +}; +var iObject = {}; var $ = function(arg) { - return $; + if (arg === document) { + return $; + } + if (arg === '#map-test') { + return { + children: function() { + return [ { id: 'id1' }, { id: 'id2' } ]; + } + }; + } + if (arg === doc_images) { + return [ { id: 'chart_image' } ]; + } + + // This is a bit tricky, the test is more or less bypassed. + // equal(_.size($('