diff options
Diffstat (limited to 'spec/inputs/test/break_multiple_values_spec.yue')
| -rw-r--r-- | spec/inputs/test/break_multiple_values_spec.yue | 840 |
1 files changed, 840 insertions, 0 deletions
diff --git a/spec/inputs/test/break_multiple_values_spec.yue b/spec/inputs/test/break_multiple_values_spec.yue new file mode 100644 index 0000000..846be00 --- /dev/null +++ b/spec/inputs/test/break_multiple_values_spec.yue | |||
| @@ -0,0 +1,840 @@ | |||
| 1 | describe "break with multiple values", -> | ||
| 2 | it "should break with multiple values from numeric for loop", -> | ||
| 3 | x, y = for i = 1, 10 | ||
| 4 | if i > 5 | ||
| 5 | break i, i * 2 | ||
| 6 | assert.same x, 6 | ||
| 7 | assert.same y, 12 | ||
| 8 | |||
| 9 | it "should break with multiple values from ipairs iterator", -> | ||
| 10 | x, y = for _, v in ipairs [1, 2, 3] | ||
| 11 | if v > 2 | ||
| 12 | break v, v * 2 | ||
| 13 | assert.same x, 3 | ||
| 14 | assert.same y, 6 | ||
| 15 | |||
| 16 | it "should break with multiple values from destructuring iterator", -> | ||
| 17 | x, y = for [a, b] in *{[10, 20], [30, 40], [40, 50]} | ||
| 18 | if b > 20 | ||
| 19 | break a, b * 2 | ||
| 20 | assert.same x, 30 | ||
| 21 | assert.same y, 80 | ||
| 22 | |||
| 23 | it "should break with multiple values from do block", -> | ||
| 24 | x, y = do | ||
| 25 | break 1, 2 | ||
| 26 | assert.same x, 1 | ||
| 27 | assert.same y, 2 | ||
| 28 | |||
| 29 | it "should break with multiple values from with statement", -> | ||
| 30 | x = with abc: 99, flag: true | ||
| 31 | break .abc if .flag | ||
| 32 | break 0 | ||
| 33 | assert.same x, 99 | ||
| 34 | |||
| 35 | it "should work with continue in comprehension", -> | ||
| 36 | input = {1,2,3,4,5,6} | ||
| 37 | output = for x in *input | ||
| 38 | continue if x % 2 == 1 | ||
| 39 | x | ||
| 40 | assert.same output, {2, 4, 6} | ||
| 41 | |||
| 42 | it "should break in comprehension with index", -> | ||
| 43 | f1 = -> | ||
| 44 | tb = [true, true, false] | ||
| 45 | index = for i = 1, #tb | ||
| 46 | break i if tb[i] | ||
| 47 | index | ||
| 48 | assert.same f1!, 1 | ||
| 49 | |||
| 50 | it "should work with continue in indexed comprehension", -> | ||
| 51 | input = {1,2,3,4,5,6} | ||
| 52 | output = for i = 1, #input | ||
| 53 | x = input[i] | ||
| 54 | continue if x % 2 == 1 | ||
| 55 | x | ||
| 56 | assert.same output, {2, 4, 6} | ||
| 57 | |||
| 58 | it "should break with multiple values in simple for loop", -> | ||
| 59 | a, b = for i = 1, 5 | ||
| 60 | if i == 2 | ||
| 61 | break i, i * 10 | ||
| 62 | assert.same a, 2 | ||
| 63 | assert.same b, 20 | ||
| 64 | |||
| 65 | it "should break with expression values", -> | ||
| 66 | sum, product = for i = 1, 3 | ||
| 67 | if i == 2 | ||
| 68 | break i + i, i * i | ||
| 69 | assert.same sum, 4 | ||
| 70 | assert.same product, 4 | ||
| 71 | |||
| 72 | it "should break with table iteration values", -> | ||
| 73 | key, value = for k, v in pairs {a: 1, b: 2, c: 3} | ||
| 74 | if k == "b" | ||
| 75 | break k, v * 10 | ||
| 76 | assert.same key, "b" | ||
| 77 | assert.same value, 20 | ||
| 78 | |||
| 79 | it "should break in for loop with multiple values", -> | ||
| 80 | count, total = for i = 1, 10 | ||
| 81 | if i == 5 | ||
| 82 | break i, i * 10 | ||
| 83 | assert.same count, 5 | ||
| 84 | assert.same total, 50 | ||
| 85 | |||
| 86 | it "should break in repeat loop with multiple values", -> | ||
| 87 | i = 1 | ||
| 88 | i, doubled = repeat | ||
| 89 | if i > 3 | ||
| 90 | break i, i * 100 | ||
| 91 | i += 1 | ||
| 92 | until false | ||
| 93 | assert.same i, 4 | ||
| 94 | assert.same doubled, 400 | ||
| 95 | |||
| 96 | it "should work with with? and break", -> | ||
| 97 | dummy = (x) -> x | ||
| 98 | result = with? value: 1 | ||
| 99 | dummy with? nil | ||
| 100 | break .value | ||
| 101 | break .value | ||
| 102 | assert.same result, 1 | ||
| 103 | |||
| 104 | it "should break with multiple values in do block", -> | ||
| 105 | x, y = do | ||
| 106 | break 10, 30 | ||
| 107 | assert.same x, 10 | ||
| 108 | assert.same y, 30 | ||
| 109 | |||
| 110 | it "should break with string and number values", -> | ||
| 111 | name, age = for k, v in pairs {name: "Alice", age: 30, city: "NYC"} | ||
| 112 | if k == "name" | ||
| 113 | break v, 25 | ||
| 114 | assert.same name, "Alice" | ||
| 115 | assert.same age, 25 | ||
| 116 | |||
| 117 | it "should break with nil values", -> | ||
| 118 | a, b, c = for i = 1, 5 | ||
| 119 | if i == 3 | ||
| 120 | break i, nil, "test" | ||
| 121 | assert.same a, 3 | ||
| 122 | assert.same b, nil | ||
| 123 | assert.same c, "test" | ||
| 124 | |||
| 125 | it "should break with boolean values", -> | ||
| 126 | found, value = for i = 1, 5 | ||
| 127 | if i == 3 | ||
| 128 | break true, "found" | ||
| 129 | assert.is_true found | ||
| 130 | assert.same value, "found" | ||
| 131 | |||
| 132 | it "should break with function call values", -> | ||
| 133 | fn = -> 42 | ||
| 134 | val, dbl = for i = 1, 3 | ||
| 135 | if i == 2 | ||
| 136 | break fn!, fn! * 2 | ||
| 137 | assert.same val, 42 | ||
| 138 | assert.same dbl, 84 | ||
| 139 | |||
| 140 | it "should break with multiple values for destructuring", -> | ||
| 141 | x, y = for [a, b] in *{[1, 2], [3, 4], [5, 6]} | ||
| 142 | if a > 2 | ||
| 143 | break b, a | ||
| 144 | assert.same x, 4 | ||
| 145 | assert.same y, 3 | ||
| 146 | |||
| 147 | it "should break in switch-like logic", -> | ||
| 148 | status, message = for i = 1, 3 | ||
| 149 | do | ||
| 150 | if i == 1 | ||
| 151 | break "ok", "one" | ||
| 152 | if i == 2 | ||
| 153 | break "ok", "two" | ||
| 154 | assert.same status, "ok" | ||
| 155 | assert.same message, "one" | ||
| 156 | |||
| 157 | it "should handle multiple break conditions", -> | ||
| 158 | idx, val = for i = 1, 10 | ||
| 159 | if i == 3 | ||
| 160 | break i, "first" | ||
| 161 | if i == 7 | ||
| 162 | break i, "second" | ||
| 163 | assert.same idx, 3 | ||
| 164 | assert.same val, "first" | ||
| 165 | |||
| 166 | it "should break with computed values", -> | ||
| 167 | sum, count = for i = 1, 5 | ||
| 168 | if i == 4 | ||
| 169 | break 10 + i, i | ||
| 170 | assert.same sum, 14 | ||
| 171 | assert.same count, 4 | ||
| 172 | |||
| 173 | it "should break with table literal", -> | ||
| 174 | name, data = for i = 1, 3 | ||
| 175 | if i == 2 | ||
| 176 | break "item", {value: i, index: i} | ||
| 177 | assert.same name, "item" | ||
| 178 | assert.same data.value, 2 | ||
| 179 | assert.same data.index, 2 | ||
| 180 | |||
| 181 | it "should break with string concatenation", -> | ||
| 182 | prefix, suffix = for i = 1, 3 | ||
| 183 | if i == 2 | ||
| 184 | break "pre", "fix" | ||
| 185 | assert.same prefix .. suffix, "prefix" | ||
| 186 | |||
| 187 | it "should break with continue in loop", -> | ||
| 188 | x, y = for i = 1, 5 | ||
| 189 | if i == 3 | ||
| 190 | break i, i * 2 | ||
| 191 | assert.same x, 3 | ||
| 192 | assert.same y, 6 | ||
| 193 | |||
| 194 | it "should break with ternary-like expression", -> | ||
| 195 | result, is_valid = for i = 1, 5 | ||
| 196 | if i == 3 | ||
| 197 | break i, i > 2 | ||
| 198 | assert.same result, 3 | ||
| 199 | assert.is_true is_valid | ||
| 200 | |||
| 201 | it "should break with arithmetic operations", -> | ||
| 202 | sum, diff = for i = 1, 5 | ||
| 203 | if i == 3 | ||
| 204 | break i + 10, 10 - i | ||
| 205 | assert.same sum, 13 | ||
| 206 | assert.same diff, 7 | ||
| 207 | |||
| 208 | it "should break with table access", -> | ||
| 209 | data = {a: 1, b: 2} | ||
| 210 | first, second = for i = 1, 3 | ||
| 211 | if i == 2 | ||
| 212 | break data.a, data.b | ||
| 213 | assert.same first, 1 | ||
| 214 | assert.same second, 2 | ||
| 215 | |||
| 216 | it "should break with literal values", -> | ||
| 217 | a, b = for i = 1, 3 | ||
| 218 | if i == 2 | ||
| 219 | break "x", "y" | ||
| 220 | assert.same a, "x" | ||
| 221 | assert.same b, "y" | ||
| 222 | |||
| 223 | it "should break with conditional expression", -> | ||
| 224 | status, msg = for i = 1, 5 | ||
| 225 | if i == 3 | ||
| 226 | break "ok", "done" | ||
| 227 | assert.same status, "ok" | ||
| 228 | assert.same msg, "done" | ||
| 229 | |||
| 230 | it "should handle multiple breaks in sequence", -> | ||
| 231 | val1, val2 = for i = 1, 10 | ||
| 232 | if i == 3 | ||
| 233 | break i, i * 2 | ||
| 234 | if i == 5 | ||
| 235 | break i, i * 3 | ||
| 236 | if i == 7 | ||
| 237 | break i, i * 4 | ||
| 238 | assert.same val1, 3 | ||
| 239 | assert.same val2, 6 | ||
| 240 | |||
| 241 | it "should handle multiple breaks in switch", -> | ||
| 242 | val1, val2 = for i = 1, 10 do switch i | ||
| 243 | when 3 then break i, i * 2 | ||
| 244 | when 5 then break i, i * 3 | ||
| 245 | when 7 then break i, i * 4 | ||
| 246 | assert.same val1, 3 | ||
| 247 | assert.same val2, 6 | ||
| 248 | |||
| 249 | it "should break with nil and default", -> | ||
| 250 | a, b = for i = 1, 5 | ||
| 251 | if i == 2 | ||
| 252 | break i, nil | ||
| 253 | assert.same a, 2 | ||
| 254 | assert.is_nil b | ||
| 255 | |||
| 256 | it "should break with false and value", -> | ||
| 257 | found, data = for i = 1, 5 | ||
| 258 | if i == 3 | ||
| 259 | break false, "item" | ||
| 260 | assert.is_false found | ||
| 261 | assert.same data, "item" | ||
| 262 | |||
| 263 | it "should break with three values", -> | ||
| 264 | a, b, c = for i = 1, 5 | ||
| 265 | if i == 3 | ||
| 266 | break i, i * 2, i * 3 | ||
| 267 | assert.same a, 3 | ||
| 268 | assert.same b, 6 | ||
| 269 | assert.same c, 9 | ||
| 270 | |||
| 271 | it "should break in while loop", -> | ||
| 272 | i = 1 | ||
| 273 | count, total = while i < 10 | ||
| 274 | if i == 5 | ||
| 275 | break i, i * 10 | ||
| 276 | i += 1 | ||
| 277 | assert.same count, 5 | ||
| 278 | assert.same total, 50 | ||
| 279 | |||
| 280 | it "should handle early break in loop", -> | ||
| 281 | a, b = for i = 1, 100 | ||
| 282 | if i == 1 | ||
| 283 | break "first", "second" | ||
| 284 | assert.same a, "first" | ||
| 285 | assert.same b, "second" | ||
| 286 | |||
| 287 | it "should break with multiple function calls", -> | ||
| 288 | add = (a, b) -> a + b | ||
| 289 | mul = (a, b) -> a * b | ||
| 290 | sum, product = for i = 1, 5 | ||
| 291 | if i == 3 | ||
| 292 | break add(i, i), mul(i, i) | ||
| 293 | assert.same sum, 6 | ||
| 294 | assert.same product, 9 | ||
| 295 | |||
| 296 | it "should work with chained comparisons", -> | ||
| 297 | x, y = for i = 1, 10 | ||
| 298 | if 3 < i and i < 7 | ||
| 299 | break i, i * i | ||
| 300 | assert.same x, 4 | ||
| 301 | assert.same y, 16 | ||
| 302 | |||
| 303 | it "should work with logical expressions", -> | ||
| 304 | flag, value = for i = 1, 5 | ||
| 305 | if i > 2 and i < 4 | ||
| 306 | break true, i | ||
| 307 | assert.is_true flag | ||
| 308 | assert.same value, 3 | ||
| 309 | |||
| 310 | it "should work with table unpacking in break", -> | ||
| 311 | tbl = {10, 20} | ||
| 312 | a, b = for i = 1, 5 | ||
| 313 | if i == 3 | ||
| 314 | break tbl[1], tbl[2] | ||
| 315 | assert.same a, 10 | ||
| 316 | assert.same b, 20 | ||
| 317 | |||
| 318 | it "should work with nested conditions", -> | ||
| 319 | result, flag = for i = 1, 10 | ||
| 320 | if i > 2 | ||
| 321 | if i < 5 | ||
| 322 | if i == 3 | ||
| 323 | break i, true | ||
| 324 | assert.same result, 3 | ||
| 325 | assert.is_true flag | ||
| 326 | |||
| 327 | it "should work with class methods", -> | ||
| 328 | class MyClass | ||
| 329 | find_pair: => | ||
| 330 | result = [ | ||
| 331 | for i = 1, 10 | ||
| 332 | if i == 5 | ||
| 333 | break i, i * 10 | ||
| 334 | ] | ||
| 335 | result | ||
| 336 | obj = MyClass! | ||
| 337 | result = obj\find_pair! | ||
| 338 | assert.same result[1], 5 | ||
| 339 | assert.same result[2], 50 | ||
| 340 | |||
| 341 | it "should work with string interpolation", -> | ||
| 342 | a, b = for i = 1, 5 | ||
| 343 | if i == 3 | ||
| 344 | break "item-#{i}", "value-#{i * 10}" | ||
| 345 | assert.same a, "item-3" | ||
| 346 | assert.same b, "value-30" | ||
| 347 | |||
| 348 | it "should work with try-catch", -> | ||
| 349 | try | ||
| 350 | result, error_msg = for i = 1, 5 | ||
| 351 | if i == 3 | ||
| 352 | break i, "found" | ||
| 353 | assert.same result, 3 | ||
| 354 | assert.same error_msg, "found" | ||
| 355 | catch e | ||
| 356 | error "Should not reach here" | ||
| 357 | |||
| 358 | it "should work with const attribute", -> | ||
| 359 | const MAX = 10 | ||
| 360 | first, second = for i = 1, MAX | ||
| 361 | if i == 5 | ||
| 362 | break i, i * 2 | ||
| 363 | assert.same first, 5 | ||
| 364 | assert.same second, 10 | ||
| 365 | |||
| 366 | it "should work with backcall style processing", -> | ||
| 367 | a, b = for i = 1, 5 | ||
| 368 | if i == 3 | ||
| 369 | break i, i * 2 | ||
| 370 | assert.same a, 3 | ||
| 371 | assert.same b, 6 | ||
| 372 | |||
| 373 | it "should work with varargs", -> | ||
| 374 | process = (...) -> | ||
| 375 | items = {...} | ||
| 376 | a, b = for i = 1, #items | ||
| 377 | if items[i] == 3 | ||
| 378 | break items[i], items[i] * 2 | ||
| 379 | a, b | ||
| 380 | result1, result2 = process 1, 2, 3, 4, 5 | ||
| 381 | assert.same result1, 3 | ||
| 382 | assert.same result2, 6 | ||
| 383 | |||
| 384 | it "should work with table slicing", -> | ||
| 385 | matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]] | ||
| 386 | sub, val = for i = 1, #matrix | ||
| 387 | if matrix[i][1] == 4 | ||
| 388 | break matrix[i][1], matrix[i][2] | ||
| 389 | assert.same sub, 4 | ||
| 390 | assert.same val, 5 | ||
| 391 | |||
| 392 | it "should work with chaining comparison", -> | ||
| 393 | result, flag = for i = 1, 10 | ||
| 394 | if 1 < i <= 5 < 10 | ||
| 395 | break i, i * i | ||
| 396 | assert.same result, 2 | ||
| 397 | assert.same flag, 4 | ||
| 398 | |||
| 399 | it "should work with implicit return", -> | ||
| 400 | fn = -> | ||
| 401 | result = [ | ||
| 402 | for i = 1, 5 | ||
| 403 | if i == 3 | ||
| 404 | break i, i * 10 | ||
| 405 | ] | ||
| 406 | result | ||
| 407 | result = fn! | ||
| 408 | assert.same result[1], 3 | ||
| 409 | assert.same result[2], 30 | ||
| 410 | |||
| 411 | it "should work with nil coalescing", -> | ||
| 412 | data = nil | ||
| 413 | first, second = for i = 1, 5 | ||
| 414 | if i == 3 | ||
| 415 | break i, i * 2 | ||
| 416 | assert.same first, 3 | ||
| 417 | assert.same second, 6 | ||
| 418 | |||
| 419 | it "should work with table literal shorthand", -> | ||
| 420 | a, b = for i = 1, 5 | ||
| 421 | if i == 2 | ||
| 422 | break "first", "second" | ||
| 423 | assert.same a, "first" | ||
| 424 | assert.same b, "second" | ||
| 425 | |||
| 426 | it "should work with method calls", -> | ||
| 427 | obj = { | ||
| 428 | start: 10 | ||
| 429 | end_val: 20 | ||
| 430 | find: => | ||
| 431 | result = [ | ||
| 432 | for i = 1, 10 | ||
| 433 | if i == 4 | ||
| 434 | break @start + i, @end_val + i | ||
| 435 | ] | ||
| 436 | result | ||
| 437 | } | ||
| 438 | result = obj\find! | ||
| 439 | assert.same result[1], 14 | ||
| 440 | assert.same result[2], 24 | ||
| 441 | |||
| 442 | it "should work with do-end assignment", -> | ||
| 443 | a, b = do | ||
| 444 | x, y = for i = 1, 5 | ||
| 445 | if i == 2 | ||
| 446 | break i, i * 5 | ||
| 447 | x, y | ||
| 448 | assert.same a, 2 | ||
| 449 | assert.same b, 10 | ||
| 450 | |||
| 451 | it "should work with unary operators", -> | ||
| 452 | result, negated = for i = 1, 5 | ||
| 453 | if i == 3 | ||
| 454 | break i, -i | ||
| 455 | assert.same result, 3 | ||
| 456 | assert.same negated, -3 | ||
| 457 | |||
| 458 | it "should work with bitwise operators", -> | ||
| 459 | a, b = for i = 1, 10 | ||
| 460 | if i == 4 | ||
| 461 | break i ~ 1, i | 8 | ||
| 462 | assert.same a, 5 | ||
| 463 | assert.same b, 12 | ||
| 464 | |||
| 465 | it "should work with modulo operation", -> | ||
| 466 | quotient, remainder = for i = 1, 10 | ||
| 467 | if i == 7 | ||
| 468 | break i // 3, i % 3 | ||
| 469 | assert.same quotient, 2 | ||
| 470 | assert.same remainder, 1 | ||
| 471 | |||
| 472 | it "should work with exponentiation", -> | ||
| 473 | base, squared = for i = 1, 5 | ||
| 474 | if i == 3 | ||
| 475 | break i, i ^ 2 | ||
| 476 | assert.same base, 3 | ||
| 477 | assert.same squared, 9 | ||
| 478 | |||
| 479 | it "should work with length operator", -> | ||
| 480 | str, len = for i = 1, 5 | ||
| 481 | if i == 3 | ||
| 482 | break "hello", #"hello" | ||
| 483 | assert.same str, "hello" | ||
| 484 | assert.same len, 5 | ||
| 485 | |||
| 486 | it "should work with table concatenation", -> | ||
| 487 | part1, part2 = for i = 1, 5 | ||
| 488 | if i == 2 | ||
| 489 | break {1, 2}, {3, 4} | ||
| 490 | assert.same part1, {1, 2} | ||
| 491 | assert.same part2, {3, 4} | ||
| 492 | |||
| 493 | it "should work with nested do blocks", -> | ||
| 494 | result = do | ||
| 495 | temp = { | ||
| 496 | for i = 1, 5 | ||
| 497 | do | ||
| 498 | if i == 3 | ||
| 499 | break i, i * 2 | ||
| 500 | } | ||
| 501 | temp[1] + temp[2] | ||
| 502 | assert.same result, 9 | ||
| 503 | |||
| 504 | it "should work with table.insert", -> | ||
| 505 | results = {} | ||
| 506 | item, index = for i = 1, 5 | ||
| 507 | if i == 3 | ||
| 508 | break i, i * 2 | ||
| 509 | table.insert results, i | ||
| 510 | assert.same results, {1, 2} | ||
| 511 | |||
| 512 | it "should break with multiple values in nested for loops", -> | ||
| 513 | x, y = for i = 1, 5 | ||
| 514 | a, b = for j = 1, 5 | ||
| 515 | if i == 2 and j == 3 | ||
| 516 | break i, j | ||
| 517 | break a, b if a | ||
| 518 | assert.same x, 2 | ||
| 519 | assert.same y, 3 | ||
| 520 | |||
| 521 | it "should break with multiple values in for nested while", -> | ||
| 522 | x, y = for i = 1, 10 | ||
| 523 | j = 1 | ||
| 524 | a, b = while j < 10 | ||
| 525 | if i == 3 and j == 5 | ||
| 526 | break i, j | ||
| 527 | j += 1 | ||
| 528 | break a, b if a and b | ||
| 529 | assert.same x, 3 | ||
| 530 | assert.same y, 5 | ||
| 531 | |||
| 532 | it "should break with multiple values in while nested for", -> | ||
| 533 | i = 1 | ||
| 534 | x, y = while i < 10 | ||
| 535 | a, b = for j = 1, 10 | ||
| 536 | if i == 3 and j == 4 | ||
| 537 | break i, j | ||
| 538 | i += 1 | ||
| 539 | break a, b if a and b | ||
| 540 | assert.same x, 3 | ||
| 541 | assert.same y, 4 | ||
| 542 | |||
| 543 | it "should break with multiple values in for nested repeat", -> | ||
| 544 | x, y = for i = 1, 5 | ||
| 545 | j = 1 | ||
| 546 | a, b = repeat | ||
| 547 | if i == 2 and j == 3 | ||
| 548 | break i, j | ||
| 549 | j += 1 | ||
| 550 | until j > 10 | ||
| 551 | break a, b if a and b | ||
| 552 | assert.same x, 2 | ||
| 553 | assert.same y, 3 | ||
| 554 | |||
| 555 | it "should break with multiple values in repeat nested for", -> | ||
| 556 | i = 1 | ||
| 557 | x, y = repeat | ||
| 558 | a, b = for j = 1, 5 | ||
| 559 | if i == 3 and j == 2 | ||
| 560 | break i, j | ||
| 561 | i += 1 | ||
| 562 | break a, b if a and b | ||
| 563 | until i > 10 | ||
| 564 | assert.same x, 3 | ||
| 565 | assert.same y, 2 | ||
| 566 | |||
| 567 | it "should break with multiple values in do nested for", -> | ||
| 568 | x, y = do | ||
| 569 | a1, b1 = for i = 1, 5 | ||
| 570 | a, b = for j = 1, 5 | ||
| 571 | if i == 2 and j == 3 | ||
| 572 | break i, j | ||
| 573 | break a, b if a and b | ||
| 574 | break a1, b1 | ||
| 575 | assert.same x, 2 | ||
| 576 | assert.same y, 3 | ||
| 577 | |||
| 578 | it "should break with multiple values in for nested do", -> | ||
| 579 | x, y = for i = 1, 5 | ||
| 580 | do | ||
| 581 | if i == 3 | ||
| 582 | break i, i * 2 | ||
| 583 | assert.same x, 3 | ||
| 584 | assert.same y, 6 | ||
| 585 | |||
| 586 | it "should break with multiple values in for nested with", -> | ||
| 587 | x, y = for i = 1, 5 | ||
| 588 | with value: i, index: i * 2 | ||
| 589 | if .value == 3 | ||
| 590 | break .value, .index | ||
| 591 | assert.same x, 3 | ||
| 592 | assert.same y, 6 | ||
| 593 | |||
| 594 | it "should break with a value in with nested for", -> | ||
| 595 | x = with {limit: 5} | ||
| 596 | break for i = 1, .limit | ||
| 597 | if i == 3 | ||
| 598 | break i | ||
| 599 | assert.same x, 3 | ||
| 600 | |||
| 601 | it "should break with multiple values in three-level nested loops", -> | ||
| 602 | x, y, z = for i = 1, 3 | ||
| 603 | a1, a2, a3 = for j = 1, 3 | ||
| 604 | b1, b2, b3 = for k = 1, 3 | ||
| 605 | if i == 2 and j == 2 and k == 2 | ||
| 606 | break i, j, k | ||
| 607 | break b1, b2, b3 if b1 | ||
| 608 | break a1, a2, a3 if a1 | ||
| 609 | assert.same x, 2 | ||
| 610 | assert.same y, 2 | ||
| 611 | assert.same z, 2 | ||
| 612 | |||
| 613 | it "should break with multiple values in while nested repeat", -> | ||
| 614 | i = 1 | ||
| 615 | x, y = while i < 10 | ||
| 616 | j = 1 | ||
| 617 | a, b = repeat | ||
| 618 | if i == 3 and j == 4 | ||
| 619 | break i, j | ||
| 620 | j += 1 | ||
| 621 | until j > 10 | ||
| 622 | i += 1 | ||
| 623 | break a, b if a | ||
| 624 | assert.same x, 3 | ||
| 625 | assert.same y, 4 | ||
| 626 | |||
| 627 | it "should break with multiple values in repeat nested while", -> | ||
| 628 | i = 1 | ||
| 629 | x, y = repeat | ||
| 630 | j = 1 | ||
| 631 | a, b = while j < 10 | ||
| 632 | if i == 4 and j == 3 | ||
| 633 | break i, j | ||
| 634 | j += 1 | ||
| 635 | i += 1 | ||
| 636 | break a, b if a | ||
| 637 | until i > 10 | ||
| 638 | assert.same x, 4 | ||
| 639 | assert.same y, 3 | ||
| 640 | |||
| 641 | it "should break with multiple values in do nested while", -> | ||
| 642 | i = 1 | ||
| 643 | x, y = do | ||
| 644 | a, b = while i < 10 | ||
| 645 | if i == 4 | ||
| 646 | break i, i * 3 | ||
| 647 | i += 1 | ||
| 648 | break a, b | ||
| 649 | assert.same x, 4 | ||
| 650 | assert.same y, 12 | ||
| 651 | |||
| 652 | it "should break with multiple values in while nested do", -> | ||
| 653 | i = 1 | ||
| 654 | x, y = while i < 10 | ||
| 655 | a, b = do | ||
| 656 | if i == 5 | ||
| 657 | break i, i * 4 | ||
| 658 | i += 1 | ||
| 659 | break a, b if a | ||
| 660 | assert.same x, 5 | ||
| 661 | assert.same y, 20 | ||
| 662 | |||
| 663 | it "should break with multiple values in do nested repeat", -> | ||
| 664 | i = 1 | ||
| 665 | x, y = do | ||
| 666 | a, b = repeat | ||
| 667 | if i == 3 | ||
| 668 | break i, i * 5 | ||
| 669 | i += 1 | ||
| 670 | until i > 10 | ||
| 671 | break a, b | ||
| 672 | assert.same x, 3 | ||
| 673 | assert.same y, 15 | ||
| 674 | |||
| 675 | it "should break with multiple values in repeat nested do", -> | ||
| 676 | i = 1 | ||
| 677 | x, y = repeat | ||
| 678 | a, b = do | ||
| 679 | if i == 2 | ||
| 680 | break i, i * 6 | ||
| 681 | i += 1 | ||
| 682 | break a, b if a | ||
| 683 | until i > 10 | ||
| 684 | assert.same x, 2 | ||
| 685 | assert.same y, 12 | ||
| 686 | |||
| 687 | it "should break with multiple values in with nested do", -> | ||
| 688 | x, y = do | ||
| 689 | with value: 10, factor: 3 | ||
| 690 | a, b = do | ||
| 691 | if .value > 5 | ||
| 692 | break .value, .factor | ||
| 693 | break a, b | ||
| 694 | assert.same x, 10 | ||
| 695 | assert.same y, 3 | ||
| 696 | |||
| 697 | it "should break with multiple values in do nested with", -> | ||
| 698 | x, y = do | ||
| 699 | with num: 42, text: "test" | ||
| 700 | break .num, .text | ||
| 701 | assert.same x, 42 | ||
| 702 | assert.same y, "test" | ||
| 703 | |||
| 704 | it "should break with multiple values in for nested for with different types", -> | ||
| 705 | name, age, active = for i = 1, 5 | ||
| 706 | a, b, c = for j = 1, 5 | ||
| 707 | if i == 2 and j == 3 | ||
| 708 | break "user-#{i}", j * 10, true | ||
| 709 | break a, b, c if a | ||
| 710 | assert.same name, "user-2" | ||
| 711 | assert.same age, 30 | ||
| 712 | assert.is_true active | ||
| 713 | |||
| 714 | it "should break with multiple values in with nested for with method calls", -> | ||
| 715 | obj = | ||
| 716 | data: [10, 20, 30, 40] | ||
| 717 | find: => | ||
| 718 | x, y = for i = 1, #@data | ||
| 719 | if @data[i] == 30 | ||
| 720 | break @data[i], i | ||
| 721 | x, y | ||
| 722 | value, index = obj\find! | ||
| 723 | assert.same value, 30 | ||
| 724 | assert.same index, 3 | ||
| 725 | |||
| 726 | it "should break with multiple values in nested loops with early termination", -> | ||
| 727 | found, idx = for i = 1, 10 | ||
| 728 | if i == 1 | ||
| 729 | break i, true | ||
| 730 | a, b = for j = 1, 10 | ||
| 731 | break i, false if j == 5 | ||
| 732 | break a, b if a | ||
| 733 | assert.same found, 1 | ||
| 734 | assert.is_true idx | ||
| 735 | |||
| 736 | it "should break with multiple values in nested loops with computed expressions", -> | ||
| 737 | sum, product = for i = 1, 5 | ||
| 738 | a, b = for j = 1, 5 | ||
| 739 | if i * j == 12 | ||
| 740 | break i + j, i * j | ||
| 741 | break a, b if a | ||
| 742 | assert.same sum, 7 | ||
| 743 | assert.same product, 12 | ||
| 744 | |||
| 745 | it "should mix break and break with values in same for expression", -> | ||
| 746 | x, y = for i = 1, 5 | ||
| 747 | if i == 3 | ||
| 748 | break i, i * 10 | ||
| 749 | if i == 4 | ||
| 750 | break | ||
| 751 | assert.same x, 3 | ||
| 752 | assert.same y, 30 | ||
| 753 | |||
| 754 | it "should return nils when plain break happens before value break", -> | ||
| 755 | x, y = for i = 1, 5 | ||
| 756 | if i == 2 | ||
| 757 | break | ||
| 758 | if i == 4 | ||
| 759 | break i, i * 10 | ||
| 760 | assert.is_nil x | ||
| 761 | assert.is_nil y | ||
| 762 | |||
| 763 | it "should mix plain break and value break across nested loops", -> | ||
| 764 | matrix = { | ||
| 765 | {1, -1, 9} | ||
| 766 | {4, 5, 9} | ||
| 767 | } | ||
| 768 | row, col = for r = 1, #matrix | ||
| 769 | a, b = for c = 1, #matrix[r] | ||
| 770 | cell = matrix[r][c] | ||
| 771 | break if cell < 0 | ||
| 772 | break r, c if cell == 9 | ||
| 773 | break a, b if a | ||
| 774 | assert.same row, 2 | ||
| 775 | assert.same col, 3 | ||
| 776 | |||
| 777 | it "should mix break and break with values in while expression", -> | ||
| 778 | i = 0 | ||
| 779 | x, y = while i < 5 | ||
| 780 | i += 1 | ||
| 781 | if i == 2 | ||
| 782 | break | ||
| 783 | if i == 4 | ||
| 784 | break i, i * 2 | ||
| 785 | assert.is_nil x | ||
| 786 | assert.is_nil y | ||
| 787 | |||
| 788 | it "should mix break and break with values in do with with block", -> | ||
| 789 | x, y = do | ||
| 790 | with value: 8, stop: false | ||
| 791 | break if .stop | ||
| 792 | break .value, .value * 2 | ||
| 793 | assert.same x, 8 | ||
| 794 | assert.same y, 16 | ||
| 795 | |||
| 796 | it "should fall back to plain break in do with with block", -> | ||
| 797 | x, y = do | ||
| 798 | with value: 8, stop: true | ||
| 799 | break if .stop | ||
| 800 | break .value, .value * 2 | ||
| 801 | break | ||
| 802 | assert.is_nil x | ||
| 803 | assert.is_nil y | ||
| 804 | |||
| 805 | it "should mix continue and break with values in for expression", -> | ||
| 806 | x, y = for i = 1, 8 | ||
| 807 | continue if i % 3 ~= 0 | ||
| 808 | break i, i + 100 if i == 6 | ||
| 809 | assert.same x, 6 | ||
| 810 | assert.same y, 106 | ||
| 811 | |||
| 812 | it "should mix continue and break with values across nested for expressions", -> | ||
| 813 | row, col = for r = 1, 3 | ||
| 814 | a, b = for c = 1, 4 | ||
| 815 | continue if c < 3 | ||
| 816 | break r, c if r == 2 and c == 3 | ||
| 817 | continue if not a | ||
| 818 | break a, b | ||
| 819 | assert.same row, 2 | ||
| 820 | assert.same col, 3 | ||
| 821 | |||
| 822 | it "should mix break continue and value break with plain break winning", -> | ||
| 823 | x, y = for i = 1, 7 | ||
| 824 | continue if i < 3 | ||
| 825 | if i == 4 | ||
| 826 | break | ||
| 827 | if i == 6 | ||
| 828 | break i, i * 10 | ||
| 829 | assert.is_nil x | ||
| 830 | assert.is_nil y | ||
| 831 | |||
| 832 | it "should mix break continue and value break with value break winning", -> | ||
| 833 | x, y = for i = 1, 9 | ||
| 834 | continue if i % 2 == 0 | ||
| 835 | if i == 5 | ||
| 836 | break i, i * 3 | ||
| 837 | if i == 9 | ||
| 838 | break | ||
| 839 | assert.same x, 5 | ||
| 840 | assert.same y, 15 | ||
