Skip to content

Commit

Permalink
More stream → iterator
Browse files Browse the repository at this point in the history
Hopefully that's all of them…
  • Loading branch information
trowski committed May 1, 2017
1 parent f914333 commit da749b2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/Internal/Producer.php
Expand Up @@ -9,8 +9,8 @@
use React\Promise\PromiseInterface as ReactPromise;

/**
* Trait used by Stream implementations. Do not use this trait in your code, instead compose your class from one of
* the available classes implementing \Amp\Stream.
* Trait used by Iterator implementations. Do not use this trait in your code, instead compose your class from one of
* the available classes implementing \Amp\Iterator.
* Note that it is the responsibility of the user of this trait to ensure that listeners have a chance to listen first
* before emitting values.
*
Expand Down
14 changes: 7 additions & 7 deletions lib/Iterator.php
Expand Up @@ -7,23 +7,23 @@
*/
interface Iterator {
/**
* Succeeds with true if an emitted value is available by calling getCurrent() or false if the stream has resolved.
* If the stream fails, the returned promise will fail with the same exception.
* Succeeds with true if an emitted value is available by calling getCurrent() or false if the iterator has
* resolved. If the iterator fails, the returned promise will fail with the same exception.
*
* @return \Amp\Promise<bool>
*
* @throws \Error If the prior promise returned from this method has not resolved.
* @throws \Throwable The exception used to fail the stream.
* @throws \Throwable The exception used to fail the iterator.
*/
public function advance(): Promise;

/**
* Gets the last emitted value or throws an exception if the stream has completed.
* Gets the last emitted value or throws an exception if the iterator has completed.
*
* @return mixed Value emitted from the stream.
* @return mixed Value emitted from the iterator.
*
* @throws \Error If the stream has resolved or advance() was not called before calling this method.
* @throws \Throwable The exception used to fail the stream.
* @throws \Error If the iterator has resolved or advance() was not called before calling this method.
* @throws \Throwable The exception used to fail the iterator.
*/
public function getCurrent();
}
28 changes: 14 additions & 14 deletions test/ConcatTest.php
Expand Up @@ -19,27 +19,27 @@ public function getArrays() {
/**
* @dataProvider getArrays
*
* @param array $streams
* @param array $iterators
* @param array $expected
*/
public function testConcat(array $streams, array $expected) {
Loop::run(function () use ($streams, $expected) {
$streams = \array_map(function (array $stream): Iterator {
return Iterator\fromIterable($stream);
}, $streams);
public function testConcat(array $iterators, array $expected) {
Loop::run(function () use ($iterators, $expected) {
$iterators = \array_map(function (array $iterator): Iterator {
return Iterator\fromIterable($iterator);
}, $iterators);

$stream = Iterator\concat($streams);
$iterator = Iterator\concat($iterators);

while (yield $stream->advance()) {
$this->assertSame(\array_shift($expected), $stream->getCurrent());
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
}
});
}

/**
* @depends testConcat
*/
public function testConcatWithFailedStream() {
public function testConcatWithFailedIterator() {
Loop::run(function () {
$exception = new TestException;
$expected = \range(1, 6);
Expand All @@ -48,11 +48,11 @@ public function testConcatWithFailedStream() {
throw $exception;
});

$stream = Iterator\concat([Iterator\fromIterable(\range(1, 5)), $producer, Iterator\fromIterable(\range(7, 10))]);
$iterator = Iterator\concat([Iterator\fromIterable(\range(1, 5)), $producer, Iterator\fromIterable(\range(7, 10))]);

try {
while (yield $stream->advance()) {
$this->assertSame(\array_shift($expected), $stream->getCurrent());
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
}
$this->fail("The exception used to fail the iterator should be thrown from advance()");
} catch (TestException $reason) {
Expand All @@ -66,7 +66,7 @@ public function testConcatWithFailedStream() {
/**
* @expectedException \TypeError
*/
public function testNonStream() {
public function testNonIterator() {
Iterator\concat([1]);
}
}
2 changes: 1 addition & 1 deletion test/FilterTest.php
Expand Up @@ -75,7 +75,7 @@ public function testCallbackThrows() {
});
}

public function testStreamFails() {
public function testIteratorFails() {
Loop::run(function () {
$invoked = false;
$exception = new TestException;
Expand Down
4 changes: 2 additions & 2 deletions test/IteratorFromIterableTest.php
Expand Up @@ -87,13 +87,13 @@ public function testTraversable() {

/**
* @expectedException \TypeError
* @dataProvider provideInvalidStreamArguments
* @dataProvider provideInvalidIteratorArguments
*/
public function testInvalid($arg) {
Iterator\fromIterable($arg);
}

public function provideInvalidStreamArguments() {
public function provideInvalidIteratorArguments() {
return [
[null],
[new \stdClass],
Expand Down
2 changes: 1 addition & 1 deletion test/MapTest.php
Expand Up @@ -76,7 +76,7 @@ public function testOnNextCallbackThrows() {
});
}

public function testStreamFails() {
public function testIteratorFails() {
Loop::run(function () {
$invoked = false;
$exception = new TestException;
Expand Down
4 changes: 2 additions & 2 deletions test/MergeTest.php
Expand Up @@ -70,7 +70,7 @@ public function testMergeWithDelayedEmits() {
/**
* @depends testMerge
*/
public function testMergeWithFailedStream() {
public function testMergeWithFailedIterator() {
Loop::run(function () {
$exception = new TestException;
$producer = new Producer(function (callable $emit) use ($exception) {
Expand All @@ -92,7 +92,7 @@ public function testMergeWithFailedStream() {
/**
* @expectedException \TypeError
*/
public function testNonStream() {
public function testNonIterator() {
Iterator\merge([1]);
}
}

0 comments on commit da749b2

Please sign in to comment.