88class CollectorsTest {
99 private $ people ;
1010
11- /**
12- * Sets up test, initializing people member
13- */
1411 #[Before]
1512 public function setUp () {
1613 $ this ->people = [
@@ -20,20 +17,9 @@ public function setUp() {
2017 ];
2118 }
2219
23- /**
24- * Compares a hashtable against an expected map.
25- *
26- * @param [:var] $expected
27- * @param util.collections.HashTable $actual
28- * @throws unittest.AssertionFailedError
29- */
30- private function assertHashTable ($ expected , $ actual ) {
31- Assert::instance (HashTable::class, $ actual );
32- $ compare = [];
33- foreach ($ actual as $ pair ) {
34- $ compare [$ pair ->key ]= $ pair ->value ;
35- }
36- return Assert::equals ($ expected , $ compare );
20+ /** Destructure map pairs */
21+ private function map ($ pair ) {
22+ yield $ pair ->key => $ pair ->value ;
3723 }
3824
3925 /** @return var[][] */
@@ -235,48 +221,62 @@ public function joining_names_with_prefix_and_suffix() {
235221
236222 #[Test, Values(from: 'employeesDepartment ' )]
237223 public function groupingBy ($ departmentOf ) {
238- $ this ->assertHashTable (
239- ['B ' => new Vector ([$ this ->people [1549 ]]), 'I ' => new Vector ([$ this ->people [1552 ], $ this ->people [6100 ]])],
240- Sequence::of ($ this ->people )->collect (Collectors::groupingBy ($ departmentOf ))
241- );
224+ $ collector = Collectors::groupingBy ($ departmentOf );
225+ Assert::that (Sequence::of ($ this ->people )->collect ($ collector ))
226+ ->mappedBy ([$ this , 'map ' ])
227+ ->isEqualTo ([
228+ 'B ' => new Vector ([$ this ->people [1549 ]]),
229+ 'I ' => new Vector ([$ this ->people [1552 ], $ this ->people [6100 ]])
230+ ])
231+ ;
242232 }
243233
244234 #[Test]
245235 public function groupingBy_with_summing_of_years () {
246- $ this ->assertHashTable (['B ' => 15 , 'I ' => 18 ], Sequence::of ($ this ->people )
247- ->collect (Collectors::groupingBy (
248- function ($ e ) { return $ e ->department (); },
249- Collectors::summing (function ($ e ) { return $ e ->years (); })
250- ))
236+ $ collector = Collectors::groupingBy (
237+ function ($ e ) { return $ e ->department (); },
238+ Collectors::summing (function ($ e ) { return $ e ->years (); })
251239 );
240+ Assert::that (Sequence::of ($ this ->people )->collect ($ collector ))
241+ ->mappedBy ([$ this , 'map ' ])
242+ ->isEqualTo (['B ' => 15 , 'I ' => 18 ])
243+ ;
252244 }
253245
254246 #[Test]
255247 public function groupingBy_with_averaging_of_years () {
256- $ this ->assertHashTable (['B ' => 15 , 'I ' => 9 ], Sequence::of ($ this ->people )
257- ->collect (Collectors::groupingBy (
258- function ($ e ) { return $ e ->department (); },
259- Collectors::averaging (function ($ e ) { return $ e ->years (); })
260- ))
248+ $ collector = Collectors::groupingBy (
249+ function ($ e ) { return $ e ->department (); },
250+ Collectors::averaging (function ($ e ) { return $ e ->years (); })
261251 );
252+ Assert::that (Sequence::of ($ this ->people )->collect ($ collector ))
253+ ->mappedBy ([$ this , 'map ' ])
254+ ->isEqualTo (['B ' => 15 , 'I ' => 9 ])
255+ ;
262256 }
263257
264258 #[Test, Values(from: 'dinosaurEmployees ' )]
265259 public function partitioningBy ($ moreThanTen ) {
266- $ this ->assertHashTable (
267- [true => new Vector ([$ this ->people [1549 ], $ this ->people [1552 ]]), false => new Vector ([$ this ->people [6100 ]])],
268- Sequence::of ($ this ->people )->collect (Collectors::partitioningBy ($ moreThanTen ))
269- );
260+ $ collector = Collectors::partitioningBy ($ moreThanTen );
261+ Assert::that (Sequence::of ($ this ->people )->collect ($ collector ))
262+ ->mappedBy ([$ this , 'map ' ])
263+ ->isEqualTo ([
264+ true => new Vector ([$ this ->people [1549 ], $ this ->people [1552 ]]),
265+ false => new Vector ([$ this ->people [6100 ]])
266+ ])
267+ ;
270268 }
271269
272270 #[Test]
273271 public function partitioningBy_handles_non_booleans () {
274- $ this ->assertHashTable (
275- [true => new Vector (['Test ' , 'Unittest ' ]), false => new Vector (['Trial & Error ' ])],
276- Sequence::of (['Test ' , 'Unittest ' , 'Trial & Error ' ])->collect (Collectors::partitioningBy (function ($ e ) {
277- return stristr ($ e , 'Test ' );
278- }))
279- );
272+ $ collector = Collectors::partitioningBy (function ($ e ) { return stristr ($ e , 'Test ' ); });
273+ Assert::that (Sequence::of (['Test ' , 'Unittest ' , 'Trial & Error ' ])->collect ($ collector ))
274+ ->mappedBy ([$ this , 'map ' ])
275+ ->isEqualTo ([
276+ true => new Vector (['Test ' , 'Unittest ' ]),
277+ false => new Vector (['Trial & Error ' ])
278+ ])
279+ ;
280280 }
281281
282282 #[Test]
0 commit comments