Completed
Push — master ( d5dc61...bf2878 )
by Randy
01:53
created

IteratorTest::testSeparate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use function Dgame\Iterator\chars;
5
use function Dgame\Iterator\iter;
6
use function Dgame\Iterator\lines;
7
use function Dgame\Iterator\only;
8
use function Dgame\Iterator\separate;
9
10
class IteratorTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
    public function testFilterEmpty()
13
    {
14
        $it = iter(['a', 'b', false, null, 0, 1])->filter();
15
16
        $this->assertEquals('b', $it->next());
17
        $this->assertEquals(1, $it->next());
18
    }
19
20
    public function testOnly()
21
    {
22
        $it = only('a')->repeat(4);
23
24
        $this->assertEquals('aaaa', $it->implode());
25
    }
26
27
    public function testChars()
28
    {
29
        $it = chars('Hallo');
30
31
        $this->assertEquals('H', $it->first());
32
        $this->assertEquals('a', $it->next());
33
        $this->assertEquals('l', $it->next());
34
        $this->assertEquals('l', $it->next());
35
        $this->assertEquals('o', $it->next());
36
    }
37
38
    public function testExplodeChars()
39
    {
40
        $it = separate(parent::class, '\\');
41
42
        $this->assertEquals('PHPUnit\Framework\TestCase', $it->implode('\\'));
43
        $this->assertEquals('PHPUnit', $it->first());
44
        $this->assertEquals('Framework', $it->next());
45
        $this->assertEquals('TestCase', $it->next());
46
    }
47
48
    public function testLines()
49
    {
50
        $this->assertEmpty(lines('')->collect());
51
        $this->assertEmpty(lines(PHP_EOL)->collect());
52
        $this->assertEmpty(lines(PHP_EOL . PHP_EOL)->collect());
53
        $this->assertEquals(['a'], lines('a')->collect());
54
        $this->assertEquals(['a', 'b'], lines("a\nb")->collect());
55
        $this->assertEquals(['a', 'b'], lines("a\rb")->collect());
56
        $this->assertEquals(['a', 'b'], lines("a\r\nb")->collect());
57
        $this->assertEquals(['a', 'b'], lines('a' . PHP_EOL . 'b')->collect());
58
        $this->assertEquals(['a', 'b'], lines('a' . PHP_EOL . 'b' . PHP_EOL)->collect());
59
    }
60
61
    public function testSeparate()
62
    {
63
        $this->assertEquals([''], separate('', PHP_EOL)->collect());
64
        $this->assertEquals(['', ''], separate(PHP_EOL, PHP_EOL)->collect());
65
        $this->assertEquals(['', '', ''], separate(PHP_EOL . PHP_EOL, PHP_EOL)->collect());
66
        $this->assertEquals(['a'], lines('a')->collect());
67
        $this->assertEquals(['a', 'b'], separate('a' . PHP_EOL . 'b', PHP_EOL)->collect());
68
        $this->assertEquals(['a', 'b', ''], separate('a' . PHP_EOL . 'b' . PHP_EOL, PHP_EOL)->collect());
69
    }
70
71
    public function testFirst()
72
    {
73
        $this->assertEquals('PHPUnit', separate(parent::class, '\\')->first());
74
    }
75
76
    public function testLast()
77
    {
78
        $this->assertEquals('TestCase', separate(parent::class, '\\')->last());
79
    }
80
81
    public function testPopFront()
82
    {
83
        $this->assertEquals('PHPUnit', separate(parent::class, '\\')->popFront());
84
    }
85
86
    public function testPopBack()
87
    {
88
        $this->assertEquals('TestCase', separate(parent::class, '\\')->popBack());
89
    }
90
91
    public function testCollect()
92
    {
93
        $this->assertEquals(['H', 'a', 'l', 'l', 'o'], chars('Hallo')->collect());
94
    }
95
96
    public function testImplode()
97
    {
98
        $this->assertEquals('Hallo', chars('Hallo')->implode());
99
    }
100
101
    public function testTake()
102
    {
103
        $this->assertEquals('Hal', chars('Hallo')->take(3)->implode());
104
    }
105
106
    public function testSkip()
107
    {
108
        $this->assertEquals('lo', chars('Hallo')->skip(3)->implode());
109
    }
110
111
    public function testValues()
112
    {
113
        $this->assertEquals(iter(['a' => 'z', 'b' => 'y'])->values()->collect(), ['z', 'y']);
114
    }
115
116
    public function testKeys()
117
    {
118
        $this->assertEquals(iter(['a' => 'z', 'b' => 'y'])->keys()->collect(), ['a', 'b']);
119
    }
120
121
    public function testAssoc()
