| 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 | gTestfile = 'try-006.js'; |
| 7 | |
| 8 | /** |
| 9 | * File Name: try-006.js |
| 10 | * ECMA Section: |
| 11 | * Description: The try statement |
| 12 | * |
| 13 | * Throw an exception from within a With block in a try block. Verify |
| 14 | * that any expected exceptions are caught. |
| 15 | * |
| 16 | * Author: [email protected] |
| 17 | * Date: 11 August 1998 |
| 18 | */ |
| 19 | var SECTION = "try-006"; |
| 20 | var VERSION = "ECMA_2"; |
| 21 | var TITLE = "The try statement"; |
| 22 | |
| 23 | startTest(); |
| 24 | writeHeaderToLog( SECTION + " "+ TITLE); |
| 25 | |
| 26 | /** |
| 27 | * This is the "check" function for test objects that will |
| 28 | * throw an exception. |
| 29 | */ |
| 30 | function throwException() { |
| 31 | throw EXCEPTION_STRING +": " + this.valueOf(); |
| 32 | } |
| 33 | var EXCEPTION_STRING = "Exception thrown:"; |
| 34 | |
| 35 | /** |
| 36 | * This is the "check" function for test objects that do not |
| 37 | * throw an exception |
| 38 | */ |
| 39 | function noException() { |
| 40 | return this.valueOf(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add test cases here |
| 45 | */ |
| 46 | TryWith( new TryObject( "hello", throwException, true )); |
| 47 | TryWith( new TryObject( "hola", noException, false )); |
| 48 | |
| 49 | /** |
| 50 | * Run the test. |
| 51 | */ |
| 52 | |
| 53 | test(); |
| 54 | |
| 55 | /** |
| 56 | * This is the object that will be the "this" in a with block. |
| 57 | */ |
| 58 | function TryObject( value, fun, exception ) { |
| 59 | this.value = value; |
| 60 | this.exception = exception; |
| 61 | |
| 62 | this.valueOf = new Function ( "return this.value" ); |
| 63 | this.check = fun; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * This function has the try block that has a with block within it. |
| 68 | * Test cases are added in this function. Within the with block, the |
| 69 | * object's "check" function is called. If the test object's exception |
| 70 | * property is true, we expect the result to be the exception value. |
| 71 | * If exception is false, then we expect the result to be the value of |
| 72 | * the object. |
| 73 | */ |
| 74 | function TryWith( object ) { |
| 75 | try { |
| 76 | with ( object ) { |
| 77 | result = check(); |
| 78 | } |
| 79 | } catch ( e ) { |
| 80 | result = e; |
| 81 | } |
| 82 | |
| 83 | new TestCase( |
| 84 | SECTION, |
| 85 | "TryWith( " + object.value +" )", |
| 86 | (object.exception ? EXCEPTION_STRING +": " + object.valueOf() : object.valueOf()), |
| 87 | result ); |
| 88 | } |