aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-04-28 17:47:20 +0900
committernsfisis <nsfisis@gmail.com>2024-04-28 17:47:20 +0900
commit453f0a0d7346c16b327e39520374a555ecc8f2e4 (patch)
tree40048ed5b4d1f50cd6c54cd66b0664c7ae792bff
parent88a77bc5846541e5d6009bc103da378650a649f3 (diff)
downloadphp-waddiwasi-453f0a0d7346c16b327e39520374a555ecc8f2e4.tar.gz
php-waddiwasi-453f0a0d7346c16b327e39520374a555ecc8f2e4.tar.zst
php-waddiwasi-453f0a0d7346c16b327e39520374a555ecc8f2e4.zip
test: check trap error message
-rw-r--r--src/Execution/TrapException.php17
-rw-r--r--src/Execution/TrapKind.php11
-rw-r--r--tests/src/SpecTestsuites/SpecTestsuiteBase.php24
3 files changed, 50 insertions, 2 deletions
diff --git a/src/Execution/TrapException.php b/src/Execution/TrapException.php
index 2948e8d..3e6a440 100644
--- a/src/Execution/TrapException.php
+++ b/src/Execution/TrapException.php
@@ -5,7 +5,24 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Execution;
use RuntimeException;
+use Throwable;
class TrapException extends RuntimeException
{
+ private readonly TrapKind $trapKind;
+
+ public function __construct(
+ string $message = "",
+ int $code = 0,
+ ?Throwable $previous = null,
+ TrapKind $trapKind = TrapKind::Unknown,
+ ) {
+ parent::__construct($message, $code, $previous);
+ $this->trapKind = $trapKind;
+ }
+
+ public function getTrapKind(): TrapKind
+ {
+ return $this->trapKind;
+ }
}
diff --git a/src/Execution/TrapKind.php b/src/Execution/TrapKind.php
new file mode 100644
index 0000000..47c1c77
--- /dev/null
+++ b/src/Execution/TrapKind.php
@@ -0,0 +1,11 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution;
+
+enum TrapKind
+{
+ case Unknown;
+ case OutOfBoundsMemoryAccess;
+}
diff --git a/tests/src/SpecTestsuites/SpecTestsuiteBase.php b/tests/src/SpecTestsuites/SpecTestsuiteBase.php
index 72d1a86..1aa37de 100644
--- a/tests/src/SpecTestsuites/SpecTestsuiteBase.php
+++ b/tests/src/SpecTestsuites/SpecTestsuiteBase.php
@@ -8,6 +8,7 @@ use Nsfisis\Waddiwasi\BinaryFormat\Decoder;
use Nsfisis\Waddiwasi\Execution\Runtime;
use Nsfisis\Waddiwasi\Execution\Store;
use Nsfisis\Waddiwasi\Execution\TrapException;
+use Nsfisis\Waddiwasi\Execution\TrapKind;
use PHPUnit\Framework\TestCase;
use function count;
@@ -84,7 +85,7 @@ abstract class SpecTestsuiteBase extends TestCase
$exception = $e;
}
$this->assertNotNull($exception, "at $line");
- // @todo check trap message
+ $this->assertTrapKind($text, $e->getTrapKind(), "at $line");
} else {
$this->assertTrue(false, "assert_trap: unknown action, $actionType");
}
@@ -176,7 +177,7 @@ abstract class SpecTestsuiteBase extends TestCase
$this->assertCount(
count($expectedResults),
$actualResults,
- "results count mismatch" . $message,
+ 'results count mismatch' . $message,
);
for ($i = 0; $i < count($expectedResults); $i++) {
@@ -222,4 +223,23 @@ abstract class SpecTestsuiteBase extends TestCase
}
}
}
+
+ private function assertTrapKind(
+ string $expectedErrorMessage,
+ TrapKind $kind,
+ string $message = '',
+ ): void {
+ if ($message !== '') {
+ $message = " ($message)";
+ }
+ $actualErrorMessage = match ($kind) {
+ TrapKind::OutOfBoundsMemoryAccess => 'out of bounds memory access',
+ TrapKind::Unknown => 'unknown',
+ };
+ $this->assertSame(
+ $expectedErrorMessage,
+ $actualErrorMessage,
+ 'trap kind mismatch' . $message,
+ );
+ }
}