122
    {
123
        $this->assertEquals(['a' => 'z', 'b' => 'y'], iter(['a' => 'z', 'b' => 'y'])->collect());
124
        $this->assertEquals(['name' => 'Foo'], iter(['name' => 'Foo', 'test' => null])->filter()->collect());
125
        $this->assertEquals(['age' => 42], iter(['name' => false, 'age' => 42])->filter()->collect());
126
    }
127
128
    public function testSlice()
129
    {
130
        $this->assertEquals('oBar', chars('FooBarQuatz')->slice(2, 6)->implode());
131
    }
132
133
    public function testChunks()
134
    {
135
        $this->assertEquals([['F', 'o'], ['o', 'B'], ['a', 'r']], chars('FooBar')->chunks(2)->collect());
136
    }
137
138
    public function testFold()
139
    {
140
        $it = iter([6, 7, 8]);
141
142
        $sum1 = function ($sum, int $a) {
143
            return $sum + $a;
144
        };
145
146
        $sum2 = function (int $sum, int $a) {
147
            return $sum + $a;
148
        };
149
150
        $this->assertEquals(21, $it->fold($sum1));
151
        $this->assertEquals(63, $it->fold($sum2, 42));
152
    }
153
154
    public function testTakeWhile()
155
    {
156
        $belowTen = function (int $item) {
157
            return $item < 10;
158
        };
159
160
        $this->assertEquals([0, 1, 2], iter([0, 1, 2, 10, 20])->takeWhile($belowTen)->collect());
161
    }
162
163
    public function testSkipWhile()
164
    {
165
        $belowTen = function (int $item) {
166
            return $item < 10;
167
        };
168
169
        $this->assertEquals([10, 20], iter([0, 1, 2, 10, 20])->skipWhile($belowTen)->collect());
170
    }
171
172
    public function testBefore()
173
    {
174
        $this->assertEquals('ab', chars('abcdef')->before('c')->implode());
175
    }
176
177
    public function testBeforeAssoc()
178
    {
179
        $this->assertEquals(['a' => 'z', 'b' => 'y'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->before('x')->collect());
180
    }
181
182
    public function testAfter()
183
    {
184
        $this->assertEquals('ef', chars('abcdef')->after('d')->implode());
185
    }
186
187
    public function testAfterAssoc()
188
    {
189
        $this->assertEquals(['d' => 'w'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->after('x')->collect());
190
    }
191
192
    public function testFrom()
193
    {
194
        $this->assertEquals('def', chars('abcdef')->from('d')->implode());
195
    }
196
197
    public function testFromAssoc()
198
    {
199
        $this->assertEquals(['c' => 'x', 'd' => 'w'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->from('x')->collect());
200
    }
201
202
    public function testUntil()
203
    {
204
        $this->assertEquals('abc', chars('abcdef')->until('c')->implode());
205
    }
206
207
    public function testUntilAssoc()
208
    {
209
        $this->assertEquals(['a' => 'z', 'b' => 'y', 'c' => 'x'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->until('x')->collect());
210
    }
211
212
    public function testAll()
213
    {
214
        $positive = function (int $item) {
215
            return $item >= 0;
216
        };
217
218
        $this->assertTrue(iter([0, 1, 2, 3])->all($positive));
219
        $this->assertFalse(iter([-1, 2, 3, 4])->all($positive));
220
    }
221
222
    public function testAny()
223
    {
224
        $positive = function (int $item) {
225
            return $item > 0;
226
        };
227
228
        $this->assertTrue(iter([-1, 0, 1])->any($positive));
229
        $this->assertFalse(iter([-1])->any($positive));
230
    }
231
232
    public function testFind()
233
    {
234
        $this->assertEquals([0], iter(['a', 'b', 'c'])->keysOf('a')->collect());
235
        $this->assertEquals([1, 2], chars('fooBar')->keysOf('o')->collect());
236
    }
237
238
    public function testKeyOf()
239
    {
240
        $it = iter(['a', 'b', 'c']);
241
242
        $this->assertEquals(1, $it->keyOf('b'));
243
        $this->assertFalse($it->keyOf('z'));
244
    }
245
246
    public function testKeyOfAssoc()
247
    {
248
        $it = iter(['a' => 'z', 'b' => 'y', 'c' => 'x']);
249
250
        $this->assertEquals('b', $it->keyOf('y'));
251
        $this->assertFalse($it->keyOf('a'));
252
    }
253
254
    public function testAmount()
255
    {
256
        $this->assertEquals(11, iter(range(0, 10))->length());
257
        $this->assertEquals(10, chars('Hallo Welt')->length());
258
    }
259
}