Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 1 | process.mixin(require("./common")); |
| 2 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 3 | var a = require('assert'); |
| 4 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 5 | function makeBlock (f) { |
| 6 | var args = Array.prototype.slice.call(arguments,1); |
| 7 | return function () { |
| 8 | return f.apply(this,args); |
| 9 | }; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 10 | } |
| 11 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 12 | assert.ok(a.AssertionError instanceof Error, |
| 13 | "a.AssertionError instanceof Error"); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 14 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 15 | assert.throws(makeBlock(a.ok, false), |
| 16 | a.AssertionError, "ok(false)"); |
| 17 | |
| 18 | assert.doesNotThrow(makeBlock(a.ok, true), |
| 19 | a.AssertionError, "ok(true)"); |
| 20 | |
| 21 | assert.doesNotThrow(makeBlock(a.ok, "test"), "ok('test')"); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 22 | |
| 23 | assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 24 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 25 | assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 26 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 27 | assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 28 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 29 | assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 30 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 31 | assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 32 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 33 | assert.doesNotThrow(makeBlock(a.equal, 2, "2"), 'equal'); |
| 34 | |
| 35 | assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 36 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 37 | assert.throws(makeBlock(a.notEqual, true, true), a.AssertionError, 'notEqual'); |
| 38 | |
| 39 | assert.throws(makeBlock(a.strictEqual, 2, "2"), a.AssertionError, 'strictEqual'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 40 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 41 | assert.throws(makeBlock(a.strictEqual, null, undefined), a.AssertionError, 'strictEqual'); |
| 42 | |
| 43 | assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, "2"), 'notStrictEqual'); |
| 44 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 45 | // deepEquals joy! |
| 46 | // 7.2 |
| 47 | assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000,3,14), new Date(2000,3,14)), |
| 48 | 'deepEqual date'); |
| 49 | |
| 50 | assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000,3,14)), |
| 51 | a.AssertionError, |
| 52 | 'deepEqual date'); |
| 53 | |
| 54 | // 7.3 |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 55 | assert.doesNotThrow(makeBlock(a.deepEqual, 4, "4"), 'deepEqual == check'); |
| 56 | assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 57 | assert.throws(makeBlock(a.deepEqual, 4, "5"), |
| 58 | a.AssertionError, |
| 59 | 'deepEqual == check'); |
| 60 | |
| 61 | // 7.4 |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 62 | // having the same number of owned properties && the same set of keys |
| 63 | assert.doesNotThrow(makeBlock(a.deepEqual, {a:4}, {a:4})); |
| 64 | assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"2"}, {a:4,b:"2"})); |
| 65 | assert.doesNotThrow(makeBlock(a.deepEqual, [4], ["4"])); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 66 | assert.throws(makeBlock(a.deepEqual, {a:4}, {a:4,b:true}), a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 67 | assert.doesNotThrow(makeBlock(a.deepEqual, ["a"], {0:"a"})); |
| 68 | //(although not necessarily the same order), |
| 69 | assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"1"}, {b:"1",a:4})); |
| 70 | var a1 = [1,2,3]; |
| 71 | var a2 = [1,2,3]; |
| 72 | a1.a = "test"; |
| 73 | a1.b = true; |
| 74 | a2.b = true; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 75 | a2.a = "test"; |
| 76 | assert.throws(makeBlock(a.deepEqual, |
| 77 | Object.keys(a1), |
| 78 | Object.keys(a2)), |
| 79 | a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 80 | assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2)); |
| 81 | |
| 82 | // having an identical prototype property |
| 83 | var nbRoot = { |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 84 | toString: function () { return this.first+' '+this.last; } |
| 85 | }; |
| 86 | |
| 87 | function nameBuilder (first,last) { |
| 88 | this.first = first; |
| 89 | this.last = last; |
| 90 | return this; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 91 | } |
| 92 | nameBuilder.prototype = nbRoot; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 93 | |
| 94 | function nameBuilder2 (first,last) { |
| 95 | this.first = first; |
| 96 | this.last = last; |
| 97 | return this; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 98 | } |
| 99 | nameBuilder2.prototype = nbRoot; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 100 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 101 | var nb1 = new nameBuilder('Ryan', 'Dahl'); |
| 102 | var nb2 = new nameBuilder2('Ryan','Dahl'); |
| 103 | |
| 104 | assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2)); |
| 105 | |
| 106 | nameBuilder2.prototype = Object; |
| 107 | nb2 = new nameBuilder2('Ryan','Dahl'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 108 | assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 109 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 110 | // String literal + object blew up my implementation... |
| 111 | assert.throws(makeBlock(a.deepEqual, 'a', {}), a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 112 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 113 | // Testing the throwing |
| 114 | function thrower (errorConstructor){ |
| 115 | throw new errorConstructor('test'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 116 | } |
| 117 | aethrow = makeBlock(thrower, a.AssertionError); |
| 118 | aethrow = makeBlock(thrower, a.AssertionError); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 119 | |
| 120 | // the basic calls work |
| 121 | assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError, 'message'); |
| 122 | assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError); |
| 123 | assert.throws(makeBlock(thrower, a.AssertionError)); |
| 124 | |
| 125 | // if not passing an error, catch all. |
| 126 | assert.throws(makeBlock(thrower, TypeError)); |
| 127 | |
| 128 | // when passing a type, only catch errors of the appropriate type |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 129 | var threw = false; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 130 | try { |
| 131 | a.throws(makeBlock(thrower, TypeError), a.AssertionError); |
| 132 | } catch (e) { |
| 133 | threw = true; |
| 134 | assert.ok(e instanceof TypeError, 'type'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 135 | } |
| 136 | assert.equal(true,threw,'a.throws with an explicit error is eating extra errors', a.AssertionError); |
| 137 | threw = false; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 138 | |
| 139 | // doesNotThrow should pass through all errors |
| 140 | try { |
| 141 | a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError); |
| 142 | } catch(e) { |
| 143 | threw = true |
| 144 | assert.ok(e instanceof TypeError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 145 | } |
| 146 | assert.equal(true,threw,'a.doesNotThrow with an explicit error is eating extra errors'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 147 | |
| 148 | // key difference is that throwing our correct error makes an assertion error |
| 149 | try { |
| 150 | a.doesNotThrow(makeBlock(thrower, TypeError), TypeError); |
| 151 | } catch(e) { |
| 152 | threw = true |
| 153 | assert.ok(e instanceof a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 154 | } |
| 155 | assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame^] | 156 | |