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