Skip to content
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

Add Examples / Unit Tests for Exceptions #775

Merged
merged 18 commits into from
Jan 15, 2025
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions examples/stdlib/exception/combinators.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ module examples/pos/exception/combinators
import exception
import result

/**
* Used as a type for `Exception` purely for independent testing
*/

/// Used as a type for `Exception` purely for independent testing
record TestException()

/**
* Returns the string if index > 0; otherwise raises a TestException.
*/

/// Returns the string if index > 0; otherwise raises a TestException.
def generalOperation(str: String, index: Int): String / Exception[TestException] = {
if (index <= 0)
do raise(TestException(), "Error: Invalid index (" ++ show(index) ++ ")")
Expand All @@ -19,7 +17,7 @@ def generalOperation(str: String, index: Int): String / Exception[TestException]
}

def main() = {
val str : String = "hello"
val str: String = "hello"

// Test for default handling of TestException
def defaultTestException { p: => String / Exception[TestException] }: Unit = {
Expand Down Expand Up @@ -60,9 +58,9 @@ def main() = {
finalizeTestException { str.generalOperation(0) } // Test: Finalizer
finalizeTestException { str.generalOperation(1) } // Test: Finalizer hello

// Test for "reifying" an Exception using Result
// Test for "reifying" an Exception usix^xng Result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Test for "reifying" an Exception usix^xng Result
// Test for "reifying" an Exception using Result

typo :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking over it!:)

def resultTestException { p: => String / Exception[TestException] }: Unit = {
val res= result[String, TestException](on[TestException]) { p() }
val res= result[String, TestException] { p() }
res match {
case Success(msg) => println("Success: " ++ msg)
case Error(exc, msg) => println(msg)
Expand Down
Loading