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 37function must_throw_strict (str) 38{ 39 try 40 { 41 eval ("'use strict'; switch (1) { default: " + str + "}"); 42 assert (false); 43 } 44 catch (e) 45 { 46 } 47 48 try 49 { 50 eval ("'use strict'; " + str); 51 assert (false); 52 } 53 catch (e) 54 { 55 } 56} 57 58switch (1) 59{ 60default: 61 62 var func = x => { return x + 3 } 63 assert (func(5) == 8); 64 65 a => 5 /* no semicolon after */ 66 67 assert (((x => 68 x + 1))(4) == 5) 69 70 assert ((a => a += 3, b => b -= 3)(4) == 1); 71 72 func = true ? x=>x+2:y=>y-2 73 assert (func(10) == 12); 74 75 func = arguments => 76 { return arguments + 4; } 77 assert (func(2) == 6); 78 79 func = ( 80 ) => { return typeof 81 arguments 82 } 83 assert (func() === "undefined"); 84 85 if (a => 0) 86 { 87 } 88 else 89 { 90 assert (false); 91 } 92 93 assert (( 94 ( 95 static 96 , 97 package 98 ) => static + package 99 ) (2, 12) == 14); 100 101 var global_var = 7; 102 103 assert (( 104 ( 105 static 106 , 107 package 108 ) => { global_var = 5; return static + package } 109 )(4, 5) == 9); 110 111 assert (global_var == 5); 112 113 func = (x , y) => {} 114 assert (func() === undefined) 115 116 assert ((x => y => z => 6)()()() == 6) 117 118 func = x => x - 6 119 var func2 = y => func(y) 120 assert (func2 (17) == 11) 121 122 func = (m) => m++ 123 assert (func (4) == 4) 124 125 func = () => 126 ((([0,0,0]))) 127 assert (func ().length == 3); 128 129 func = (a = 5, b = 7 * 2) => a + b; 130 assert (func() == 19); 131 assert (func(1) == 15); 132 133 func = (a = Math.cos(0)) => a; 134 assert (func() == 1); 135} 136 137must_throw ("var x => x;"); 138must_throw ("(()) => 0"); 139must_throw ("((x)) => 0"); 140must_throw ("(((x))) => 0"); 141must_throw ("(x,) => 0"); 142must_throw ("(x==6) => 0"); 143must_throw ("(x y) => 0"); 144must_throw ("(x,y,) => 0"); 145must_throw ("x\n => 0"); 146must_throw ("this => 0"); 147must_throw ("(true) => 0"); 148must_throw ("()\n=>5"); 149must_throw ("3 + x => 3"); 150must_throw ("3 || x => 3"); 151must_throw ("a = 3 || (x,y) => 3"); 152must_throw ("x => {} (4)"); 153must_throw ("!x => 4"); 154must_throw ("x => {} = 1"); 155must_throw ("x => {} a = 1"); 156must_throw ("x => {} ? 1 : 0"); 157must_throw ("(x,x,x) => 0"); 158must_throw ("(x,x,x) => { }"); 159must_throw_strict ("(package) => 0"); 160must_throw_strict ("(package) => { return 5 }"); 161must_throw_strict ("(x,x,x) => 0"); 162must_throw_strict ("(x,x,x) => { }"); 163 164var f = (a) => 1; 165assert(f() === 1); 166 167var f = (a => 2); 168assert(f() === 2); 169 170var f = ((((a => ((3)))))); 171assert(f() === 3); 172 173var f = (((a) => 4)); 174assert(f() === 4); 175 176var f = (a,b) => 5; 177assert(f() === 5); 178 179var f = (((a,b) => 6)); 180assert(f() === 6); 181 182var f = ((a,b) => x => (a) => 7); 183assert(f()()() === 7); 184 185var f = (((a=1,b=2) => ((x => (((a) => 8)))))); 186assert(f()()() === 8); 187