Skip to content

Commit efce9e5

Browse files
committed
added some control flow tests
1 parent 5e62f3b commit efce9e5

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

tests/misc/coroutines/src/Main.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function main() {
22
utest.UTest.run([
3-
new TestStaticFields(),
3+
new TestBasic(),
4+
new TestControlFlow(),
45
new TestGenerator(),
56
#if js
67
new TestJsPromise(),

tests/misc/coroutines/src/TestStaticFields.hx renamed to tests/misc/coroutines/src/TestBasic.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class TestStaticFields extends utest.Test {
1+
class TestBasic extends utest.Test {
22
function testSimpleStart(async:Async) {
33
simple.start(42, result -> {
44
Assert.equals(42, result);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)