-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: (43 commits) specify next release add groups support disable time assertions and composite shrinking proofs in the CI add Tag::ci and ::local Revert "include the last values that result in failure" include the last values that result in failure add memory limit for the coverage as well fix test add memory limit to verify blackbox can disable it discard psalm error allow to display the generated scenario inside the phpunit emulation fix messages not being displayed do not print vendor stack trace on macOS CI add Set\MutuallyExclusive increase allowed delta as usleep is not very precise add Set\Slice to the changelog reduce the number of scenarri for the time assertions add Set\Slice add Assert::memory() add Assert::time() ...
- Loading branch information
Showing
48 changed files
with
2,465 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Assert memory usage | ||
|
||
You can use BlackBox to make sure use less than a specified amount of memory to make sure your code doesn't have a memory leak. You can do so with the `Assert::memory()` method. | ||
|
||
In order for this assertion to work properly you need to: | ||
- add `declare(ticks = 1);` at the top of file where you call the assertion | ||
- the callable cannot use the anonymous function short notation | ||
|
||
For example: | ||
|
||
```php | ||
<?php | ||
declare(strict_types = 1); | ||
declare(ticks = 1); | ||
|
||
return static function() { | ||
yield test( | ||
'Your test name', | ||
static function($assert) { | ||
$assert | ||
->memory(static function() { | ||
$yourSystem = new YourSystem(); | ||
$yourSystem->doStuff(); | ||
}) | ||
->inLessThan() | ||
->megaBytes(1); | ||
}, | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Adding assertions | ||
|
||
BlackBox comes with a fixed set of assertions provided by the `Assert` class. But sometime you may want to reuse an assertion (or set of assertions) thoughout your proofs. You can do so with the `Assert::matches()` method. | ||
|
||
Let's say you want to validate a Uuid, you could create a static method on a class like this: | ||
|
||
```php | ||
use Innmind\BlackBox\Runner\Assert; | ||
|
||
final class Uuid | ||
{ | ||
/** | ||
* @return callable(Assert): void | ||
*/ | ||
public static function of(mixed $value): callable | ||
{ | ||
return static fn(Assert $assert) => $assert | ||
->string($value) | ||
->matches('~^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$~'); | ||
} | ||
} | ||
``` | ||
|
||
And in your proof you would use it like this: | ||
|
||
```php | ||
use Innmind\BlackBox\{ | ||
Set, | ||
Runner\Assert, | ||
}; | ||
|
||
yield proof( | ||
'Name of your proof', | ||
given(Set\Type::any()), | ||
static fn(Assert $assert, $value) => $assert->matches(Uuid::of($value)), | ||
); | ||
``` | ||
|
||
**Note**: this example proof will fail because generated values are not uuids. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Tests\Innmind\BlackBox\PHPUnit; | ||
|
||
use Innmind\BlackBox\{ | ||
PHPUnit\BlackBox, | ||
Set, | ||
}; | ||
use PHPUnit\{ | ||
Framework\TestCase, | ||
Framework\Attributes\DataProvider, | ||
}; | ||
|
||
class BlackBoxTest extends TestCase | ||
{ | ||
use BlackBox; | ||
|
||
public function testTrait() | ||
{ | ||
$class = new class() { | ||
use BlackBox; | ||
|
||
public function assert(): int | ||
{ | ||
$called = 0; | ||
$this | ||
->forAll(Set\Integers::any()) | ||
->then(static function() use (&$called) { | ||
++$called; | ||
}); | ||
|
||
return $called; | ||
} | ||
}; | ||
|
||
// 200 because it reads the `BLACKBOX_SET_SIZE` env var | ||
$this->assertSame(200, $class->assert()); | ||
} | ||
|
||
public function testDoesntFailWhenTheExceptionIsExpected() | ||
{ | ||
$this | ||
->forAll(Set\Strings::any(), Set\Integers::above(0)) | ||
->then(function($message, $code) { | ||
$exception = new class($message, $code) extends \Exception { | ||
}; | ||
|
||
$this->expectException(\get_class($exception)); | ||
$this->expectExceptionMessage($message); | ||
$this->expectExceptionCode($code); | ||
|
||
throw $exception; | ||
}); | ||
} | ||
|
||
#[DataProvider('ints')] | ||
public function testDataProviderCompatibility($a, $b) | ||
{ | ||
$this->assertIsInt($a); | ||
$this->assertIsInt($b); | ||
} | ||
|
||
public static function ints(): iterable | ||
{ | ||
return self::forAll( | ||
Set\Integers::any(), | ||
Set\Integers::any(), | ||
)->asDataProvider(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.