Chromium Code Reviews| Index: test/mjsunit/harmony/generators-iteration.js |
| diff --git a/test/mjsunit/harmony/generators-iteration.js b/test/mjsunit/harmony/generators-iteration.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..57c0e41f9cf677705f6cc3691c50c81b999c194e |
| --- /dev/null |
| +++ b/test/mjsunit/harmony/generators-iteration.js |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2013 the V8 project authors. All rights reserved. |
| +// Redistribution and use in source and binary forms, with or without |
| +// modification, are permitted provided that the following conditions are |
| +// met: |
| +// |
| +// * Redistributions of source code must retain the above copyright |
| +// notice, this list of conditions and the following disclaimer. |
| +// * Redistributions in binary form must reproduce the above |
| +// copyright notice, this list of conditions and the following |
| +// disclaimer in the documentation and/or other materials provided |
| +// with the distribution. |
| +// * Neither the name of Google Inc. nor the names of its |
| +// contributors may be used to endorse or promote products derived |
| +// from this software without specific prior written permission. |
| +// |
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + |
| +// Flags: --harmony-generators |
| + |
| +// Test generator iteration. |
| + |
| +function* g1() { } |
| +function* g2() { yield 1; } |
| +function* g3() { yield 1; yield 2; } |
| +function* g4() { yield 1; yield 2; return 3; } |
| +function* g5() { return 1; } |
| +function* g6() { var x = yield 1; return x; } |
| +function* g7() { var x = yield 1; yield 2; return x; } |
| +function* g8() { for (var x = 0; x < 4; x++) { yield x; } } |
| + |
| +function TestNext() { |
| + function assertIteratorValues(g, expected_values) { |
| + var iter = g(); |
| + for (var i = 0; i < expected_values.length; i++) { |
| + assertEquals(expected_values[i], iter.next()); |
| + } |
| + assertThrows(function() { iter.next(); }, Error); |
| + } |
| + assertIteratorValues(g1, [undefined]); |
| + assertIteratorValues(g2, [1, undefined]); |
| + assertIteratorValues(g3, [1, 2, undefined]); |
| + assertIteratorValues(g4, [1, 2, 3]); |
| + assertIteratorValues(g5, [1]); |
| + assertIteratorValues(g6, [1, undefined]); |
| + assertIteratorValues(g7, [1, 2, undefined]); |
| + assertIteratorValues(g8, [0, 1, 2, 3, undefined]); |
| +} |
| +TestNext(); |
| + |
|
Michael Starzinger
2013/04/21 22:45:21
nit: Two newlines between the test cases makes it
|
| +function TestSend() { |
| + function assertIteratorValues(g, send_val, expected_values) { |
| + var iter = g(); |
| + for (var i = 0; i < expected_values.length; i++) { |
| + assertEquals(expected_values[i], iter.send(send_val)); |
| + } |
| + assertThrows(function() { iter.send(send_val); }, Error); |
| + } |
| + assertIteratorValues(g1, "foo", [undefined]); |
| + assertIteratorValues(g2, "foo", [1, undefined]); |
| + assertIteratorValues(g3, "foo", [1, 2, undefined]); |
| + assertIteratorValues(g4, "foo", [1, 2, 3]); |
| + assertIteratorValues(g5, "foo", [1]); |
| + assertIteratorValues(g6, "foo", [1, "foo"]); |
| + assertIteratorValues(g7, "foo", [1, 2, "foo"]); |
| + assertIteratorValues(g8, "foo", [0, 1, 2, 3, undefined]); |
| +} |
| +TestSend(); |
| + |
| +function TestThrow() { |
| + function assertIteratorValues(g, expected_values) { |
| + for (var i = 0; i < expected_values.length; i++) { |
| + var iter = g(); |
| + for (var j = 0; j < i; j++) { |
| + assertEquals(expected_values[j], iter.next()); |
| + } |
| + function Sentinel() {} |
| + assertThrows(function () { iter.throw(new Sentinel); }, Sentinel); |
| + assertThrows(function () { iter.next(); }, Error); |
| + } |
| + } |
| + assertIteratorValues(g1, [undefined]); |
| + assertIteratorValues(g2, [1, undefined]); |
| + assertIteratorValues(g3, [1, 2, undefined]); |
| + assertIteratorValues(g4, [1, 2, 3]); |
| + assertIteratorValues(g5, [1]); |
| + assertIteratorValues(g6, [1, undefined]); |
| + assertIteratorValues(g7, [1, 2, undefined]); |
| + assertIteratorValues(g8, [0, 1, 2, 3, undefined]); |
| +} |
| +TestThrow(); |
| + |
| +function TestRecursion() { |
| + function TestNextRecursion() { |
| + function* g() { yield iter.next(); } |
|
Michael Starzinger
2013/04/21 22:45:21
This throws only because "iter" is undefined, not
wingo
2013/04/23 13:51:04
Good catch; fixed this and the two following cases
|
| + return g().next(); |
| + } |
| + function TestSendRecursion() { |
| + function* g() { yield iter.send(42); } |
|
Michael Starzinger
2013/04/21 22:45:21
Likewise.
|
| + return g().next(); |
| + } |
| + function TestThrowRecursion() { |
| + function* g() { yield iter.throw(1); } |
|
Michael Starzinger
2013/04/21 22:45:21
Likewise.
|
| + return g().next(); |
| + } |
| + assertThrows(TestNextRecursion, Error); |
|
Michael Starzinger
2013/04/21 22:45:21
Add a third argument to these three assertThrows c
wingo
2013/04/23 13:51:04
The error that is thrown is Error, which does not
|
| + assertThrows(TestSendRecursion, Error); |
| + assertThrows(TestThrowRecursion, Error); |
| +} |
| +TestRecursion(); |
|
Michael Starzinger
2013/04/21 22:45:21
The existing test cases look good. But can we add
wingo
2013/04/23 13:51:04
Good ideas all. The receiver tests that I added a
|