File: generator_rewind.phpt

package info (click to toggle)
php5 5.6.33%2Bdfsg-0%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 157,872 kB
  • sloc: ansic: 756,065; php: 22,030; sh: 12,311; cpp: 8,771; xml: 6,179; yacc: 1,564; exp: 1,514; makefile: 1,467; pascal: 1,147; awk: 538; perl: 315; sql: 22
file content (62 lines) | stat: -rw-r--r-- 960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
--TEST--
A generator can only be rewinded before or at the first yield
--FILE--
<?php

function gen() {
    echo "before yield\n";
    yield;
    echo "after yield\n";
    yield;
}

$gen = gen();
$gen->rewind();
$gen->rewind();
$gen->next();

try {
    $gen->rewind();
} catch (Exception $e) {
    echo "\n", $e, "\n\n";
}

function &gen2() {
    $foo = 'bar';
    yield $foo;
    yield $foo;
}

$gen = gen2();
foreach ($gen as $v) { }
try {
    foreach ($gen as $v) { }
} catch (Exception $e) {
    echo $e, "\n\n";
}

function gen3() {
    echo "in generator\n";

    if (false) yield;
}

$gen = gen3();
$gen->rewind();

?>
--EXPECTF--
before yield
after yield

exception 'Exception' with message 'Cannot rewind a generator that was already run' in %s:%d
Stack trace:
#0 %s(%d): Generator->rewind()
#1 {main}

exception 'Exception' with message 'Cannot traverse an already closed generator' in %s:%d
Stack trace:
#0 %s(%d): unknown()
#1 {main}

in generator