1 /* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 23 File Name: 11.6.1-2.js 24 ECMA Section: 11.6.1 The addition operator ( + ) 25 Description: 26 27 The addition operator either performs string concatenation or numeric 28 addition. 29 30 The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression 31 is evaluated as follows: 32 33 1. Evaluate AdditiveExpression. 34 2. Call GetValue(Result(1)). 35 3. Evaluate MultiplicativeExpression. 36 4. Call GetValue(Result(3)). 37 5. Call ToPrimitive(Result(2)). 38 6. Call ToPrimitive(Result(4)). 39 7. If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12. 40 (Note that this step differs from step 3 in the algorithm for comparison 41 for the relational operators in using or instead of and.) 42 8. Call ToNumber(Result(5)). 43 9. Call ToNumber(Result(6)). 44 10. Apply the addition operation to Result(8) and Result(9). See the discussion below (11.6.3). 45 11. Return Result(10). 46 12. Call ToString(Result(5)). 47 13. Call ToString(Result(6)). 48 14. Concatenate Result(12) followed by Result(13). 49 15. Return Result(14). 50 51 Note that no hint is provided in the calls to ToPrimitive in steps 5 and 6. 52 All native ECMAScript objects except Date objects handle the absence of a 53 hint as if the hint Number were given; Date objects handle the absence of a 54 hint as if the hint String were given. Host objects may handle the absence 55 of a hint in some other manner. 56 57 This test does only covers cases where the Additive or Mulplicative expression 58 ToPrimitive is a string. 59 60 Author: christine (at) netscape.com 61 Date: 12 november 1997 62 */ 63 var SECTION = "11.6.1-2"; 64 var VERSION = "ECMA_1"; 65 startTest(); 66 67 var testcases = getTestCases(); 68 69 writeHeaderToLog( SECTION + " The Addition operator ( + )"); 70 test(); 71 72 function test() { 73 for ( tc=0; tc < testcases.length; tc++ ) { 74 testcases[tc].passed = writeTestCaseResult( 75 testcases[tc].expect, 76 testcases[tc].actual, 77 testcases[tc].description +" = "+ 78 testcases[tc].actual ); 79 80 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 81 } 82 stopTest(); 83 return ( testcases ); 84 } 85 function getTestCases() { 86 var array = new Array(); 87 var item = 0; 88 89 // tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is 90 // a boolean primitive and a boolean object, and "MyValuelessObject", where the value is 91 // set in the object's prototype, not the object itself. 92 93 array[item++] = new TestCase( SECTION, 94 "var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2", 95 "stringfalse", 96 eval("var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2") ); 97 98 array[item++] = new TestCase( SECTION, 99 "var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2", 100 "truestring", 101 eval("var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2") ); 102 103 array[item++] = new TestCase( SECTION, 104 "var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2", 105 "truestring", 106 eval("var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2") ); 107 108 array[item++] = new TestCase( SECTION, 109 "var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2", 110 "truestring", 111 eval("var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2") ); 112 113 array[item++] = new TestCase( SECTION, 114 "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2", 115 "stringfalse", 116 eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2") ); 117 118 array[item++] = new TestCase( SECTION, 119 "var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2", 120 "truestring", 121 eval("var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") ); 122 123 array[item++] = new TestCase( SECTION, 124 "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2", 125 "[object Object][object Object]", 126 eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2") ); 127 128 array[item++] = new TestCase( SECTION, 129 "var EXP_1 = new MyValuelessObject('string'); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2", 130 "stringfalse", 131 eval("var EXP_1 = new MyValuelessObject('string'); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2") ); 132 133 array[item++] = new TestCase( SECTION, 134 "var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2", 135 "stringfalse", 136 eval("var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2") ); 137 138 // tests for number primitive, number object, Object object, a "MyObject" whose value is 139 // a number primitive and a number object, and "MyValuelessObject", where the value is 140 // set in the object's prototype, not the object itself. 141 142 array[item++] = new TestCase( SECTION, 143 "var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2", 144 "100string", 145 eval("var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2") ); 146 147 array[item++] = new TestCase( SECTION, 148 "var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2", 149 "string-1", 150 eval("var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2") ); 151 152 array[item++] = new TestCase( SECTION, 153 "var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2", 154 "100string", 155 eval("var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2") ); 156 157 array[item++] = new TestCase( SECTION, 158 "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2", 159 "string-1", 160 eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2") ); 161 162 array[item++] = new TestCase( SECTION, 163 "var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2", 164 "100string", 165 eval("var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") ); 166 167 array[item++] = new TestCase( SECTION, 168 "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2", 169 "[object Object][object Object]", 170 eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2") ); 171 172 array[item++] = new TestCase( SECTION, 173 "var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject('string'); EXP_1 + EXP_2", 174 "100string", 175 eval("var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject('string'); EXP_1 + EXP_2") ); 176 177 array[item++] = new TestCase( SECTION, 178 "var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2", 179 "string-1", 180 eval("var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2") ); 181 return ( array ); 182 } 183 function MyProtoValuelessObject() { 184 this.valueOf = new Function ( "" ); 185 this.__proto__ = null; 186 } 187 function MyProtolessObject( value ) { 188 this.valueOf = new Function( "return this.value" ); 189 this.__proto__ = null; 190 this.value = value; 191 } 192 function MyValuelessObject(value) { 193 this.__proto__ = new MyPrototypeObject(value); 194 } 195 function MyPrototypeObject(value) { 196 this.valueOf = new Function( "return this.value;" ); 197 this.toString = new Function( "return (this.value + '');" ); 198 this.value = value; 199 } 200 function MyObject( value ) { 201 this.valueOf = new Function( "return this.value" ); 202 this.value = value; 203 } 204