Skip to content

Commit a74d6d3

Browse files
authored
Merge pull request #23 from Katalam/absolute
add an absolute expectation
2 parents 6bf35f3 + 5fdb921 commit a74d6d3

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
22
.phpunit.cache
3+
.idea

Diff for: 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(-3);
69+
```

Diff for: 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 doesn't equal abs($number)");
137+
}
130138
}

Diff for: 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)->not->toBeAbsoluteOf($number)
12+
->and(-$number)->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, "-8 doesn't equal abs(8)");
18+
19+
test('failures not', function (): void {
20+
expect(8)->not->toBeAbsoluteOf(-8);
21+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)