Skip to content

Commit 6cbbb8b

Browse files
committed
fix: rename sum of to summation of
1 parent 9b7f923 commit 6cbbb8b

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ This plugin affords math related expectations.
6161
expect(4)->not->toBeFactorialOf(2);
6262
```
6363

64-
#### `toBeSumOf()`
64+
#### `toBeSummationOf()`
6565
$$\sum\limits_n^k x * 2$$
6666
```php
67-
expect(2)->toBeSumOf(n: 0, k: 1, static fn (int $x) => $x * 2);
68-
expect(3)->not->toBeSumOf(n: 0, k: 1, static fn (int $x) => $x * 2);
67+
expect(2)->toBeSumOf(static fn (int $x) => $x * 2, from: 0, to: 1);
68+
expect(3)->not->toBeSumOf(static fn (int $x) => $x * 2, from: 0, to: 1);
6969
```
7070

7171
#### `toBeProdOf()`

src/Expectation.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,25 @@ public function toBeFactorialOf(int $number): PestExpectation
131131
/**
132132
* @return PestExpectation<TValue>
133133
*/
134-
public function toBeSumOf(int $n, int $k, callable $step): PestExpectation
134+
public function toBeSumOf(array $numbers): PestExpectation
135135
{
136136
$sum = 0;
137+
foreach ($numbers as $number) {
138+
expect($number)->toBeNumeric();
139+
$sum += $number;
140+
}
137141

138-
foreach (range($n, $k) as $i) {
142+
return expect($this->value === $sum)->toBeTrue("$this->value !== $sum");
143+
}
144+
145+
/**
146+
* @return PestExpectation<TValue>
147+
*/
148+
public function toBeSummationOf(callable $step, int $from, int $to): PestExpectation
149+
{
150+
$sum = 0;
151+
152+
foreach (range($from, $to) as $i) {
139153
$stepSum = $step($i);
140154
expect($stepSum)->toBeNumeric();
141155
$sum += $stepSum;

tests/toBeSummationOf.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (int $sum, int $from, int $to, callable $step): void {
6+
expect($sum)->toBeSummationOf($step, $from, $to);
7+
})->with([
8+
[110, 0, 10, static fn (int $i): int => $i * 2],
9+
[418, 2, 20, static fn (int $i): int => $i * 2],
10+
[1628, 4, 40, static fn (int $i): int => $i * 2],
11+
[3630, 6, 60, static fn (int $i): int => $i * 2],
12+
[6424, 8, 80, static fn (int $i): int => $i * 2],
13+
]);
14+
15+
it('passes not', function (callable $step, int $from, int $to): void {
16+
expect(1)->not->toBeSummationOf($step, $from, $to);
17+
})->with([
18+
[static fn (int $i): int => $i * 2, 0, 10],
19+
[static fn (int $i): int => $i * 2, 2, 20],
20+
[static fn (int $i): int => $i * 2, 4, 40],
21+
[static fn (int $i): int => $i * 2, 6, 60],
22+
[static fn (int $i): int => $i * 2, 8, 80],
23+
]);
24+
25+
test('failures', function (): void {
26+
expect(1)->toBeSummationOf(static fn (int $i): int => $i * 2, 0, 1);
27+
})->throws(ExpectationFailedException::class);
28+
29+
test('failures not', function (): void {
30+
expect(2)->not->toBeSummationOf(static fn (int $i): int => $i * 2, 0 ,1);
31+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)