Skip to content

Commit 19dada1

Browse files
authored
Improve documentation (#125)
* Test documentation has sections for all public methods * Improve documentation
1 parent 44ebe11 commit 19dada1

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ All entry points always return an instance of the pipeline.
113113
| `prepend()` | Appends the contents of an interable to the end of the pipeline. | `array_merge` |
114114
| `unshift()` | Prepends the pipeline with a list of values. | `array_unshift` |
115115
| `zip()` | Takes a number of iterables, transposing them together with the current sequence, if any. | `array_map(null, ...$array)`, Python's `zip()`, transposition |
116+
| `reservoir()` | Reservoir sampling with an optional weighting function. | |
116117
| `flatten()` | Flattens inputs: `[[1, 2], [3, 4]]` becomes `[1, 2, 3, 4]`. | `flat_map`, `flatten`, `collect_concat` |
117118
| `unpack()` | Unpacks arrays into arguments for a callback. Flattens inputs if no callback provided. | |
118119
| `chunk()` | Chunks the pipeline into arrays of specified length. | `array_chunk` |
119120
| `filter()` | Removes elements unless a callback returns true. Removes falsey values if no callback provided. | `array_filter`, `Where` |
120121
| `slice()` | Extracts a slice from the inputs. Keys are not discarded intentionally. Suppors negative values for both arguments. | `array_slice` |
121-
| `reduce()` | Reduces input values to a single value. Defaults to summation. Requires an initial value. | `array_reduce`, `Aggregate`, `Sum` |
122+
| `fold()` | Reduces input values to a single value. Defaults to summation. Requires an initial value. | `array_reduce`, `Aggregate`, `Sum` |
123+
| `reduce()` | Alias to `fold()` with a reversed order of arguments. | `array_reduce` |
124+
| `flip()` | Swaps keys and values. | `array_flip` |
125+
| `max()` | Finds the highest value. | `max` |
126+
| `min()` | Finds the lowest value. | `min` |
122127
| `toArray()` | Returns an array with all values. Eagerly executed. | `dict`, `ToDictionary` |
123128
| `__construct()` | Can be provided with an optional initial iterator. Used in the `take()` function from above. | |
124129

tests/DocumentationTest.php

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright 2017, 2018 Alexey Kopytko <[email protected]>
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace Tests\Pipeline;
21+
22+
use PHPUnit\Framework\ExpectationFailedException;
23+
use PHPUnit\Framework\TestCase;
24+
use Pipeline\Standard;
25+
use ReflectionClass;
26+
use ReflectionMethod;
27+
use function file_get_contents;
28+
use function implode;
29+
use function Pipeline\take;
30+
use function preg_match_all;
31+
use function sprintf;
32+
use function strpos;
33+
use function substr;
34+
35+
/**
36+
* Test documentation has sections for all public methods.
37+
*
38+
* @coversNothing
39+
*
40+
* @internal
41+
*/
42+
final class DocumentationTest extends TestCase
43+
{
44+
private static string $readme;
45+
private static string $headers;
46+
47+
public static function setUpBeforeClass(): void
48+
{
49+
self::$readme = file_get_contents(__DIR__.'/../README.md');
50+
51+
preg_match_all('/^#.*/m', self::$readme, $matches);
52+
self::$headers = implode("\n", $matches[0]);
53+
}
54+
55+
/**
56+
* @param array<ReflectionClass> $interfaces
57+
*/
58+
private static function interfaceFilter(array $interfaces, ReflectionMethod $method): bool
59+
{
60+
foreach ($interfaces as $interface) {
61+
if ($interface->hasMethod($method->getName())) {
62+
return false;
63+
}
64+
}
65+
66+
return true;
67+
}
68+
69+
public static function provideMethods(): iterable
70+
{
71+
$reflection = new ReflectionClass(new Standard());
72+
$interfaces = $reflection->getInterfaces();
73+
74+
return take($reflection->getMethods(ReflectionMethod::IS_PUBLIC))
75+
->filter(fn (ReflectionMethod $method) => self::interfaceFilter($interfaces, $method))
76+
->cast(fn (ReflectionMethod $method) => [$method->getName()]);
77+
}
78+
79+
/**
80+
* @dataProvider provideMethods
81+
*/
82+
public function testMethodHasMention(string $methodName): void
83+
{
84+
try {
85+
$this->assertMatchesRegularExpression(
86+
sprintf('/`%s\(\)`/', $methodName),
87+
self::$readme,
88+
"There's no mention of {$methodName}."
89+
);
90+
} catch (ExpectationFailedException $e) {
91+
$message = $e->getMessage();
92+
$this->fail(substr($message, 0, strpos($message, "\n")));
93+
}
94+
}
95+
96+
/**
97+
* @dataProvider provideMethods
98+
*/
99+
public function testMethodHasHeader(string $methodName): void
100+
{
101+
try {
102+
$this->assertMatchesRegularExpression(
103+
sprintf('/^##.*(->|`)%s\(\)`/m', $methodName),
104+
self::$headers,
105+
"There's no header dedicated to {$methodName}."
106+
);
107+
} catch (ExpectationFailedException $e) {
108+
$message = $e->getMessage();
109+
$this->markTestIncomplete(substr($message, 0, strpos($message, "\n")));
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)