GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 2e42f3...4471cd )
by Baptiste
01:50
created

SetTest::testMapTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Set,
8
    Map,
9
    Str,
10
    Sequence,
11
};
12
use function Innmind\Immutable\unwrap;
13
use PHPUnit\Framework\TestCase;
14
15
class SetTest extends TestCase
16
{
17
    public function testInterface()
18
    {
19
        $s = Set::of('int');
20
21
        $this->assertInstanceOf(\Countable::class, $s);
22
        $this->assertSame('int', $s->type());
23
    }
24
25
    public function testOf()
26
    {
27
        $this->assertTrue(
28
            Set::of('int', 1, 1, 2, 3)->equals(
29
                Set::of('int')
30
                    ->add(1)
31
                    ->add(2)
32
                    ->add(3)
33
            )
34
        );
35
    }
36
37
    public function testDefer()
38
    {
39
        $loaded = false;
40
        $set = Set::defer('int', (function() use (&$loaded) {
41
            yield 1;
42
            yield 2;
43
            yield 3;
44
            $loaded = true;
45
        })());
46
47
        $this->assertInstanceOf(Set::class, $set);
48
        $this->assertFalse($loaded);
49
        $this->assertSame([1, 2, 3], unwrap($set));
50
        $this->assertTrue($loaded);
51
    }
52
53
    public function testLazy()
54
    {
55
        $loaded = false;
56
        $set = Set::lazy('int', function() use (&$loaded) {
57
            yield 1;
58
            yield 2;
59
            yield 3;
60
            $loaded = true;
61
        });
62
63
        $this->assertInstanceOf(Set::class, $set);
64
        $this->assertFalse($loaded);
65
        $this->assertSame([1, 2, 3], unwrap($set));
66
        $this->assertTrue($loaded);
67
    }
68
69
    public function testMixed()
70
    {
71
        $set = Set::mixed(1, '2', 3, 1);
72
73
        $this->assertInstanceOf(Set::class, $set);
74
        $this->assertSame('mixed', $set->type());
75
        $this->assertSame([1, '2', 3], unwrap($set));
76
    }
77
78
    public function testInts()
79
    {
80
        $set = Set::ints(1, 2, 3, 1);
81
82
        $this->assertInstanceOf(Set::class, $set);
83
        $this->assertSame('int', $set->type());
84
        $this->assertSame([1, 2, 3], unwrap($set));
85
    }
86
87
    public function testFloats()
88
    {
89
        $set = Set::floats(1, 2, 3.2, 1);
90
91
        $this->assertInstanceOf(Set::class, $set);
92
        $this->assertSame('float', $set->type());
93
        $this->assertSame([1.0, 2.0, 3.2], unwrap($set));
94
    }
95
96
    public function testStrings()
97
    {
98
        $set = Set::strings('1', '2', '3', '1');
99
100
        $this->assertInstanceOf(Set::class, $set);
101
        $this->assertSame('string', $set->type());
102
        $this->assertSame(['1', '2', '3'], unwrap($set));
103
    }
104
105
    public function testObjects()
106
    {
107
        $a = new \stdClass;
108
        $b = new \stdClass;
109
        $c = new \stdClass;
110
        $set = Set::objects($a, $b, $c, $a);
111
112
        $this->assertInstanceOf(Set::class, $set);
113
        $this->assertSame('object', $set->type());
114
        $this->assertSame([$a, $b, $c], unwrap($set));
115
    }
116
117
    public function testAdd()
118
    {
119
        $this->assertSame(0, Set::of('in')->size());
120
121
        $s = Set::of('int')->add(42);
122
123
        $this->assertSame(1, $s->size());
124
        $this->assertSame(1, $s->count());
125
        $s->add(24);
126
        $this->assertSame(1, $s->size());
127
        $s = $s->add(24);
128
        $this->assertInstanceOf(Set::class, $s);
129
        $this->assertSame(2, $s->size());
130
        $s = $s->add(24);
131
        $this->assertSame(2, $s->size());
132
        $this->assertSame([42, 24], unwrap($s));
133
134
        $this->assertSame(
135
            [1, 2, 3],
136
            unwrap(Set::ints(1)(2)(3)),
137
        );
138
    }
139
140
    public function testThrowWhenAddindInvalidElementType()
141
    {
142
        $this->expectException(\TypeError::class);
143
        $this->expectExceptionMessage('Argument 1 must be of type int, float given');
144
145
        Set::of('int')->add(42.0);
146
    }
147
148
    public function testIntersect()
149
    {
150
        $s = Set::of('int')
151
            ->add(24)
152
            ->add(42)
153
            ->add(66);
154
155
        $s2 = $s->intersect(Set::of('int')->add(42));
156
        $this->assertNotSame($s, $s2);
157
        $this->assertInstanceOf(Set::class, $s2);
158
        $this->assertSame($s->type(), $s2->type());
159
        $this->assertSame([24, 42, 66], unwrap($s));
160
        $this->assertSame([42], unwrap($s2));
161
    }
162
163
    public function testThrowWhenIntersectingSetsOfDifferentTypes()
164
    {
165
        $this->expectException(\TypeError::class);
166
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
167
168
        Set::of('int')->intersect(Set::of('float'));
169
    }
170
171
    public function testContains()
172
    {
173
        $s = Set::of('int');
174
175
        $this->assertFalse($s->contains(42));
176
        $s = $s->add(42);
177
        $this->assertTrue($s->contains(42));
178
    }
179
180
    public function testRemove()
181
    {
182
        $s = Set::of('int')
183
            ->add(24)
184
            ->add(42)
185
            ->add(66)
186
            ->add(90)
187
            ->add(114);
188
189
        $s2 = $s->remove(42);
190
        $this->assertNotSame($s, $s2);
191
        $this->assertInstanceOf(Set::class, $s2);
192
        $this->assertSame($s->type(), $s2->type());
193
        $this->assertSame([24, 42, 66, 90, 114], unwrap($s));
194
        $this->assertSame([24, 66, 90, 114], unwrap($s2));
195
        $this->assertSame([42, 66, 90, 114], unwrap($s->remove(24)));
196
        $this->assertSame([24, 42, 90, 114], unwrap($s->remove(66)));
197
        $this->assertSame([24, 42, 66, 114], unwrap($s->remove(90)));
198
        $this->assertSame([24, 42, 66, 90], unwrap($s->remove(114)));
199
    }
200
201
    public function testDiff()
202
    {
203
        $s = Set::of('int')
204
            ->add(24)
205
            ->add(42)
206
            ->add(66);
207
208
        $s2 = $s->diff(Set::of('int')->add(42));
209
        $this->assertNotSame($s, $s2);
210
        $this->assertInstanceOf(Set::class, $s2);
211
        $this->assertSame($s->type(), $s2->type());
212
        $this->assertSame([24, 42, 66], unwrap($s));
213
        $this->assertSame([24, 66], unwrap($s2));
214
    }
215
216
    public function testThrowWhenDiffingSetsOfDifferentType()
217
    {
218
        $this->expectException(\TypeError::class);
219
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
220
221
        Set::of('int')->diff(Set::of('float'));
222
    }
223
224
    public function testEquals()
225
    {
226
        $s = Set::of('int')
227
            ->add(24)
228
            ->add(42)
229
            ->add(66);
230
231
        $this->assertTrue(
232
            $s->equals(
233
                Set::of('int')
234
                    ->add(24)
235
                    ->add(66)
236
                    ->add(42)
237
            )
238
        );
239
        $this->assertTrue(Set::of('int')->equals(Set::of('int')));
240
        $this->assertFalse(
241
            $s->equals(
242
                Set::of('int')
243
                    ->add(24)
244
                    ->add(66)
245
            )
246
        );
247
    }
248
249
    public function testThrowWhenCheckingEqualityBetweenSetsOfDifferentType()
250
    {
251
        $this->expectException(\TypeError::class);
252
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
253
254
        Set::of('int')->equals(Set::of('float'));
255
    }
256
257
    public function testFilter()
258
    {
259
        $s = Set::of('int')
260
            ->add(1)
261
            ->add(2)
262
            ->add(3)
263
            ->add(4);
264
265
        $s2 = $s->filter(function(int $v) {
266
            return $v % 2 === 0;
267
        });
268
        $this->assertNotSame($s, $s2);
269
        $this->assertInstanceOf(Set::class, $s2);
270
        $this->assertSame($s->type(), $s2->type());
271
        $this->assertSame([1, 2, 3, 4], unwrap($s));
272
        $this->assertSame([2, 4], unwrap($s2));
273
    }
274
275
    public function testForeach()
276
    {
277
        $s = Set::of('int')
278
            ->add(1)
279
            ->add(2)
280
            ->add(3)
281
            ->add(4);
282
        $count = 0;
283
284
        $s->foreach(function(int $v) use (&$count) {
285
            $this->assertSame(++$count, $v);
286
        });
287
        $this->assertSame(4, $count);
288
    }
289
290
    public function testGroupBy()
291
    {
292
        $s = Set::of('int')
293
            ->add(1)
294
            ->add(2)
295
            ->add(3)
296
            ->add(4);
297
298
        $m = $s->groupBy(function(int $v) {
299
            return $v % 2;
300
        });
301
        $this->assertInstanceOf(Map::class, $m);
302
        $this->assertSame('int', $m->keyType());
303
        $this->assertSame(Set::class, $m->valueType());
304
        $this->assertSame('int', $m->get(0)->type());
305
        $this->assertSame('int', $m->get(1)->type());
306
        $this->assertSame([1, 0], unwrap($m->keys()));
307
        $this->assertSame([1, 3], unwrap($m->get(1)));
308
        $this->assertSame([2, 4], unwrap($m->get(0)));
309
    }
310
311
    public function testMap()
312
    {
313
        $s = Set::of('int')
314
            ->add(1)
315
            ->add(2)
316
            ->add(3)
317
            ->add(4);
318
319
        $s2 = $s->map(function(int $v) {
320
            return $v**2;
321
        });
322
        $this->assertNotSame($s, $s2);
323
        $this->assertInstanceOf(Set::class, $s2);
324
        $this->assertSame($s->type(), $s2->type());
325
        $this->assertSame([1, 2, 3, 4], unwrap($s));
326
        $this->assertSame([1, 4, 9, 16], unwrap($s2));
327
    }
328
329
    public function testMapTo()
330
    {
331
        $a = Set::ints(1, 2, 3, 4);
332
        $b = $a->mapTo('string', fn($i) => (string) $i);
333
334
        $this->assertInstanceOf(Set::class, $b);
335
        $this->assertNotSame($a, $b);
336
        $this->assertSame('string', $b->type());
337
        $this->assertSame(['1', '2', '3', '4'], unwrap($b));
338
    }
339
340
    public function testThrowWhenTryingToModifyValueTypeInMap()
341
    {
342
        $this->expectException(\TypeError::class);
343
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
344
345
        Set::of('int')
346
            ->add(1)
347
            ->map(function(int $value) {
348
                return (string) $value;
349
            });
350
    }
351
352
    public function testPartition()
353
    {
354
        $s = Set::of('int')
355
            ->add(1)
356
            ->add(2)
357
            ->add(3)
358
            ->add(4);
359
360
        $s2 = $s->partition(function(int $v) {
361
            return $v % 2 === 0;
362
        });
363
        $this->assertNotSame($s, $s2);
364
        $this->assertInstanceOf(Map::class, $s2);
365
        $this->assertSame('bool', $s2->keyType());
366
        $this->assertSame(Set::class, $s2->valueType());
367
        $this->assertSame([1, 2, 3, 4], unwrap($s));
368
        $this->assertInstanceOf(Set::class, $s2->get(true));
369
        $this->assertInstanceOf(Set::class, $s2->get(false));
370
        $this->assertSame($s->type(), $s2->get(true)->type());
371
        $this->assertSame($s->type(), $s2->get(false)->type());
372
        $this->assertSame([2, 4], unwrap($s2->get(true)));
373
        $this->assertSame([1, 3], unwrap($s2->get(false)));
374
    }
375
376
    public function testSort()
377
    {
378
        $s = Set::of('int')
379
            ->add(1)
380
            ->add(2)
381
            ->add(3)
382
            ->add(4);
383
384
        $s2 = $s->sort(function(int $a, int $b) {
385
            return $a < $b;
386
        });
387
        $this->assertInstanceOf(Sequence::class, $s2);
388
        $this->assertSame('int', $s2->type());
389
        $this->assertSame([1, 2, 3, 4], unwrap($s));
390
        $this->assertSame([4, 3, 2, 1], unwrap($s2));
391
    }
392
393
    public function testMerge()
394
    {
395
        $s = Set::of('int')
396
            ->add(24)
397
            ->add(42)
398
            ->add(66);
399
400
        $this->assertTrue(
401
            $s
402
                ->merge(
403
                    Set::of('int')
404
                        ->add(24)
405
                        ->add(42)
406
                        ->add(66)
407
                )
408
                ->equals($s)
409
        );
410
        $this->assertSame(
411
            [24, 42, 66, 90, 114],
412
            unwrap($s->merge(
413
                Set::of('int')
414
                    ->add(90)
415
                    ->add(114)
416
            )),
417
        );
418
        $this->assertSame([24, 42, 66], unwrap($s));
419
        $this->assertSame($s->type(), $s->merge(Set::of('int'))->type());
420
    }
421
422
    public function testThrowWhenMergingSetsOfDifferentType()
423
    {
424
        $this->expectException(\TypeError::class);
425
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
426
427
        Set::of('int')->merge(Set::of('float'));
428
    }
429
430
    public function testReduce()
431
    {
432
        $s = Set::of('int')
433
            ->add(4)
434
            ->add(3)
435
            ->add(2);
436
437
        $v = $s->reduce(
438
            42,
439
            function (float $carry, int $value): float {
440
                return $carry / $value;
441
            }
442
        );
443
444
        $this->assertSame(1.75, $v);
445
        $this->assertSame([4, 3, 2], unwrap($s));
446
    }
447
448
    public function testVariableSet()
449
    {
450
        $this->assertSame(
451
            ['foo', 42, 42.1, true, []],
452
            unwrap(Set::of('variable')
453
                ->add('foo')
454
                ->add(42)
455
                ->add(42.1)
456
                ->add(true)
457
                ->add([]))
458
        );
459
    }
460
461
    public function testEmpty()
462
    {
463
        $this->assertTrue(Set::of('int')->empty());
464
        $this->assertFalse(Set::of('int', 1)->empty());
465
    }
466
467
    public function testToSequenceOf()
468
    {
469
        $set = Set::ints(1, 2, 3);
470
        $sequence = $set->toSequenceOf('string|int', function($i) {
471
            yield (string) $i;
472
            yield $i;
473
        });
474
475
        $this->assertInstanceOf(Sequence::class, $sequence);
476
        $this->assertSame(
477
            ['1', 1, '2', 2, '3', 3],
478
            unwrap($sequence),
479
        );
480
        $this->assertSame(
481
            [1, 2, 3],
482
            unwrap($set->toSequenceOf('int')),
483
        );
484
    }
485
486
    public function testToSetOf()
487
    {
488
        $initial = Set::ints(1, 2, 3);
489
        $set = $initial->toSetOf('string|int', function($i) {
490
            yield (string) $i;
491
            yield $i;
492
        });
493
494
        $this->assertInstanceOf(Set::class, $set);
495
        $this->assertSame(
496
            ['1', 1, '2', 2, '3', 3],
497
            unwrap($set),
498
        );
499
        $this->assertSame(
500
            [1, 2, 3],
501
            unwrap($initial->toSetOf('int')),
502
        );
503
    }
504
505
    public function testToMapOf()
506
    {
507
        $set = Set::ints(1, 2, 3);
508
        $map = $set->toMapOf('string', 'int', fn($i) => yield (string) $i => $i);
509
510
        $this->assertInstanceOf(Map::class, $map);
511
        $this->assertCount(3, $map);
512
        $this->assertSame(1, $map->get('1'));
513
        $this->assertSame(2, $map->get('2'));
514
        $this->assertSame(3, $map->get('3'));
515
    }
516
}
517