Debian Codegraph @

rhino/testsrc/tests/ecma_3/Expressions/11.6.1-1.js

raw symbols SCIP
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 * Date: 14 Mar 2003
9 * SUMMARY: Testing left-associativity of the + operator
10 *
11 * See ECMA-262 Ed.3, Section 11.6.1, "The Addition operator"
12 * See http://bugzilla.mozilla.org/show_bug.cgi?id=196290
13 *
14 * The upshot: |a + b + c| should always equal |(a + b) + c|
15 *
16 */
17 //-----------------------------------------------------------------------------
18 var gTestfile = '11.6.1-1.js';
19 var UBound = 0;
20 var BUGNUMBER = 196290;
21 var summary = 'Testing left-associativity of the + operator';
22 var status = '';
23 var statusitems = [];
24 var actual = '';
25 var actualvalues = [];
26 var expect= '';
27 var expectedvalues = [];
28
29
30 status = inSection(1);
31 actual = 1 + 1 + 'px';
32 expect = '2px';
33 addThis();
34
35 status = inSection(2);
36 actual = 'px' + 1 + 1;
37 expect = 'px11';
38 addThis();
39
40 status = inSection(3);
41 actual = 1 + 1 + 1 + 'px';
42 expect = '3px';
43 addThis();
44
45 status = inSection(4);
46 actual = 1 + 1 + 'a' + 1 + 1 + 'b';
47 expect = '2a11b';
48 addThis();
49
50 /*
51 * The next sections test the + operator via eval()
52 */
53 status = inSection(5);
54 actual = sumThese(1, 1, 'a', 1, 1, 'b');
55 expect = '2a11b';
56 addThis();
57
58 status = inSection(6);
59 actual = sumThese(new Number(1), new Number(1), 'a');
60 expect = '2a';
61 addThis();
62
63 status = inSection(7);
64 actual = sumThese('a', new Number(1), new Number(1));
65 expect = 'a11';
66 addThis();
67
68
69
70 //-----------------------------------------------------------------------------
71 test();
72 //-----------------------------------------------------------------------------
73
74
75
76 function addThis()
77 {
78 statusitems[UBound] = status;
79 actualvalues[UBound] = actual;
80 expectedvalues[UBound] = expect;
81 UBound++;
82 }
83
84 /*
85 * Applies the + operator to the provided arguments via eval().
86 *
87 * Form an eval string of the form 'arg1 + arg2 + arg3', but
88 * remember to add double-quotes inside the eval string around
89 * any argument that is of string type. For example, suppose the
90 * arguments were 11, 'a', 22. Then the eval string should be
91 *
92 * arg1 + quoteThis(arg2) + arg3
93 *
94 * If we didn't put double-quotes around the string argument,
95 * we'd get this for an eval string:
96 *
97 * '11 + a + 22'
98 *
99 * If we eval() this, we get 'ReferenceError: a is not defined'.
100 * With proper quoting, we get eval('11 + "a" + 22') as desired.
101 */
102 function sumThese()
103 {
104 var sEval = '';
105 var arg;
106 var i;
107
108 var L = arguments.length;
109 for (i=0; i<L; i++)
110 {
111 arg = arguments[i];
112 if (typeof arg === 'string')
113 arg = quoteThis(arg);
114
115 if (i < L-1)
116 sEval += arg + ' + ';
117 else
118 sEval += arg;
119 }
120
121 return eval(sEval);
122 }
123
124
125 function quoteThis(x)
126 {
127 return '"' + x + '"';
128 }
129
130
131 function test()
132 {
133 enterFunc('test');
134 printBugNumber(BUGNUMBER);
135 printStatus(summary);
136
137 for (var i=0; i<UBound; i++)
138 {
139 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
140 }
141
142 exitFunc ('test');
143 }