forked from xp-framework/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequenceInputStreamTest.class.php
More file actions
executable file
·113 lines (96 loc) · 3.08 KB
/
SequenceInputStreamTest.class.php
File metadata and controls
executable file
·113 lines (96 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php namespace io\unittest;
use ArrayIterator;
use io\streams\{InputStream, MemoryInputStream, SequenceInputStream};
use lang\IllegalArgumentException;
use test\{Assert, Expect, Test};
class SequenceInputStreamTest {
/** Drains a stream */
private function drain(InputStream $stream): array {
$r= [];
while ($available= $stream->available()) {
$r[]= [$available, $stream->read()];
}
$r[]= [$stream->available(), $stream->read()];
return $r;
}
/** Creates a memory input stream with a `closed` property */
private function closeable(string $input): MemoryInputStream {
return new class($input) extends MemoryInputStream {
public $closed= false;
public function close() { $this->closed= true; }
};
}
#[Test, Expect(IllegalArgumentException::class)]
public function without_arguments() {
new SequenceInputStream();
}
#[Test, Expect(IllegalArgumentException::class)]
public function with_empty_array() {
new SequenceInputStream([]);
}
#[Test]
public function drain_one() {
$fixture= new SequenceInputStream(new MemoryInputStream('Test'));
Assert::equals([[4, 'Test'], [0, '']], $this->drain($fixture));
}
#[Test]
public function drain_multiple() {
$fixture= new SequenceInputStream(
new MemoryInputStream('One'),
new MemoryInputStream('Two')
);
Assert::equals([[3, 'One'], [3, 'Two'], [0, '']], $this->drain($fixture));
}
#[Test]
public function drain_array() {
$fixture= new SequenceInputStream([
new MemoryInputStream('One'),
new MemoryInputStream('Two')
]);
Assert::equals([[3, 'One'], [3, 'Two'], [0, '']], $this->drain($fixture));
}
#[Test]
public function drain_iterator() {
$fixture= new SequenceInputStream(new ArrayIterator([
yield new MemoryInputStream('One'),
yield new MemoryInputStream('Two')
]));
Assert::equals([[3, 'One'], [3, 'Two'], [0, '']], $this->drain($fixture));
}
#[Test]
public function drain_generator() {
$streams= function() {
yield new MemoryInputStream('One');
yield new MemoryInputStream('Two');
};
$fixture= new SequenceInputStream($streams());
Assert::equals([[3, 'One'], [3, 'Two'], [0, '']], $this->drain($fixture));
}
#[Test]
public function using_only_read() {
$fixture= new SequenceInputStream(
new MemoryInputStream('One'),
new MemoryInputStream('Two')
);
Assert::equals('One', $fixture->read());
Assert::equals('Two', $fixture->read());
Assert::equals('', $fixture->read());
}
#[Test]
public function close_closes_all_streams() {
$one= $this->closeable('One');
$two= $this->closeable('Two');
$fixture= new SequenceInputStream($one, $two);
$fixture->close();
Assert::equals([true, true], [$one->closed, $two->closed]);
}
#[Test]
public function streams_closed_when_drained() {
$one= $this->closeable('One');
$two= $this->closeable('Two');
$fixture= new SequenceInputStream($one, $two);
$this->drain($fixture);
$fixture->close();
Assert::equals([true, true], [$one->closed, $two->closed]);
}
}