Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /*** |
michael@0 | 2 | |
michael@0 | 3 | MochiKit.Test 1.4.2 |
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 | MochiKit.Base._deps('Test', ['Base']); |
michael@0 | 12 | |
michael@0 | 13 | MochiKit.Test.NAME = "MochiKit.Test"; |
michael@0 | 14 | MochiKit.Test.VERSION = "1.4.2"; |
michael@0 | 15 | MochiKit.Test.__repr__ = function () { |
michael@0 | 16 | return "[" + this.NAME + " " + this.VERSION + "]"; |
michael@0 | 17 | }; |
michael@0 | 18 | |
michael@0 | 19 | MochiKit.Test.toString = function () { |
michael@0 | 20 | return this.__repr__(); |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | |
michael@0 | 24 | MochiKit.Test.EXPORT = ["runTests"]; |
michael@0 | 25 | MochiKit.Test.EXPORT_OK = []; |
michael@0 | 26 | |
michael@0 | 27 | MochiKit.Test.runTests = function (obj) { |
michael@0 | 28 | if (typeof(obj) == "string") { |
michael@0 | 29 | obj = JSAN.use(obj); |
michael@0 | 30 | } |
michael@0 | 31 | var suite = new MochiKit.Test.Suite(); |
michael@0 | 32 | suite.run(obj); |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | MochiKit.Test.Suite = function () { |
michael@0 | 36 | this.testIndex = 0; |
michael@0 | 37 | MochiKit.Base.bindMethods(this); |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | MochiKit.Test.Suite.prototype = { |
michael@0 | 41 | run: function (obj) { |
michael@0 | 42 | try { |
michael@0 | 43 | obj(this); |
michael@0 | 44 | } catch (e) { |
michael@0 | 45 | this.traceback(e); |
michael@0 | 46 | } |
michael@0 | 47 | }, |
michael@0 | 48 | traceback: function (e) { |
michael@0 | 49 | var items = MochiKit.Iter.sorted(MochiKit.Base.items(e)); |
michael@0 | 50 | print("not ok " + this.testIndex + " - Error thrown"); |
michael@0 | 51 | for (var i = 0; i < items.length; i++) { |
michael@0 | 52 | var kv = items[i]; |
michael@0 | 53 | if (kv[0] == "stack") { |
michael@0 | 54 | kv[1] = kv[1].split(/\n/)[0]; |
michael@0 | 55 | } |
michael@0 | 56 | this.print("# " + kv.join(": ")); |
michael@0 | 57 | } |
michael@0 | 58 | }, |
michael@0 | 59 | print: function (s) { |
michael@0 | 60 | print(s); |
michael@0 | 61 | }, |
michael@0 | 62 | is: function (got, expected, /* optional */message) { |
michael@0 | 63 | var res = 1; |
michael@0 | 64 | var msg = null; |
michael@0 | 65 | try { |
michael@0 | 66 | res = MochiKit.Base.compare(got, expected); |
michael@0 | 67 | } catch (e) { |
michael@0 | 68 | msg = "Can not compare " + typeof(got) + ":" + typeof(expected); |
michael@0 | 69 | } |
michael@0 | 70 | if (res) { |
michael@0 | 71 | msg = "Expected value did not compare equal"; |
michael@0 | 72 | } |
michael@0 | 73 | if (!res) { |
michael@0 | 74 | return this.testResult(true, message); |
michael@0 | 75 | } |
michael@0 | 76 | return this.testResult(false, message, |
michael@0 | 77 | [[msg], ["got:", got], ["expected:", expected]]); |
michael@0 | 78 | }, |
michael@0 | 79 | |
michael@0 | 80 | testResult: function (pass, msg, failures) { |
michael@0 | 81 | this.testIndex += 1; |
michael@0 | 82 | if (pass) { |
michael@0 | 83 | this.print("ok " + this.testIndex + " - " + msg); |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | this.print("not ok " + this.testIndex + " - " + msg); |
michael@0 | 87 | if (failures) { |
michael@0 | 88 | for (var i = 0; i < failures.length; i++) { |
michael@0 | 89 | this.print("# " + failures[i].join(" ")); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | }, |
michael@0 | 93 | |
michael@0 | 94 | isDeeply: function (got, expected, /* optional */message) { |
michael@0 | 95 | var m = MochiKit.Base; |
michael@0 | 96 | var res = 1; |
michael@0 | 97 | try { |
michael@0 | 98 | res = m.compare(got, expected); |
michael@0 | 99 | } catch (e) { |
michael@0 | 100 | // pass |
michael@0 | 101 | } |
michael@0 | 102 | if (res === 0) { |
michael@0 | 103 | return this.ok(true, message); |
michael@0 | 104 | } |
michael@0 | 105 | var gk = m.keys(got); |
michael@0 | 106 | var ek = m.keys(expected); |
michael@0 | 107 | gk.sort(); |
michael@0 | 108 | ek.sort(); |
michael@0 | 109 | if (m.compare(gk, ek)) { |
michael@0 | 110 | // differing keys |
michael@0 | 111 | var cmp = {}; |
michael@0 | 112 | var i; |
michael@0 | 113 | for (i = 0; i < gk.length; i++) { |
michael@0 | 114 | cmp[gk[i]] = "got"; |
michael@0 | 115 | } |
michael@0 | 116 | for (i = 0; i < ek.length; i++) { |
michael@0 | 117 | if (ek[i] in cmp) { |
michael@0 | 118 | delete cmp[ek[i]]; |
michael@0 | 119 | } else { |
michael@0 | 120 | cmp[ek[i]] = "expected"; |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | var diffkeys = m.keys(cmp); |
michael@0 | 124 | diffkeys.sort(); |
michael@0 | 125 | var gotkeys = []; |
michael@0 | 126 | var expkeys = []; |
michael@0 | 127 | while (diffkeys.length) { |
michael@0 | 128 | var k = diffkeys.shift(); |
michael@0 | 129 | if (k in Object.prototype) { |
michael@0 | 130 | continue; |
michael@0 | 131 | } |
michael@0 | 132 | (cmp[k] == "got" ? gotkeys : expkeys).push(k); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | return this.testResult((!res), msg, |
michael@0 | 139 | (msg ? [["got:", got], ["expected:", expected]] : undefined) |
michael@0 | 140 | ); |
michael@0 | 141 | }, |
michael@0 | 142 | |
michael@0 | 143 | ok: function (res, message) { |
michael@0 | 144 | return this.testResult(res, message); |
michael@0 | 145 | } |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | MochiKit.Test.__new__ = function () { |
michael@0 | 149 | var m = MochiKit.Base; |
michael@0 | 150 | |
michael@0 | 151 | this.EXPORT_TAGS = { |
michael@0 | 152 | ":common": this.EXPORT, |
michael@0 | 153 | ":all": m.concat(this.EXPORT, this.EXPORT_OK) |
michael@0 | 154 | }; |
michael@0 | 155 | |
michael@0 | 156 | m.nameFunctions(this); |
michael@0 | 157 | |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | MochiKit.Test.__new__(); |
michael@0 | 161 | |
michael@0 | 162 | MochiKit.Base._exportSymbols(this, MochiKit.Test); |