com php-src: Add iterable tests: Zend/tests/type_declarations /iterable_001.phpt Zend/tests/type_declarations/i terable_002.phpt
Zend/tests/type_declarations/ite rable_003.phpt ext/standard/tests/general_functio ns/is_iterable.phpt

From: Date: Fri, 03 Jun 2016 23:41:03 +0000
Subject: com php-src: Add iterable tests: Zend/tests/type_declarations /iterable_001.phpt Zend/tests/type_declarations/i terable_002.phpt
Zend/tests/type_declarations/ite rable_003.phpt ext/standard/tests/general_functio ns/is_iterable.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-92753@lists.php.net to get a copy of this message
Commit: bea9df52811d1b5f99fd59dfd4d8232d8448b538 Author: Aaron Piotrowski <aaron@trowski.com> Fri, 3 Jun 2016 18:41:03 -0500 Parents: 8146c47d85dad9883d9153d221dc415b0ee3c12d Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=bea9df52811d1b5f99fd59dfd4d8232d8448b538 Log: Add iterable tests Changed paths: A Zend/tests/type_declarations/iterable_001.phpt A Zend/tests/type_declarations/iterable_002.phpt A Zend/tests/type_declarations/iterable_003.phpt A ext/standard/tests/general_functions/is_iterable.phpt Diff: diff --git a/Zend/tests/type_declarations/iterable_001.phpt b/Zend/tests/type_declarations/iterable_001.phpt new file mode 100644 index 0000000..f0c8bba --- /dev/null +++ b/Zend/tests/type_declarations/iterable_001.phpt @@ -0,0 +1,48 @@ +--TEST-- +iterable type#001 +--FILE-- +<?php + +function test(iterable $iterable) { + var_dump($iterable); +} + +function gen() { + yield 1; + yield 2; + yield 3; +}; + +test([1, 2, 3]); +test(gen()); +test(new ArrayIterator([1, 2, 3])); + +try { + test(1); +} catch (Throwable $e) { + echo $e->getMessage(); +} + +--EXPECTF-- +array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) +} +object(Generator)#1 (0) { +} +object(ArrayIterator)#1 (1) { + ["storage":"ArrayIterator":private]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } +} +Argument 1 passed to test() must be iterable, integer given, called in %s on line %d diff --git a/Zend/tests/type_declarations/iterable_002.phpt b/Zend/tests/type_declarations/iterable_002.phpt new file mode 100644 index 0000000..74f6d83 --- /dev/null +++ b/Zend/tests/type_declarations/iterable_002.phpt @@ -0,0 +1,21 @@ +--TEST-- +iterable type#002 - Default values +--FILE-- +<?php + +function foo(iterable $iterable = null) { + // Null should be allowed as a default value +} + +function bar(iterable $iterable = []) { + // Array should be allowed as a default value +} + +function baz(iterable $iterable = 1) { + // No other values should be allowed as defaults +} + +?> +--EXPECTF-- + +Fatal error: Default value for parameters with iterable type can only be an array or NULL in %s on line %d diff --git a/Zend/tests/type_declarations/iterable_003.phpt b/Zend/tests/type_declarations/iterable_003.phpt new file mode 100644 index 0000000..8c91c99 --- /dev/null +++ b/Zend/tests/type_declarations/iterable_003.phpt @@ -0,0 +1,32 @@ +--TEST-- +iterable type#003 - Return types +--FILE-- +<?php + +function foo(): iterable { + return []; +} +function bar(): iterable { + return (function () { yield; })(); +} + +function baz(): iterable { + return 1; +} + +var_dump(foo()); +var_dump(bar()); + +try { + baz(); +} catch (Throwable $e) { + echo $e->getMessage(); +} + +?> +--EXPECT-- +array(0) { +} +object(Generator)#2 (0) { +} +Return value of baz() must be iterable, integer returned diff --git a/ext/standard/tests/general_functions/is_iterable.phpt b/ext/standard/tests/general_functions/is_iterable.phpt new file mode 100644 index 0000000..e0822d7 --- /dev/null +++ b/ext/standard/tests/general_functions/is_iterable.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test is_iterable() function +--FILE-- +<?php + +function gen() { + yield; +} + +var_dump(is_iterable([1, 2, 3])); +var_dump(is_iterable(new ArrayIterator([1, 2, 3]))); +var_dump(is_iterable(gen())); +var_dump(is_iterable(1)); +var_dump(is_iterable(3.14)); +var_dump(is_iterable(new stdClass())); + +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(false) +bool(false) +bool(false)

« previous php.cvs (#92753) next »