|
| 1 | +class TestControlFlow extends utest.Test { |
| 2 | + function testIfThen(async:Async) { |
| 3 | + @:coroutine function f(x) { |
| 4 | + if (x) return 1; |
| 5 | + return 2; |
| 6 | + } |
| 7 | + mapCalls.start([true, false], f, result -> { |
| 8 | + Assert.same([1, 2], result); |
| 9 | + async.done(); |
| 10 | + }); |
| 11 | + } |
| 12 | + |
| 13 | + function testIfThenReturnNoValue(async:Async) { |
| 14 | + var v; |
| 15 | + @:coroutine function f(x) { |
| 16 | + v = 1; |
| 17 | + if (x) { |
| 18 | + return; |
| 19 | + } |
| 20 | + v = 2; |
| 21 | + } |
| 22 | + @:coroutine function f2(x) { f(x); return v; } |
| 23 | + mapCalls.start([true, false], f2, result -> { |
| 24 | + Assert.same([1, 2], result); |
| 25 | + async.done(); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + function testIfThenElse(async:Async) { |
| 30 | + @:coroutine function f(x) { |
| 31 | + return if (x) 1 else 2; |
| 32 | + } |
| 33 | + mapCalls.start([true, false], f, result -> { |
| 34 | + Assert.same([1, 2], result); |
| 35 | + async.done(); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + function testSwitchNoDefault(async:Async) { |
| 40 | + @:coroutine function f(x) { |
| 41 | + switch (x) { |
| 42 | + case 1: return "a"; |
| 43 | + case 2: return "b"; |
| 44 | + case 3: return "c"; |
| 45 | + } |
| 46 | + return "d"; |
| 47 | + } |
| 48 | + mapCalls.start([1, 2, 3, 4], f, result -> { |
| 49 | + Assert.same(["a", "b", "c", "d"], result); |
| 50 | + async.done(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + function testSwitchDefault(async:Async) { |
| 55 | + @:coroutine function f(x) { |
| 56 | + switch (x) { |
| 57 | + case 1: return "a"; |
| 58 | + case 2: return "b"; |
| 59 | + case 3: return "c"; |
| 60 | + default: return "d"; |
| 61 | + } |
| 62 | + return "e"; |
| 63 | + } |
| 64 | + mapCalls.start([1, 2, 3, 4], f, result -> { |
| 65 | + Assert.same(["a", "b", "c", "d"], result); |
| 66 | + async.done(); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + function testLoop(async:Async) { |
| 71 | + @:coroutine function f(x) { |
| 72 | + var results = []; |
| 73 | + var i = 0; |
| 74 | + while (i < 10) { |
| 75 | + if (i == 5 && x == 1) break; |
| 76 | + if (i == 6 && x == 2) { i++; continue; } |
| 77 | + results.push(i); |
| 78 | + i++; |
| 79 | + } |
| 80 | + return results; |
| 81 | + } |
| 82 | + mapCalls.start([0, 1, 2], f, result -> { |
| 83 | + Assert.same([ |
| 84 | + [0,1,2,3,4,5,6,7,8,9], |
| 85 | + [0,1,2,3,4], |
| 86 | + [0,1,2,3,4,5,7,8,9] |
| 87 | + ], result); |
| 88 | + async.done(); |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +@:coroutine |
| 94 | +private function mapCalls<TArg,TRet>(args:Array<TArg>, f:Coroutine<TArg->TRet>):Array<TRet> { |
| 95 | + return [for (arg in args) f(arg)]; |
| 96 | +} |
0 commit comments