dom/tests/mochitest/ajax/mochikit/MochiKit/Test.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /***
michael@0 2
michael@0 3 MochiKit.Test 1.4
michael@0 4
michael@0 5 See <http://mochikit.com/> for documentation, downloads, license, etc.
michael@0 6
michael@0 7 (c) 2005 Bob Ippolito. All rights Reserved.
michael@0 8
michael@0 9 ***/
michael@0 10
michael@0 11 if (typeof(dojo) != 'undefined') {
michael@0 12 dojo.provide('MochiKit.Test');
michael@0 13 dojo.require('MochiKit.Base');
michael@0 14 }
michael@0 15
michael@0 16 if (typeof(JSAN) != 'undefined') {
michael@0 17 JSAN.use("MochiKit.Base", []);
michael@0 18 }
michael@0 19
michael@0 20 try {
michael@0 21 if (typeof(MochiKit.Base) == 'undefined') {
michael@0 22 throw "";
michael@0 23 }
michael@0 24 } catch (e) {
michael@0 25 throw "MochiKit.Test depends on MochiKit.Base!";
michael@0 26 }
michael@0 27
michael@0 28 if (typeof(MochiKit.Test) == 'undefined') {
michael@0 29 MochiKit.Test = {};
michael@0 30 }
michael@0 31
michael@0 32 MochiKit.Test.NAME = "MochiKit.Test";
michael@0 33 MochiKit.Test.VERSION = "1.4";
michael@0 34 MochiKit.Test.__repr__ = function () {
michael@0 35 return "[" + this.NAME + " " + this.VERSION + "]";
michael@0 36 };
michael@0 37
michael@0 38 MochiKit.Test.toString = function () {
michael@0 39 return this.__repr__();
michael@0 40 };
michael@0 41
michael@0 42
michael@0 43 MochiKit.Test.EXPORT = ["runTests"];
michael@0 44 MochiKit.Test.EXPORT_OK = [];
michael@0 45
michael@0 46 MochiKit.Test.runTests = function (obj) {
michael@0 47 if (typeof(obj) == "string") {
michael@0 48 obj = JSAN.use(obj);
michael@0 49 }
michael@0 50 var suite = new MochiKit.Test.Suite();
michael@0 51 suite.run(obj);
michael@0 52 };
michael@0 53
michael@0 54 MochiKit.Test.Suite = function () {
michael@0 55 this.testIndex = 0;
michael@0 56 MochiKit.Base.bindMethods(this);
michael@0 57 };
michael@0 58
michael@0 59 MochiKit.Test.Suite.prototype = {
michael@0 60 run: function (obj) {
michael@0 61 try {
michael@0 62 obj(this);
michael@0 63 } catch (e) {
michael@0 64 this.traceback(e);
michael@0 65 }
michael@0 66 },
michael@0 67 traceback: function (e) {
michael@0 68 var items = MochiKit.Iter.sorted(MochiKit.Base.items(e));
michael@0 69 print("not ok " + this.testIndex + " - Error thrown");
michael@0 70 for (var i = 0; i < items.length; i++) {
michael@0 71 var kv = items[i];
michael@0 72 if (kv[0] == "stack") {
michael@0 73 kv[1] = kv[1].split(/\n/)[0];
michael@0 74 }
michael@0 75 this.print("# " + kv.join(": "));
michael@0 76 }
michael@0 77 },
michael@0 78 print: function (s) {
michael@0 79 print(s);
michael@0 80 },
michael@0 81 is: function (got, expected, /* optional */message) {
michael@0 82 var res = 1;
michael@0 83 var msg = null;
michael@0 84 try {
michael@0 85 res = MochiKit.Base.compare(got, expected);
michael@0 86 } catch (e) {
michael@0 87 msg = "Can not compare " + typeof(got) + ":" + typeof(expected);
michael@0 88 }
michael@0 89 if (res) {
michael@0 90 msg = "Expected value did not compare equal";
michael@0 91 }
michael@0 92 if (!res) {
michael@0 93 return this.testResult(true, message);
michael@0 94 }
michael@0 95 return this.testResult(false, message,
michael@0 96 [[msg], ["got:", got], ["expected:", expected]]);
michael@0 97 },
michael@0 98
michael@0 99 testResult: function (pass, msg, failures) {
michael@0 100 this.testIndex += 1;
michael@0 101 if (pass) {
michael@0 102 this.print("ok " + this.testIndex + " - " + msg);
michael@0 103 return;
michael@0 104 }
michael@0 105 this.print("not ok " + this.testIndex + " - " + msg);
michael@0 106 if (failures) {
michael@0 107 for (var i = 0; i < failures.length; i++) {
michael@0 108 this.print("# " + failures[i].join(" "));
michael@0 109 }
michael@0 110 }
michael@0 111 },
michael@0 112
michael@0 113 isDeeply: function (got, expected, /* optional */message) {
michael@0 114 var m = MochiKit.Base;
michael@0 115 var res = 1;
michael@0 116 try {
michael@0 117 res = m.compare(got, expected);
michael@0 118 } catch (e) {
michael@0 119 // pass
michael@0 120 }
michael@0 121 if (res === 0) {
michael@0 122 return this.ok(true, message);
michael@0 123 }
michael@0 124 var gk = m.keys(got);
michael@0 125 var ek = m.keys(expected);
michael@0 126 gk.sort();
michael@0 127 ek.sort();
michael@0 128 if (m.compare(gk, ek)) {
michael@0 129 // differing keys
michael@0 130 var cmp = {};
michael@0 131 var i;
michael@0 132 for (i = 0; i < gk.length; i++) {
michael@0 133 cmp[gk[i]] = "got";
michael@0 134 }
michael@0 135 for (i = 0; i < ek.length; i++) {
michael@0 136 if (ek[i] in cmp) {
michael@0 137 delete cmp[ek[i]];
michael@0 138 } else {
michael@0 139 cmp[ek[i]] = "expected";
michael@0 140 }
michael@0 141 }
michael@0 142 var diffkeys = m.keys(cmp);
michael@0 143 diffkeys.sort();
michael@0 144 var gotkeys = [];
michael@0 145 var expkeys = [];
michael@0 146 while (diffkeys.length) {
michael@0 147 var k = diffkeys.shift();
michael@0 148 if (k in Object.prototype) {
michael@0 149 continue;
michael@0 150 }
michael@0 151 (cmp[k] == "got" ? gotkeys : expkeys).push(k);
michael@0 152 }
michael@0 153
michael@0 154
michael@0 155 }
michael@0 156
michael@0 157 return this.testResult((!res), msg,
michael@0 158 (msg ? [["got:", got], ["expected:", expected]] : undefined)
michael@0 159 );
michael@0 160 },
michael@0 161
michael@0 162 ok: function (res, message) {
michael@0 163 return this.testResult(res, message);
michael@0 164 }
michael@0 165 };
michael@0 166
michael@0 167 MochiKit.Test.__new__ = function () {
michael@0 168 var m = MochiKit.Base;
michael@0 169
michael@0 170 this.EXPORT_TAGS = {
michael@0 171 ":common": this.EXPORT,
michael@0 172 ":all": m.concat(this.EXPORT, this.EXPORT_OK)
michael@0 173 };
michael@0 174
michael@0 175 m.nameFunctions(this);
michael@0 176
michael@0 177 };
michael@0 178
michael@0 179 MochiKit.Test.__new__();
michael@0 180
michael@0 181 MochiKit.Base._exportSymbols(this, MochiKit.Test);

mercurial