|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 * File Name: try-007.js |
|
9 * ECMA Section: |
|
10 * Description: The try statement |
|
11 * |
|
12 * This test has a for-in statement within a try block. |
|
13 * |
|
14 * |
|
15 * Author: christine@netscape.com |
|
16 * Date: 11 August 1998 |
|
17 */ |
|
18 var SECTION = "try-007"; |
|
19 var VERSION = "ECMA_2"; |
|
20 var TITLE = "The try statement: for-in"; |
|
21 |
|
22 startTest(); |
|
23 writeHeaderToLog( SECTION + " "+ TITLE); |
|
24 |
|
25 /** |
|
26 * This is the "check" function for test objects that will |
|
27 * throw an exception. |
|
28 */ |
|
29 function throwException() { |
|
30 throw EXCEPTION_STRING +": " + this.valueOf(); |
|
31 } |
|
32 var EXCEPTION_STRING = "Exception thrown:"; |
|
33 |
|
34 /** |
|
35 * This is the "check" function for test objects that do not |
|
36 * throw an exception |
|
37 */ |
|
38 function noException() { |
|
39 return this.valueOf(); |
|
40 } |
|
41 |
|
42 /** |
|
43 * Add test cases here |
|
44 */ |
|
45 TryForIn( new TryObject( "hello", throwException, true )); |
|
46 TryForIn( new TryObject( "hola", noException, false )); |
|
47 |
|
48 /** |
|
49 * Run the test. |
|
50 */ |
|
51 |
|
52 test(); |
|
53 |
|
54 /** |
|
55 * This is the object that will be the "this" in a with block. |
|
56 * The check function is either throwException() or noException(). |
|
57 * See above. |
|
58 * |
|
59 */ |
|
60 function TryObject( value, fun, exception ) { |
|
61 this.value = value; |
|
62 this.exception = exception; |
|
63 |
|
64 this.check = fun; |
|
65 this.valueOf = function () { return this.value; } |
|
66 } |
|
67 |
|
68 /** |
|
69 * This function has a for-in statement within a try block. Test cases |
|
70 * are added after the try-catch-finally statement. Within the for-in |
|
71 * block, call a function that can throw an exception. Verify that any |
|
72 * exceptions are properly caught. |
|
73 */ |
|
74 |
|
75 function TryForIn( object ) { |
|
76 try { |
|
77 for ( p in object ) { |
|
78 if ( typeof object[p] == "function" ) { |
|
79 result = object[p](); |
|
80 } |
|
81 } |
|
82 } catch ( e ) { |
|
83 result = e; |
|
84 } |
|
85 |
|
86 new TestCase( |
|
87 SECTION, |
|
88 "TryForIn( " + object+ " )", |
|
89 (object.exception ? EXCEPTION_STRING +": " + object.value : object.value), |
|
90 result ); |
|
91 |
|
92 } |