js/src/tests/ecma_2/Statements/try-001.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-001.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     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-001.js
    1.12 + *  ECMA Section:
    1.13 + *  Description:        The try statement
    1.14 + *
    1.15 + *  This test contains try, catch, and finally blocks.  An exception is
    1.16 + *  sometimes thrown by a function called from within the try block.
    1.17 + *
    1.18 + *
    1.19 + *  Author:             christine@netscape.com
    1.20 + *  Date:               11 August 1998
    1.21 + */
    1.22 +var SECTION = "";
    1.23 +var VERSION = "ECMA_2";
    1.24 +var TITLE   = "The try statement";
    1.25 +
    1.26 +startTest();
    1.27 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.28 +
    1.29 +var INVALID_JAVA_INTEGER_VALUE = "Invalid value for java.lang.Integer constructor";
    1.30 +
    1.31 +TryNewJavaInteger( "3.14159", INVALID_JAVA_INTEGER_VALUE );
    1.32 +TryNewJavaInteger( NaN, INVALID_JAVA_INTEGER_VALUE );
    1.33 +TryNewJavaInteger( 0,  0 );
    1.34 +TryNewJavaInteger( -1, -1 );
    1.35 +TryNewJavaInteger( 1,  1 );
    1.36 +TryNewJavaInteger( Infinity, Infinity );
    1.37 +
    1.38 +test();
    1.39 +
    1.40 +/**
    1.41 + *  Check to see if the input is valid for java.lang.Integer. If it is
    1.42 + *  not valid, throw INVALID_JAVA_INTEGER_VALUE.  If input is valid,
    1.43 + *  return Number( v )
    1.44 + *
    1.45 + */
    1.46 +
    1.47 +function newJavaInteger( v ) {
    1.48 +  value = Number( v );
    1.49 +  if ( Math.floor(value) != value || isNaN(value) ) {
    1.50 +    throw ( INVALID_JAVA_INTEGER_VALUE );
    1.51 +  } else {
    1.52 +    return value;
    1.53 +  }
    1.54 +}
    1.55 +
    1.56 +/**
    1.57 + *  Call newJavaInteger( value ) from within a try block.  Catch any
    1.58 + *  exception, and store it in result.  Verify that we got the right
    1.59 + *  return value from newJavaInteger in cases in which we do not expect
    1.60 + *  exceptions, and that we got the exception in cases where an exception
    1.61 + *  was expected.
    1.62 + */
    1.63 +function TryNewJavaInteger( value, expect ) {
    1.64 +  var finalTest = false;
    1.65 +
    1.66 +  try {
    1.67 +    result = newJavaInteger( value );
    1.68 +  } catch ( e ) {
    1.69 +    result = String( e );
    1.70 +  } finally {
    1.71 +    finalTest = true;
    1.72 +  }
    1.73 +  new TestCase(
    1.74 +    SECTION,
    1.75 +    "newJavaValue( " + value +" )",
    1.76 +    expect,
    1.77 +    result);
    1.78 +
    1.79 +  new TestCase(
    1.80 +    SECTION,
    1.81 +    "newJavaValue( " + value +" ) hit finally block",
    1.82 +    true,
    1.83 +    finalTest);
    1.84 +
    1.85 +}
    1.86 +

mercurial