Ryan Dahl | 55048cd | 2011-03-10 08:54:52 | [diff] [blame] | 1 | // Copyright Joyent, Inc. and other Node contributors. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | // copy of this software and associated documentation files (the |
| 5 | // "Software"), to deal in the Software without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | // persons to whom the Software is furnished to do so, subject to the |
| 9 | // following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included |
| 12 | // in all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 22 | var common = require('../common'); |
Ryan Dahl | a0159b4 | 2010-12-04 23:20:34 | [diff] [blame] | 23 | var assert = require('assert'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 24 | var a = require('assert'); |
| 25 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 26 | function makeBlock(f) { |
| 27 | var args = Array.prototype.slice.call(arguments, 1); |
| 28 | return function() { |
| 29 | return f.apply(this, args); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 30 | }; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 31 | } |
| 32 | |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 33 | assert.ok(common.indirectInstanceOf(a.AssertionError.prototype, Error), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 34 | 'a.AssertionError instanceof Error'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 35 | |
Maciej Małecki | 365fdbf | 2011-10-01 11:52:24 | [diff] [blame] | 36 | assert.throws(makeBlock(a, false), a.AssertionError, 'ok(false)'); |
| 37 | |
| 38 | assert.doesNotThrow(makeBlock(a, true), a.AssertionError, 'ok(true)'); |
| 39 | |
| 40 | assert.doesNotThrow(makeBlock(a, 'test', 'ok(\'test\')')); |
| 41 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 42 | assert.throws(makeBlock(a.ok, false), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 43 | a.AssertionError, 'ok(false)'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 44 | |
| 45 | assert.doesNotThrow(makeBlock(a.ok, true), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 46 | a.AssertionError, 'ok(true)'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 47 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 48 | assert.doesNotThrow(makeBlock(a.ok, 'test'), 'ok(\'test\')'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 49 | |
| 50 | assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 51 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 52 | assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 53 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 54 | assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 55 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 56 | assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 57 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 58 | assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal'); |
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.doesNotThrow(makeBlock(a.equal, 2, '2'), 'equal'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 61 | |
| 62 | assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 63 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 64 | assert.throws(makeBlock(a.notEqual, true, true), |
| 65 | a.AssertionError, 'notEqual'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 66 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 67 | assert.throws(makeBlock(a.strictEqual, 2, '2'), |
| 68 | a.AssertionError, 'strictEqual'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 69 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 70 | assert.throws(makeBlock(a.strictEqual, null, undefined), |
| 71 | a.AssertionError, 'strictEqual'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 72 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 73 | assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, '2'), 'notStrictEqual'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 74 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 75 | // deepEquals joy! |
| 76 | // 7.2 |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 77 | assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000, 3, 14), |
| 78 | new Date(2000, 3, 14)), 'deepEqual date'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 79 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 80 | assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000, 3, 14)), |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 81 | a.AssertionError, |
| 82 | 'deepEqual date'); |
| 83 | |
| 84 | // 7.3 |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 85 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/, /a/)); |
Maciej Małecki | 5c7532e | 2011-12-19 22:28:42 | [diff] [blame] | 86 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/g, /a/g)); |
| 87 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/i, /a/i)); |
| 88 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/m, /a/m)); |
| 89 | assert.doesNotThrow(makeBlock(a.deepEqual, /a/igm, /a/igm)); |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 90 | assert.throws(makeBlock(a.deepEqual, /ab/, /a/)); |
Maciej Małecki | 5c7532e | 2011-12-19 22:28:42 | [diff] [blame] | 91 | assert.throws(makeBlock(a.deepEqual, /a/g, /a/)); |
| 92 | assert.throws(makeBlock(a.deepEqual, /a/i, /a/)); |
| 93 | assert.throws(makeBlock(a.deepEqual, /a/m, /a/)); |
| 94 | assert.throws(makeBlock(a.deepEqual, /a/igm, /a/im)); |
| 95 | |
| 96 | var re1 = /a/; |
| 97 | re1.lastIndex = 3; |
| 98 | assert.throws(makeBlock(a.deepEqual, re1, /a/)); |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 99 | |
| 100 | |
| 101 | // 7.4 |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 102 | assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 103 | assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check'); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 104 | assert.throws(makeBlock(a.deepEqual, 4, '5'), |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 105 | a.AssertionError, |
| 106 | 'deepEqual == check'); |
| 107 | |
Pedro Teixeira | a805012 | 2011-02-02 11:09:02 | [diff] [blame] | 108 | // 7.5 |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 109 | // having the same number of owned properties && the same set of keys |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 110 | assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4})); |
| 111 | assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'})); |
| 112 | assert.doesNotThrow(makeBlock(a.deepEqual, [4], ['4'])); |
| 113 | assert.throws(makeBlock(a.deepEqual, {a: 4}, {a: 4, b: true}), |
| 114 | a.AssertionError); |
| 115 | assert.doesNotThrow(makeBlock(a.deepEqual, ['a'], {0: 'a'})); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 116 | //(although not necessarily the same order), |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 117 | assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4})); |
| 118 | var a1 = [1, 2, 3]; |
| 119 | var a2 = [1, 2, 3]; |
| 120 | a1.a = 'test'; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 121 | a1.b = true; |
| 122 | a2.b = true; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 123 | a2.a = 'test'; |
| 124 | assert.throws(makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)), |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 125 | a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 126 | assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2)); |
| 127 | |
| 128 | // having an identical prototype property |
| 129 | var nbRoot = { |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 130 | toString: function() { return this.first + ' ' + this.last; } |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 131 | }; |
| 132 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 133 | function nameBuilder(first, last) { |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 134 | this.first = first; |
| 135 | this.last = last; |
| 136 | return this; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 137 | } |
| 138 | nameBuilder.prototype = nbRoot; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 139 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 140 | function nameBuilder2(first, last) { |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 141 | this.first = first; |
| 142 | this.last = last; |
| 143 | return this; |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 144 | } |
| 145 | nameBuilder2.prototype = nbRoot; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 146 | |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 147 | var nb1 = new nameBuilder('Ryan', 'Dahl'); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 148 | var nb2 = new nameBuilder2('Ryan', 'Dahl'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 149 | |
| 150 | assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2)); |
| 151 | |
| 152 | nameBuilder2.prototype = Object; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 153 | nb2 = new nameBuilder2('Ryan', 'Dahl'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 154 | assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 155 | |
teppeis | 00a7456 | 2014-12-21 15:56:33 | [diff] [blame] | 156 | // primitives and object |
| 157 | assert.throws(makeBlock(a.deepEqual, null, {}), a.AssertionError); |
| 158 | assert.throws(makeBlock(a.deepEqual, undefined, {}), a.AssertionError); |
| 159 | assert.throws(makeBlock(a.deepEqual, 'a', ['a']), a.AssertionError); |
| 160 | assert.throws(makeBlock(a.deepEqual, 'a', {0: 'a'}), a.AssertionError); |
| 161 | assert.throws(makeBlock(a.deepEqual, 1, {}), a.AssertionError); |
| 162 | assert.throws(makeBlock(a.deepEqual, true, {}), a.AssertionError); |
| 163 | if (typeof Symbol === 'symbol') { |
| 164 | assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), a.AssertionError); |
| 165 | } |
| 166 | |
| 167 | // primitive wrappers and object |
| 168 | assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), ['a']), a.AssertionError); |
| 169 | assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), {0: 'a'}), a.AssertionError); |
| 170 | assert.doesNotThrow(makeBlock(a.deepEqual, new Number(1), {}), a.AssertionError); |
| 171 | assert.doesNotThrow(makeBlock(a.deepEqual, new Boolean(true), {}), a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 172 | |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 173 | // Testing the throwing |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 174 | function thrower(errorConstructor) { |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 175 | throw new errorConstructor('test'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 176 | } |
Ryan Dahl | a0159b4 | 2010-12-04 23:20:34 | [diff] [blame] | 177 | var aethrow = makeBlock(thrower, a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 178 | aethrow = makeBlock(thrower, a.AssertionError); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 179 | |
| 180 | // the basic calls work |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 181 | assert.throws(makeBlock(thrower, a.AssertionError), |
| 182 | a.AssertionError, 'message'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 183 | assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError); |
| 184 | assert.throws(makeBlock(thrower, a.AssertionError)); |
| 185 | |
| 186 | // if not passing an error, catch all. |
| 187 | assert.throws(makeBlock(thrower, TypeError)); |
| 188 | |
| 189 | // when passing a type, only catch errors of the appropriate type |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 190 | var threw = false; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 191 | try { |
| 192 | a.throws(makeBlock(thrower, TypeError), a.AssertionError); |
| 193 | } catch (e) { |
| 194 | threw = true; |
| 195 | assert.ok(e instanceof TypeError, 'type'); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 196 | } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 197 | assert.equal(true, threw, |
| 198 | 'a.throws with an explicit error is eating extra errors', |
| 199 | a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 200 | threw = false; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 201 | |
| 202 | // doesNotThrow should pass through all errors |
| 203 | try { |
| 204 | a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 205 | } catch (e) { |
| 206 | threw = true; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 207 | assert.ok(e instanceof TypeError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 208 | } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 209 | assert.equal(true, threw, |
| 210 | 'a.doesNotThrow with an explicit error is eating extra errors'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 211 | |
| 212 | // key difference is that throwing our correct error makes an assertion error |
| 213 | try { |
| 214 | a.doesNotThrow(makeBlock(thrower, TypeError), TypeError); |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 215 | } catch (e) { |
| 216 | threw = true; |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 217 | assert.ok(e instanceof a.AssertionError); |
Karl Guertin | 4f679fd | 2009-11-30 02:20:37 | [diff] [blame] | 218 | } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 219 | assert.equal(true, threw, |
| 220 | 'a.doesNotThrow is not catching type matching errors'); |
Ryan Dahl | dd35637 | 2009-12-29 18:37:40 | [diff] [blame] | 221 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 222 | assert.throws(function() {assert.ifError(new Error('test error'))}); |
| 223 | assert.doesNotThrow(function() {assert.ifError(null)}); |
| 224 | assert.doesNotThrow(function() {assert.ifError()}); |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 225 | |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 226 | // make sure that validating using constructor really works |
| 227 | threw = false; |
| 228 | try { |
| 229 | assert.throws( |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 230 | function() { |
isaacs | 0cdf85e | 2012-02-18 23:01:35 | [diff] [blame] | 231 | throw ({}); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 232 | }, |
| 233 | Array |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 234 | ); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 235 | } catch (e) { |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 236 | threw = true; |
| 237 | } |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 238 | assert.ok(threw, 'wrong constructor validation'); |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 239 | |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 240 | // use a RegExp to validate error message |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 241 | a.throws(makeBlock(thrower, TypeError), /test/); |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 242 | |
| 243 | // use a fn to validate error object |
| 244 | a.throws(makeBlock(thrower, TypeError), function(err) { |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 245 | if ((err instanceof TypeError) && /test/.test(err)) { |
Oleg Slobodskoi | 23cf938 | 2010-12-21 17:42:52 | [diff] [blame] | 246 | return true; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 247 | } |
Oleg Slobodskoi | 0208341 | 2010-11-26 23:03:31 | [diff] [blame] | 248 | }); |
Ryan Dahl | 6394ba2 | 2011-03-30 17:18:12 | [diff] [blame] | 249 | |
| 250 | |
| 251 | // GH-207. Make sure deepEqual doesn't loop forever on circular refs |
| 252 | |
| 253 | var b = {}; |
| 254 | b.b = b; |
| 255 | |
| 256 | var c = {}; |
| 257 | c.b = c; |
| 258 | |
| 259 | var gotError = false; |
| 260 | try { |
| 261 | assert.deepEqual(b, c); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 262 | } catch (e) { |
Ryan Dahl | 6394ba2 | 2011-03-30 17:18:12 | [diff] [blame] | 263 | gotError = true; |
| 264 | } |
| 265 | |
Mike Pennisi | aae51ec | 2014-02-24 19:16:40 | [diff] [blame] | 266 | // GH-7178. Ensure reflexivity of deepEqual with `arguments` objects. |
| 267 | var args = (function() { return arguments; })(); |
| 268 | a.throws(makeBlock(a.deepEqual, [], args)); |
| 269 | a.throws(makeBlock(a.deepEqual, args, [])); |
| 270 | |
Ryan Dahl | 6394ba2 | 2011-03-30 17:18:12 | [diff] [blame] | 271 | console.log('All OK'); |
| 272 | assert.ok(gotError); |
| 273 | |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 274 | |
| 275 | // #217 |
| 276 | function testAssertionMessage(actual, expected) { |
| 277 | try { |
| 278 | assert.equal(actual, ''); |
| 279 | } catch (e) { |
| 280 | assert.equal(e.toString(), |
Ryan Dahl | 0696e78 | 2011-08-09 21:18:16 | [diff] [blame] | 281 | ['AssertionError:', expected, '==', '""'].join(' ')); |
Glen Mailer | 66b8c3c | 2013-09-11 16:18:25 | [diff] [blame] | 282 | assert.ok(e.generatedMessage, "Message not marked as generated"); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | testAssertionMessage(undefined, '"undefined"'); |
| 286 | testAssertionMessage(null, 'null'); |
| 287 | testAssertionMessage(true, 'true'); |
| 288 | testAssertionMessage(false, 'false'); |
| 289 | testAssertionMessage(0, '0'); |
| 290 | testAssertionMessage(100, '100'); |
| 291 | testAssertionMessage(NaN, '"NaN"'); |
| 292 | testAssertionMessage(Infinity, '"Infinity"'); |
| 293 | testAssertionMessage(-Infinity, '"-Infinity"'); |
| 294 | testAssertionMessage('', '""'); |
| 295 | testAssertionMessage('foo', '"foo"'); |
| 296 | testAssertionMessage([], '[]'); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 297 | testAssertionMessage([1, 2, 3], '[1,2,3]'); |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 298 | testAssertionMessage(/a/, '"/a/"'); |
| 299 | testAssertionMessage(/abc/gim, '"/abc/gim"'); |
| 300 | testAssertionMessage(function f() {}, '"function f() {}"'); |
| 301 | testAssertionMessage({}, '{}'); |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 302 | testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}'); |
| 303 | testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, |
koichik | 5f97c9a | 2011-07-10 09:47:41 | [diff] [blame] | 304 | '{"a":"NaN","b":"Infinity","c":"-Infinity"}'); |
| 305 | |
koichik | 72bc4dc | 2012-03-24 07:00:14 | [diff] [blame] | 306 | // #2893 |
| 307 | try { |
| 308 | assert.throws(function () { |
| 309 | assert.ifError(null); |
| 310 | }); |
| 311 | } catch (e) { |
| 312 | threw = true; |
| 313 | assert.equal(e.message, 'Missing expected exception..'); |
| 314 | } |
| 315 | assert.ok(threw); |
Ryan Doenges | 6101eb1 | 2013-04-14 01:48:00 | [diff] [blame] | 316 | |
| 317 | // #5292 |
| 318 | try { |
| 319 | assert.equal(1, 2); |
| 320 | } catch (e) { |
| 321 | assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2') |
Glen Mailer | 66b8c3c | 2013-09-11 16:18:25 | [diff] [blame] | 322 | assert.ok(e.generatedMessage, 'Message not marked as generated'); |
Ryan Doenges | 6101eb1 | 2013-04-14 01:48:00 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | try { |
| 326 | assert.equal(1, 2, 'oh no'); |
| 327 | } catch (e) { |
| 328 | assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no') |
Glen Mailer | 66b8c3c | 2013-09-11 16:18:25 | [diff] [blame] | 329 | assert.equal(e.generatedMessage, false, |
| 330 | 'Message incorrectly marked as generated'); |
Ryan Doenges | 6101eb1 | 2013-04-14 01:48:00 | [diff] [blame] | 331 | } |