Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 1 | 'use strict'; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 2 | var common = require('../common'); |
Ryan Dahl | a0159b4 | 2010-12-04 23:20:34 | [diff] [blame] | 3 | var assert = require('assert'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 4 | var a = require('assert'); |
| 5 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 6 | function makeBlock(f) { |
| 7 | var args = Array.prototype.slice.call(arguments, 1); |
| 8 | return function() { |
| 9 | return f.apply(this, args); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 10 | }; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 11 | } |
| 12 | |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 13 | assert.ok(common.indirectInstanceOf(a.AssertionError.prototype, Error), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 14 | 'a.AssertionError instanceof Error'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 15 | |
Maciej Małecki | 365fdbf | 2011-10-01 11:52:24 | [diff] [blame] | 16 | assert.throws(makeBlock(a, false), a.AssertionError, 'ok(false)'); |
| 17 | |
| 18 | assert.doesNotThrow(makeBlock(a, true), a.AssertionError, 'ok(true)'); |
| 19 | |
| 20 | assert.doesNotThrow(makeBlock(a, 'test', 'ok(\'test\')')); |
| 21 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 22 | assert.throws(makeBlock(a.ok, false), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 23 | a.AssertionError, 'ok(false)'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 24 | |
| 25 | assert.doesNotThrow(makeBlock(a.ok, true), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 26 | a.AssertionError, 'ok(true)'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 27 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 28 | assert.doesNotThrow(makeBlock(a.ok, 'test'), 'ok(\'test\')'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 29 | |
| 30 | assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 31 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 32 | assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 33 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 34 | assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 35 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 36 | assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 37 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 38 | assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 39 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 40 | assert.doesNotThrow(makeBlock(a.equal, 2, '2'), 'equal'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 41 | |
| 42 | assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 43 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 44 | assert.throws(makeBlock(a.notEqual, true, true), |
| 45 | a.AssertionError, 'notEqual'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 46 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 47 | assert.throws(makeBlock(a.strictEqual, 2, '2'), |
| 48 | a.AssertionError, 'strictEqual'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 49 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 50 | assert.throws(makeBlock(a.strictEqual, null, undefined), |
| 51 | a.AssertionError, 'strictEqual'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 52 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 53 | assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, '2'), 'notStrictEqual'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 54 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 55 | // deepEquals joy! |
| 56 | // 7.2 |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 57 | assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000, 3, 14), |
| 58 | new Date(2000, 3, 14)), 'deepEqual date'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 59 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 60 | assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000, 3, 14)), |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 61 | a.AssertionError, |
| 62 | 'deepEqual date'); |
| 63 | |
| 64 | // 7.3 |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 65 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/, /a/)); |
Maciej Małecki | 5c7532e | 2011-12-19 22:28:42 | [diff] [blame] | 66 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/g, /a/g)); |
| 67 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/i, /a/i)); |
| 68 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/m, /a/m)); |
| 69 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/igm, /a/igm)); |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 70 | assert.throws(makeBlock(a.deepEqual, /ab/, /a/)); |
Maciej Małecki | 5c7532e | 2011-12-19 22:28:42 | [diff] [blame] | 71 | assert.throws(makeBlock(a.deepEqual, /a/g, /a/)); |
| 72 | assert.throws(makeBlock(a.deepEqual, /a/i, /a/)); |
| 73 | assert.throws(makeBlock(a.deepEqual, /a/m, /a/)); |
| 74 | assert.throws(makeBlock(a.deepEqual, /a/igm, /a/im)); |
| 75 | |
| 76 | var re1 = /a/; |
| 77 | re1.lastIndex = 3; |
| 78 | assert.throws(makeBlock(a.deepEqual, re1, /a/)); |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 79 | |
| 80 | |
| 81 | // 7.4 |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 82 | assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 83 | assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check'); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 84 | assert.throws(makeBlock(a.deepEqual, 4, '5'), |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 85 | a.AssertionError, |
| 86 | 'deepEqual == check'); |
| 87 | |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 88 | // 7.5 |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 89 | // having the same number of owned properties && the same set of keys |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 90 | assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4})); |
| 91 | assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'})); |
| 92 | assert.doesNotThrow(makeBlock(a.deepEqual, [4], ['4'])); |
| 93 | assert.throws(makeBlock(a.deepEqual, {a: 4}, {a: 4, b: true}), |
| 94 | a.AssertionError); |
| 95 | assert.doesNotThrow(makeBlock(a.deepEqual, ['a'], {0: 'a'})); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 96 | //(although not necessarily the same order), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 97 | assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4})); |
| 98 | var a1 = [1, 2, 3]; |
| 99 | var a2 = [1, 2, 3]; |
| 100 | a1.a = 'test'; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 101 | a1.b = true; |
| 102 | a2.b = true; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 103 | a2.a = 'test'; |
| 104 | assert.throws(makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)), |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 105 | a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 106 | assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2)); |
| 107 | |
| 108 | // having an identical prototype property |
| 109 | var nbRoot = { |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 110 | toString: function() { return this.first + ' ' + this.last; } |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 111 | }; |
| 112 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 113 | function nameBuilder(first, last) { |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 114 | this.first = first; |
| 115 | this.last = last; |
| 116 | return this; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 117 | } |
| 118 | nameBuilder.prototype = nbRoot; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 119 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 120 | function nameBuilder2(first, last) { |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 121 | this.first = first; |
| 122 | this.last = last; |
| 123 | return this; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 124 | } |
| 125 | nameBuilder2.prototype = nbRoot; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 126 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 127 | var nb1 = new nameBuilder('Ryan', 'Dahl'); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 128 | var nb2 = new nameBuilder2('Ryan', 'Dahl'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 129 | |
| 130 | assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2)); |
| 131 | |
| 132 | nameBuilder2.prototype = Object; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 133 | nb2 = new nameBuilder2('Ryan', 'Dahl'); |
Vladimir Kurchatkin | e7573f9 | 2015-01-28 15:27:20 | [diff] [blame] | 134 | assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2)); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 135 | |
teppeis | 00a7456 | 2014-12-21 15:56:33 | [diff] [blame] | 136 | // primitives and object |
| 137 | assert.throws(makeBlock(a.deepEqual, null, {}), a.AssertionError); |
| 138 | assert.throws(makeBlock(a.deepEqual, undefined, {}), a.AssertionError); |
| 139 | assert.throws(makeBlock(a.deepEqual, 'a', ['a']), a.AssertionError); |
| 140 | assert.throws(makeBlock(a.deepEqual, 'a', {0: 'a'}), a.AssertionError); |
| 141 | assert.throws(makeBlock(a.deepEqual, 1, {}), a.AssertionError); |
| 142 | assert.throws(makeBlock(a.deepEqual, true, {}), a.AssertionError); |
| 143 | if (typeof Symbol === 'symbol') { |
| 144 | assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), a.AssertionError); |
| 145 | } |
| 146 | |
| 147 | // primitive wrappers and object |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 148 | assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), ['a']), |
| 149 | a.AssertionError); |
| 150 | assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), {0: 'a'}), |
| 151 | a.AssertionError); |
| 152 | assert.doesNotThrow(makeBlock(a.deepEqual, new Number(1), {}), |
| 153 | a.AssertionError); |
| 154 | assert.doesNotThrow(makeBlock(a.deepEqual, new Boolean(true), {}), |
| 155 | a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 156 | |
Vladimir Kurchatkin | 3f473ef | 2015-01-28 16:48:56 | [diff] [blame] | 157 | //deepStrictEqual |
| 158 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, new Date(2000, 3, 14), |
| 159 | new Date(2000, 3, 14)), 'deepStrictEqual date'); |
| 160 | |
| 161 | assert.throws(makeBlock(a.deepStrictEqual, new Date(), new Date(2000, 3, 14)), |
| 162 | a.AssertionError, |
| 163 | 'deepStrictEqual date'); |
| 164 | |
| 165 | // 7.3 - strict |
| 166 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/, /a/)); |
| 167 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/g, /a/g)); |
| 168 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/i, /a/i)); |
| 169 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/m, /a/m)); |
| 170 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/igm, /a/igm)); |
| 171 | assert.throws(makeBlock(a.deepStrictEqual, /ab/, /a/)); |
| 172 | assert.throws(makeBlock(a.deepStrictEqual, /a/g, /a/)); |
| 173 | assert.throws(makeBlock(a.deepStrictEqual, /a/i, /a/)); |
| 174 | assert.throws(makeBlock(a.deepStrictEqual, /a/m, /a/)); |
| 175 | assert.throws(makeBlock(a.deepStrictEqual, /a/igm, /a/im)); |
| 176 | |
| 177 | var re1 = /a/; |
| 178 | re1.lastIndex = 3; |
| 179 | assert.throws(makeBlock(a.deepStrictEqual, re1, /a/)); |
| 180 | |
| 181 | |
| 182 | // 7.4 - strict |
| 183 | assert.throws(makeBlock(a.deepStrictEqual, 4, '4'), |
| 184 | a.AssertionError, |
| 185 | 'deepStrictEqual === check'); |
| 186 | |
| 187 | assert.throws(makeBlock(a.deepStrictEqual, true, 1), |
| 188 | a.AssertionError, |
| 189 | 'deepStrictEqual === check'); |
| 190 | |
| 191 | assert.throws(makeBlock(a.deepStrictEqual, 4, '5'), |
| 192 | a.AssertionError, |
| 193 | 'deepStrictEqual === check'); |
| 194 | |
| 195 | // 7.5 - strict |
| 196 | // having the same number of owned properties && the same set of keys |
| 197 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4})); |
| 198 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, |
| 199 | {a: 4, b: '2'}, |
| 200 | {a: 4, b: '2'})); |
| 201 | assert.throws(makeBlock(a.deepStrictEqual, [4], ['4'])); |
| 202 | assert.throws(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4, b: true}), |
| 203 | a.AssertionError); |
| 204 | assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'})); |
| 205 | //(although not necessarily the same order), |
| 206 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, |
| 207 | {a: 4, b: '1'}, |
| 208 | {b: '1', a: 4})); |
| 209 | |
| 210 | assert.throws(makeBlock(a.deepStrictEqual, |
| 211 | [0, 1, 2, 'a', 'b'], |
| 212 | [0, 1, 2, 'b', 'a']), |
| 213 | a.AssertionError); |
| 214 | |
| 215 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, a1, a2)); |
| 216 | |
| 217 | // Prototype check |
| 218 | function Constructor1(first, last) { |
| 219 | this.first = first; |
| 220 | this.last = last; |
| 221 | } |
| 222 | |
| 223 | function Constructor2(first, last) { |
| 224 | this.first = first; |
| 225 | this.last = last; |
| 226 | } |
| 227 | |
| 228 | var obj1 = new Constructor1('Ryan', 'Dahl'); |
| 229 | var obj2 = new Constructor2('Ryan', 'Dahl'); |
| 230 | |
| 231 | assert.throws(makeBlock(a.deepStrictEqual, obj1, obj2), a.AssertionError); |
| 232 | |
| 233 | Constructor2.prototype = Constructor1.prototype; |
| 234 | obj2 = new Constructor2('Ryan', 'Dahl'); |
| 235 | |
| 236 | assert.doesNotThrow(makeBlock(a.deepStrictEqual, obj1, obj2)); |
| 237 | |
| 238 | // primitives |
| 239 | assert.throws(makeBlock(assert.deepStrictEqual, 4, '4'), |
| 240 | a.AssertionError); |
| 241 | assert.throws(makeBlock(assert.deepStrictEqual, true, 1), |
| 242 | a.AssertionError); |
| 243 | assert.throws(makeBlock(assert.deepStrictEqual, Symbol(), Symbol()), |
| 244 | a.AssertionError); |
| 245 | |
| 246 | var s = Symbol(); |
| 247 | assert.doesNotThrow(makeBlock(assert.deepStrictEqual, s, s)); |
| 248 | |
| 249 | |
| 250 | // primitives and object |
| 251 | assert.throws(makeBlock(a.deepStrictEqual, null, {}), a.AssertionError); |
| 252 | assert.throws(makeBlock(a.deepStrictEqual, undefined, {}), a.AssertionError); |
| 253 | assert.throws(makeBlock(a.deepStrictEqual, 'a', ['a']), a.AssertionError); |
| 254 | assert.throws(makeBlock(a.deepStrictEqual, 'a', {0: 'a'}), a.AssertionError); |
| 255 | assert.throws(makeBlock(a.deepStrictEqual, 1, {}), a.AssertionError); |
| 256 | assert.throws(makeBlock(a.deepStrictEqual, true, {}), a.AssertionError); |
| 257 | assert.throws(makeBlock(assert.deepStrictEqual, Symbol(), {}), |
| 258 | a.AssertionError); |
| 259 | |
| 260 | |
| 261 | // primitive wrappers and object |
| 262 | assert.throws(makeBlock(a.deepStrictEqual, new String('a'), ['a']), |
| 263 | a.AssertionError); |
| 264 | assert.throws(makeBlock(a.deepStrictEqual, new String('a'), {0: 'a'}), |
| 265 | a.AssertionError); |
| 266 | assert.throws(makeBlock(a.deepStrictEqual, new Number(1), {}), |
| 267 | a.AssertionError); |
| 268 | assert.throws(makeBlock(a.deepStrictEqual, new Boolean(true), {}), |
| 269 | a.AssertionError); |
| 270 | |
| 271 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 272 | // Testing the throwing |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 273 | function thrower(errorConstructor) { |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 274 | throw new errorConstructor('test'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 275 | } |
Ryan Dahl | a0159b4 | 2010-12-04 23:20:34 | [diff] [blame] | 276 | var aethrow = makeBlock(thrower, a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 277 | aethrow = makeBlock(thrower, a.AssertionError); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 278 | |
| 279 | // the basic calls work |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 280 | assert.throws(makeBlock(thrower, a.AssertionError), |
| 281 | a.AssertionError, 'message'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 282 | assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError); |
| 283 | assert.throws(makeBlock(thrower, a.AssertionError)); |
| 284 | |
| 285 | // if not passing an error, catch all. |
| 286 | assert.throws(makeBlock(thrower, TypeError)); |
| 287 | |
| 288 | // when passing a type, only catch errors of the appropriate type |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 289 | var threw = false; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 290 | try { |
| 291 | a.throws(makeBlock(thrower, TypeError), a.AssertionError); |
| 292 | } catch (e) { |
| 293 | threw = true; |
| 294 | assert.ok(e instanceof TypeError, 'type'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 295 | } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 296 | assert.equal(true, threw, |
| 297 | 'a.throws with an explicit error is eating extra errors', |
| 298 | a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 299 | threw = false; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 300 | |
| 301 | // doesNotThrow should pass through all errors |
| 302 | try { |
| 303 | a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 304 | } catch (e) { |
| 305 | threw = true; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 306 | assert.ok(e instanceof TypeError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 307 | } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 308 | assert.equal(true, threw, |
| 309 | 'a.doesNotThrow with an explicit error is eating extra errors'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 310 | |
| 311 | // key difference is that throwing our correct error makes an assertion error |
| 312 | try { |
| 313 | a.doesNotThrow(makeBlock(thrower, TypeError), TypeError); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 314 | } catch (e) { |
| 315 | threw = true; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 316 | assert.ok(e instanceof a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 317 | } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 318 | assert.equal(true, threw, |
| 319 | 'a.doesNotThrow is not catching type matching errors'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 320 | |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 321 | assert.throws(function() {assert.ifError(new Error('test error'));}); |
| 322 | assert.doesNotThrow(function() {assert.ifError(null);}); |
| 323 | assert.doesNotThrow(function() {assert.ifError();}); |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 324 | |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 325 | // make sure that validating using constructor really works |
| 326 | threw = false; |
| 327 | try { |
| 328 | assert.throws( |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 329 | function() { |
isaacs | 0cdf85e | 2012-02-18 23:01:35 | [diff] [blame] | 330 | throw ({}); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 331 | }, |
| 332 | Array |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 333 | ); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 334 | } catch (e) { |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 335 | threw = true; |
| 336 | } |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 337 | assert.ok(threw, 'wrong constructor validation'); |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 338 | |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 339 | // use a RegExp to validate error message |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 340 | a.throws(makeBlock(thrower, TypeError), /test/); |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 341 | |
| 342 | // use a fn to validate error object |
| 343 | a.throws(makeBlock(thrower, TypeError), function(err) { |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 344 | if ((err instanceof TypeError) && /test/.test(err)) { |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 345 | return true; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 346 | } |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 347 | }); |
Ryan Dahl | 6394ba2 | 2011-03-30 17:18:12 | [diff] [blame] | 348 | |
| 349 | |
| 350 | // GH-207. Make sure deepEqual doesn't loop forever on circular refs |
| 351 | |
| 352 | var b = {}; |
| 353 | b.b = b; |
| 354 | |
| 355 | var c = {}; |
| 356 | c.b = c; |
| 357 | |
| 358 | var gotError = false; |
| 359 | try { |
| 360 | assert.deepEqual(b, c); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 361 | } catch (e) { |
Ryan Dahl | 6394ba2 | 2011-03-30 17:18:12 | [diff] [blame] | 362 | gotError = true; |
| 363 | } |
| 364 | |
Mike Pennisi | aae51ec | 2014-02-24 19:16:40 | [diff] [blame] | 365 | // GH-7178. Ensure reflexivity of deepEqual with `arguments` objects. |
| 366 | var args = (function() { return arguments; })(); |
| 367 | a.throws(makeBlock(a.deepEqual, [], args)); |
| 368 | a.throws(makeBlock(a.deepEqual, args, [])); |
Ryan Dahl | 6394ba2 | 2011-03-30 17:18:12 | [diff] [blame] | 369 | assert.ok(gotError); |
| 370 | |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 371 | |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 372 | var circular = {y: 1}; |
| 373 | circular.x = circular; |
| 374 | |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 375 | function testAssertionMessage(actual, expected) { |
| 376 | try { |
| 377 | assert.equal(actual, ''); |
| 378 | } catch (e) { |
| 379 | assert.equal(e.toString(), |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 380 | ['AssertionError:', expected, '==', '\'\''].join(' ')); |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 381 | assert.ok(e.generatedMessage, 'Message not marked as generated'); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 382 | } |
| 383 | } |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 384 | |
| 385 | testAssertionMessage(undefined, 'undefined'); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 386 | testAssertionMessage(null, 'null'); |
| 387 | testAssertionMessage(true, 'true'); |
| 388 | testAssertionMessage(false, 'false'); |
| 389 | testAssertionMessage(0, '0'); |
| 390 | testAssertionMessage(100, '100'); |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 391 | testAssertionMessage(NaN, 'NaN'); |
| 392 | testAssertionMessage(Infinity, 'Infinity'); |
| 393 | testAssertionMessage(-Infinity, '-Infinity'); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 394 | testAssertionMessage('', '""'); |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 395 | testAssertionMessage('foo', '\'foo\''); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 396 | testAssertionMessage([], '[]'); |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 397 | testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]'); |
| 398 | testAssertionMessage(/a/, '/a/'); |
| 399 | testAssertionMessage(/abc/gim, '/abc/gim'); |
| 400 | testAssertionMessage(function f() {}, '[Function: f]'); |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 401 | testAssertionMessage(function() {}, '[Function]'); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 402 | testAssertionMessage({}, '{}'); |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 403 | testAssertionMessage(circular, '{ y: 1, x: [Circular] }'); |
| 404 | testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }'); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 405 | testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, |
cjihrig | 40e29dc | 2015-01-30 15:25:32 | [diff] [blame] | 406 | '{ a: NaN, b: Infinity, c: -Infinity }'); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 407 | |
koichik | 72bc4dc | 2012-03-24 07:00:14 | [diff] [blame] | 408 | // #2893 |
| 409 | try { |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 410 | assert.throws(function() { |
koichik | 72bc4dc | 2012-03-24 07:00:14 | [diff] [blame] | 411 | assert.ifError(null); |
| 412 | }); |
| 413 | } catch (e) { |
| 414 | threw = true; |
| 415 | assert.equal(e.message, 'Missing expected exception..'); |
| 416 | } |
| 417 | assert.ok(threw); |
Ryan Doenges | 6101eb1 | 2013-04-14 01:48:00 | [diff] [blame] | 418 | |
| 419 | // #5292 |
| 420 | try { |
| 421 | assert.equal(1, 2); |
| 422 | } catch (e) { |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 423 | assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2'); |
Glen Mailer | 66b8c3c | 2013-09-11 16:18:25 | [diff] [blame] | 424 | assert.ok(e.generatedMessage, 'Message not marked as generated'); |
Ryan Doenges | 6101eb1 | 2013-04-14 01:48:00 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | try { |
| 428 | assert.equal(1, 2, 'oh no'); |
| 429 | } catch (e) { |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 430 | assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no'); |
Glen Mailer | 66b8c3c | 2013-09-11 16:18:25 | [diff] [blame] | 431 | assert.equal(e.generatedMessage, false, |
| 432 | 'Message incorrectly marked as generated'); |
Ryan Doenges | 6101eb1 | 2013-04-14 01:48:00 | [diff] [blame] | 433 | } |
cjihrig | 14dc917 | 2015-01-12 16:13:18 | [diff] [blame] | 434 | |
| 435 | // Verify that throws() and doesNotThrow() throw on non-function block |
| 436 | function testBlockTypeError(method, block) { |
| 437 | var threw = true; |
| 438 | |
| 439 | try { |
| 440 | method(block); |
| 441 | threw = false; |
| 442 | } catch (e) { |
| 443 | assert.equal(e.toString(), 'TypeError: block must be a function'); |
| 444 | } |
| 445 | |
| 446 | assert.ok(threw); |
| 447 | } |
| 448 | |
| 449 | testBlockTypeError(assert.throws, 'string'); |
| 450 | testBlockTypeError(assert.doesNotThrow, 'string'); |
| 451 | testBlockTypeError(assert.throws, 1); |
| 452 | testBlockTypeError(assert.doesNotThrow, 1); |
| 453 | testBlockTypeError(assert.throws, true); |
| 454 | testBlockTypeError(assert.doesNotThrow, true); |
| 455 | testBlockTypeError(assert.throws, false); |
| 456 | testBlockTypeError(assert.doesNotThrow, false); |
| 457 | testBlockTypeError(assert.throws, []); |
| 458 | testBlockTypeError(assert.doesNotThrow, []); |
| 459 | testBlockTypeError(assert.throws, {}); |
| 460 | testBlockTypeError(assert.doesNotThrow, {}); |
| 461 | testBlockTypeError(assert.throws, /foo/); |
| 462 | testBlockTypeError(assert.doesNotThrow, /foo/); |
| 463 | testBlockTypeError(assert.throws, null); |
| 464 | testBlockTypeError(assert.doesNotThrow, null); |
| 465 | testBlockTypeError(assert.throws, undefined); |
| 466 | testBlockTypeError(assert.doesNotThrow, undefined); |
| 467 | |
| 468 | console.log('All OK'); |