Commit:    1f70a4c5fea97aa577aa5d9ee5f33d91d70e690d
Author:    Nikita Popov <ni...@php.net>         Fri, 20 Jul 2012 17:40:04 +0200
Parents:   612c2490b7973d71d472860ade48d7ab342b5911
Branches:  master

Link:       
http://git.php.net/?p=php-src.git;a=commitdiff;h=1f70a4c5fea97aa577aa5d9ee5f33d91d70e690d

Log:
Add some more tests

Changed paths:
  A  Zend/tests/generators/fibonacci.phpt
  A  Zend/tests/generators/generator_closure.phpt
  A  Zend/tests/generators/generator_closure_with_this.phpt
  A  Zend/tests/generators/generator_static_method.phpt


Diff:
diff --git a/Zend/tests/generators/fibonacci.phpt 
b/Zend/tests/generators/fibonacci.phpt
new file mode 100644
index 0000000..35b3135
--- /dev/null
+++ b/Zend/tests/generators/fibonacci.phpt
@@ -0,0 +1,36 @@
+--TEST--
+Creating an infinite fibonacci list using a generator
+--FILE--
+<?php
+
+function fib() {
+    list($a, $b) = [1, 1];
+    while (true) {
+        yield $b;
+        list($a, $b) = [$b, $a + $b];
+    }
+}
+
+foreach (fib() as $n) {
+    if ($n > 1000) break;
+
+    var_dump($n);
+}
+
+?>
+--EXPECT--
+int(1)
+int(2)
+int(3)
+int(5)
+int(8)
+int(13)
+int(21)
+int(34)
+int(55)
+int(89)
+int(144)
+int(233)
+int(377)
+int(610)
+int(987)
diff --git a/Zend/tests/generators/generator_closure.phpt 
b/Zend/tests/generators/generator_closure.phpt
new file mode 100644
index 0000000..bf80066
--- /dev/null
+++ b/Zend/tests/generators/generator_closure.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Closures can be generators
+--FILE--
+<?php
+
+$genFactory = function() {
+    yield 1;
+    yield 2;
+    yield 3;
+};
+
+foreach ($genFactory() as $value) {
+    var_dump($value);
+}
+
+?>
+--EXPECT--
+int(1)
+int(2)
+int(3)
diff --git a/Zend/tests/generators/generator_closure_with_this.phpt 
b/Zend/tests/generators/generator_closure_with_this.phpt
new file mode 100644
index 0000000..d5a4861
--- /dev/null
+++ b/Zend/tests/generators/generator_closure_with_this.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Non-static closures can be generators
+--FILE--
+<?php
+
+class Test {
+    public function getGenFactory() {
+        return function() {
+            yield $this;
+        };
+    }
+}
+
+$genFactory = (new Test)->getGenFactory();
+var_dump($genFactory()->current());
+
+?>
+--EXPECT--
+object(Test)#1 (0) {
+}
diff --git a/Zend/tests/generators/generator_static_method.phpt 
b/Zend/tests/generators/generator_static_method.phpt
new file mode 100644
index 0000000..cd9b450
--- /dev/null
+++ b/Zend/tests/generators/generator_static_method.phpt
@@ -0,0 +1,29 @@
+--TEST--
+A static method can be a generator
+--FILE--
+<?php
+
+class Test {
+    public static function gen() {
+        var_dump(get_class());
+        var_dump(get_called_class());
+        yield 1;
+        yield 2;
+        yield 3;
+    }
+}
+
+class ExtendedTest extends Test {
+}
+
+foreach (ExtendedTest::gen() as $i) {
+    var_dump($i);
+}
+
+?>
+--EXPECT--
+string(4) "Test"
+string(12) "ExtendedTest"
+int(1)
+int(2)
+int(3)


--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to