Skip to content

Commit

Permalink
fix: spaceship opcode cannot be stringified
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed Jul 12, 2024
1 parent c4f26ca commit 73fe866
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/muqsit/arithmexp/token/OpcodeToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static function opcodeToString(int $code) : string{
self::OP_BINARY_MUL => "*",
self::OP_BINARY_OR_SYMBOL => "||",
self::OP_BINARY_OR_TEXTUAL => "or",
self::OP_BINARY_SPACESHIP => "<=>",
self::OP_BINARY_SUB, self::OP_UNARY_NVE => "-",
self::OP_BINARY_XOR => "xor",
self::OP_UNARY_NOT => "!"
Expand Down
21 changes: 21 additions & 0 deletions tests/muqsit/arithmexp/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
use muqsit\arithmexp\token\FunctionCallToken;
use muqsit\arithmexp\token\IdentifierToken;
use muqsit\arithmexp\token\NumericLiteralToken;
use muqsit\arithmexp\token\OpcodeToken;
use muqsit\arithmexp\token\Token;
use muqsit\arithmexp\token\UnaryOperatorToken;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use RuntimeException;
use UnhandledMatchError;
use function array_filter;
use function str_starts_with;
use function strlen;
use const ARRAY_FILTER_USE_KEY;
use const M_PI;

final class ParserTest extends TestCase{
Expand Down Expand Up @@ -209,6 +217,19 @@ public function testOperatorNonAssociativity() : void{
TestUtil::assertExpressionsEqual($this->uo_parser->parse("(x > y) == (x < z)"), $this->uo_parser->parse("x > y == x < z"));
}

public function testOpcodeTokenToString() : void{
$codes = (new ReflectionClass(OpcodeToken::class))->getConstants();
$codes = array_filter($codes, fn(string $key) => str_starts_with($key, "OP_"), ARRAY_FILTER_USE_KEY);
foreach($codes as $name => $code){
try{
$string = OpcodeToken::opcodeToString($code);
}catch(UnhandledMatchError $e){
throw new RuntimeException("Failed to stringify opcode {$name}", $e->getCode(), $e);
}
$this->assertGreaterThan(0, strlen($string));
}
}

public function testBadFunctionCallToUndefinedFunction() : void{
TestUtil::assertParserThrows($this->parser, "x * fn() / 3", ParseException::ERR_UNRESOLVABLE_FCALL, 4, 8);
}
Expand Down
2 changes: 1 addition & 1 deletion virion.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: arithmexp
antigen: muqsit\arithmexp
api: [4.21.1, 5.0.0]
version: 0.1.30
version: 1.0.1
author: Muqsit

0 comments on commit 73fe866

Please sign in to comment.