• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16function must_throw (str)
17{
18  try
19  {
20    eval ("switch (1) { default: " + str + "}");
21    assert (false);
22  }
23  catch (e)
24  {
25  }
26
27  try
28  {
29    eval (str);
30    assert (false);
31  }
32  catch (e)
33  {
34  }
35}
36
37var a = 'A';
38var b = 'B';
39
40switch (1)
41{
42default:
43
44  ``;
45  `abc`;
46  `ab${a+b}${ `x` }c`;
47
48  assert (`` === '');
49  assert (`abc` === 'abc');
50  assert (`ab\
51  c` === 'ab  c');
52  assert (`ab
53  c` === 'ab\n  c');
54
55  assert (`prefix${a}` === 'prefixA');
56  assert (`${a}postfix` === 'Apostfix');
57  assert (`prefix${a}postfix` === 'prefixApostfix');
58
59  assert (`${a}${b}` === 'AB');
60  assert (`${a},${b}` === 'A,B');
61  assert (`${a}${b}${a}${b}` === 'ABAB');
62  assert (`${a},${b},${a},${b}` === 'A,B,A,B');
63  assert (`$${a},${b},${a},${b}$` === '$A,B,A,B$');
64
65  assert (`\${}` === '${}');
66  assert (`$\{}` === '${}');
67  assert (`x${  `y` + `z`  }x` === 'xyzx');
68  assert (`x${  `y` , `z`  }x` === 'xzx');
69
70  function f(x) { return x + 1; }
71
72  /* Precedence. */
73  var c = 1;
74  assert (`x${  f(1) * f(2)  }x${ c = 4 }` === 'x6x4');
75  assert (c === 4);
76  assert (`m${0 || 93}n${7 && 0}o` === 'm93n0o');
77
78  /* Result is always a string. */
79  assert (`${  function() { return true } () }` === 'true');
80  assert (`${  function() { return a.length } () }` === '1');
81
82  /* Result is a single string with its properties. */
83  assert(`${a}${b}${a}${b}`.length === 4);
84}
85
86must_throw ("`");
87must_throw ("`${");
88must_throw ("`${7");
89must_throw ("`${}`");
90must_throw ("`${1}");
91must_throw ("`${1}.${");
92must_throw ("`${1}.${2}");
93
94// line break normalization
95var cr = eval("`a" + String.fromCharCode(13) + "b`");
96var lf = eval("`a" + String.fromCharCode(10) + "b`");
97var crlf = eval("`a" + String.fromCharCode(13,10) + "b`");
98
99assert(cr.length === 3);
100assert(lf.length === 3);
101assert(crlf.length === 3);
102assert(cr[1] === lf[1]);
103assert(lf[1] === crlf[1]);
104assert(crlf[1] === '\n');
105