js/src/tests/ecma_2/Statements/try-006.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-006.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     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-006.js
    1.12 + *  ECMA Section:
    1.13 + *  Description:        The try statement
    1.14 + *
    1.15 + *  Throw an exception from within a With block in a try block.  Verify
    1.16 + *  that any expected exceptions are caught.
    1.17 + *
    1.18 + *  Author:             christine@netscape.com
    1.19 + *  Date:               11 August 1998
    1.20 + */
    1.21 +var SECTION = "try-006";
    1.22 +var VERSION = "ECMA_2";
    1.23 +var TITLE   = "The try statement";
    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 +TryWith( new TryObject( "hello", throwException, true ));
    1.49 +TryWith( 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 + */
    1.60 +function TryObject( value, fun, exception ) {
    1.61 +  this.value = value;
    1.62 +  this.exception = exception;
    1.63 +
    1.64 +  this.valueOf = new Function ( "return this.value" );
    1.65 +  this.check = fun;
    1.66 +}
    1.67 +
    1.68 +/**
    1.69 + *  This function has the try block that has a with block within it.
    1.70 + *  Test cases are added in this function.  Within the with block, the
    1.71 + *  object's "check" function is called.  If the test object's exception
    1.72 + *  property is true, we expect the result to be the exception value.
    1.73 + *  If exception is false, then we expect the result to be the value of
    1.74 + *  the object.
    1.75 + */
    1.76 +function TryWith( object ) {
    1.77 +  try {
    1.78 +    with ( object ) {
    1.79 +      result = check();
    1.80 +    }
    1.81 +  } catch ( e ) {
    1.82 +    result = e;
    1.83 +  }
    1.84 +
    1.85 +  new TestCase(
    1.86 +    SECTION,
    1.87 +    "TryWith( " + object.value +" )",
    1.88 +    (object.exception ? EXCEPTION_STRING +": " + object.valueOf() : object.valueOf()),
    1.89 +    result );
    1.90 +}

mercurial