-
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add yield tests from nadako/haxe-coroutines#6
- Loading branch information
Showing
18 changed files
with
904 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@:keepSub | ||
@:keep | ||
class BaseCase implements utest.ITest { | ||
var dummy:String = ''; | ||
|
||
public function new() {} | ||
|
||
public function setup() { | ||
dummy = ''; | ||
} | ||
|
||
function assert<T>(expected:Array<T>, generator:Iterator<T>, ?p:haxe.PosInfos) { | ||
dummy = ''; | ||
for (it in generator) { | ||
Assert.equals(expected.shift(), it, p); | ||
} | ||
Assert.equals(0, expected.length, p); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import utest.Assert; | ||
import utest.Async; | ||
import haxe.coro.Coroutine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package yield; | ||
|
||
import yield.Yield; | ||
|
||
@:build(yield.YieldMacro.build()) | ||
class TestYieldBasic extends BaseCase { | ||
public function testBasicYieldReturn() { | ||
assert([10, 20], basicYieldReturn()); | ||
Assert.equals('123', dummy); | ||
} | ||
|
||
@:yield function basicYieldReturn():Iterator<Int> { | ||
dummy += '1'; | ||
@:yield return 10; | ||
dummy += '2'; | ||
@:yield return 20; | ||
dummy += '3'; | ||
} | ||
|
||
#if broken | ||
|
||
public function testBasicYieldReturn_multipleIterations() { | ||
var generator = basicYieldReturn(); | ||
assert([10, 20], generator); | ||
Assert.equals('123', dummy); | ||
assert([10, 20], generator); | ||
Assert.equals('123', dummy); | ||
} | ||
|
||
#end | ||
|
||
public function testBasicYieldBreak() { | ||
assert([10], basicYieldBreak()); | ||
Assert.equals('12', dummy); | ||
} | ||
|
||
@:yield function basicYieldBreak() { | ||
dummy += '1'; | ||
@:yield return 10; | ||
dummy += '2'; | ||
return; | ||
dummy += '3'; | ||
@:yield return 20; | ||
dummy += '4'; | ||
} | ||
|
||
public function testLocalVars() { | ||
assert([10, 25, 40, 19, 30], localVars(10, 20, 30)); | ||
} | ||
|
||
@:yield function localVars(a:Int, b:Int, a1:Int) { | ||
var q = b; | ||
@:yield return a; | ||
var a = 5; | ||
@:yield return a + q; | ||
var q = q * 2; | ||
@:yield return q; | ||
for (a in 1...2) { | ||
q = a * 10; | ||
} | ||
for (c in 1...2) { | ||
q += 5; | ||
} | ||
for (i in 0...2) { | ||
for (j in 0...2) { | ||
q += i + j; | ||
} | ||
} | ||
@:yield return q; | ||
@:yield return a1; | ||
} | ||
|
||
public function testLocalVars_sameVarNameInTwoChildScopes() { | ||
assert([10], localVars_sameVarNameInTwoChildScopes(true)); | ||
assert([20], localVars_sameVarNameInTwoChildScopes(false)); | ||
} | ||
|
||
@:yield function localVars_sameVarNameInTwoChildScopes(condition:Bool) { | ||
if (condition) { | ||
var v = 10; | ||
@:yield return v; | ||
} else { | ||
var v = 'ab'; | ||
@:yield return v.length * 10; | ||
} | ||
} | ||
|
||
public function testLocalFunction() { | ||
assert([10, 20, 30], localFunction()); | ||
} | ||
|
||
@:yield function localFunction() { | ||
inline function local1() | ||
return 20; | ||
function local2() { | ||
return 30; | ||
} | ||
@:yield return 10; | ||
@:yield return local1(); | ||
var value = local2(); | ||
@:yield return value; | ||
} | ||
|
||
public function testInheritance() { | ||
var result = [for (it in descendantsOfParent()) it]; | ||
Assert.equals(2, result.length); | ||
} | ||
|
||
@:yield function descendantsOfParent():Iterator<Parent> { | ||
@:yield return new Child1(); | ||
@:yield return new Child2(); | ||
} | ||
} | ||
|
||
private class Parent { | ||
public function new() {} | ||
} | ||
|
||
private class Child1 extends Parent {} | ||
private class Child2 extends Parent {} | ||
|
||
function main() { | ||
utest.UTest.run([ | ||
new TestYieldBasic() | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package yield; | ||
|
||
import yield.Yield; | ||
|
||
@:build(yield.YieldMacro.build()) | ||
class TestYieldClosure extends BaseCase { | ||
// @:yield function closure(arg) { | ||
// var fn = @:yield function(arg2) { | ||
// } | ||
// @:yield function another(arg2) { | ||
// trace({arg2:arg2}); | ||
// } | ||
// } | ||
|
||
var anchor:Dynamic; | ||
|
||
public function testClosure() { | ||
assert([20, 40, 60, 80, 20, 40, 60, 80, 100], closure(2)); | ||
Assert.equals('1234512345', dummy); | ||
} | ||
|
||
@:yield function closure(arg) { | ||
var a:Dynamic = arg; | ||
anchor = a; | ||
var fn = @:yield function(arg2) { | ||
var b:Dynamic = arg; | ||
anchor = b; | ||
dummy += '1'; | ||
@:yield return arg * 10; | ||
dummy += '2'; | ||
@:yield return cast a * 20; // TODO: I had to insert these casts because this was errorring with Float should be Int | ||
dummy += '3'; | ||
@:yield return cast b * 30; | ||
dummy += '4'; | ||
@:yield return arg2 * 40; | ||
dummy += '5'; | ||
} | ||
for(i in fn(a)) { | ||
@:yield return i; | ||
} | ||
@:yield function another(arg2) { | ||
var b:Dynamic = arg; | ||
anchor = b; | ||
dummy += '1'; | ||
@:yield return arg * 10; | ||
dummy += '2'; | ||
@:yield return cast a * 20; | ||
dummy += '3'; | ||
@:yield return cast b * 30; | ||
dummy += '4'; | ||
@:yield return arg2 * 40; | ||
dummy += '5'; | ||
for(i in (@:yield function() @:yield return arg2 * 50)()) { | ||
@:yield return i; | ||
} | ||
} | ||
for(i in another(a)) { | ||
@:yield return i; | ||
} | ||
} | ||
|
||
|
||
public function testClosure_nested() { | ||
assert([100], closure_nested(10)); | ||
} | ||
|
||
@:yield function closure_nested(arg) { | ||
@:yield function another(arg2) { | ||
var fn = @:yield function() @:yield return arg2 * 10; | ||
for(i in fn()) @:yield return i; | ||
} | ||
for(i in another(arg)) { | ||
@:yield return i; | ||
} | ||
} | ||
|
||
|
||
public function testClosure_withoutYield() { | ||
assert([0, 10], closure_withoutYield(1)); | ||
} | ||
|
||
@:yield function closure_withoutYield(arg:Int) { | ||
var fn = function() return arg * 10; | ||
for(i in 0...2) { | ||
@:yield return fn() * i; | ||
} | ||
} | ||
} |
Oops, something went wrong.