From 3b46c168a5ce6490106447159cb8dfa72b15d6a2 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 7 Jan 2017 22:55:34 +0100 Subject: [PATCH] Improve tests and kill some Humbug mutants --- humbug.json.dist | 3 ++ lib/Listener.php | 1 - lib/Struct.php | 6 +-- test/CoroutineTest.php | 84 ++++++++++++++++++++++++++++++------------ test/StructTest.php | 15 ++++++++ 5 files changed, 82 insertions(+), 27 deletions(-) diff --git a/humbug.json.dist b/humbug.json.dist index 0854060..4197f79 100644 --- a/humbug.json.dist +++ b/humbug.json.dist @@ -2,6 +2,9 @@ "source": { "directories": [ "lib" + ], + "excludes": [ + "lib/functions.php" ] }, "timeout": 10, diff --git a/lib/Listener.php b/lib/Listener.php index 95a3180..cd3f0f3 100644 --- a/lib/Listener.php +++ b/lib/Listener.php @@ -200,7 +200,6 @@ class Listener { $values = $this->values; $this->values = []; - $this->position = -1; $deferreds = $this->backPressure; $this->backPressure = []; diff --git a/lib/Struct.php b/lib/Struct.php index 0a18096..d3c6b80 100644 --- a/lib/Struct.php +++ b/lib/Struct.php @@ -30,7 +30,7 @@ trait Struct { private function generateStructPropertyError(string $property): string { $suggestion = $this->suggestPropertyName($property); $suggestStr = ($suggestion == "") ? "" : " ... did you mean \"{$suggestion}?\""; - + return \sprintf( "%s property \"%s\" does not exist%s", \str_replace("\0", "@", \get_class($this)), // Handle anonymous class names. @@ -42,8 +42,8 @@ trait Struct { private function suggestPropertyName(string $badProperty): string { $badProperty = \strtolower($badProperty); $bestMatch = ""; - $bestMatchPercentage = 0.0; - $byRefPercentage = 0.0; + $bestMatchPercentage = 0; + foreach ($this as $property => $value) { // Never suggest properties that begin with an underscore if ($property[0] === "_") { diff --git a/test/CoroutineTest.php b/test/CoroutineTest.php index aa4cf8f..7c61b06 100644 --- a/test/CoroutineTest.php +++ b/test/CoroutineTest.php @@ -13,10 +13,10 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $value = 1; $generator = function () use (&$yielded, $value) { - $yielded = (yield new Success($value)); + $yielded = yield new Success($value); }; - $coroutine = new Coroutine($generator()); + new Coroutine($generator()); $this->assertSame($value, $yielded); } @@ -25,7 +25,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $exception = new \Exception; $generator = function () use (&$yielded, $exception) { - $yielded = (yield new Failure($exception)); + $yielded = yield new Failure($exception); }; $coroutine = new Coroutine($generator()); @@ -47,10 +47,10 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { Loop::execute(function () use (&$yielded, $value) { $generator = function () use (&$yielded, $value) { - $yielded = (yield new Pause(self::TIMEOUT, $value)); + $yielded = yield new Pause(self::TIMEOUT, $value); }; - $coroutine = new Coroutine($generator()); + new Coroutine($generator()); }); $this->assertSame($value, $yielded); @@ -74,7 +74,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $fail = true; }; - $coroutine = new Coroutine($generator()); + new Coroutine($generator()); if ($fail) { $this->fail("Failed promise reason not thrown into generator"); @@ -240,10 +240,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$result) { + $coroutine->when(function ($exception, $value) use (&$reason, &$result) { + $reason = $exception; $result = $value; }); + $this->assertNull($reason); $this->assertNull($result); } @@ -257,7 +259,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$reason) { + $coroutine->when(function ($exception) use (&$reason) { $reason = $exception; }); @@ -282,7 +284,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$reason) { + $coroutine->when(function ($exception) use (&$reason) { $reason = $exception; }); @@ -308,7 +310,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$reason) { + $coroutine->when(function ($exception) use (&$reason) { $reason = $exception; }); @@ -336,7 +338,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$reason) { + $coroutine->when(function ($exception) use (&$reason) { $reason = $exception; }); }); @@ -367,7 +369,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$reason) { + $coroutine->when(function ($exception) use (&$reason) { $reason = $exception; }); }); @@ -395,7 +397,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$reason) { + $coroutine->when(function ($exception) use (&$reason) { $reason = $exception; }); @@ -419,7 +421,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$invoked) { + $coroutine->when(function () use (&$invoked) { $invoked = true; }); }); @@ -448,7 +450,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$invoked) { + $coroutine->when(function () use (&$invoked) { $invoked = true; }); }); @@ -467,7 +469,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); $invoked = false; - $coroutine->when(function ($exception, $value) use (&$invoked) { + $coroutine->when(function () use (&$invoked) { $invoked = true; }); @@ -492,14 +494,17 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { return $value; }); + /** @var Promise $promise */ $promise = $callable($promise); $this->assertInstanceOf(Promise::class, $promise); - $promise->when(function ($exception, $value) use (&$result) { + $promise->when(function ($exception, $value) use (&$reason, &$result) { + $reason = $exception; $result = $value; }); + $this->assertNull($reason); $this->assertSame($value, $result); } @@ -512,14 +517,17 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { return $value; }); + /** @var Promise $promise */ $promise = $callable($value); $this->assertInstanceOf(Promise::class, $promise); - $promise->when(function ($exception, $value) use (&$result) { + $promise->when(function ($exception, $value) use (&$reason, &$result) { + $reason = $exception; $result = $value; }); + $this->assertNull($reason); $this->assertSame($value, $result); } @@ -532,15 +540,40 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { throw $exception; }); + /** @var Promise $promise */ $promise = $callable(); $this->assertInstanceOf(Promise::class, $promise); - $promise->when(function ($exception, $value) use (&$reason) { + $promise->when(function ($exception, $value) use (&$reason, &$result) { $reason = $exception; + $result = $value; }); $this->assertSame($exception, $reason); + $this->assertNull($result); + } + + /** + * @depends testCoroutineFunction + */ + public function testCoroutineFunctionWithSuccessReturnCallback() { + $callable = Amp\coroutine(function () { + return new Success(42); + }); + + /** @var Promise $promise */ + $promise = $callable(); + + $this->assertInstanceOf(Promise::class, $promise); + + $promise->when(function ($exception, $value) use (&$reason, &$result) { + $reason = $exception; + $result = $value; + }); + + $this->assertNull($reason); + $this->assertSame(42, $result); } public function testCoroutineResolvedWithReturn() { @@ -553,10 +586,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$result) { + $coroutine->when(function ($exception, $value) use (&$reason, &$result) { + $reason = $exception; $result = $value; }); + $this->assertNull($reason); $this->assertSame($value, $result); } @@ -576,11 +611,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$result) { + $coroutine->when(function ($exception, $value) use (&$reason, &$result) { + $reason = $exception; $result = $value; }); - + $this->assertNull($reason); $this->assertSame($value, $result); } @@ -603,10 +639,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase { $coroutine = new Coroutine($generator()); - $coroutine->when(function ($exception, $value) use (&$result) { + $coroutine->when(function ($exception, $value) use (&$reason, &$result) { + $reason = $exception; $result = $value; }); + $this->assertNull($reason); $this->assertSame($value, $result); } } diff --git a/test/StructTest.php b/test/StructTest.php index e9c6cdb..212c7e7 100644 --- a/test/StructTest.php +++ b/test/StructTest.php @@ -5,6 +5,7 @@ namespace Amp\Test; class StructTestFixture { use \Amp\Struct; public $callback; + public $_foofoofoofoofoofoofoofoobar; } class StructTest extends \PHPUnit_Framework_TestCase { @@ -52,4 +53,18 @@ class StructTest extends \PHPUnit_Framework_TestCase { $struct = new StructTestFixture; $test = $struct->__propertySuggestThreshold; } + + public function testSetErrorWithoutSuggestionBecauseUnderscore() { + // Use regexp to ensure no property is suggested, because expected message is a prefix then and still passes + $this->setExpectedExceptionRegExp(\Error::class, "(Amp\\\\Test\\\\StructTestFixture property \"foofoofoofoofoofoofoofoobar\" does not exist$)"); + $struct = new StructTestFixture; + $struct->foofoofoofoofoofoofoofoobar = "test"; + } + + public function testGetErrorWithoutSuggestionBecauseUnderscore() { + // Use regexp to ensure no property is suggested, because expected message is a prefix then and still passes + $this->setExpectedExceptionRegExp(\Error::class, "(Amp\\\\Test\\\\StructTestFixture property \"foofoofoofoofoofoofoofoobar\" does not exist$)"); + $struct = new StructTestFixture; + $test = $struct->foofoofoofoofoofoofoofoobar; + } }