-
-
Notifications
You must be signed in to change notification settings - Fork 360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add toBeOneOf() expectation #1290
base: 3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
use Pest\Support\ExpectationPipeline; | ||
use Pest\Support\Reflection; | ||
use PHPUnit\Architecture\Elements\ObjectDescription; | ||
use PHPUnit\Framework\AssertionFailedError; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
use ReflectionEnum; | ||
use ReflectionMethod; | ||
|
@@ -324,6 +325,41 @@ public function when(callable|bool $condition, callable $callback): self | |
return $this; | ||
} | ||
|
||
/** | ||
* Asserts that only one of the given tests pass with the given expectation target. | ||
* | ||
* @param (\Closure(self<TValue>): (mixed|void)) ...$tests | ||
* @return self<TValue> | ||
*/ | ||
public function toBeOneOf(Closure ...$tests): self | ||
{ | ||
if ($tests === []) { | ||
return $this; | ||
} | ||
|
||
$matches = []; | ||
$exceptions = []; | ||
|
||
foreach ($tests as $key => $test) { | ||
try { | ||
$test(new Expectation($this->value)); | ||
$matches[] = $key; | ||
} catch (AssertionFailedError) { | ||
$exceptions[] = $key; | ||
} | ||
} | ||
|
||
if (count($matches) === 1) { | ||
return $this; | ||
} | ||
|
||
if (count($matches) > 1) { | ||
throw new ExpectationFailedException('Failed asserting value matches exactly one expectation (matches: '.implode(', ', $matches).').'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than outputting the keys, I'd just output a count of passes and fails for a few reasons:
You can also output the total count of assertions ran by checking the delta of |
||
} | ||
|
||
throw new ExpectationFailedException('Failed asserting value matches any expectations.'); | ||
} | ||
|
||
/** | ||
* Dynamically calls methods on the class or creates a new higher order expectation. | ||
* | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||
<?php | ||||
|
||||
use PHPUnit\Framework\ExpectationFailedException; | ||||
|
||||
expect(true)->toBeTrue()->and(false)->toBeFalse(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
test('risky with no assertions', function () { | ||||
expect(1)->toBeOneOf(); | ||||
})->throwsNoExceptions(); | ||||
|
||||
test('to be one of', function () { | ||||
expect(1)->toBeOneOf(fn ($e) => $e->toBe(1), fn ($e) => $e->toBe(2), fn ($e) => $e->toBe(3)); | ||||
}); | ||||
|
||||
test('it does not short-circuit', function () { | ||||
$executed = 0; | ||||
expect(1)->toBeOneOf(function ($e) use (&$executed) { | ||||
$executed++; | ||||
|
||||
return $e->toBe(1); | ||||
}, function ($e) use (&$executed) { | ||||
$executed++; | ||||
|
||||
return $e->toBe(2); | ||||
}, function ($e) use (&$executed) { | ||||
$executed++; | ||||
|
||||
return $e->toBe(3); | ||||
}); | ||||
|
||||
expect($executed)->toBe(3); | ||||
}); | ||||
|
||||
test('failure with multiple matches', function () { | ||||
expect(1)->toBeOneOf(fn ($e) => $e->toBe(1), fn ($e) => $e->toBe(1)); | ||||
})->throws(ExpectationFailedException::class, 'Failed asserting value matches exactly one expectation (matches: 0, 1).'); | ||||
|
||||
test('failure with no matches', function () { | ||||
expect(1)->toBeOneOf(fn ($e) => $e->toBe(2), fn ($e) => $e->toBe(2)); | ||||
})->throws(ExpectationFailedException::class, 'Failed asserting value matches any expectations.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion this should throw