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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // Test cases borrowed and adapted from: |
michael@0 | 5 | // https://github.com/joyent/node/blob/6101eb184db77d0b11eb96e48744e57ecce4b73d/test/simple/test-assert.js |
michael@0 | 6 | // MIT license: http://opensource.org/licenses/MIT |
michael@0 | 7 | |
michael@0 | 8 | function run_test() { |
michael@0 | 9 | let ns = {}; |
michael@0 | 10 | Components.utils.import("resource://testing-common/Assert.jsm", ns); |
michael@0 | 11 | let assert = new ns.Assert(); |
michael@0 | 12 | |
michael@0 | 13 | function makeBlock(f, ...args) { |
michael@0 | 14 | return function() { |
michael@0 | 15 | return f.apply(assert, args); |
michael@0 | 16 | }; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | function protoCtrChain(o) { |
michael@0 | 20 | let result = []; |
michael@0 | 21 | while (o = o.__proto__) { |
michael@0 | 22 | result.push(o.constructor); |
michael@0 | 23 | } |
michael@0 | 24 | return result.join(); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | function indirectInstanceOf(obj, cls) { |
michael@0 | 28 | if (obj instanceof cls) { |
michael@0 | 29 | return true; |
michael@0 | 30 | } |
michael@0 | 31 | let clsChain = protoCtrChain(cls.prototype); |
michael@0 | 32 | let objChain = protoCtrChain(obj); |
michael@0 | 33 | return objChain.slice(-clsChain.length) === clsChain; |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | assert.ok(indirectInstanceOf(ns.Assert.AssertionError.prototype, Error), |
michael@0 | 37 | "Assert.AssertionError instanceof Error"); |
michael@0 | 38 | |
michael@0 | 39 | assert.throws(makeBlock(assert.ok, false), |
michael@0 | 40 | ns.Assert.AssertionError, "ok(false)"); |
michael@0 | 41 | |
michael@0 | 42 | assert.ok(true, "ok(true)"); |
michael@0 | 43 | |
michael@0 | 44 | assert.ok("test", "ok('test')"); |
michael@0 | 45 | |
michael@0 | 46 | assert.throws(makeBlock(assert.equal, true, false), ns.Assert.AssertionError, "equal"); |
michael@0 | 47 | |
michael@0 | 48 | assert.equal(null, null, "equal"); |
michael@0 | 49 | |
michael@0 | 50 | assert.equal(undefined, undefined, "equal"); |
michael@0 | 51 | |
michael@0 | 52 | assert.equal(null, undefined, "equal"); |
michael@0 | 53 | |
michael@0 | 54 | assert.equal(true, true, "equal"); |
michael@0 | 55 | |
michael@0 | 56 | assert.notEqual(true, false, "notEqual"); |
michael@0 | 57 | |
michael@0 | 58 | assert.throws(makeBlock(assert.notEqual, true, true), |
michael@0 | 59 | ns.Assert.AssertionError, "notEqual"); |
michael@0 | 60 | |
michael@0 | 61 | assert.throws(makeBlock(assert.strictEqual, 2, "2"), |
michael@0 | 62 | ns.Assert.AssertionError, "strictEqual"); |
michael@0 | 63 | |
michael@0 | 64 | assert.throws(makeBlock(assert.strictEqual, null, undefined), |
michael@0 | 65 | ns.Assert.AssertionError, "strictEqual"); |
michael@0 | 66 | |
michael@0 | 67 | assert.notStrictEqual(2, "2", "notStrictEqual"); |
michael@0 | 68 | |
michael@0 | 69 | // deepEquals joy! |
michael@0 | 70 | // 7.2 |
michael@0 | 71 | assert.deepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14), "deepEqual date"); |
michael@0 | 72 | |
michael@0 | 73 | assert.throws(makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)), |
michael@0 | 74 | ns.Assert.AssertionError, |
michael@0 | 75 | "deepEqual date"); |
michael@0 | 76 | |
michael@0 | 77 | // 7.3 |
michael@0 | 78 | assert.deepEqual(/a/, /a/); |
michael@0 | 79 | assert.deepEqual(/a/g, /a/g); |
michael@0 | 80 | assert.deepEqual(/a/i, /a/i); |
michael@0 | 81 | assert.deepEqual(/a/m, /a/m); |
michael@0 | 82 | assert.deepEqual(/a/igm, /a/igm); |
michael@0 | 83 | assert.throws(makeBlock(assert.deepEqual, /ab/, /a/)); |
michael@0 | 84 | assert.throws(makeBlock(assert.deepEqual, /a/g, /a/)); |
michael@0 | 85 | assert.throws(makeBlock(assert.deepEqual, /a/i, /a/)); |
michael@0 | 86 | assert.throws(makeBlock(assert.deepEqual, /a/m, /a/)); |
michael@0 | 87 | assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im)); |
michael@0 | 88 | |
michael@0 | 89 | let re1 = /a/; |
michael@0 | 90 | re1.lastIndex = 3; |
michael@0 | 91 | assert.throws(makeBlock(assert.deepEqual, re1, /a/)); |
michael@0 | 92 | |
michael@0 | 93 | // 7.4 |
michael@0 | 94 | assert.deepEqual(4, "4", "deepEqual == check"); |
michael@0 | 95 | assert.deepEqual(true, 1, "deepEqual == check"); |
michael@0 | 96 | assert.throws(makeBlock(assert.deepEqual, 4, "5"), |
michael@0 | 97 | ns.Assert.AssertionError, |
michael@0 | 98 | "deepEqual == check"); |
michael@0 | 99 | |
michael@0 | 100 | // 7.5 |
michael@0 | 101 | // having the same number of owned properties && the same set of keys |
michael@0 | 102 | assert.deepEqual({a: 4}, {a: 4}); |
michael@0 | 103 | assert.deepEqual({a: 4, b: "2"}, {a: 4, b: "2"}); |
michael@0 | 104 | assert.deepEqual([4], ["4"]); |
michael@0 | 105 | assert.throws(makeBlock(assert.deepEqual, {a: 4}, {a: 4, b: true}), |
michael@0 | 106 | ns.Assert.AssertionError); |
michael@0 | 107 | assert.deepEqual(["a"], {0: "a"}); |
michael@0 | 108 | |
michael@0 | 109 | let a1 = [1, 2, 3]; |
michael@0 | 110 | let a2 = [1, 2, 3]; |
michael@0 | 111 | a1.a = "test"; |
michael@0 | 112 | a1.b = true; |
michael@0 | 113 | a2.b = true; |
michael@0 | 114 | a2.a = "test"; |
michael@0 | 115 | assert.throws(makeBlock(assert.deepEqual, Object.keys(a1), Object.keys(a2)), |
michael@0 | 116 | ns.Assert.AssertionError); |
michael@0 | 117 | assert.deepEqual(a1, a2); |
michael@0 | 118 | |
michael@0 | 119 | let nbRoot = { |
michael@0 | 120 | toString: function() { return this.first + " " + this.last; } |
michael@0 | 121 | }; |
michael@0 | 122 | |
michael@0 | 123 | function nameBuilder(first, last) { |
michael@0 | 124 | this.first = first; |
michael@0 | 125 | this.last = last; |
michael@0 | 126 | return this; |
michael@0 | 127 | } |
michael@0 | 128 | nameBuilder.prototype = nbRoot; |
michael@0 | 129 | |
michael@0 | 130 | function nameBuilder2(first, last) { |
michael@0 | 131 | this.first = first; |
michael@0 | 132 | this.last = last; |
michael@0 | 133 | return this; |
michael@0 | 134 | } |
michael@0 | 135 | nameBuilder2.prototype = nbRoot; |
michael@0 | 136 | |
michael@0 | 137 | let nb1 = new nameBuilder("Ryan", "Dahl"); |
michael@0 | 138 | let nb2 = new nameBuilder2("Ryan", "Dahl"); |
michael@0 | 139 | |
michael@0 | 140 | assert.deepEqual(nb1, nb2); |
michael@0 | 141 | |
michael@0 | 142 | nameBuilder2.prototype = Object; |
michael@0 | 143 | nb2 = new nameBuilder2("Ryan", "Dahl"); |
michael@0 | 144 | assert.throws(makeBlock(assert.deepEqual, nb1, nb2), ns.Assert.AssertionError); |
michael@0 | 145 | |
michael@0 | 146 | // String literal + object |
michael@0 | 147 | assert.throws(makeBlock(assert.deepEqual, "a", {}), ns.Assert.AssertionError); |
michael@0 | 148 | |
michael@0 | 149 | // Testing the throwing |
michael@0 | 150 | function thrower(errorConstructor) { |
michael@0 | 151 | throw new errorConstructor("test"); |
michael@0 | 152 | } |
michael@0 | 153 | let aethrow = makeBlock(thrower, ns.Assert.AssertionError); |
michael@0 | 154 | aethrow = makeBlock(thrower, ns.Assert.AssertionError); |
michael@0 | 155 | |
michael@0 | 156 | // the basic calls work |
michael@0 | 157 | assert.throws(makeBlock(thrower, ns.Assert.AssertionError), |
michael@0 | 158 | ns.Assert.AssertionError, "message"); |
michael@0 | 159 | assert.throws(makeBlock(thrower, ns.Assert.AssertionError), ns.Assert.AssertionError); |
michael@0 | 160 | assert.throws(makeBlock(thrower, ns.Assert.AssertionError)); |
michael@0 | 161 | |
michael@0 | 162 | // if not passing an error, catch all. |
michael@0 | 163 | assert.throws(makeBlock(thrower, TypeError)); |
michael@0 | 164 | |
michael@0 | 165 | // when passing a type, only catch errors of the appropriate type |
michael@0 | 166 | let threw = false; |
michael@0 | 167 | try { |
michael@0 | 168 | assert.throws(makeBlock(thrower, TypeError), ns.Assert.AssertionError); |
michael@0 | 169 | } catch (e) { |
michael@0 | 170 | threw = true; |
michael@0 | 171 | assert.ok(e instanceof TypeError, "type"); |
michael@0 | 172 | } |
michael@0 | 173 | assert.equal(true, threw, |
michael@0 | 174 | "Assert.throws with an explicit error is eating extra errors", |
michael@0 | 175 | ns.Assert.AssertionError); |
michael@0 | 176 | threw = false; |
michael@0 | 177 | |
michael@0 | 178 | function ifError(err) { |
michael@0 | 179 | if (err) { |
michael@0 | 180 | throw err; |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | assert.throws(function() { |
michael@0 | 184 | ifError(new Error("test error")); |
michael@0 | 185 | }); |
michael@0 | 186 | |
michael@0 | 187 | // make sure that validating using constructor really works |
michael@0 | 188 | threw = false; |
michael@0 | 189 | try { |
michael@0 | 190 | assert.throws( |
michael@0 | 191 | function() { |
michael@0 | 192 | throw ({}); |
michael@0 | 193 | }, |
michael@0 | 194 | Array |
michael@0 | 195 | ); |
michael@0 | 196 | } catch (e) { |
michael@0 | 197 | threw = true; |
michael@0 | 198 | } |
michael@0 | 199 | assert.ok(threw, "wrong constructor validation"); |
michael@0 | 200 | |
michael@0 | 201 | // use a RegExp to validate error message |
michael@0 | 202 | assert.throws(makeBlock(thrower, TypeError), /test/); |
michael@0 | 203 | |
michael@0 | 204 | // use a fn to validate error object |
michael@0 | 205 | assert.throws(makeBlock(thrower, TypeError), function(err) { |
michael@0 | 206 | if ((err instanceof TypeError) && /test/.test(err)) { |
michael@0 | 207 | return true; |
michael@0 | 208 | } |
michael@0 | 209 | }); |
michael@0 | 210 | |
michael@0 | 211 | // Make sure deepEqual doesn't loop forever on circular refs |
michael@0 | 212 | |
michael@0 | 213 | let b = {}; |
michael@0 | 214 | b.b = b; |
michael@0 | 215 | |
michael@0 | 216 | let c = {}; |
michael@0 | 217 | c.b = c; |
michael@0 | 218 | |
michael@0 | 219 | let gotError = false; |
michael@0 | 220 | try { |
michael@0 | 221 | assert.deepEqual(b, c); |
michael@0 | 222 | } catch (e) { |
michael@0 | 223 | gotError = true; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | dump("All OK\n"); |
michael@0 | 227 | assert.ok(gotError); |
michael@0 | 228 | |
michael@0 | 229 | function testAssertionMessage(actual, expected) { |
michael@0 | 230 | try { |
michael@0 | 231 | assert.equal(actual, ""); |
michael@0 | 232 | } catch (e) { |
michael@0 | 233 | assert.equal(e.toString(), |
michael@0 | 234 | ["AssertionError:", expected, "==", '""'].join(" ")); |
michael@0 | 235 | } |
michael@0 | 236 | } |
michael@0 | 237 | testAssertionMessage(undefined, '"undefined"'); |
michael@0 | 238 | testAssertionMessage(null, "null"); |
michael@0 | 239 | testAssertionMessage(true, "true"); |
michael@0 | 240 | testAssertionMessage(false, "false"); |
michael@0 | 241 | testAssertionMessage(0, "0"); |
michael@0 | 242 | testAssertionMessage(100, "100"); |
michael@0 | 243 | testAssertionMessage(NaN, '"NaN"'); |
michael@0 | 244 | testAssertionMessage(Infinity, '"Infinity"'); |
michael@0 | 245 | testAssertionMessage(-Infinity, '"-Infinity"'); |
michael@0 | 246 | testAssertionMessage("", '""'); |
michael@0 | 247 | testAssertionMessage("foo", '"foo"'); |
michael@0 | 248 | testAssertionMessage([], "[]"); |
michael@0 | 249 | testAssertionMessage([1, 2, 3], "[1,2,3]"); |
michael@0 | 250 | testAssertionMessage(/a/, '"/a/"'); |
michael@0 | 251 | testAssertionMessage(/abc/gim, '"/abc/gim"'); |
michael@0 | 252 | testAssertionMessage(function f() {}, '"function f() {}"'); |
michael@0 | 253 | testAssertionMessage({}, "{}"); |
michael@0 | 254 | testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}'); |
michael@0 | 255 | testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, |
michael@0 | 256 | '{"a":"NaN","b":"Infinity","c":"-Infinity"}'); |
michael@0 | 257 | |
michael@0 | 258 | // https://github.com/joyent/node/issues/2893 |
michael@0 | 259 | try { |
michael@0 | 260 | assert.throws(function () { |
michael@0 | 261 | ifError(null); |
michael@0 | 262 | }); |
michael@0 | 263 | } catch (e) { |
michael@0 | 264 | threw = true; |
michael@0 | 265 | assert.equal(e.message, "Missing expected exception.."); |
michael@0 | 266 | } |
michael@0 | 267 | assert.ok(threw); |
michael@0 | 268 | |
michael@0 | 269 | // https://github.com/joyent/node/issues/5292 |
michael@0 | 270 | try { |
michael@0 | 271 | assert.equal(1, 2); |
michael@0 | 272 | } catch (e) { |
michael@0 | 273 | assert.equal(e.toString().split("\n")[0], "AssertionError: 1 == 2") |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | try { |
michael@0 | 277 | assert.equal(1, 2, "oh no"); |
michael@0 | 278 | } catch (e) { |
michael@0 | 279 | assert.equal(e.toString().split("\n")[0], "AssertionError: oh no - 1 == 2") |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | // Export Assert.jsm methods to become globally accessible. |
michael@0 | 283 | export_assertions(); |
michael@0 | 284 | |
michael@0 | 285 | // Test XPCShell-test integration: |
michael@0 | 286 | ok(true, "OK, this went well"); |
michael@0 | 287 | deepEqual(/a/g, /a/g, "deep equal should work on RegExp"); |
michael@0 | 288 | deepEqual(/a/igm, /a/igm, "deep equal should work on RegExp"); |
michael@0 | 289 | deepEqual({a: 4, b: "1"}, {b: "1", a: 4}, "deep equal should work on regular Object"); |
michael@0 | 290 | deepEqual(a1, a2, "deep equal should work on Array with Object properties"); |
michael@0 | 291 | |
michael@0 | 292 | // Test robustness of reporting: |
michael@0 | 293 | equal(new ns.Assert.AssertionError({ |
michael@0 | 294 | actual: { |
michael@0 | 295 | toJSON: function() { |
michael@0 | 296 | throw "bam!"; |
michael@0 | 297 | } |
michael@0 | 298 | }, |
michael@0 | 299 | expected: "foo", |
michael@0 | 300 | operator: "=" |
michael@0 | 301 | }).message, "[object Object] = \"foo\""); |
michael@0 | 302 | } |