Skip to content

Commit 73e2d4a

Browse files
committed
feat: add to be absolute of
1 parent 259c47b commit 73e2d4a

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ This plugin afford math related expectations.
6060
expect(6)->toBeFactorialOf(3);
6161
expect(4)->not->toBeFactorialOf(2);
6262
```
63+
64+
#### `toBeAbsoluteOf()`
65+
$$\mid -3 \mid$$
66+
```php
67+
expect(3)->toBeAbsoluteOf(-3);
68+
expect(3)->not->toBeAbsoluteOf(4);
69+
```

src/Expectation.php

+8
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,12 @@ public function toBeFactorialOf(int $number): PestExpectation
127127

128128
return expect($this->value === $factorial)->toBeTrue();
129129
}
130+
131+
/**
132+
* @return PestExpectation<TValue>
133+
*/
134+
public function toBeAbsoluteOf(int|float $number): PestExpectation
135+
{
136+
return expect($this->value === abs($number))->toBeTrue("$this->value !== abs($number)");
137+
}
130138
}

tests/toBeAbsolute.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (int|float $number): void {
6+
expect($number)->toBeAbsoluteOf($number)
7+
->and($number)->toBeAbsoluteOf(-$number);
8+
})->with([1, 3, 5, 7, 9, 4.6, 7.8]);
9+
10+
it('passes not', function (int|float $number): void {
11+
expect($number + 1)->not->toBeAbsoluteOf($number)
12+
->and($number + 1)->not->toBeAbsoluteOf(-$number);
13+
})->with([1, 3, 5, 7, 9, 4.6, 7.8]);
14+
15+
test('failures', function (): void {
16+
expect(-8)->toBeAbsoluteOf(8);
17+
})->throws(ExpectationFailedException::class);
18+
19+
test('failures not', function (): void {
20+
expect(8)->not->toBeAbsoluteOf(8);
21+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)