Skip to content

Commit

Permalink
regenerate the Set\Call value each time a scenario is run
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Dec 2, 2023
1 parent 7b47b9e commit 95a2060
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Changed

- When disabling memory limit it now longer tries to reset the memory limit to the previous value. (It seems PHP prevents setting the limit lower to the peak memory usage)
- `Innmind\BlackBox\Set\Call` regenarate the value each time a scenario is run

### Fixed

Expand Down
14 changes: 12 additions & 2 deletions proofs/set/call.php
Expand Up @@ -22,15 +22,25 @@ static function($assert) {
},
)->tag(Tag::ci, Tag::local);
yield test(
'Set\Call regenerate the value when shrinking',
'Set\Call is not shrinkable',
static function($assert) {
$set = Set\Call::of(static fn() => new \stdClass)->values(Random::default);
$current = $set->current();

$assert->false($current->shrinkable());
},
)->tag(Tag::ci, Tag::local);

yield test(
'Set\Call regenerate the value each time it is accessed',
static function($assert) {
$set = Set\Call::of(static fn() => new \stdClass)->values(Random::default);
$current = $set->current();

$assert
->expected($current->unwrap())
->not()
->same($current->shrink()->a()->unwrap());
->same($current->unwrap());
},
)->tag(Tag::ci, Tag::local);
};
6 changes: 5 additions & 1 deletion src/Set/Call.php
Expand Up @@ -16,6 +16,10 @@ final class Call
*/
public static function of(callable $call): Set
{
return Integers::any()->map(static fn() => $call());
return FromGenerator::mutable(static function() use ($call) {
while (true) {
yield $call;
}
})->map(static fn($call) => $call());
}
}
35 changes: 29 additions & 6 deletions src/Set/FromGenerator.php
Expand Up @@ -24,6 +24,7 @@ final class FromGenerator implements Set
private \Closure $generatorFactory;
/** @var \Closure(T): bool */
private \Closure $predicate;
private bool $immutable;

/**
* @psalm-mutation-free
Expand All @@ -36,10 +37,16 @@ private function __construct(
callable $generatorFactory,
int $size,
\Closure $predicate,
bool $immutable,
) {
if (!$generatorFactory(Random::mersenneTwister) instanceof \Generator) {

Check failure on line 42 in src/Set/FromGenerator.php

View workflow job for this annotation

GitHub Actions / Psalm (8.2, highest)

ImpureFunctionCall

src/Set/FromGenerator.php:42:14: ImpureFunctionCall: Cannot call an impure function from a mutation-free context (see https://psalm.dev/202)

Check failure on line 42 in src/Set/FromGenerator.php

View workflow job for this annotation

GitHub Actions / Psalm (8.3, highest)

ImpureFunctionCall

src/Set/FromGenerator.php:42:14: ImpureFunctionCall: Cannot call an impure function from a mutation-free context (see https://psalm.dev/202)
throw new \TypeError('Argument 1 must be of type callable(): \Generator');
}

$this->generatorFactory = \Closure::fromCallable($generatorFactory);
$this->size = $size;
$this->predicate = $predicate;
$this->immutable = $immutable;
}

/**
Expand All @@ -51,11 +58,19 @@ private function __construct(
*/
public static function of(callable $generatorFactory): self
{
if (!$generatorFactory(Random::mersenneTwister) instanceof \Generator) {
throw new \TypeError('Argument 1 must be of type callable(): \Generator');
}
return new self($generatorFactory, 100, static fn(): bool => true, true);
}

return new self($generatorFactory, 100, static fn(): bool => true);
/**
* @template V
*
* @param callable(Random): \Generator<V> $generatorFactory
*
* @return self<V>
*/
public static function mutable(callable $generatorFactory): self
{
return new self($generatorFactory, 100, static fn(): bool => true, false);
}

/**
Expand All @@ -67,6 +82,7 @@ public function take(int $size): Set
$this->generatorFactory,
$size,
$this->predicate,
$this->immutable,
);
}

Expand All @@ -90,6 +106,7 @@ static function(mixed $value) use ($previous, $predicate): bool {

return $predicate($value);
},
$this->immutable,
);
}

Expand All @@ -98,7 +115,10 @@ static function(mixed $value) use ($previous, $predicate): bool {
*/
public function map(callable $map): Set
{
return Decorate::immutable($map, $this);
return match ($this->immutable) {
true => Decorate::immutable($map, $this),
false => Decorate::mutable($map, $this),
};
}

public function values(Random $random): \Generator
Expand All @@ -110,7 +130,10 @@ public function values(Random $random): \Generator
$value = $generator->current();

if (($this->predicate)($value)) {
yield Value::immutable($value);
yield match ($this->immutable) {
true => Value::immutable($value),
false => Value::mutable(static fn() => $value),
};

++$iterations;
}
Expand Down

0 comments on commit 95a2060

Please sign in to comment.