js/src/tests/ecma/Expressions/11.6.1-2.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/Expressions/11.6.1-2.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,130 @@
     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:          11.6.1-2.js
    1.12 +   ECMA Section:       11.6.1 The addition operator ( + )
    1.13 +   Description:
    1.14 +
    1.15 +   The addition operator either performs string concatenation or numeric
    1.16 +   addition.
    1.17 +
    1.18 +   The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression
    1.19 +   is evaluated as follows:
    1.20 +
    1.21 +   1.  Evaluate AdditiveExpression.
    1.22 +   2.  Call GetValue(Result(1)).
    1.23 +   3.  Evaluate MultiplicativeExpression.
    1.24 +   4.  Call GetValue(Result(3)).
    1.25 +   5.  Call ToPrimitive(Result(2)).
    1.26 +   6.  Call ToPrimitive(Result(4)).
    1.27 +   7.  If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12.
    1.28 +   (Note that this step differs from step 3 in the algorithm for comparison
    1.29 +   for the relational operators in using or instead of and.)
    1.30 +   8.  Call ToNumber(Result(5)).
    1.31 +   9.  Call ToNumber(Result(6)).
    1.32 +   10. Apply the addition operation to Result(8) and Result(9). See the discussion below (11.6.3).
    1.33 +   11. Return Result(10).
    1.34 +   12. Call ToString(Result(5)).
    1.35 +   13. Call ToString(Result(6)).
    1.36 +   14. Concatenate Result(12) followed by Result(13).
    1.37 +   15. Return Result(14).
    1.38 +
    1.39 +   Note that no hint is provided in the calls to ToPrimitive in steps 5 and 6.
    1.40 +   All native ECMAScript objects except Date objects handle the absence of a
    1.41 +   hint as if the hint Number were given; Date objects handle the absence of a
    1.42 +   hint as if the hint String were given. Host objects may handle the absence
    1.43 +   of a hint in some other manner.
    1.44 +
    1.45 +   This test does only covers cases where the Additive or Mulplicative expression
    1.46 +   ToPrimitive is a string.
    1.47 +
    1.48 +   Author:             christine@netscape.com
    1.49 +   Date:               12 november 1997
    1.50 +*/
    1.51 +var SECTION = "11.6.1-2";
    1.52 +var VERSION = "ECMA_1";
    1.53 +startTest();
    1.54 +
    1.55 +writeHeaderToLog( SECTION + " The Addition operator ( + )");
    1.56 +
    1.57 +// tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is
    1.58 +// a boolean primitive and a boolean object.
    1.59 +
    1.60 +new TestCase(   SECTION,
    1.61 +                "var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2",
    1.62 +                "stringfalse",
    1.63 +                eval("var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2") );
    1.64 +
    1.65 +new TestCase(   SECTION,
    1.66 +                "var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2",
    1.67 +                "truestring",
    1.68 +                eval("var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2") );
    1.69 +
    1.70 +new TestCase(   SECTION,
    1.71 +                "var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2",
    1.72 +                "truestring",
    1.73 +                eval("var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2") );
    1.74 +
    1.75 +new TestCase(   SECTION,
    1.76 +                "var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2",
    1.77 +                "truestring",
    1.78 +                eval("var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2") );
    1.79 +
    1.80 +new TestCase(   SECTION,
    1.81 +                "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2",
    1.82 +                "stringfalse",
    1.83 +                eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2") );
    1.84 +
    1.85 +new TestCase(   SECTION,
    1.86 +                "var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2",
    1.87 +                "truestring",
    1.88 +                eval("var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") );
    1.89 +
    1.90 +new TestCase(   SECTION,
    1.91 +                "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2",
    1.92 +                "[object Object][object Object]",
    1.93 +                eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2") );
    1.94 +
    1.95 +// tests for number primitive, number object, Object object, a "MyObject" whose value is
    1.96 +// a number primitive and a number object.
    1.97 +
    1.98 +new TestCase(   SECTION,
    1.99 +                "var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2",
   1.100 +                "100string",
   1.101 +                eval("var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2") );
   1.102 +
   1.103 +new TestCase(   SECTION,
   1.104 +                "var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2",
   1.105 +                "string-1",
   1.106 +                eval("var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2") );
   1.107 +
   1.108 +new TestCase(   SECTION,
   1.109 +                "var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2",
   1.110 +                "100string",
   1.111 +                eval("var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2") );
   1.112 +
   1.113 +new TestCase(   SECTION,
   1.114 +                "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2",
   1.115 +                "string-1",
   1.116 +                eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2") );
   1.117 +
   1.118 +new TestCase(   SECTION,
   1.119 +                "var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2",
   1.120 +                "100string",
   1.121 +                eval("var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") );
   1.122 +
   1.123 +new TestCase(   SECTION,
   1.124 +                "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2",
   1.125 +                "[object Object][object Object]",
   1.126 +                eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2") );
   1.127 +
   1.128 +test();
   1.129 +
   1.130 +function MyObject( value ) {
   1.131 +  this.valueOf = new Function( "return this.value" );
   1.132 +  this.value = value;
   1.133 +}

mercurial