Skip to content

Instantly share code, notes, and snippets.

@osfameron
Last active September 23, 2020 20:38
Show Gist options
  • Save osfameron/fdac0b9927d7fef0f0cf636e52214f34 to your computer and use it in GitHub Desktop.
Save osfameron/fdac0b9927d7fef0f0cf636e52214f34 to your computer and use it in GitHub Desktop.
Unison effect handling/testing sketch
-- basic algebraic effect with just 2 capabilities
ability Effect where
print : Text ->{Effect} ()
input : {Effect} Text
-- `run main` will run the code
main = 'handle !prog with IOHandler
-- this is the program
prog _ =
print "What's your name?"
name = input
print ("Hello " Text.++ name)
()
-- Here's how we want to test it!
-- expectPrint checks the call
-- sampleInput injects a sample input back into the program
-- result verifies the final output.
test> prog.tests.t1 =
testEffect '(prog |> expectPrint "What's your name?"
|> sampleInput "Hakim"
|> expectPrint "Hello Hakim"
|> result ()
)
IOHandler =
cases
{print e -> resume} ->
printLine e
handle !resume with IOHandler
{input -> resume} ->
x = !readLine
handle resume x with IOHandler
{u} -> u
expectPrint p r = handle !r with testPrintHandler p
sampleInput i r = handle !r with testInputHandler i
result res r = handle !r with testResultHandler res
testPrintHandler p =
cases
{print v -> next} ->
if v == p then
next
else
raise ("Expected " ++ p ++ " but got " Text.++ v)
{w} -> raise "Unexpected"
testInputHandler i =
cases
{input -> resume} ->
'(resume i)
{u} -> raise "No input found"
testResultHandler r =
cases
{w} -> if w == r then
"OK"
else
raise "Wrong result!"
testEffect p =
res = toEither p
match res with
Right x -> [Ok "Successful test!"]
Left y -> [Fail y]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment