js/src/tests/ecma_6/Generators/delegating-yield-8.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Test that yield* can appear in a loop, and alongside yield.
michael@0 2
michael@0 3 function* countdown(n) {
michael@0 4 while (n > 0) {
michael@0 5 yield n;
michael@0 6 yield* countdown(--n);
michael@0 7 }
michael@0 8 return 34;
michael@0 9 }
michael@0 10
michael@0 11 function collect_results(iter) {
michael@0 12 var ret = [];
michael@0 13 var result;
michael@0 14 do {
michael@0 15 result = iter.next();
michael@0 16 ret.push(result);
michael@0 17 } while (!result.done);
michael@0 18 return ret;
michael@0 19 }
michael@0 20
michael@0 21 var expected = [
michael@0 22 // yield in countdown(3), n == 3
michael@0 23 {value: 3, done: false},
michael@0 24 // yield in yield* countdown(2), n == 2
michael@0 25 {value: 2, done: false},
michael@0 26 // yield in nested yield* countdown(1), n == 1
michael@0 27 {value: 1, done: false},
michael@0 28 // countdown(0) yields no values
michael@0 29 // second go-through of countdown(2) loop, n == 1
michael@0 30 {value: 1, done: false},
michael@0 31 // second go-through of countdown(3) loop, n == 2
michael@0 32 {value: 2, done: false},
michael@0 33 // yield in yield* countdown(1), n == 1
michael@0 34 {value: 1, done: false},
michael@0 35 // third go-through of countdown(3) loop, n == 1
michael@0 36 {value: 1, done: false},
michael@0 37 // done
michael@0 38 {value: 34, done: true}
michael@0 39 ];
michael@0 40
michael@0 41 assertDeepEq(expected, collect_results(countdown(3)));
michael@0 42
michael@0 43 if (typeof reportCompare == "function")
michael@0 44 reportCompare(true, true);

mercurial