1
0
mirror of https://github.com/php/php-src.git synced 2026-04-27 18:23:26 +02:00

Add iterable tests

This commit is contained in:
Aaron Piotrowski
2016-06-03 18:41:03 -05:00
parent 8146c47d85
commit bea9df5281
4 changed files with 125 additions and 0 deletions
@@ -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
@@ -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
@@ -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
@@ -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)