* Runs code() and asserts that it throws the specified exception.
(code, type_opt, cause_opt)
| 155 | * Runs code() and asserts that it throws the specified exception. |
| 156 | */ |
| 157 | function assertThrows(code, type_opt, cause_opt) { |
| 158 | try { |
| 159 | if (typeof code == 'function') { |
| 160 | code(); |
| 161 | } else { |
| 162 | eval(code); |
| 163 | } |
| 164 | } catch (e) { |
| 165 | if (typeof type_opt == 'function') { |
| 166 | assertInstanceof(e, type_opt); |
| 167 | } |
| 168 | if (arguments.length >= 3) { |
| 169 | assertEquals(cause_opt, e.message, 'thrown exception type mismatch'); |
| 170 | } |
| 171 | // Success. |
| 172 | return; |
| 173 | } |
| 174 | var expected = arguments.length >= 3 ? cause_opt : |
| 175 | typeof type_opt == 'function' ? type_opt : 'any exception'; |
| 176 | fail(expected, 'no exception', 'expected thrown exception'); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
no test coverage detected
searching dependent graphs…