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-3.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 is a Date. 59 60 Author: christine (at) netscape.com 61 Date: 12 november 1997 62 */ 63 var SECTION = "11.6.1-3"; 64 var VERSION = "ECMA_1"; 65 startTest(); 66 var testcases = getTestCases(); 67 68 writeHeaderToLog( SECTION + " The Addition operator ( + )"); 69 test(); 70 71 function test() { 72 for ( tc=0; tc < testcases.length; tc++ ) { 73 testcases[tc].passed = writeTestCaseResult( 74 testcases[tc].expect, 75 testcases[tc].actual, 76 testcases[tc].description +" = "+ 77 testcases[tc].actual ); 78 79 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 80 } 81 stopTest(); 82 return ( testcases ); 83 } 84 function getTestCases() { 85 var array = new Array(); 86 var item = 0; 87 88 // tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is 89 // a boolean primitive and a boolean object, and "MyValuelessObject", where the value is 90 // set in the object's prototype, not the object itself. 91 92 var DATE1 = new Date(); 93 94 array[item++] = new TestCase( SECTION, 95 "var DATE1 = new Date(); DATE1 + DATE1", 96 DATE1.toString() + DATE1.toString(), 97 DATE1 + DATE1 ); 98 99 array[item++] = new TestCase( SECTION, 100 "var DATE1 = new Date(); DATE1 + 0", 101 DATE1.toString() + 0, 102 DATE1 + 0 ); 103 104 array[item++] = new TestCase( SECTION, 105 "var DATE1 = new Date(); DATE1 + new Number(0)", 106 DATE1.toString() + 0, 107 DATE1 + new Number(0) ); 108 109 array[item++] = new TestCase( SECTION, 110 "var DATE1 = new Date(); DATE1 + true", 111 DATE1.toString() + "true", 112 DATE1 + true ); 113 114 array[item++] = new TestCase( SECTION, 115 "var DATE1 = new Date(); DATE1 + new Boolean(true)", 116 DATE1.toString() + "true", 117 DATE1 + new Boolean(true) ); 118 119 array[item++] = new TestCase( SECTION, 120 "var DATE1 = new Date(); DATE1 + new Boolean(true)", 121 DATE1.toString() + "true", 122 DATE1 + new Boolean(true) ); 123 124 var MYOB1 = new MyObject( DATE1 ); 125 var MYOB2 = new MyValuelessObject( DATE1 ); 126 var MYOB3 = new MyProtolessObject( DATE1 ); 127 var MYOB4 = new MyProtoValuelessObject( DATE1 ); 128 129 array[item++] = new TestCase( SECTION, 130 "MYOB1 = new MyObject(DATE1); MYOB1 + new Number(1)", 131 "[object Object]1", 132 MYOB1 + new Number(1) ); 133 134 array[item++] = new TestCase( SECTION, 135 "MYOB1 = new MyObject(DATE1); MYOB1 + 1", 136 "[object Object]1", 137 MYOB1 + 1 ); 138 139 array[item++] = new TestCase( SECTION, 140 "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + 'string'", 141 DATE1.toString() + "string", 142 MYOB2 + 'string' ); 143 144 array[item++] = new TestCase( SECTION, 145 "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + new String('string')", 146 DATE1.toString() + "string", 147 MYOB2 + new String('string') ); 148 /* 149 array[item++] = new TestCase( SECTION, 150 "MYOB3 = new MyProtolessObject(DATE1); MYOB3 + new Boolean(true)", 151 DATE1.toString() + "true", 152 MYOB3 + new Boolean(true) ); 153 */ 154 array[item++] = new TestCase( SECTION, 155 "MYOB1 = new MyObject(DATE1); MYOB1 + true", 156 "[object Object]true", 157 MYOB1 + true ); 158 159 return ( array ); 160 } 161 function MyProtoValuelessObject() { 162 this.valueOf = new Function ( "" ); 163 this.__proto__ = null; 164 } 165 function MyProtolessObject( value ) { 166 this.valueOf = new Function( "return this.value" ); 167 this.__proto__ = null; 168 this.value = value; 169 } 170 function MyValuelessObject(value) { 171 this.__proto__ = new MyPrototypeObject(value); 172 } 173 function MyPrototypeObject(value) { 174 this.valueOf = new Function( "return this.value;" ); 175 this.toString = new Function( "return (this.value + '');" ); 176 this.value = value; 177 } 178 function MyObject( value ) { 179 this.valueOf = new Function( "return this.value" ); 180 this.value = value; 181 } 182