1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable\Map; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Map\ObjectKeys, |
8
|
|
|
Map\DoubleIndex, |
9
|
|
|
Map\Implementation, |
10
|
|
|
Map, |
11
|
|
|
Pair, |
12
|
|
|
Str, |
13
|
|
|
Set, |
14
|
|
|
Sequence, |
15
|
|
|
}; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
class ObjectKeysTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testInterface() |
21
|
|
|
{ |
22
|
|
|
$m = new ObjectKeys; |
23
|
|
|
|
24
|
|
|
$this->assertInstanceOf(Map\Implementation::class, $m); |
25
|
|
|
$this->assertInstanceOf(\Countable::class, $m); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testPut() |
29
|
|
|
{ |
30
|
|
|
$m = new ObjectKeys; |
31
|
|
|
|
32
|
|
|
$this->assertSame(0, $m->size()); |
33
|
|
|
$m2 = ($m)(new \stdClass, 42); |
34
|
|
|
$this->assertNotSame($m, $m2); |
35
|
|
|
$this->assertSame(0, $m->size()); |
36
|
|
|
$this->assertSame(1, $m2->size()); |
37
|
|
|
|
38
|
|
|
$m = new ObjectKeys; |
39
|
|
|
$m = $m |
40
|
|
|
($a = new \stdClass, 24) |
41
|
|
|
($b = new \stdClass, 42) |
42
|
|
|
($c = new \stdClass, 66) |
43
|
|
|
($d = new \stdClass, 90) |
44
|
|
|
($c, 1); |
45
|
|
|
|
46
|
|
|
$this->assertSame(24, $this->get($m, $a)); |
47
|
|
|
$this->assertSame(42, $this->get($m, $b)); |
48
|
|
|
$this->assertSame(1, $this->get($m, $c)); |
49
|
|
|
$this->assertSame(90, $this->get($m, $d)); |
50
|
|
|
$this->assertSame(4, $m->size()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGet() |
54
|
|
|
{ |
55
|
|
|
$m = new ObjectKeys; |
56
|
|
|
$m = ($m)($a = new \stdClass, 24); |
57
|
|
|
|
58
|
|
|
$this->assertSame(24, $this->get($m, $a)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testReturnNothingWhenGettingUnknownKey() |
62
|
|
|
{ |
63
|
|
|
$this->assertNull($this->get(new ObjectKeys, new \stdClass)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testContains() |
67
|
|
|
{ |
68
|
|
|
$m = new ObjectKeys; |
69
|
|
|
$m = ($m)($a = new \stdClass, 24); |
70
|
|
|
|
71
|
|
|
$this->assertFalse($m->contains(24)); |
72
|
|
|
$this->assertTrue($m->contains($a)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testClear() |
76
|
|
|
{ |
77
|
|
|
$m = new ObjectKeys; |
78
|
|
|
$m = ($m)(new \stdClass, 42.0); |
79
|
|
|
|
80
|
|
|
$m2 = $m->clear(); |
81
|
|
|
$this->assertNotSame($m, $m2); |
82
|
|
|
$this->assertInstanceOf(ObjectKeys::class, $m2); |
83
|
|
|
$this->assertSame(1, $m->size()); |
84
|
|
|
$this->assertSame(0, $m2->size()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testEquals() |
88
|
|
|
{ |
89
|
|
|
$m = (new ObjectKeys)($a = new \stdClass, 42); |
90
|
|
|
$m2 = (new ObjectKeys)($a, 42); |
91
|
|
|
|
92
|
|
|
$this->assertTrue($m->equals($m2)); |
93
|
|
|
$this->assertFalse($m->equals(($m2)(new \stdClass, 66))); |
94
|
|
|
$this->assertFalse($m->equals(($m2)($a, 24))); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testFilter() |
98
|
|
|
{ |
99
|
|
|
$m = (new ObjectKeys) |
100
|
|
|
($a = new \stdClass, 1) |
101
|
|
|
($b = new \stdClass, 2) |
102
|
|
|
($c = new \stdClass, 3) |
103
|
|
|
($d = new \stdClass, 4); |
104
|
|
|
|
105
|
|
|
$m2 = $m->filter(static function(\stdClass $key, int $value) { |
106
|
|
|
return $value % 2 === 0; |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
$this->assertNotSame($m, $m2); |
110
|
|
|
$this->assertInstanceOf(ObjectKeys::class, $m2); |
111
|
|
|
$this->assertSame(4, $m->size()); |
112
|
|
|
$this->assertSame(2, $m2->size()); |
113
|
|
|
$this->assertTrue($m2->contains($b)); |
114
|
|
|
$this->assertTrue($m2->contains($d)); |
115
|
|
|
$this->assertFalse($m2->contains($a)); |
116
|
|
|
$this->assertFalse($m2->contains($c)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testForeach() |
120
|
|
|
{ |
121
|
|
|
$m = (new ObjectKeys) |
122
|
|
|
(new \stdClass, 1) |
123
|
|
|
(new \stdClass, 2) |
124
|
|
|
(new \stdClass, 3) |
125
|
|
|
(new \stdClass, 4); |
126
|
|
|
$count = 0; |
127
|
|
|
|
128
|
|
|
$m->foreach(function(\stdClass $key, int $value) use (&$count) { |
129
|
|
|
++$count; |
130
|
|
|
$this->assertSame($count, $value); |
131
|
|
|
}); |
132
|
|
|
$this->assertSame(4, $count); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testGroupEmptyMap() |
136
|
|
|
{ |
137
|
|
|
$this->assertTrue( |
138
|
|
|
(new ObjectKeys) |
139
|
|
|
->groupBy(static function() {}) |
140
|
|
|
->equals(Map::of()), |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testGroupBy() |
145
|
|
|
{ |
146
|
|
|
$m = (new ObjectKeys) |
147
|
|
|
($a = new \stdClass, 1) |
148
|
|
|
($b = new \stdClass, 2) |
149
|
|
|
($c = new \stdClass, 3) |
150
|
|
|
($d = new \stdClass, 4); |
151
|
|
|
|
152
|
|
|
$m2 = $m->groupBy(static function(\stdClass $key, int $value) { |
153
|
|
|
return $value % 2; |
154
|
|
|
}); |
155
|
|
|
$this->assertNotSame($m, $m2); |
156
|
|
|
$this->assertInstanceOf(Map::class, $m2); |
157
|
|
|
$this->assertTrue($m2->contains(0)); |
158
|
|
|
$this->assertTrue($m2->contains(1)); |
159
|
|
|
$this->assertSame(2, $this->get($m2, 0)->size()); |
160
|
|
|
$this->assertSame(2, $this->get($m2, 1)->size()); |
161
|
|
|
$this->assertSame(1, $this->get($this->get($m2, 1), $a)); |
162
|
|
|
$this->assertSame(2, $this->get($this->get($m2, 0), $b)); |
163
|
|
|
$this->assertSame(3, $this->get($this->get($m2, 1), $c)); |
164
|
|
|
$this->assertSame(4, $this->get($this->get($m2, 0), $d)); |
165
|
|
|
} |
166
|
|
|
public function testKeys() |
167
|
|
|
{ |
168
|
|
|
$m = (new ObjectKeys) |
169
|
|
|
($a = new \stdClass, 1) |
170
|
|
|
($b = new \stdClass, 2) |
171
|
|
|
($c = new \stdClass, 3) |
172
|
|
|
($d = new \stdClass, 5); |
173
|
|
|
|
174
|
|
|
$k = $m->keys(); |
175
|
|
|
$this->assertInstanceOf(Set::class, $k); |
176
|
|
|
$this->assertSame([$a, $b, $c, $d], $k->toList()); |
177
|
|
|
$this->assertTrue($k->equals($m->keys())); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testValues() |
181
|
|
|
{ |
182
|
|
|
$m = (new ObjectKeys) |
183
|
|
|
(new \stdClass, 1) |
184
|
|
|
(new \stdClass, 2) |
185
|
|
|
(new \stdClass, 3) |
186
|
|
|
(new \stdClass, 5) |
187
|
|
|
(new \stdClass, 5); |
188
|
|
|
|
189
|
|
|
$v = $m->values(); |
190
|
|
|
$this->assertInstanceOf(Sequence::class, $v); |
191
|
|
|
$this->assertSame([1, 2, 3, 5, 5], $v->toList()); |
192
|
|
|
$this->assertTrue($v->equals($m->values())); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testMap() |
196
|
|
|
{ |
197
|
|
|
$m = (new ObjectKeys) |
198
|
|
|
($a = new \stdClass, 1) |
199
|
|
|
($b = new \stdClass, 2) |
200
|
|
|
($c = new \stdClass, 3) |
201
|
|
|
($d = new \stdClass, 4); |
202
|
|
|
|
203
|
|
|
$m2 = $m->map(static function(\stdClass $key, int $value) { |
204
|
|
|
return $value**2; |
205
|
|
|
}); |
206
|
|
|
$this->assertNotSame($m, $m2); |
207
|
|
|
$this->assertInstanceOf(ObjectKeys::class, $m2); |
208
|
|
|
$this->assertSame([$a, $b, $c, $d], $m->keys()->toList()); |
209
|
|
|
$this->assertSame([1, 2, 3, 4], $m->values()->toList()); |
210
|
|
|
$this->assertSame([$a, $b, $c, $d], $m2->keys()->toList()); |
211
|
|
|
$this->assertSame([1, 4, 9, 16], $m2->values()->toList()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testRemove() |
215
|
|
|
{ |
216
|
|
|
$m = (new ObjectKeys) |
217
|
|
|
($a = new \stdClass, 1) |
218
|
|
|
($b = new \stdClass, 2) |
219
|
|
|
($c = new \stdClass, 3) |
220
|
|
|
($d = new \stdClass, 4) |
221
|
|
|
($e = new \stdClass, 5); |
222
|
|
|
|
223
|
|
|
$m2 = $m->remove(new \stdClass); |
224
|
|
|
$this->assertSame($m, $m2); |
225
|
|
|
$this->assertSame([$a, $b, $c, $d, $e], $m->keys()->toList()); |
226
|
|
|
$this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
227
|
|
|
|
228
|
|
|
$m2 = $m->remove($d); |
229
|
|
|
$this->assertNotSame($m, $m2); |
230
|
|
|
$this->assertInstanceOf(ObjectKeys::class, $m2); |
231
|
|
|
$this->assertSame([$a, $b, $c, $d, $e], $m->keys()->toList()); |
232
|
|
|
$this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
233
|
|
|
$this->assertSame([$a, $b, $c, $e], $m2->keys()->toList()); |
234
|
|
|
$this->assertSame([1, 2, 3, 5], $m2->values()->toList()); |
235
|
|
|
|
236
|
|
|
$m2 = $m->remove($e); |
237
|
|
|
$this->assertNotSame($m, $m2); |
238
|
|
|
$this->assertInstanceOf(ObjectKeys::class, $m2); |
239
|
|
|
$this->assertSame([$a, $b, $c, $d, $e], $m->keys()->toList()); |
240
|
|
|
$this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
241
|
|
|
$this->assertSame([$a, $b, $c, $d], $m2->keys()->toList()); |
242
|
|
|
$this->assertSame([1, 2, 3, 4], $m2->values()->toList()); |
243
|
|
|
|
244
|
|
|
$m2 = $m->remove($a); |
245
|
|
|
$this->assertNotSame($m, $m2); |
246
|
|
|
$this->assertInstanceOf(ObjectKeys::class, $m2); |
247
|
|
|
$this->assertSame([$a, $b, $c, $d, $e], $m->keys()->toList()); |
248
|
|
|
$this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
249
|
|
|
$this->assertSame([$b, $c, $d, $e], $m2->keys()->toList()); |
250
|
|
|
$this->assertSame([2, 3, 4, 5], $m2->values()->toList()); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testMerge() |
254
|
|
|
{ |
255
|
|
|
$m = (new ObjectKeys) |
256
|
|
|
($s = new \stdClass, 24) |
257
|
|
|
($s2 = new \stdClass, 42); |
258
|
|
|
$m2 = (new ObjectKeys) |
259
|
|
|
($s3 = new \stdClass, 24) |
260
|
|
|
($s2, 66) |
261
|
|
|
($s4 = new \stdClass, 42); |
262
|
|
|
|
263
|
|
|
$m3 = $m->merge($m2); |
264
|
|
|
$this->assertNotSame($m, $m3); |
265
|
|
|
$this->assertNotSame($m2, $m3); |
266
|
|
|
$this->assertInstanceOf(ObjectKeys::class, $m3); |
267
|
|
|
$this->assertSame(4, $m3->size()); |
268
|
|
|
$this->assertSame([$s, $s2], $m->keys()->toList()); |
269
|
|
|
$this->assertSame([24, 42], $m->values()->toList()); |
270
|
|
|
$this->assertSame([$s3, $s2, $s4], $m2->keys()->toList()); |
271
|
|
|
$this->assertSame([24, 66, 42], $m2->values()->toList()); |
272
|
|
|
$this->assertSame([$s, $s2, $s3, $s4], $m3->keys()->toList()); |
273
|
|
|
$this->assertSame([24, 66, 24, 42], $m3->values()->toList()); |
274
|
|
|
$this->assertFalse($m3->equals($m2->merge($m))); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testPartition() |
278
|
|
|
{ |
279
|
|
|
$m = (new ObjectKeys) |
280
|
|
|
($a = new \stdClass, 1) |
281
|
|
|
($b = new \stdClass, 2) |
282
|
|
|
($c = new \stdClass, 3) |
283
|
|
|
($d = new \stdClass, 4) |
284
|
|
|
($e = new \stdClass, 5); |
285
|
|
|
|
286
|
|
|
$p = $m->partition(static function(\stdClass $i, int $v) { |
287
|
|
|
return $v % 2 === 0; |
288
|
|
|
}); |
289
|
|
|
|
290
|
|
|
$this->assertInstanceOf(Map::class, $p); |
291
|
|
|
$this->assertNotSame($p, $m); |
292
|
|
|
$this->assertSame( |
293
|
|
|
[true, false], |
294
|
|
|
$p->keys()->toList(), |
295
|
|
|
); |
296
|
|
|
$this->assertSame( |
297
|
|
|
[$b, $d], |
298
|
|
|
$this->get($p, true)->keys()->toList(), |
299
|
|
|
); |
300
|
|
|
$this->assertSame( |
301
|
|
|
[2, 4], |
302
|
|
|
$this->get($p, true)->values()->toList(), |
303
|
|
|
); |
304
|
|
|
$this->assertSame( |
305
|
|
|
[$a, $c, $e], |
306
|
|
|
$this->get($p, false)->keys()->toList(), |
307
|
|
|
); |
308
|
|
|
$this->assertSame( |
309
|
|
|
[1, 3, 5], |
310
|
|
|
$this->get($p, false)->values()->toList(), |
311
|
|
|
); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function testReduce() |
315
|
|
|
{ |
316
|
|
|
$m = (new ObjectKeys)($a = new \stdClass, 4); |
317
|
|
|
|
318
|
|
|
$v = $m->reduce( |
319
|
|
|
42, |
320
|
|
|
static function(float $carry, \stdClass $key, int $value): float { |
321
|
|
|
return $carry / $value; |
322
|
|
|
}, |
323
|
|
|
); |
324
|
|
|
|
325
|
|
|
$this->assertSame(10.5, $v); |
326
|
|
|
$this->assertSame([$a], $m->keys()->toList()); |
327
|
|
|
$this->assertSame([4], $m->values()->toList()); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function testEmpty() |
331
|
|
|
{ |
332
|
|
|
$this->assertTrue((new ObjectKeys)->empty()); |
333
|
|
|
$this->assertFalse((new ObjectKeys)(new \stdClass, 1)->empty()); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function testSwitchImplementationWhenAddingNonKeyObject() |
337
|
|
|
{ |
338
|
|
|
$map = (new ObjectKeys)(1, 2); |
339
|
|
|
|
340
|
|
|
$this->assertInstanceOf(DoubleIndex::class, $map); |
341
|
|
|
$this->assertCount(1, $map); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
public function testFind() |
345
|
|
|
{ |
346
|
|
|
$a = new \stdClass; |
347
|
|
|
$b = new \stdClass; |
348
|
|
|
$c = new \stdClass; |
349
|
|
|
$map = (new ObjectKeys) |
350
|
|
|
($a, 2) |
351
|
|
|
($b, 4) |
352
|
|
|
($c, 6); |
353
|
|
|
|
354
|
|
|
$this->assertSame( |
355
|
|
|
4, |
356
|
|
|
$map |
357
|
|
|
->find(static fn($k) => $k === $b) |
358
|
|
|
->map(static fn($pair) => $pair->value()) |
359
|
|
|
->match( |
360
|
|
|
static fn($value) => $value, |
361
|
|
|
static fn() => null, |
362
|
|
|
), |
363
|
|
|
); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
private function get($map, $index) |
367
|
|
|
{ |
368
|
|
|
return $map->get($index)->match( |
369
|
|
|
static fn($value) => $value, |
370
|
|
|
static fn() => null, |
371
|
|
|
); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|