summaryrefslogtreecommitdiff
path: root/Zend/tests/generators/generator_rewind.phpt
blob: 651f4d9e24031adb83545288b5a3d5e9fa13737e (plain)
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
--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: Cannot rewind a generator that was already run in %s:%d
Stack trace:
#0 %s(%d): Generator->rewind()
#1 {main}

Exception: Cannot traverse an already closed generator in %s:%d
Stack trace:
#0 {main}

in generator