1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00

Improve tests and kill some Humbug mutants

This commit is contained in:
Niklas Keller 2017-01-07 22:55:34 +01:00
parent dfa3b82485
commit 3b46c168a5
5 changed files with 82 additions and 27 deletions

View File

@ -2,6 +2,9 @@
"source": { "source": {
"directories": [ "directories": [
"lib" "lib"
],
"excludes": [
"lib/functions.php"
] ]
}, },
"timeout": 10, "timeout": 10,

View File

@ -200,7 +200,6 @@ class Listener {
$values = $this->values; $values = $this->values;
$this->values = []; $this->values = [];
$this->position = -1;
$deferreds = $this->backPressure; $deferreds = $this->backPressure;
$this->backPressure = []; $this->backPressure = [];

View File

@ -30,7 +30,7 @@ trait Struct {
private function generateStructPropertyError(string $property): string { private function generateStructPropertyError(string $property): string {
$suggestion = $this->suggestPropertyName($property); $suggestion = $this->suggestPropertyName($property);
$suggestStr = ($suggestion == "") ? "" : " ... did you mean \"{$suggestion}?\""; $suggestStr = ($suggestion == "") ? "" : " ... did you mean \"{$suggestion}?\"";
return \sprintf( return \sprintf(
"%s property \"%s\" does not exist%s", "%s property \"%s\" does not exist%s",
\str_replace("\0", "@", \get_class($this)), // Handle anonymous class names. \str_replace("\0", "@", \get_class($this)), // Handle anonymous class names.
@ -42,8 +42,8 @@ trait Struct {
private function suggestPropertyName(string $badProperty): string { private function suggestPropertyName(string $badProperty): string {
$badProperty = \strtolower($badProperty); $badProperty = \strtolower($badProperty);
$bestMatch = ""; $bestMatch = "";
$bestMatchPercentage = 0.0; $bestMatchPercentage = 0;
$byRefPercentage = 0.0;
foreach ($this as $property => $value) { foreach ($this as $property => $value) {
// Never suggest properties that begin with an underscore // Never suggest properties that begin with an underscore
if ($property[0] === "_") { if ($property[0] === "_") {

View File

@ -13,10 +13,10 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$value = 1; $value = 1;
$generator = function () use (&$yielded, $value) { $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); $this->assertSame($value, $yielded);
} }
@ -25,7 +25,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception; $exception = new \Exception;
$generator = function () use (&$yielded, $exception) { $generator = function () use (&$yielded, $exception) {
$yielded = (yield new Failure($exception)); $yielded = yield new Failure($exception);
}; };
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
@ -47,10 +47,10 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
Loop::execute(function () use (&$yielded, $value) { Loop::execute(function () use (&$yielded, $value) {
$generator = 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); $this->assertSame($value, $yielded);
@ -74,7 +74,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$fail = true; $fail = true;
}; };
$coroutine = new Coroutine($generator()); new Coroutine($generator());
if ($fail) { if ($fail) {
$this->fail("Failed promise reason not thrown into generator"); $this->fail("Failed promise reason not thrown into generator");
@ -240,10 +240,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$result) { $coroutine->when(function ($exception, $value) use (&$reason, &$result) {
$reason = $exception;
$result = $value; $result = $value;
}); });
$this->assertNull($reason);
$this->assertNull($result); $this->assertNull($result);
} }
@ -257,7 +259,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$reason) { $coroutine->when(function ($exception) use (&$reason) {
$reason = $exception; $reason = $exception;
}); });
@ -282,7 +284,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$reason) { $coroutine->when(function ($exception) use (&$reason) {
$reason = $exception; $reason = $exception;
}); });
@ -308,7 +310,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$reason) { $coroutine->when(function ($exception) use (&$reason) {
$reason = $exception; $reason = $exception;
}); });
@ -336,7 +338,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$reason) { $coroutine->when(function ($exception) use (&$reason) {
$reason = $exception; $reason = $exception;
}); });
}); });
@ -367,7 +369,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$reason) { $coroutine->when(function ($exception) use (&$reason) {
$reason = $exception; $reason = $exception;
}); });
}); });
@ -395,7 +397,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$reason) { $coroutine->when(function ($exception) use (&$reason) {
$reason = $exception; $reason = $exception;
}); });
@ -419,7 +421,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$invoked) { $coroutine->when(function () use (&$invoked) {
$invoked = true; $invoked = true;
}); });
}); });
@ -448,7 +450,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$invoked) { $coroutine->when(function () use (&$invoked) {
$invoked = true; $invoked = true;
}); });
}); });
@ -467,7 +469,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$invoked = false; $invoked = false;
$coroutine->when(function ($exception, $value) use (&$invoked) { $coroutine->when(function () use (&$invoked) {
$invoked = true; $invoked = true;
}); });
@ -492,14 +494,17 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
return $value; return $value;
}); });
/** @var Promise $promise */
$promise = $callable($promise); $promise = $callable($promise);
$this->assertInstanceOf(Promise::class, $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; $result = $value;
}); });
$this->assertNull($reason);
$this->assertSame($value, $result); $this->assertSame($value, $result);
} }
@ -512,14 +517,17 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
return $value; return $value;
}); });
/** @var Promise $promise */
$promise = $callable($value); $promise = $callable($value);
$this->assertInstanceOf(Promise::class, $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; $result = $value;
}); });
$this->assertNull($reason);
$this->assertSame($value, $result); $this->assertSame($value, $result);
} }
@ -532,15 +540,40 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
throw $exception; throw $exception;
}); });
/** @var Promise $promise */
$promise = $callable(); $promise = $callable();
$this->assertInstanceOf(Promise::class, $promise); $this->assertInstanceOf(Promise::class, $promise);
$promise->when(function ($exception, $value) use (&$reason) { $promise->when(function ($exception, $value) use (&$reason, &$result) {
$reason = $exception; $reason = $exception;
$result = $value;
}); });
$this->assertSame($exception, $reason); $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() { public function testCoroutineResolvedWithReturn() {
@ -553,10 +586,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$result) { $coroutine->when(function ($exception, $value) use (&$reason, &$result) {
$reason = $exception;
$result = $value; $result = $value;
}); });
$this->assertNull($reason);
$this->assertSame($value, $result); $this->assertSame($value, $result);
} }
@ -576,11 +611,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$result) { $coroutine->when(function ($exception, $value) use (&$reason, &$result) {
$reason = $exception;
$result = $value; $result = $value;
}); });
$this->assertNull($reason);
$this->assertSame($value, $result); $this->assertSame($value, $result);
} }
@ -603,10 +639,12 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$coroutine = new Coroutine($generator()); $coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$result) { $coroutine->when(function ($exception, $value) use (&$reason, &$result) {
$reason = $exception;
$result = $value; $result = $value;
}); });
$this->assertNull($reason);
$this->assertSame($value, $result); $this->assertSame($value, $result);
} }
} }

View File

@ -5,6 +5,7 @@ namespace Amp\Test;
class StructTestFixture { class StructTestFixture {
use \Amp\Struct; use \Amp\Struct;
public $callback; public $callback;
public $_foofoofoofoofoofoofoofoobar;
} }
class StructTest extends \PHPUnit_Framework_TestCase { class StructTest extends \PHPUnit_Framework_TestCase {
@ -52,4 +53,18 @@ class StructTest extends \PHPUnit_Framework_TestCase {
$struct = new StructTestFixture; $struct = new StructTestFixture;
$test = $struct->__propertySuggestThreshold; $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;
}
} }