3 throws.js -- Adds a `throws_` method to AnotherWay test objects.
5 Copyright 2005 MetaCarta, Inc., released under the BSD License.
8 A reference to this file needs to be added to `run-tests.html` in the
9 head element after the AnotherWay classes are created:
11 <script type="text/javascript" src="throws.js"></script>
13 Then, it can be used just like the `ok`, `fail` and other such methods
18 t.throws_(function () {new OpenLayers.View.Map.Dynamic();},
19 ReferenceError("No container supplied."),
20 "OpenLayers.View.Map.Dynamic instantiation with no container "
23 This was inspired by the `assertRaises` method of Python's unittest
26 Possible future enhancements:
28 * Contribute to official AnotherWay distribution.
29 * Use `apply` rather than require a inner function (or as an option).
30 * Preserve the stack fields.
34 Test.AnotherWay._test_object_t.prototype.throws_ =
35 function (fn, expectedException, doc) {
38 Executes the supplied function object catching any exception(s)
39 thrown, then verifies the supplied expected exception occurred.
41 If no exception is thrown the test fails.
43 If an exception is thrown and it does not match the supplied
44 expected exception the test fails.
46 If the exception thrown matches the supplied expected exception
49 Two exceptions "match" if Test.AnotherWay's `eq` method considers
50 the two equal when their respective stacks are ignored.
52 fn - The function object to be executed
53 expectedException - The exception object expected to result
54 doc - Description of the test
56 Note: The name of this method is `throws_` (with a trailing
57 underscore) as `throws` is a reserved identifier and can
58 not be used as a method name.
60 Note: This function does not preserve the stack field associated
61 with either exception.
64 var theCaughtException = null;
68 } catch (innerCaughtException) {
69 // As `innerCaughtException` is not visible outside the scope
70 // of this `catch` block we need to make it visible explicitly.
71 theCaughtException = innerCaughtException;
74 if (theCaughtException) {
75 // We delete the stacks before comparison as they will never match.
76 delete theCaughtException.stack;
77 delete expectedException.stack;
78 this.eq(theCaughtException, expectedException, doc);