generator_rewind.phpt 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. --TEST--
  2. A generator can only be rewinded before or at the first yield
  3. --FILE--
  4. <?php
  5. function gen() {
  6. echo "before yield\n";
  7. yield;
  8. echo "after yield\n";
  9. yield;
  10. }
  11. $gen = gen();
  12. $gen->rewind();
  13. $gen->rewind();
  14. $gen->next();
  15. try {
  16. $gen->rewind();
  17. } catch (Exception $e) {
  18. echo "\n", $e, "\n\n";
  19. }
  20. function &gen2() {
  21. $foo = 'bar';
  22. yield $foo;
  23. yield $foo;
  24. }
  25. $gen = gen2();
  26. foreach ($gen as $v) { }
  27. try {
  28. foreach ($gen as $v) { }
  29. } catch (Exception $e) {
  30. echo $e, "\n\n";
  31. }
  32. function gen3() {
  33. echo "in generator\n";
  34. if (false) yield;
  35. }
  36. $gen = gen3();
  37. $gen->rewind();
  38. ?>
  39. --EXPECTF--
  40. before yield
  41. after yield
  42. exception 'Exception' with message 'Cannot rewind a generator that was already run' in %s:%d
  43. Stack trace:
  44. #0 %s(%d): Generator->rewind()
  45. #1 {main}
  46. exception 'Exception' with message 'Cannot traverse an already closed generator' in %s:%d
  47. Stack trace:
  48. #0 %s(%d): unknown()
  49. #1 {main}
  50. in generator