Skip to content

Commit

Permalink
Merge pull request #24 from faissaloux/tobemaxof
Browse files Browse the repository at this point in the history
`toBeMaxOf()`
  • Loading branch information
faissaloux authored Jul 30, 2024
2 parents a74d6d3 + 69381f1 commit 6d150d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ This plugin afford math related expectations.
expect(128)->not->toBePowerOf(3);
```

#### `toBeMaxOf()`
```php
expect(6)->toBeMaxOf([-6, 0, 6]);
expect(5.5)->not->toBeMaxOf([2, 4.2, 5.5, 6]);
```

#### `toBeEven()`
```php
expect(6)->toBeEven();
Expand Down
9 changes: 9 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,13 @@ public function toBeAbsoluteOf(int|float $number): PestExpectation
{
return expect($this->value === abs($number))->toBeTrue("$this->value doesn't equal abs($number)");
}

/**
* @param array<int|float> $stack
* @return PestExpectation<TValue>
*/
public function toBeMaxOf(array $stack): PestExpectation
{
return expect($this->value === max($stack))->toBeTrue();
}
}
27 changes: 27 additions & 0 deletions tests/toBeMaxOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

it('passes', function (int|float $value, array $stack): void {
expect($value)->toBeMaxOf($stack);
})->with([
[1, [-6, 0, 1]],
[6, [-6, 0, 6]],
[5.5, [2, 4.2, 5.5]],
]);

it('passes not', function (int|float $value, array $stack): void {
expect($value)->not->toBeMaxOf($stack);
})->with([
[4, [2, 4, 6]],
[6, [2, 4]],
[5.5, [2, 4.2, 5.5, 6]],
]);

test('failures', function (): void {
expect(4)->toBeMaxOf([2, 4, 6]);
})->throws(ExpectationFailedException::class);

test('failures not', function (): void {
expect(6)->not->toBeMaxOf([2, 4, 6]);
})->throws(ExpectationFailedException::class);

0 comments on commit 6d150d6

Please sign in to comment.