parent
44fdc0b387
commit
4caed34458
10 changed files with 304 additions and 7 deletions
@ -0,0 +1,18 @@ |
||||
<?php |
||||
function phpunit_multi_values(): array |
||||
{ |
||||
$first = 1; |
||||
$second = 'two'; |
||||
return [$first, $second]; |
||||
} |
||||
|
||||
function phpunit_multi_consumer(): void |
||||
{ |
||||
[$first, $second] = phpunit_multi_values(); |
||||
$array = phpunit_multi_values(); |
||||
} |
||||
|
||||
function phpunit_multi_side_effect(): array |
||||
{ |
||||
return [time(), 2]; |
||||
} |
||||
@ -0,0 +1,40 @@ |
||||
<?php |
||||
|
||||
use PHPUnit\Framework\TestCase; |
||||
use TypePhp\CompilerTest; |
||||
|
||||
final class MultiReturnTest extends TestCase |
||||
{ |
||||
public function testGeneratesTupleFastPathAndArrayCompatibilityAdapter(): void |
||||
{ |
||||
global $translator; |
||||
$compiler = CompilerTest::create(ROOT_PATH); |
||||
$translator = $compiler; |
||||
$file = __DIR__ . '/../code/multi-return-tuple.php'; |
||||
$compiler->addFiles([$file]); |
||||
$compiler->prepareFile($file); |
||||
$cppFile = $compiler->convertFile($file); |
||||
$code = file_get_contents($cppFile); |
||||
|
||||
$this->assertStringContainsString( |
||||
'std::tuple<php::Var, php::Var> typephp::detail::multi_return::php_phpunit_multi_values()', |
||||
$code, |
||||
); |
||||
$this->assertStringContainsString( |
||||
'std::tie(first, second) = typephp::detail::multi_return::php_phpunit_multi_values()', |
||||
$code, |
||||
); |
||||
$this->assertStringContainsString( |
||||
'php::Array php_phpunit_multi_values()', |
||||
$code, |
||||
); |
||||
$this->assertStringContainsString( |
||||
'array = php_phpunit_multi_values()', |
||||
$code, |
||||
); |
||||
$this->assertStringNotContainsString( |
||||
'typephp::detail::multi_return::php_phpunit_multi_side_effect', |
||||
$code, |
||||
); |
||||
} |
||||
} |
||||
@ -0,0 +1,22 @@ |
||||
--TEST-- |
||||
Tuple multi-return fast path supports namespaced functions |
||||
--FILE-- |
||||
<?php |
||||
namespace MultiReturnExample { |
||||
function values(): array |
||||
{ |
||||
return [10, 'namespaced']; |
||||
} |
||||
} |
||||
|
||||
namespace { |
||||
function main(): void |
||||
{ |
||||
[$number, $text] = \MultiReturnExample\values(); |
||||
var_dump($number, $text); |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(10) |
||||
string(10) "namespaced" |
||||
@ -0,0 +1,77 @@ |
||||
--TEST-- |
||||
Fixed list returns use tuple fast path while preserving array semantics |
||||
--FILE-- |
||||
<?php |
||||
function multi_values(): array |
||||
{ |
||||
$a = 1; |
||||
$b = 'two'; |
||||
$c = true; |
||||
return [$a, $b, $c]; |
||||
} |
||||
|
||||
function inferred_multi_values() |
||||
{ |
||||
return [4, 'five']; |
||||
} |
||||
|
||||
function multi_with_default(int $value = 7): array |
||||
{ |
||||
return [$value, 'default']; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
[$a, $b, $c] = multi_values(); |
||||
var_dump($a, $b, $c); |
||||
|
||||
$array = multi_values(); |
||||
var_dump($array); |
||||
|
||||
$function = 'multi_values'; |
||||
var_dump($function()); |
||||
|
||||
[$x, $y] = multi_values(); |
||||
var_dump($x, $y); |
||||
|
||||
[$h, , $j] = multi_values(); |
||||
var_dump($h, $j); |
||||
|
||||
[$d, $e] = inferred_multi_values(); |
||||
var_dump($d, $e); |
||||
|
||||
[$f, $g] = multi_with_default(); |
||||
[$k, $l] = multi_with_default(value: 8); |
||||
var_dump($f, $g, $k, $l); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(1) |
||||
string(3) "two" |
||||
bool(true) |
||||
array(3) { |
||||
[0]=> |
||||
int(1) |
||||
[1]=> |
||||
string(3) "two" |
||||
[2]=> |
||||
bool(true) |
||||
} |
||||
array(3) { |
||||
[0]=> |
||||
int(1) |
||||
[1]=> |
||||
string(3) "two" |
||||
[2]=> |
||||
bool(true) |
||||
} |
||||
int(1) |
||||
string(3) "two" |
||||
int(1) |
||||
bool(true) |
||||
int(4) |
||||
string(4) "five" |
||||
int(7) |
||||
string(7) "default" |
||||
int(8) |
||||
string(7) "default" |
||||
Loading…
Reference in new issue