5 See <http://mochikit.com/> for documentation, downloads, license, etc.
7 (c) 2005 Bob Ippolito. All rights Reserved.
11 if (typeof(dojo) != 'undefined') {
12 dojo.provide('MochiKit.Test');
13 dojo.require('MochiKit.Base');
16 if (typeof(JSAN) != 'undefined') {
17 JSAN.use("MochiKit.Base", []);
21 if (typeof(MochiKit.Base) == 'undefined') {
25 throw "MochiKit.Test depends on MochiKit.Base!";
28 if (typeof(MochiKit.Test) == 'undefined') {
32 MochiKit.Test.NAME = "MochiKit.Test";
33 MochiKit.Test.VERSION = "1.4";
34 MochiKit.Test.__repr__ = function () {
35 return "[" + this.NAME + " " + this.VERSION + "]";
38 MochiKit.Test.toString = function () {
39 return this.__repr__();
43 MochiKit.Test.EXPORT = ["runTests"];
44 MochiKit.Test.EXPORT_OK = [];
46 MochiKit.Test.runTests = function (obj) {
47 if (typeof(obj) == "string") {
50 var suite = new MochiKit.Test.Suite();
54 MochiKit.Test.Suite = function () {
56 MochiKit.Base.bindMethods(this);
59 MochiKit.Test.Suite.prototype = {
67 traceback: function (e) {
68 var items = MochiKit.Iter.sorted(MochiKit.Base.items(e));
69 print("not ok " + this.testIndex + " - Error thrown");
70 for (var i = 0; i < items.length; i++) {
72 if (kv[0] == "stack") {
73 kv[1] = kv[1].split(/\n/)[0];
75 this.print("# " + kv.join(": "));
81 is: function (got, expected, /* optional */message) {
85 res = MochiKit.Base.compare(got, expected);
87 msg = "Can not compare " + typeof(got) + ":" + typeof(expected);
90 msg = "Expected value did not compare equal";
93 return this.testResult(true, message);
95 return this.testResult(false, message,
96 [[msg], ["got:", got], ["expected:", expected]]);
99 testResult: function (pass, msg, failures) {
102 this.print("ok " + this.testIndex + " - " + msg);
105 this.print("not ok " + this.testIndex + " - " + msg);
107 for (var i = 0; i < failures.length; i++) {
108 this.print("# " + failures[i].join(" "));
113 isDeeply: function (got, expected, /* optional */message) {
114 var m = MochiKit.Base;
117 res = m.compare(got, expected);
122 return this.ok(true, message);
124 var gk = m.keys(got);
125 var ek = m.keys(expected);
128 if (m.compare(gk, ek)) {
132 for (i = 0; i < gk.length; i++) {
135 for (i = 0; i < ek.length; i++) {
139 cmp[ek[i]] = "expected";
142 var diffkeys = m.keys(cmp);
146 while (diffkeys.length) {
147 var k = diffkeys.shift();
148 if (k in Object.prototype) {
151 (cmp[k] == "got" ? gotkeys : expkeys).push(k);
157 return this.testResult((!res), msg,
158 (msg ? [["got:", got], ["expected:", expected]] : undefined)
162 ok: function (res, message) {
163 return this.testResult(res, message);
167 MochiKit.Test.__new__ = function () {
168 var m = MochiKit.Base;
171 ":common": this.EXPORT,
172 ":all": m.concat(this.EXPORT, this.EXPORT_OK)
175 m.nameFunctions(this);
179 MochiKit.Test.__new__();
181 MochiKit.Base._exportSymbols(this, MochiKit.Test);