mirror of
https://github.com/danog/amp.git
synced 2024-11-26 20:15:00 +01:00
Improve tests and kill some Humbug mutants
This commit is contained in:
parent
dfa3b82485
commit
3b46c168a5
@ -2,6 +2,9 @@
|
||||
"source": {
|
||||
"directories": [
|
||||
"lib"
|
||||
],
|
||||
"excludes": [
|
||||
"lib/functions.php"
|
||||
]
|
||||
},
|
||||
"timeout": 10,
|
||||
|
@ -200,7 +200,6 @@ class Listener {
|
||||
|
||||
$values = $this->values;
|
||||
$this->values = [];
|
||||
$this->position = -1;
|
||||
|
||||
$deferreds = $this->backPressure;
|
||||
$this->backPressure = [];
|
||||
|
@ -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] === "_") {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user