1 Testing ucode switch statements. 2 3 4 1. Ensure that execution starts at the first matching case. 5 6 -- Expect stdout -- 7 1a 8 -- End -- 9 10 -- Testcase -- 11 {% 12 switch (1) { 13 case 1: 14 print("1a\n"); 15 break; 16 17 case 1: 18 print("1b\n"); 19 break; 20 21 case 2: 22 print("2\n"); 23 break; 24 } 25 %} 26 -- End -- 27 28 29 2. Ensure that default case is only used if no case matches, 30 even if declared first. 31 32 -- Expect stdout -- 33 1 34 default 35 -- End -- 36 37 -- Testcase -- 38 {% 39 for (n in [1, 3]) { 40 switch (n) { 41 default: 42 print("default\n"); 43 break; 44 45 case 1: 46 print("1\n"); 47 break; 48 49 case 2: 50 print("2\n"); 51 break; 52 } 53 } 54 %} 55 -- End -- 56 57 58 3. Ensure that cases without break fall through into 59 subsequent cases. 60 61 -- Expect stdout -- 62 1 63 2 64 default 65 1 66 2 67 -- End -- 68 69 -- Testcase -- 70 {% 71 for (n in [1, 3]) { 72 switch (n) { 73 default: 74 print("default\n"); 75 76 case 1: 77 print("1\n"); 78 79 case 2: 80 print("2\n"); 81 } 82 } 83 %} 84 -- End -- 85 86 87 4. Ensure that a single default case matches. 88 89 -- Expect stdout -- 90 default 91 default 92 -- End -- 93 94 -- Testcase -- 95 {% 96 for (n in [1, 3]) { 97 switch (n) { 98 default: 99 print("default\n"); 100 } 101 } 102 %} 103 -- End -- 104 105 106 5. Ensure that duplicate default cases emit a syntax 107 error during parsing. 108 109 -- Expect stderr -- 110 Syntax error: more than one switch default case 111 In line 6, byte 3: 112 113 ` default:` 114 ^-- Near here 115 116 117 Syntax error: Expecting expression 118 In line 8, byte 2: 119 120 ` }` 121 ^-- Near here 122 123 124 -- End -- 125 126 -- Testcase -- 127 {% 128 switch (1) { 129 default: 130 print("default1\n"); 131 132 default: 133 print("default2\n"); 134 } 135 %} 136 -- End -- 137 138 139 6. Ensure that case values use strict comparison. 140 141 -- Expect stdout -- 142 b 143 b 144 -- End -- 145 146 -- Testcase -- 147 {% 148 switch (1.0) { 149 case 1: 150 print("a\n"); 151 break; 152 153 case 1.0: 154 print("b\n"); 155 break; 156 } 157 158 switch ("123") { 159 case 123: 160 print("a\n"); 161 break; 162 163 case "123": 164 print("b\n"); 165 break; 166 } 167 %} 168 -- End -- 169 170 171 7. Ensure that case values may be complex expressions. 172 173 -- Expect stdout -- 174 2, 3, 1 175 -- End -- 176 177 -- Testcase -- 178 {% 179 switch (1) { 180 case a = 2, b = 3, c = 1: 181 print(join(", ", [ a, b, c ]), "\n"); 182 break; 183 } 184 %} 185 -- End -- 186 187 188 8. Ensure that empty switch statements are accepted by the 189 parser and that the test expression is evaluated. 190 191 -- Expect stdout -- 192 true 193 -- End -- 194 195 -- Testcase -- 196 {% 197 x = false; 198 199 switch (x = true) { 200 201 } 202 203 print(x, "\n"); 204 %} 205 -- End -- 206 207 208 9. Ensure that `return` breaks out of switch statements. 209 210 -- Expect stdout -- 211 one 212 two 213 -- End -- 214 215 -- Testcase -- 216 {% 217 function test(n) { 218 switch (n) { 219 case 1: 220 return "one"; 221 222 case 2: 223 return "two"; 224 225 default: 226 return "three"; 227 } 228 } 229 230 print(test(1), "\n"); 231 print(test(2), "\n"); 232 %} 233 -- End -- 234 235 236 10. Ensure that `continue` breaks out of switch statements. 237 238 -- Expect stdout -- 239 one 240 two 241 -- End -- 242 243 -- Testcase -- 244 {% 245 for (n in [1,2]) { 246 switch (n) { 247 case 1: 248 print("one\n"); 249 continue; 250 251 case 2: 252 print("two\n"); 253 continue; 254 255 default: 256 print("three\n"); 257 } 258 } 259 %} 260 -- End -- 261 262 263 11. Ensure that exceptions break out of switch statements. 264 265 -- Expect stdout -- 266 one 267 -- End -- 268 269 -- Expect stderr -- 270 Died 271 In test(), line 6, byte 8: 272 called from anonymous function ([stdin]:17:14) 273 274 ` die();` 275 Near here ------^ 276 277 278 -- End -- 279 280 -- Testcase -- 281 {% 282 function test(n) { 283 switch (n) { 284 case 1: 285 print("one\n"); 286 die(); 287 288 case 2: 289 print("two\n"); 290 die(); 291 292 default: 293 print("three\n"); 294 } 295 } 296 297 print(test(1), "\n"); 298 %} 299 -- End -- 300 301 302 12. Ensure that consecutive cases values are properly handled. 303 304 -- Expect stdout -- 305 three and four 306 -- End -- 307 308 -- Testcase -- 309 {% 310 switch (3) { 311 case 1: 312 case 2: 313 print("one and two\n"); 314 break; 315 316 case 3: 317 case 4: 318 print("three and four\n"); 319 break; 320 321 default: 322 print("five\n"); 323 } 324 %} 325 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt