From eb8b568fc9e9ad44d234df6617f5c3525800fe7c Mon Sep 17 00:00:00 2001 From: inoas Date: Thu, 16 Jan 2025 16:21:15 +0100 Subject: [PATCH] wip --- CHANGELOG.md | 31 ++-- src/given.gleam | 417 +++++++++++++++++++++++++++++++++++------- test/given_test.gleam | 249 ++++++++++++++++--------- 3 files changed, 534 insertions(+), 163 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68da443..b1e05f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,21 +21,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), use qualified `given.error(in:...` or unqualified import `given_error_in` instead. - Added: - - `given.where` - - `given.empty` - - `given.not_empty` - - `given.all` - - `given.any` - - `given.not_all` - - `given.not_any` - - `given.all_ok` - - `given.any_ok` - - `given.all_error` - - `given.any_error` - - `given.all_some` - - `given.any_some` - - `given.all_none` - - `given.any_none` + - `given.all` to check if all elements in a list are true. + - `given.any` to check if any elements in a list are true. + - `given.not_all` to check if all elements in a list are false. + - `given.not_any` to check if any elements in a list are false. + - `given.when` to allow for more complex lazy conditions. + - `given.when_not` to allow for more complex lazy conditions. + - `given.empty` to check if a list is empty. + - `given.not_empty` to check if a list is not empty. + - `given.all_ok` to check if all results are ok. + - `given.any_ok` to check if any results are ok. + - `given.all_error` to check if all results are errors. + - `given.any_error` to check if any results are errors. + - `given.all_some` to check if all options are some. + - `given.any_some` to check if any options are some. + - `given.all_none` to check if all options are none. + - `given.any_none` to check if any options are none. ## [4.1.1] - 2025-01-01 diff --git a/src/given.gleam b/src/given.gleam index 1d76e1e..7040921 100644 --- a/src/given.gleam +++ b/src/given.gleam @@ -1,6 +1,6 @@ //// This library attempts to make guards: //// -//// - Applicable to `Bool`, `Result` and `Option` types. +//// - Applicable to `Bool`, `Result`, `Option` and `List` types. //// - Ergonomic to use by providing ways to handle both branches early. //// - Expressive by making it easy to read through function names and labels. //// - Comprehendable by not having to negate the conditions. @@ -13,15 +13,15 @@ import gleam/list import gleam/option.{type Option, None, Some} import gleam/result +/// Checks if the condition is true and runs the consequence if it is, else +/// runs the alternative. +/// /// ## Examples /// /// ```gleam /// import given -/// import gleam/int -/// let user_understood = case int.random(1) { -/// 1 -> True -/// _ -> False -/// } +/// +/// let user_understood = True /// /// use <- given.that(user_understood, return: fn() { "Great!" }) /// // …else handle case where user did not understand here… @@ -30,11 +30,8 @@ import gleam/result /// /// ```gleam /// import given.{that as given} -/// import gleam/int -/// let user_understood = case int.random(1) { -/// 1 -> True -/// _ -> False -/// } +/// +/// let user_understood = True /// /// use <- given(user_understood, return: fn() { "Great!" }) /// // …else handle case where user did not understand here… @@ -52,20 +49,37 @@ pub fn that( } } +/// Checks if any of the conditions are true and runs the consequence if any +/// are, else runs the alternative. +/// /// ## Examples /// /// ```gleam /// import given +/// +/// let is_admin = False +/// let is_editor = True +/// +/// use <- given.any([is_admin, is_editor], return: fn() { "Great!" }) +/// +/// // …else handle case where user has no special role… +/// "Woof!" +/// ``` +/// +/// ```gleam +/// import given +/// /// let is_admin = False /// let is_editor = True /// -/// use <- given.any(true_of: [is_admin, is_editor], return: fn() { "Great!" }) +/// use <- given.any(are_true_in: [is_admin, is_editor], return: fn() { "Great!" }) +/// /// // …else handle case where user has no special role… /// "Woof!" /// ``` /// pub fn any( - true_of requirements: List(Bool), + are_true_in requirements: List(Bool), return consequence: fn() -> b, else_return alternative: fn() -> b, ) -> b { @@ -75,20 +89,36 @@ pub fn any( } } +/// Checks if all of the conditions are true and runs the consequence if all +/// are, else runs the alternative. +/// /// ## Examples /// /// ```gleam /// import given +/// /// let is_active = True /// let is_confirmed = True /// /// use <- given.all([is_active, is_confirmed], return: fn() { "Great!" }) +/// /// // …else handle case where user is not both active and confirmed… /// "Woof!" /// ``` /// +/// ```gleam +/// import given +/// +/// let is_active = True +/// let is_confirmed = True +/// +/// use <- given.all(are_true_in: [is_active, is_confirmed], return: fn() { "Great!" }) +/// +/// // …else handle case where user is not both active and confirmed… +/// "Woof!" +/// ``` pub fn all( - true_of requirements: List(Bool), + are_true_in requirements: List(Bool), return consequence: fn() -> b, else_return alternative: fn() -> b, ) -> b { @@ -98,33 +128,44 @@ pub fn all( } } +/// Checks if the condition is false and runs the consequence if it is, else +/// runs the alternative. +/// /// ## Examples /// /// ```gleam /// import given -/// import gleam/int -/// let user_understood = case int.random(1) { -/// 1 -> True -/// _ -> False -/// } +/// +/// let user_understood = True /// /// use <- given.not(user_understood, return: fn() { "Woof!" }) +/// +/// // …else handle case where user understood here… +/// "Great!" +/// ``` +/// +/// ```gleam +/// import given +/// +/// let user_understood = True +/// +/// use <- given.not(the_case: user_understood, return: fn() { "Woof!" }) +/// /// // …else handle case where user understood here… /// "Great!" /// ``` /// /// ```gleam /// import given.{not as not_given} -/// import gleam/int -/// let user_understood = case int.random(1) { -/// 1 -> True -/// _ -> False -/// } +/// +/// let user_understood = True /// /// use <- not_given(user_understood, return: fn() { "Woof!" }) +/// /// // …else handle case where user understood here… /// "Great!" /// ``` +/// pub fn not( the_case requirement: Bool, return consequence: fn() -> b, @@ -136,30 +177,37 @@ pub fn not( } } +/// Checks if any of the conditions are false and runs the consequence if any +/// are, else runs the alternative. +/// /// ## Examples /// /// ```gleam /// import given +/// /// let is_admin = False /// let is_editor = True /// /// use <- given.not_any([is_admin, is_editor], return: fn() { "At least either Admin or Editor!" }) +/// /// // …else handle case where user no special role… /// "Woof!" /// ``` /// /// ```gleam /// import given +/// /// let is_admin = False /// let is_editor = True /// -/// use <- given.not_any(true_of: [is_admin, is_editor], return: fn() { "At least either Admin or Editor!" }) +/// use <- given.not_any(are_true_in: [is_admin, is_editor], return: fn() { "At least either Admin or Editor!" }) +/// /// // …else handle case where user no special role… /// "Woof!" /// ``` /// pub fn not_any( - true_of requirements: List(Bool), + are_true_in requirements: List(Bool), return consequence: fn() -> b, else_return alternative: fn() -> b, ) -> b { @@ -169,29 +217,36 @@ pub fn not_any( } } +/// Checks if all of the conditions are false and runs the consequence if all +/// are, else runs the alternative. +/// /// ## Examples /// /// ```gleam -/// import given.{given_all} +/// import given +/// /// let is_active = True /// let is_confirmed = True /// /// use <- given.not_all([is_active, is_confirmed], return: fn() { "Cylone Sleeper Agent!" }) +/// /// // …else handle case where user is neither active nor confirmed… /// "Woof!" /// ``` /// /// ```gleam -/// import given.{given_all} +/// import given +/// /// let is_active = True /// let is_confirmed = True /// -/// use <- given.not_all(true_of: [is_active, is_confirmed], return: fn() { "Cylone Sleeper Agent!" }) +/// use <- given.not_all(are_true_in: [is_active, is_confirmed], return: fn() { "Cylone Sleeper Agent!" }) +/// /// // …else handle case where user is neither active nor confirmed… /// "Woof!" /// pub fn not_all( - true_of requirements: List(Bool), + are_true_in requirements: List(Bool), return consequence: fn() -> b, else_return alternative: fn() -> b, ) -> b { @@ -201,10 +256,37 @@ pub fn not_all( } } -// TODO: Examples -// -pub fn where( - is condition: fn() -> Bool, +/// Checks if the condition function returns `True` and runs the consequence if +/// it is, else runs the alternative. +/// +/// Use to lazily evaluate a complex condition and return early if they fail. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let enabled = fn() { False } +/// +/// use <- given.when(enabled, else_return: fn() { "Not an Admin" }) +/// +/// // …handle case where user is an Admin… +/// "Indeed an Admin" +/// ``` +/// +/// ```gleam +/// import given +/// +/// let enabled = fn() { False } +/// +/// use <- given.when(enabled, return: fn() { "Indeed an Admin" }) +/// +/// // …handle case where user is not an Admin… +/// "Not an Admin" +/// ``` +/// +pub fn when( + the_case condition: fn() -> Bool, else_return alternative: fn() -> b, return consequence: fn() -> b, ) -> b { @@ -214,12 +296,66 @@ pub fn where( } } -// TODO: Examples -// +/// Checks if the condition function returns `False` and runs the consequence if +/// it is, else runs the alternative. +/// +/// Use to lazily evaluate a complex condition and return early if they fail. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let enabled = fn() { False } +/// +/// use <- given.when_not(enabled, else_return: fn() { "Indeed an Admin" }) +/// +/// // …handle case where user is not an Admin… +/// "Not an Admin" +/// ``` +/// +/// ```gleam +/// import given +/// +/// let enabled = fn() { False } +/// +/// use <- given.when_not(enabled, return: fn() { "Not an Admin" }) +/// +/// // …handle case where user is an Admin… +/// "Indeed an Admin" +/// ``` +/// +pub fn when_not( + the_case condition: fn() -> Bool, + else_return alternative: fn() -> b, + return consequence: fn() -> b, +) -> b { + case condition() { + False -> consequence() + True -> alternative() + } +} + +/// Checks if the list is empty and runs the consequence if it is, else runs +/// the alternative. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let list = [] +/// +/// use <- given.empty(list, else_return: fn() { "Not empty" }) +/// +/// // …handle empty list here… +/// "Empty" +/// ``` +/// pub fn empty( list list: List(a), - return consequence: fn() -> b, else_return alternative: fn() -> b, + return consequence: fn() -> b, ) -> b { case list { [] -> consequence() @@ -227,12 +363,26 @@ pub fn empty( } } -// TODO: Examples -// +/// Checks if the list is not empty and runs the consequence if it is, else +/// runs the alternative. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let list = [] +/// +/// use <- given.not_empty(list, else_return: fn() { "Empty" }) +/// +/// // …handle non-empty list here… +/// "Not empty" +/// ``` +/// pub fn not_empty( list list: List(a), - return consequence: fn() -> b, else_return alternative: fn() -> b, + return consequence: fn() -> b, ) -> b { case list { [] -> alternative() @@ -240,25 +390,33 @@ pub fn not_empty( } } +/// Checks if the result is an `Ok` and runs the consequence if it is, else +/// runs the alternative. +/// /// ## Examples /// /// ```gleam /// import given +/// /// let result = Ok("Great") /// /// use ok_value <- given.ok(in: result, else_return: fn(error_value) { "Error" }) +/// /// // …handle Ok value here… /// "Ok" /// ``` /// /// ```gleam /// import given.{ok as given_ok_in} +/// /// let result = Ok("Great") /// /// use ok_value <- given_ok_in(result, else_return: fn(error_value) { "Error" }) +/// /// // …handle Ok value here… /// "Ok" /// ``` +/// pub fn ok( in rslt: Result(a, e), else_return alternative: fn(e) -> b, @@ -270,10 +428,25 @@ pub fn ok( } } -// TODO: Examples -// +/// Checks if all of the results are `Ok` and runs the consequence - passing in +/// the `Ok` values - if they are, else runs the alternative passing in all +/// `Ok` and `Error` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let results = [Ok("Great"), Error("Bad")] +/// +/// use oks <- given.all_ok(in: results, else_return: fn(_oks, _errors) { "Some Errors" }) +/// +/// // …handle all OKs here… +/// "All OKs" +/// ``` +/// pub fn all_ok( - of rslts: List(Result(a, e)), + in rslts: List(Result(a, e)), else_return alternative: fn(List(a), List(e)) -> b, return consequence: fn(List(a)) -> b, ) -> b { @@ -285,10 +458,25 @@ pub fn all_ok( } } -// TODO: Examples -// +/// Checks if any of the results are `Ok` and runs the consequence - passing in +/// the `Ok` and `Error` values - if they are, else runs the alternative passing +/// in all `Error` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let results = [Ok("Great"), Error("Bad")] +/// +/// use <- given.any_ok(in: results, else_return: fn(errors) { "All Errors" }) +/// +/// // …handle at least some OKs here… +/// "At least some OKs" +/// ``` +/// pub fn any_ok( - of rslts: List(Result(a, e)), + in rslts: List(Result(a, e)), else_return alternative: fn(List(e)) -> b, return consequence: fn(List(a), List(e)) -> b, ) -> b { @@ -300,22 +488,29 @@ pub fn any_ok( } } +/// Checks if the result is an `Error` and runs the consequence if it is, else +/// runs the alternative. +/// /// ## Examples /// /// ```gleam /// import given +/// /// let result = Error(Nil) /// /// use error_value <- given.error(in: result, else_return: fn(ok_value) { "Ok" }) +/// /// // …handle Error value here… /// "Error" /// ``` /// /// ```gleam /// import given.{error as given_error_in} +/// /// let result = Error(Nil) /// /// use error_value <- given_error_in(result, else_return: fn(ok_value) { "Ok" }) +/// /// // …handle Error value here… /// "Error" /// ``` @@ -331,10 +526,25 @@ pub fn error( } } -// TODO: Examples -// +/// Checks if all of the results are `Error` and runs the consequence - passing +/// in the `Error` values - if they are, else runs the alternative passing in +/// all `Ok` and `Error` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let results = [Ok("Great"), Error("Bad")] +/// +/// use errors <- given.all_error(in: results, else_return: fn(_oks, _errors) { "Only some Errors" }) +/// +/// // …handle all errors here… +/// "All Errors" +/// ``` +/// pub fn all_error( - of rslts: List(Result(a, e)), + in rslts: List(Result(a, e)), else_return alternative: fn(List(a), List(e)) -> b, return consequence: fn(List(e)) -> b, ) -> b { @@ -346,10 +556,22 @@ pub fn all_error( } } -// TODO: Examples -// +/// Checks if any of the results are `Error` and runs the consequence - passing +/// in the `Ok` and `Error` values - if they are, else runs the alternative +/// passing in all `Ok` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let results = [Ok("Great"), Error("Bad")] +/// +/// use oks, errors <- given.any_error(in: results, else_return: fn(_oks) { "Only OKs" }) +/// ``` +/// pub fn any_error( - of rslts: List(Result(a, e)), + in rslts: List(Result(a, e)), else_return alternative: fn(List(a), List(e)) -> b, return consequence: fn(List(e)) -> b, ) -> b { @@ -366,9 +588,11 @@ pub fn any_error( /// ```gleam /// import given /// import gleam/option.{Some} +/// /// let option = Some("One") /// /// use some_value <- given.some(in: option, else_return: fn() { "None" }) +/// /// // …handle Some value here… /// "Some value" /// ``` @@ -376,9 +600,11 @@ pub fn any_error( /// ```gleam /// import given.{some as given_some_in} /// import gleam/option.{Some} +/// /// let option = Some("One") /// /// use some_value <- given_some_in(option, else_return: fn() { "None" }) +/// /// // …handle Some value here… /// "Some value" /// ``` @@ -394,10 +620,25 @@ pub fn some( } } -// TODO: Examples -// +/// Checks if all of the options are `Some` and runs the consequence - passing +/// in the `Some` values - if they are, else runs the alternative passing in +/// the `Some` and a count of the `None` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let options = [Some("One"), None] +/// +/// use <- given.all_some(in: options, else_return: fn(_somes, _nones_count) { "Some are None" }) +/// +/// // …handle all Some values here… +/// "All are Some" +/// ``` +/// pub fn all_some( - of optns: List(Option(a)), + in optns: List(Option(a)), else_return alternative: fn(List(a), Int) -> b, return consequence: fn(List(a)) -> b, ) -> b { @@ -409,10 +650,25 @@ pub fn all_some( } } -// TODO: Examples -// +/// Checks if any of the options are `Some` and runs the consequence - passing +/// in the `Some` values and a count of the `None` values - if they are, else +/// runs the alternative passing in the count of `None` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let options = [Some("One"), None] +/// +/// use <- given.any_some(in: options, else_return: fn(_somes, _nones_count) { "All are None" }) +/// +/// // …handle at least some None values here… +/// "At least some are None" +/// ``` +/// pub fn any_some( - of optns: List(Option(a)), + in optns: List(Option(a)), else_return alternative: fn(Int) -> b, return consequence: fn(List(a), Int) -> b, ) -> b { @@ -429,9 +685,11 @@ pub fn any_some( /// ```gleam /// import given /// import gleam/option.{None} +/// /// let option = None /// /// use <- given.none(in: option, else_return: fn(some_value) { "Some value" }) +/// /// // …handle None here… /// "None" /// ``` @@ -439,10 +697,12 @@ pub fn any_some( /// ```gleam /// import given.{none as given_none_in} /// import gleam/option.{None} +/// /// let option = None /// /// use <- given_none_in(option, else_return: fn(some_value) { "Some value" }) /// // …handle None here… +/// /// "None" /// ``` /// @@ -457,10 +717,24 @@ pub fn none( } } -// TODO: Examples -// +/// Checks if all of the options are `None` and runs the consequence if they +/// are, else runs the alternative passing in the `Some` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let options = [Some("One"), None] +/// +/// use <- given.all_none(in: options, else_return: fn(_somes, _nones_count) { "Some are Some" }) +/// +/// // …handle all None values here… +/// "All are None" +/// ``` +/// pub fn all_none( - of optns: List(Option(a)), + in optns: List(Option(a)), else_return alternative: fn(List(a), Int) -> b, return consequence: fn() -> b, ) -> b { @@ -472,10 +746,25 @@ pub fn all_none( } } -// TODO: Examples -// +/// Checks if any of the options are `None` and runs the consequence if they +/// are, else runs the alternative passing in the `Some` values and the count +/// of `None` values. +/// +/// ## Examples +/// +/// ```gleam +/// import given +/// +/// let options = [Some("One"), None] +/// +/// use <- given.any_none(in: options, else_return: fn(_somes) { "All are Some" }) +/// +/// // …handle at least some None values here… +/// "At least some are None" +/// ``` +/// pub fn any_none( - of optns: List(Option(a)), + in optns: List(Option(a)), else_return alternative: fn(List(a)) -> b, return consequence: fn(List(a), Int) -> b, ) -> b { diff --git a/test/given_test.gleam b/test/given_test.gleam index ee32268..24e6b8f 100644 --- a/test/given_test.gleam +++ b/test/given_test.gleam @@ -7,162 +7,243 @@ pub fn main() { gleeunit.main() } -const ok_great = "Great! ✨" +const great = "Great! ✨" -const error_woof = "Woof! 🐶" +const woof = "Woof! 🐶" -pub fn given_test() { +pub fn that_test() { { let user_understood = False - use <- given.that(user_understood, return: fn() { ok_great }) + + use <- given.that(user_understood, return: fn() { great }) + // …else user handles case where user did not understand here… - error_woof + woof } - |> should.equal(error_woof) + |> should.equal(woof) { let user_understood = True - use <- given.that(user_understood, return: fn() { ok_great }) + + use <- given.that(user_understood, return: fn() { great }) + // …else user handles case where user did not understand here… - error_woof + woof + } + |> should.equal(great) +} + +pub fn any_test() { + { + let is_admin = False + let is_editor = True + + use <- given.any([is_admin, is_editor], return: fn() { great }) + + // …else handle case where user has no special role… + woof + } + |> should.equal(great) +} + +pub fn all_test() { + { + let is_active = True + let is_confirmed = True + + use <- given.all([is_active, is_confirmed], return: fn() { great }) + + // …else handle case where user is not both active and confirmed… + woof } - |> should.equal(ok_great) + |> should.equal(great) } -pub fn not_given_test() { +pub fn not_test() { { let user_understood = False - use <- given.not(user_understood, return: fn() { ok_great }) + + use <- given.not(user_understood, return: fn() { great }) + // …else user handles case where user understood here… - error_woof + woof } - |> should.equal(ok_great) + |> should.equal(great) { let user_understood = True - use <- given.not(user_understood, return: fn() { ok_great }) + + use <- given.not(user_understood, return: fn() { great }) + // …else user handles case where user understood here… - error_woof + woof + } + |> should.equal(woof) +} + +pub fn not_any_test() { + { + let is_admin = False + let is_editor = True + + use <- given.not_any([is_admin, is_editor], return: fn() { + "At least either Admin or Editor!" + }) + + // …else handle case where user no special role… + "User has no special role!" + } + |> should.equal("User has no special role!") +} + +pub fn not_all_test() { + { + let is_human = False + let is_robot = False + + use <- given.not_all([is_human, is_robot], return: fn() { + "Obsolete model detected." + }) + + // …else handle case where user is neither active nor confirmed… + "I am a Cylon!" } - |> should.equal(error_woof) + |> should.equal("Obsolete model detected.") } -pub fn given_ok_test() { +pub fn when_test() { + { + let enabled = fn() { False } + + use <- given.when(enabled, else_return: fn() { "Not an Admin" }) + + // …handle case where user is an Admin… + "Indeed an Admin" + } + |> should.equal("Not an Admin") + { - let result = Ok(ok_great) + let enabled = fn() { False } + + use <- given.when(enabled, return: fn() { "Indeed an Admin" }) + + // …handle case where user is not an Admin… + "Not an Admin" + } + |> should.equal("Not an Admin") +} + +pub fn when_not_test() { + { + let enabled = fn() { False } + + use <- given.when_not(enabled, else_return: fn() { "Indeed an Admin" }) + + // …handle case where user is an Admin… + "Not an Admin" + } + |> should.equal("Not an Admin") + // { + // let enabled = fn() { False } + + // use <- given.when_not(enabled, return: fn() { "Not an Admin" }) + + // // …handle case where user is not an Admin… + // "Indeed an Admin" + // } + // |> should.equal("Not an Admin") +} + +pub fn ok_test() { + { + let result = Ok(great) use ok_value <- given.ok(in: result, else_return: fn(error_value) { error_value }) + // …user handles Ok value here… ok_value } - |> should.equal(ok_great) + |> should.equal(great) { - let result = Error(error_woof) + let result = Error(woof) use ok_value <- given.ok(in: result, else_return: fn(error_value) { error_value }) + // …user handles Ok value here… ok_value } - |> should.equal(error_woof) + |> should.equal(woof) } -pub fn given_ok_unusual_usage_test() { +pub fn error_test() { { - let result = Ok(ok_great) - use _error_value <- given.ok(in: result, return: fn(_ok_value) { ok_great }) - // …user handles Error value here… - error_woof - } - |> should.equal(ok_great) + let result = Error(woof) - { - let result = Error(error_woof) - use _error_value <- given.ok(in: result, return: fn(_ok_value) { ok_great }) - // …user handles Error value here… - error_woof - } - |> should.equal(error_woof) -} - -pub fn given_error_test() { - { - let result = Error(error_woof) use _error_value <- given.error(in: result, else_return: fn(_ok_value) { - ok_great + great }) + // …user handles Error value here… - error_woof + woof } - |> should.equal(error_woof) + |> should.equal(woof) { - let result = Ok(ok_great) + let result = Ok(great) + use _error_value <- given.error(in: result, else_return: fn(_ok_value) { - ok_great + great }) + // …user handles Error value here… - error_woof + woof } - |> should.equal(ok_great) + |> should.equal(great) } -pub fn given_some_test() { +pub fn some_test() { { - let option = Some(ok_great) - use _some_value <- given.some(in: option, else_return: fn() { error_woof }) + let option = Some(great) + + use _some_value <- given.some(in: option, else_return: fn() { woof }) + // …user handles Some value here… - ok_great + great } - |> should.equal(ok_great) + |> should.equal(great) { - let option = Some(ok_great) - use _some_value <- given.some(in: option, else_return: fn() { error_woof }) + let option = Some(great) + + use _some_value <- given.some(in: option, else_return: fn() { woof }) + // …user handles Some value here… - ok_great + great } - |> should.equal(ok_great) + |> should.equal(great) } -pub fn given_none_test() { +pub fn none_test() { { - let option = Some(ok_great) - use <- given.none(in: option, else_return: fn(_some_value) { ok_great }) + let option = Some(great) + + use <- given.none(in: option, else_return: fn(_some_value) { great }) + // …user handles None here… - error_woof + woof } - |> should.equal(ok_great) + |> should.equal(great) { let option = None - use <- given.none(in: option, else_return: fn(_some_value) { ok_great }) - // …user handles None here… - "None encountered!" - } - |> should.equal("None encountered!") -} -pub fn given_none_unusual_test() { - { - let option = Some(ok_great) - use some_value <- given.none(in: option, return: fn() { - "None encountered!" - }) - // …user handles Some value here… - some_value - } - |> should.equal(ok_great) + use <- given.none(in: option, else_return: fn(_some_value) { great }) - { - let option = None - use else_some_value <- given.none(in: option, return: fn() { - "None encountered!" - }) - // …user handles Some value here… - else_some_value + // …user handles None here… + "None encountered!" } |> should.equal("None encountered!") }