-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement fold
operator
#27
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72cb8ee
chore: replace all `scoped` into `supervised` in code samples
geminicaprograms 416ad0d
chore: use `e.toThrowable` to re-throw from failed source
geminicaprograms 19c6cd1
fix: don't catch exception when `headOption` is called
geminicaprograms e13bcc2
fix: class references in scaladoc
geminicaprograms 4c456aa
fix: scaladoc description
geminicaprograms 775f8db
feat: implement `fold` operator
geminicaprograms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsFoldTest extends AnyFlatSpec with Matchers { | ||
behavior of "Source.fold" | ||
|
||
it should "throw ChannelClosedException.Error with exception and message that was thrown during retrieval" in supervised { | ||
the[ChannelClosedException.Error] thrownBy { | ||
Source | ||
.failed[Int](new RuntimeException("source is broken")) | ||
.fold(0)((acc, n) => acc + n) | ||
} should have message "java.lang.RuntimeException: source is broken" | ||
} | ||
|
||
it should "throw ChannelClosedException.Error for source failed without exception" in supervised { | ||
the[ChannelClosedException.Error] thrownBy { | ||
Source | ||
.failedWithoutReason[Int]() | ||
.fold(0)((acc, n) => acc + n) | ||
} | ||
} | ||
|
||
it should "throw exception thrown in `f` when `f` throws" in supervised { | ||
the[RuntimeException] thrownBy { | ||
Source | ||
.fromValues(1) | ||
.fold(0)((_, _) => throw new RuntimeException("Function `f` is broken")) | ||
} should have message "Function `f` is broken" | ||
} | ||
|
||
it should "return `zero` value from fold on the empty source" in supervised { | ||
Source.empty[Int].fold(0)((acc, n) => acc + n) shouldBe 0 | ||
} | ||
|
||
it should "return fold on non-empty source" in supervised { | ||
Source.fromValues(1, 2).fold(0)((acc, n) => acc + n) shouldBe 3 | ||
} | ||
|
||
it should "drain the source" in supervised { | ||
val s = Source.fromValues(1) | ||
s.fold(0)((acc, n) => acc + n) shouldBe 1 | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: Can we have another test case for a scenario where the function passed to
fold
throws an exception?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done