js/src/tests/ecma_2/Statements/try-007.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_2/Statements/try-007.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,92 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 + *  File Name:          try-007.js
    1.12 + *  ECMA Section:
    1.13 + *  Description:        The try statement
    1.14 + *
    1.15 + *  This test has a for-in statement within a try block.
    1.16 + *
    1.17 + *
    1.18 + *  Author:             christine@netscape.com
    1.19 + *  Date:               11 August 1998
    1.20 + */
    1.21 +var SECTION = "try-007";
    1.22 +var VERSION = "ECMA_2";
    1.23 +var TITLE   = "The try statement:  for-in";
    1.24 +
    1.25 +startTest();
    1.26 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.27 +
    1.28 +/**
    1.29 + *  This is the "check" function for test objects that will
    1.30 + *  throw an exception.
    1.31 + */
    1.32 +function throwException() {
    1.33 +  throw EXCEPTION_STRING +": " + this.valueOf();
    1.34 +}
    1.35 +var EXCEPTION_STRING = "Exception thrown:";
    1.36 +
    1.37 +/**
    1.38 + *  This is the "check" function for test objects that do not
    1.39 + *  throw an exception
    1.40 + */
    1.41 +function noException() {
    1.42 +  return this.valueOf();
    1.43 +}
    1.44 +
    1.45 +/**
    1.46 + *  Add test cases here
    1.47 + */
    1.48 +TryForIn( new TryObject( "hello", throwException, true ));
    1.49 +TryForIn( new TryObject( "hola",  noException, false ));
    1.50 +
    1.51 +/**
    1.52 + *  Run the test.
    1.53 + */
    1.54 +
    1.55 +test();
    1.56 +
    1.57 +/**
    1.58 + *  This is the object that will be the "this" in a with block.
    1.59 + *  The check function is either throwException() or noException().
    1.60 + *  See above.
    1.61 + *
    1.62 + */
    1.63 +function TryObject( value, fun, exception ) {
    1.64 +  this.value = value;
    1.65 +  this.exception = exception;
    1.66 +
    1.67 +  this.check = fun;
    1.68 +  this.valueOf = function () { return this.value; }
    1.69 +}
    1.70 +
    1.71 +/**
    1.72 + *  This function has a for-in statement within a try block.  Test cases
    1.73 + *  are added after the try-catch-finally statement.  Within the for-in
    1.74 + *  block, call a function that can throw an exception.  Verify that any
    1.75 + *  exceptions are properly caught.
    1.76 + */
    1.77 +
    1.78 +function TryForIn( object ) {
    1.79 +  try {
    1.80 +    for ( p in object ) {
    1.81 +      if ( typeof object[p] == "function" ) {
    1.82 +	result = object[p]();
    1.83 +      }
    1.84 +    }
    1.85 +  } catch ( e ) {
    1.86 +    result = e;
    1.87 +  }
    1.88 +
    1.89 +  new TestCase(
    1.90 +    SECTION,
    1.91 +    "TryForIn( " + object+ " )",
    1.92 +    (object.exception ? EXCEPTION_STRING +": " + object.value : object.value),
    1.93 +    result );
    1.94 +
    1.95 +}

mercurial