- 引入 CompilerTest 类用于测试模式运行 - 在 CompilerBase 中添加 forTest 标志和 TestError 异常 - 实现静态变量重复定义的检测和报错功能 - 添加 phpunit 配置文件和引导文件 - 创建重复静态变量的测试用例 - 添加基本静态变量功能的测试文件 - 修改 Translator 的 addFiles 方法支持文件列表添加 - convert 方法中添加文件路径解析功能pull/1/head
parent
3a3c8eb6b6
commit
76c1104234
9 changed files with 182 additions and 3 deletions
@ -0,0 +1,8 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="./phpunit/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> |
||||||
|
<testsuites> |
||||||
|
<testsuite name="Tests"> |
||||||
|
<directory suffix="Test.php">./phpunit</directory> |
||||||
|
</testsuite> |
||||||
|
</testsuites> |
||||||
|
</phpunit> |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
|
||||||
|
use PhpAot\Php\CompilerTest; |
||||||
|
|
||||||
|
require __DIR__ . '/../bin/bootstrap.php'; |
||||||
|
|
||||||
|
CompilerTest::getInstance(ROOT_PATH); |
||||||
|
|
||||||
@ -0,0 +1,6 @@ |
|||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
static $a = 10; |
||||||
|
static $a = 11; |
||||||
|
} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
|
||||||
|
use PhpAot\Php\CompilerTest; |
||||||
|
use PhpAot\Php\Exception\TestError; |
||||||
|
use PHPUnit\Framework\TestCase; |
||||||
|
|
||||||
|
class ErrorTest extends TestCase |
||||||
|
{ |
||||||
|
public function testDuplicateStaticVar() |
||||||
|
{ |
||||||
|
try { |
||||||
|
$o = CompilerTest::getInstance(); |
||||||
|
$file = __DIR__ . '/../code/duplicate.php'; |
||||||
|
$o->addFiles([$file]); |
||||||
|
$o->convert($file); |
||||||
|
} catch (TestError $exception) { |
||||||
|
$this->assertStringContainsString('Duplicate static variable', $exception->getMessage()); |
||||||
|
return; |
||||||
|
} |
||||||
|
$this->fail(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* This file is part of Swoole-Compiler(AOT). |
||||||
|
* |
||||||
|
* @link https://www.swoole.com/ |
||||||
|
* @contact service@swoole.com |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace PhpAot\Php; |
||||||
|
|
||||||
|
class CompilerTest extends Translator |
||||||
|
{ |
||||||
|
protected static ?self $instance = null; |
||||||
|
|
||||||
|
public static function getInstance(string $rootPath = ''): CompilerTest |
||||||
|
{ |
||||||
|
if (!self::$instance) { |
||||||
|
self::$instance = new self($rootPath); |
||||||
|
self::$instance->forTest = true; |
||||||
|
} |
||||||
|
return self::$instance; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,8 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace PhpAot\Php\Exception; |
||||||
|
|
||||||
|
class TestError extends \RuntimeException |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,87 @@ |
|||||||
|
--TEST-- |
||||||
|
Static keyword - basic tests |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function staticNonStatic() { |
||||||
|
echo "---------\n"; |
||||||
|
$a=0; |
||||||
|
echo "$a\n"; |
||||||
|
static $a=10; |
||||||
|
echo "$a\n"; |
||||||
|
$a++; |
||||||
|
} |
||||||
|
|
||||||
|
function manyInits() { |
||||||
|
static $counter=0; |
||||||
|
echo "------------- Call $counter --------------\n"; |
||||||
|
static $a, $b=10, $c=20, $d, $e=30; |
||||||
|
echo "Uninitialized : $a\n"; |
||||||
|
echo "Initialized to 10: $b\n"; |
||||||
|
echo "Initialized to 20: $c\n"; |
||||||
|
echo "Uninitialized : $d\n"; |
||||||
|
echo "Initialized to 30: $e\n"; |
||||||
|
$a++; |
||||||
|
$b++; |
||||||
|
$c++; |
||||||
|
$d++; |
||||||
|
$e++; |
||||||
|
$counter++; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
function main() { |
||||||
|
echo "\nSame variable used as static and non static.\n"; |
||||||
|
staticNonStatic(); |
||||||
|
staticNonStatic(); |
||||||
|
staticNonStatic(); |
||||||
|
|
||||||
|
echo "\nLots of initialisations in the same statement.\n"; |
||||||
|
manyInits(); |
||||||
|
manyInits(); |
||||||
|
manyInits(); |
||||||
|
|
||||||
|
echo "\nUsing static keyword at global scope\n"; |
||||||
|
for ($i=0; $i<3; $i++) { |
||||||
|
static $s, $k=10; |
||||||
|
echo "$s $k\n"; |
||||||
|
$s++; |
||||||
|
$k++; |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
Same variable used as static and non static. |
||||||
|
--------- |
||||||
|
0 |
||||||
|
10 |
||||||
|
--------- |
||||||
|
0 |
||||||
|
11 |
||||||
|
--------- |
||||||
|
0 |
||||||
|
12 |
||||||
|
|
||||||
|
Lots of initialisations in the same statement. |
||||||
|
------------- Call 0 -------------- |
||||||
|
Uninitialized : |
||||||
|
Initialized to 10: 10 |
||||||
|
Initialized to 20: 20 |
||||||
|
Uninitialized : |
||||||
|
Initialized to 30: 30 |
||||||
|
------------- Call 1 -------------- |
||||||
|
Uninitialized : 1 |
||||||
|
Initialized to 10: 11 |
||||||
|
Initialized to 20: 21 |
||||||
|
Uninitialized : 1 |
||||||
|
Initialized to 30: 31 |
||||||
|
------------- Call 2 -------------- |
||||||
|
Uninitialized : 2 |
||||||
|
Initialized to 10: 12 |
||||||
|
Initialized to 20: 22 |
||||||
|
Uninitialized : 2 |
||||||
|
Initialized to 30: 32 |
||||||
|
|
||||||
|
Using static keyword at global scope |
||||||
|
10 |
||||||
|
1 11 |
||||||
|
2 12 |
||||||
Loading…
Reference in new issue