michael@0: /*** michael@0: michael@0: MochiKit.Test 1.4 michael@0: michael@0: See for documentation, downloads, license, etc. michael@0: michael@0: (c) 2005 Bob Ippolito. All rights Reserved. michael@0: michael@0: ***/ michael@0: michael@0: if (typeof(dojo) != 'undefined') { michael@0: dojo.provide('MochiKit.Test'); michael@0: dojo.require('MochiKit.Base'); michael@0: } michael@0: michael@0: if (typeof(JSAN) != 'undefined') { michael@0: JSAN.use("MochiKit.Base", []); michael@0: } michael@0: michael@0: try { michael@0: if (typeof(MochiKit.Base) == 'undefined') { michael@0: throw ""; michael@0: } michael@0: } catch (e) { michael@0: throw "MochiKit.Test depends on MochiKit.Base!"; michael@0: } michael@0: michael@0: if (typeof(MochiKit.Test) == 'undefined') { michael@0: MochiKit.Test = {}; michael@0: } michael@0: michael@0: MochiKit.Test.NAME = "MochiKit.Test"; michael@0: MochiKit.Test.VERSION = "1.4"; michael@0: MochiKit.Test.__repr__ = function () { michael@0: return "[" + this.NAME + " " + this.VERSION + "]"; michael@0: }; michael@0: michael@0: MochiKit.Test.toString = function () { michael@0: return this.__repr__(); michael@0: }; michael@0: michael@0: michael@0: MochiKit.Test.EXPORT = ["runTests"]; michael@0: MochiKit.Test.EXPORT_OK = []; michael@0: michael@0: MochiKit.Test.runTests = function (obj) { michael@0: if (typeof(obj) == "string") { michael@0: obj = JSAN.use(obj); michael@0: } michael@0: var suite = new MochiKit.Test.Suite(); michael@0: suite.run(obj); michael@0: }; michael@0: michael@0: MochiKit.Test.Suite = function () { michael@0: this.testIndex = 0; michael@0: MochiKit.Base.bindMethods(this); michael@0: }; michael@0: michael@0: MochiKit.Test.Suite.prototype = { michael@0: run: function (obj) { michael@0: try { michael@0: obj(this); michael@0: } catch (e) { michael@0: this.traceback(e); michael@0: } michael@0: }, michael@0: traceback: function (e) { michael@0: var items = MochiKit.Iter.sorted(MochiKit.Base.items(e)); michael@0: print("not ok " + this.testIndex + " - Error thrown"); michael@0: for (var i = 0; i < items.length; i++) { michael@0: var kv = items[i]; michael@0: if (kv[0] == "stack") { michael@0: kv[1] = kv[1].split(/\n/)[0]; michael@0: } michael@0: this.print("# " + kv.join(": ")); michael@0: } michael@0: }, michael@0: print: function (s) { michael@0: print(s); michael@0: }, michael@0: is: function (got, expected, /* optional */message) { michael@0: var res = 1; michael@0: var msg = null; michael@0: try { michael@0: res = MochiKit.Base.compare(got, expected); michael@0: } catch (e) { michael@0: msg = "Can not compare " + typeof(got) + ":" + typeof(expected); michael@0: } michael@0: if (res) { michael@0: msg = "Expected value did not compare equal"; michael@0: } michael@0: if (!res) { michael@0: return this.testResult(true, message); michael@0: } michael@0: return this.testResult(false, message, michael@0: [[msg], ["got:", got], ["expected:", expected]]); michael@0: }, michael@0: michael@0: testResult: function (pass, msg, failures) { michael@0: this.testIndex += 1; michael@0: if (pass) { michael@0: this.print("ok " + this.testIndex + " - " + msg); michael@0: return; michael@0: } michael@0: this.print("not ok " + this.testIndex + " - " + msg); michael@0: if (failures) { michael@0: for (var i = 0; i < failures.length; i++) { michael@0: this.print("# " + failures[i].join(" ")); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: isDeeply: function (got, expected, /* optional */message) { michael@0: var m = MochiKit.Base; michael@0: var res = 1; michael@0: try { michael@0: res = m.compare(got, expected); michael@0: } catch (e) { michael@0: // pass michael@0: } michael@0: if (res === 0) { michael@0: return this.ok(true, message); michael@0: } michael@0: var gk = m.keys(got); michael@0: var ek = m.keys(expected); michael@0: gk.sort(); michael@0: ek.sort(); michael@0: if (m.compare(gk, ek)) { michael@0: // differing keys michael@0: var cmp = {}; michael@0: var i; michael@0: for (i = 0; i < gk.length; i++) { michael@0: cmp[gk[i]] = "got"; michael@0: } michael@0: for (i = 0; i < ek.length; i++) { michael@0: if (ek[i] in cmp) { michael@0: delete cmp[ek[i]]; michael@0: } else { michael@0: cmp[ek[i]] = "expected"; michael@0: } michael@0: } michael@0: var diffkeys = m.keys(cmp); michael@0: diffkeys.sort(); michael@0: var gotkeys = []; michael@0: var expkeys = []; michael@0: while (diffkeys.length) { michael@0: var k = diffkeys.shift(); michael@0: if (k in Object.prototype) { michael@0: continue; michael@0: } michael@0: (cmp[k] == "got" ? gotkeys : expkeys).push(k); michael@0: } michael@0: michael@0: michael@0: } michael@0: michael@0: return this.testResult((!res), msg, michael@0: (msg ? [["got:", got], ["expected:", expected]] : undefined) michael@0: ); michael@0: }, michael@0: michael@0: ok: function (res, message) { michael@0: return this.testResult(res, message); michael@0: } michael@0: }; michael@0: michael@0: MochiKit.Test.__new__ = function () { michael@0: var m = MochiKit.Base; michael@0: michael@0: this.EXPORT_TAGS = { michael@0: ":common": this.EXPORT, michael@0: ":all": m.concat(this.EXPORT, this.EXPORT_OK) michael@0: }; michael@0: michael@0: m.nameFunctions(this); michael@0: michael@0: }; michael@0: michael@0: MochiKit.Test.__new__(); michael@0: michael@0: MochiKit.Base._exportSymbols(this, MochiKit.Test);