Skip to content

Commit

Permalink
feat(std): add a few functions to std.test & std.effect.error
Browse files Browse the repository at this point in the history
  • Loading branch information
Etherian committed Oct 15, 2019
1 parent 3a5ef58 commit 58e0043
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 7 additions & 0 deletions std/effect/error.glu
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

let { Eff, inject_rest, ? } = import! std.effect
let { Result } = import! std.result
let { Option } = import! std.option
let { (<<) } = import! std.function
let { wrap } = import! std.applicative

Expand All @@ -23,6 +24,11 @@ let ok_or_throw r : Result e t -> Eff [| error : Error e | r |] t =
| Ok t -> wrap t
| Err e -> throw e

let some_or_throw e o : e -> Option a -> Eff [| error : Error e | r |] a =
match o with
| Some x -> wrap x
| None -> throw e

/// Eliminates the `Error` effect and returns a `Result`
let run_error eff : forall e . Eff [| error : Error e | r |] a -> Eff [| | r |] (Result e a) =
let loop ve : Eff [| error : Error e | r |] a -> Eff [| | r |] (Result e a) =
Expand Down Expand Up @@ -56,5 +62,6 @@ let catch eff handler : forall e . Eff [| error : Error e | r |] a -> (e -> Eff
catch,
throw,
ok_or_throw,
some_or_throw,
run_error,
}
28 changes: 25 additions & 3 deletions std/test.glu
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

let string = import! std.string
let { wrap } = import! std.applicative
let prelude @ { Semigroup } = import! std.prelude
let { flat_map } = import! std.monad
let float = import! std.float
let int = import! std.int
let list @ { List, ? } = import! std.list
let { Foldable, foldl } = import! std.foldable
let { Option } = import! std.option
let { (<>) } = import! std.semigroup
let { Result } = import! std.result
let { Semigroup, (<>) } = import! std.semigroup
let { error } = import! std.prim
let { id } = import! std.function
let { (>>), id } = import! std.function
let { ? } = import! std.io

let { assert } = import! std.assert

let effect @ { Eff, ? } = import! std.effect
let { Writer, run_writer, tell } = import! std.effect.writer
let { Error, run_error } = import! std.effect.error
let { Lift, run_lift } = import! std.effect.lift


Expand All @@ -38,6 +40,22 @@ let assert_neq l r : [Show a] -> [Eq a] -> a -> a -> Eff [| writer : Test | r |]
if l /= r then wrap ()
else tell (Cons ("Assertion failed: " <> show l <> " == " <> show r) Nil)

let assert_ok res : [Show e] -> Result e a -> Eff [| writer : Test | r |] () =
match res with
| Ok _ -> wrap ()
| Err e -> tell (Cons ("Assertion failed: found error " <> show e) Nil)

let assert_err res : [Show a] -> Result e a -> Eff [| writer : Test | r |] () =
match res with
| Ok x -> tell (Cons ("Assertion failed: expected error, found " <> show x) Nil)
| Err _ -> wrap ()

let assert_errorless : [Show e] -> Eff [| error : Error e, writer : Test | r |] a -> Eff [| writer : Test | r |] () =
run_error >> flat_map assert_ok

let assert_erroring : forall e . [Show a] -> Eff [| error : Error e, writer : Test | r |] a -> Eff [| writer : Test | r |] () =
run_error >> flat_map assert_err

rec let run_raw test : Eff [| writer : Test | r |] a -> Eff [| | r |] (List String) =
do test = run_writer test
wrap test.writer
Expand All @@ -63,6 +81,10 @@ rec let run_io test : TestEffIO r a -> IO () =
assert,
assert_eq,
assert_neq,
assert_ok,
assert_err,
assert_errorless,
assert_erroring,

run_raw,
run,
Expand Down

0 comments on commit 58e0043

Please sign in to comment.