* Like assert_throws_js but allows specifying the assertion type * (assert_throws_js or promise_rejects_js, in practice).
(constructor, func, description,
assertion_type)
| 2189 | * (assert_throws_js or promise_rejects_js, in practice). |
| 2190 | */ |
| 2191 | function assert_throws_js_impl(constructor, func, description, |
| 2192 | assertion_type) |
| 2193 | { |
| 2194 | try { |
| 2195 | func.call(this); |
| 2196 | assert(false, assertion_type, description, |
| 2197 | "${func} did not throw", {func:func}); |
| 2198 | } catch (e) { |
| 2199 | if (e instanceof AssertionError) { |
| 2200 | throw e; |
| 2201 | } |
| 2202 | |
| 2203 | // Basic sanity-checks on the thrown exception. |
| 2204 | assert(typeof e === "object", |
| 2205 | assertion_type, description, |
| 2206 | "${func} threw ${e} with type ${type}, not an object", |
| 2207 | {func:func, e:e, type:typeof e}); |
| 2208 | |
| 2209 | assert(e !== null, |
| 2210 | assertion_type, description, |
| 2211 | "${func} threw null, not an object", |
| 2212 | {func:func}); |
| 2213 | |
| 2214 | // Basic sanity-check on the passed-in constructor |
| 2215 | assert(typeof constructor === "function", |
| 2216 | assertion_type, description, |
| 2217 | "${constructor} is not a constructor", |
| 2218 | {constructor:constructor}); |
| 2219 | var obj = constructor; |
| 2220 | while (obj) { |
| 2221 | if (typeof obj === "function" && |
| 2222 | obj.name === "Error") { |
| 2223 | break; |
| 2224 | } |
| 2225 | obj = Object.getPrototypeOf(obj); |
| 2226 | } |
| 2227 | assert(obj != null, |
| 2228 | assertion_type, description, |
| 2229 | "${constructor} is not an Error subtype", |
| 2230 | {constructor:constructor}); |
| 2231 | |
| 2232 | // And checking that our exception is reasonable |
| 2233 | assert(e.constructor === constructor && |
| 2234 | e.name === constructor.name, |
| 2235 | assertion_type, description, |
| 2236 | "${func} threw ${actual} (${actual_name}) expected instance of ${expected} (${expected_name})", |
| 2237 | {func:func, actual:e, actual_name:e.name, |
| 2238 | expected:constructor, |
| 2239 | expected_name:constructor.name}); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | // TODO: Figure out how to document the overloads better. |
| 2244 | // sphinx-js doesn't seem to handle @variation correctly, |
no test coverage detected