michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * File Name: try-007.js michael@0: * ECMA Section: michael@0: * Description: The try statement michael@0: * michael@0: * This test has a for-in statement within a try block. michael@0: * michael@0: * michael@0: * Author: christine@netscape.com michael@0: * Date: 11 August 1998 michael@0: */ michael@0: var SECTION = "try-007"; michael@0: var VERSION = "ECMA_2"; michael@0: var TITLE = "The try statement: for-in"; michael@0: michael@0: startTest(); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: /** michael@0: * This is the "check" function for test objects that will michael@0: * throw an exception. michael@0: */ michael@0: function throwException() { michael@0: throw EXCEPTION_STRING +": " + this.valueOf(); michael@0: } michael@0: var EXCEPTION_STRING = "Exception thrown:"; michael@0: michael@0: /** michael@0: * This is the "check" function for test objects that do not michael@0: * throw an exception michael@0: */ michael@0: function noException() { michael@0: return this.valueOf(); michael@0: } michael@0: michael@0: /** michael@0: * Add test cases here michael@0: */ michael@0: TryForIn( new TryObject( "hello", throwException, true )); michael@0: TryForIn( new TryObject( "hola", noException, false )); michael@0: michael@0: /** michael@0: * Run the test. michael@0: */ michael@0: michael@0: test(); michael@0: michael@0: /** michael@0: * This is the object that will be the "this" in a with block. michael@0: * The check function is either throwException() or noException(). michael@0: * See above. michael@0: * michael@0: */ michael@0: function TryObject( value, fun, exception ) { michael@0: this.value = value; michael@0: this.exception = exception; michael@0: michael@0: this.check = fun; michael@0: this.valueOf = function () { return this.value; } michael@0: } michael@0: michael@0: /** michael@0: * This function has a for-in statement within a try block. Test cases michael@0: * are added after the try-catch-finally statement. Within the for-in michael@0: * block, call a function that can throw an exception. Verify that any michael@0: * exceptions are properly caught. michael@0: */ michael@0: michael@0: function TryForIn( object ) { michael@0: try { michael@0: for ( p in object ) { michael@0: if ( typeof object[p] == "function" ) { michael@0: result = object[p](); michael@0: } michael@0: } michael@0: } catch ( e ) { michael@0: result = e; michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "TryForIn( " + object+ " )", michael@0: (object.exception ? EXCEPTION_STRING +": " + object.value : object.value), michael@0: result ); michael@0: michael@0: }