Skip to content

Commit

Permalink
Add more unit tests for PreProcessor::commentOut()
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed Oct 6, 2023
1 parent 7c2d75f commit c6f5fb0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 35 deletions.
19 changes: 12 additions & 7 deletions tests/muqsit/preprocessor/PreProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace muqsit\preprocessor;

use PHPUnit\Framework\TestCase;
use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path;
use function explode;
use function file_get_contents;
Expand Down Expand Up @@ -36,14 +37,18 @@ private function assertDiff(PreProcessor $processor, array $differences) : void{
}

public function testCommentOutMethodCalls() : void{
$processor = $this->buildPreProcessorForSample("logger-debug-method-call.php");
$processor->commentOut(\Logger::class, "debug");
$processor = $this->buildPreProcessorForSample("comment-out-method-call.php");
$processor->commentOut(\Logger::class, "debug"); // non-static method
$processor->commentOut(Utils::class, "validateCallableSignature"); // static method
$this->assertDiff($processor, [
[15, "\t\t/* \$this->getLogger()->debug(\"Plugin enabled timestamp: \" . \\time()) */;"],
[18, "\t\t/* \$logger->debug(\"Logging from {\$this->getName()}\") */;"],
[23, "\t\t/* \$child->debug(\"Hello world\") */;"],
[25, "\t\t/* \$this->l1->debug(\"test phpdoc typed property\") */;"],
[26, "\t\t/* \$this->l2->debug(\"test native typed property\") */;"]
[24, "\t\t/* \$this->getLogger()->debug(\"Plugin enabled timestamp: \" . time()) */;"],
[27, "\t\t/* \$logger->debug(\"Logging from {\$this->getName()}\") */;"],
[32, "\t\t/* \$child->debug(\"Hello world\") */;"],
[34, "\t\t/* \$this->l1->debug(\"test phpdoc typed property\") */;"],
[35, "\t\t/* \$this->l2->debug(\"test native typed property\") */;"],
[38, "\t\t\$l->info(\"x\"); /* \$l->debug(\"y\") */; \$l->notice(\"z\");"],
[45, "\t\t/* \\pocketmine\\utils\\Utils::validateCallableSignature(static fn(\\pocketmine\\player\\Player \$player): bool => true, \$listener) */;"],
[54, "\t\t/* \$utils::validateCallableSignature(static fn(\\pocketmine\\entity\\Entity \$entity): bool => true, \$listener) */;"]
]);
}
}
57 changes: 57 additions & 0 deletions tests/muqsit/preprocessor/samples/comment-out-method-call.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace muqsit\preprocessor;

use Closure;
use Logger;
use PrefixedLogger;
use pocketmine\entity\Entity;
use pocketmine\player\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\Utils;
use Ramsey\Uuid\Uuid;

final class SampleLoggerDebug extends PluginBase{

/** @var Logger */
private $l1;

private Logger $l2;

protected function onEnable() : void{
$this->getLogger()->debug("Plugin enabled timestamp: " . time());

$logger = $this->getServer()->getLogger();
$logger->debug("Logging from {$this->getName()}");

$logger->notice("This isn't a debug message");

$child = new PrefixedLogger($this->getLogger(), "Prefix");
$child->debug("Hello world");

$this->l1->debug("test phpdoc typed property");
$this->l2->debug("test native typed property");

$l = $this->l1;
$l->info("x"); $l->debug("y"); $l->notice("z");
}

/**
* @param Closure(Player) : bool $listener
*/
public function registerPlayerListener(Closure $listener) : void{
Utils::validateCallableSignature(static fn(Player $player) : bool => true, $listener);
$listener($this->getServer()->getPlayerByUUID(Uuid::fromString(Uuid::NIL)));
}

/**
* @param Closure(Entity) : bool $listener
*/
public function registerEntityListener(Closure $listener) : void{
$utils = Utils::class;
$utils::validateCallableSignature(static fn(Entity $entity) : bool => true, $listener);
$listener($this->getServer()->getWorldManager()->findEntity(0));
}
}
28 changes: 0 additions & 28 deletions tests/muqsit/preprocessor/samples/logger-debug-method-call.php

This file was deleted.

0 comments on commit c6f5fb0

Please sign in to comment.