|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 11.6.1-3.js |
|
9 ECMA Section: 11.6.1 The addition operator ( + ) |
|
10 Description: |
|
11 |
|
12 The addition operator either performs string concatenation or numeric |
|
13 addition. |
|
14 |
|
15 The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression |
|
16 is evaluated as follows: |
|
17 |
|
18 1. Evaluate AdditiveExpression. |
|
19 2. Call GetValue(Result(1)). |
|
20 3. Evaluate MultiplicativeExpression. |
|
21 4. Call GetValue(Result(3)). |
|
22 5. Call ToPrimitive(Result(2)). |
|
23 6. Call ToPrimitive(Result(4)). |
|
24 7. If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12. |
|
25 (Note that this step differs from step 3 in the algorithm for comparison |
|
26 for the relational operators in using or instead of and.) |
|
27 8. Call ToNumber(Result(5)). |
|
28 9. Call ToNumber(Result(6)). |
|
29 10. Apply the addition operation to Result(8) and Result(9). See the discussion below (11.6.3). |
|
30 11. Return Result(10). |
|
31 12. Call ToString(Result(5)). |
|
32 13. Call ToString(Result(6)). |
|
33 14. Concatenate Result(12) followed by Result(13). |
|
34 15. Return Result(14). |
|
35 |
|
36 Note that no hint is provided in the calls to ToPrimitive in steps 5 and 6. |
|
37 All native ECMAScript objects except Date objects handle the absence of a |
|
38 hint as if the hint Number were given; Date objects handle the absence of a |
|
39 hint as if the hint String were given. Host objects may handle the absence |
|
40 of a hint in some other manner. |
|
41 |
|
42 This test does only covers cases where the Additive or Mulplicative expression |
|
43 is a Date. |
|
44 |
|
45 Author: christine@netscape.com |
|
46 Date: 12 november 1997 |
|
47 */ |
|
48 var SECTION = "11.6.1-3"; |
|
49 var VERSION = "ECMA_1"; |
|
50 startTest(); |
|
51 |
|
52 writeHeaderToLog( SECTION + " The Addition operator ( + )"); |
|
53 |
|
54 // tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is |
|
55 // a boolean primitive and a boolean object. |
|
56 |
|
57 var DATE1 = new Date(); |
|
58 |
|
59 new TestCase( SECTION, |
|
60 "var DATE1 = new Date(); DATE1 + DATE1", |
|
61 DATE1.toString() + DATE1.toString(), |
|
62 DATE1 + DATE1 ); |
|
63 |
|
64 new TestCase( SECTION, |
|
65 "var DATE1 = new Date(); DATE1 + 0", |
|
66 DATE1.toString() + 0, |
|
67 DATE1 + 0 ); |
|
68 |
|
69 new TestCase( SECTION, |
|
70 "var DATE1 = new Date(); DATE1 + new Number(0)", |
|
71 DATE1.toString() + 0, |
|
72 DATE1 + new Number(0) ); |
|
73 |
|
74 new TestCase( SECTION, |
|
75 "var DATE1 = new Date(); DATE1 + true", |
|
76 DATE1.toString() + "true", |
|
77 DATE1 + true ); |
|
78 |
|
79 new TestCase( SECTION, |
|
80 "var DATE1 = new Date(); DATE1 + new Boolean(true)", |
|
81 DATE1.toString() + "true", |
|
82 DATE1 + new Boolean(true) ); |
|
83 |
|
84 new TestCase( SECTION, |
|
85 "var DATE1 = new Date(); DATE1 + new Boolean(true)", |
|
86 DATE1.toString() + "true", |
|
87 DATE1 + new Boolean(true) ); |
|
88 |
|
89 var MYOB1 = new MyObject( DATE1 ); |
|
90 |
|
91 new TestCase( SECTION, |
|
92 "MYOB1 = new MyObject(DATE1); MYOB1 + new Number(1)", |
|
93 "[object Object]1", |
|
94 MYOB1 + new Number(1) ); |
|
95 |
|
96 new TestCase( SECTION, |
|
97 "MYOB1 = new MyObject(DATE1); MYOB1 + 1", |
|
98 "[object Object]1", |
|
99 MYOB1 + 1 ); |
|
100 |
|
101 new TestCase( SECTION, |
|
102 "MYOB1 = new MyObject(DATE1); MYOB1 + true", |
|
103 "[object Object]true", |
|
104 MYOB1 + true ); |
|
105 |
|
106 test(); |
|
107 |
|
108 function MyPrototypeObject(value) { |
|
109 this.valueOf = new Function( "return this.value;" ); |
|
110 this.toString = new Function( "return (this.value + '');" ); |
|
111 this.value = value; |
|
112 } |
|
113 function MyObject( value ) { |
|
114 this.valueOf = new Function( "return this.value" ); |
|
115 this.value = value; |
|
116 } |