Project

General

Profile

1
/*
2

    
3
  throws.js -- Adds a `throws_` method to AnotherWay test objects.
4

    
5
  Copyright 2005 MetaCarta, Inc., released under the BSD License.
6

    
7

    
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:
10

    
11
    <script type="text/javascript" src="throws.js"></script>
12

    
13
  Then, it can be used just like the `ok`, `fail` and other such methods
14
  in your unit tests.
15

    
16
  e.g. 
17

    
18
   t.throws_(function () {new OpenLayers.View.Map.Dynamic();},
19
             ReferenceError("No container supplied."),
20
             "OpenLayers.View.Map.Dynamic instantiation with no container "
21
             + "must throw.");
22

    
23
  This was inspired by the `assertRaises` method of Python's unittest
24
  library.
25

    
26
  Possible future enhancements:
27

    
28
    * Contribute to official AnotherWay distribution.
29
    * Use `apply` rather than require a inner function (or as an option).
30
    * Preserve the stack fields.
31

    
32
 */
33

    
34
Test.AnotherWay._test_object_t.prototype.throws_ = 
35
function (fn, expectedException, doc) {
36
    /*
37
      
38
       Executes the supplied function object catching any exception(s)
39
       thrown, then verifies the supplied expected exception occurred.
40
      
41
       If no exception is thrown the test fails.
42

    
43
       If an exception is thrown and it does not match the supplied
44
       expected exception the test fails.
45

    
46
       If the exception thrown matches the supplied expected exception
47
       the test passes.
48

    
49
       Two exceptions "match" if Test.AnotherWay's `eq` method considers
50
       the two equal when their respective stacks are ignored.
51

    
52
                      fn - The function object to be executed
53
       expectedException - The exception object expected to result
54
                     doc - Description of the test
55

    
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.
59

    
60
       Note: This function does not preserve the stack field associated
61
             with either exception.
62

    
63
     */
64
    var theCaughtException = null;
65

    
66
    try {
67
        fn();
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;
72
    }
73

    
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);
79
    } else {
80
        this.fail(doc);
81
    }
82
}
83

    
(35-35/35)