$command) { $type = $command['type']; $line = $command['line']; $methodName = 'test' . implode('', array_map(ucfirst(...), explode('_', $type))) . $i; $commandTest = match ($type) { 'module' => buildModuleCommandTest($command), 'assert_return' => buildAssertReturnCommandTest($command), 'assert_trap' => buildAssertTrapCommandTest($command), 'assert_malformed' => buildAssertMalformedCommandTest($command), 'assert_invalid' => buildAssertInvalidCommandTest($command), 'assert_exhaustion' => buildAssertExhaustionCommandTest($command), 'assert_uninstantiable' => buildAssertUninstantiableCommandTest($command), 'assert_unlinkable' => buildAssertUnlinkableCommandTest($command), 'action' => buildActionCommandTest($command), 'register' => buildRegisterCommandTest($command), default => exit("Unknown command type: {$type}\n"), }; if ($commandTest === null) { fwrite($fp, " #[DoesNotPerformAssertions]\n"); } fwrite($fp, " public function {$methodName}(): void\n"); fwrite($fp, " {\n"); if ($commandTest !== null) { fwrite($fp, " \$this->{$commandTest['method']}(\n"); foreach ($commandTest['args'] as $argName => $argValue) { $argValue = toPhpLiteral($argValue); fwrite($fp, " {$argName}: {$argValue},\n"); } fwrite($fp, " line: {$line},\n"); fwrite($fp, " );\n"); } fwrite($fp, " }\n"); if ($i !== count($commands) - 1) { fwrite($fp, "\n"); } } fwrite($fp, "}\n"); fclose($fp); } function wastJsonFileToPascalCase(string $s): string { $s = strtr(basename($s), [ '.json' => '', '-' => '_', ]); return implode('', array_map(ucfirst(...), explode('_', $s))); } function buildModuleCommandTest(array $command): array { return [ 'method' => 'runModuleCommand', 'args' => [ 'filename' => $command['filename'], 'name' => $command['name'] ?? null, ], ]; } function buildAssertReturnCommandTest(array $command): array { return [ 'method' => 'runAssertReturnCommand', 'args' => [ 'action' => $command['action'], 'expected' => $command['expected'], ], ]; } function buildAssertTrapCommandTest(array $command): array { return [ 'method' => 'runAssertTrapCommand', 'args' => [ 'action' => $command['action'], 'text' => $command['text'], ], ]; } function buildAssertMalformedCommandTest(array $command): ?array { $moduleType = $command['module_type']; if ($moduleType === 'text') { // Our interpreter does not support text format (.wat format). return null; } return [ 'method' => 'runAssertMalformedCommand', 'args' => [ 'filename' => $command['filename'], 'text' => $command['text'], ], ]; } function buildAssertInvalidCommandTest(array $command): ?array { $moduleType = $command['module_type']; if ($moduleType === 'text') { // Our interpreter does not support text format (.wat format). return null; } return [ 'method' => 'runAssertInvalidCommand', 'args' => [ 'filename' => $command['filename'], 'text' => $command['text'], ], ]; } function buildAssertExhaustionCommandTest(array $command): array { return [ 'method' => 'runAssertExhaustionCommand', 'args' => [ 'action' => $command['action'], 'text' => $command['text'], ], ]; } function buildAssertUninstantiableCommandTest(array $command): ?array { $moduleType = $command['module_type']; if ($moduleType === 'text') { // Our interpreter does not support text format (.wat format). return null; } return [ 'method' => 'runAssertUninstantiableCommand', 'args' => [ 'filename' => $command['filename'], 'text' => $command['text'], ], ]; } function buildAssertUnlinkableCommandTest(array $command): ?array { $moduleType = $command['module_type']; if ($moduleType === 'text') { // Our interpreter does not support text format (.wat format). return null; } return [ 'method' => 'runAssertUnlinkableCommand', 'args' => [ 'filename' => $command['filename'], 'text' => $command['text'], ], ]; } function buildActionCommandTest(array $command): array { return [ 'method' => 'runActionCommand', 'args' => [ 'action' => $command['action'], ], ]; } function buildRegisterCommandTest(array $command): array { return [ 'method' => 'runRegisterCommand', 'args' => [ 'name' => $command['name'] ?? null, 'as' => $command['as'], ], ]; } function toPhpLiteral(mixed $value): string { if ($value === null) { return 'null'; } elseif (is_int($value)) { return (string) $value; } elseif (is_string($value)) { $value = strtr($value, [ '\\' => '\\\\', "'" => "\\'", ]); return "'{$value}'"; } elseif (is_array($value) && array_is_list($value)) { return '[' . implode(', ', array_map(toPhpLiteral(...), $value)) . ']'; } elseif (is_array($value)) { return '[' . implode(', ', array_map(fn ($k) => "'{$k}' => " . toPhpLiteral($value[$k]), array_keys($value))) . ']'; } return 'ERROR'; }