From d362ef26db894ea52c6abd34489aa9efdbc0c89b Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 14 Jan 2023 13:22:39 +0530 Subject: chore: adds test cases + fixes Test effect scope --- tests/builtins.spec.ts | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/builtins.spec.ts (limited to 'tests') diff --git a/tests/builtins.spec.ts b/tests/builtins.spec.ts new file mode 100644 index 0000000..cd4b5c4 --- /dev/null +++ b/tests/builtins.spec.ts @@ -0,0 +1,96 @@ +import { Bind, BindTo, Do, Effect, Label, Pure } from '../stdlib/effect' +import { Throw, Try } from '../stdlib/exception' +import { PutStringLn } from '../stdlib/stdio' +import { DefineEffect, JsExpr, SetEvalEnvironment } from '../stdlib/sys' +import { Test, AssertEqualsK, AssertEquals } from '../stdlib/test' + +type testBind = Do<[ + PutStringLn<'Bind & BindTo'>, + + Test<'should bind value to function and kind', [ + Bind, () => + AssertEquals>, + + Bind, AssertEqualsK<[1,2,3]>>, + ]>, + + Test<'should bind label correctly', [ + BindTo<"value", Pure<{ a: 'b', c: { d: 1 } }>>, + + Bind, AssertEqualsK<{ a: 'b', c: { d: 1 } }>>, + ]>, + + Test<'should bind only inside Do scope', [ + Do<[ + BindTo<"value", Pure<"some value">>, + Bind, AssertEqualsK<"some value">>, + ]>, + + Bind< + Try, <_>() => Pure<"none">>, + AssertEqualsK<"none"> + >, + ]>, + + Test<'should bind only inside Bind scope', [ + Bind< + BindTo<"value", Pure<"some value">>, + <_>() => Bind, AssertEqualsK<"some value">> + >, + + Bind< + Try, <_>() => Pure<"none">>, + AssertEqualsK<"none"> + >, + ]>, +]> + +type testExcpt = Do<[ + PutStringLn<'Try/Throw'>, + + Test<'should return the result of effect', [ + Bind, <_>() => Pure<0>>, + AssertEqualsK<20>>, + ]>, + + Test<'should return the result of error handler', [ + Bind, Pure<20> ]>, <_>() => Pure<0>>, + AssertEqualsK<0>>, + + Bind, Pure<20> ]>, () => Pure>, + AssertEqualsK<"wow">>, + ]>, +]> + +interface MyEffect<_a extends number, _b extends string, _c extends unknown> extends Effect {} +type testFFI = Do<[ + PutStringLn<'FFI'>, + + Test<'should return the result of js expression', [ + Bind, AssertEqualsK<28980>>, + ]>, + + Test<'should test custom effect', [ + DefineEffect<'MyEffect', `([a, b, c], ctx) => { + return [ + ctx.getTypeValue(a) * 2, + ctx.getTypeValue(b) + '!', + [...ctx.getTypeValue(c), 'wow'], + ] + }`>, + Bind, AssertEqualsK<[ + 8, "hey!", [1, 2, 'wow'], + ]>>, + ]>, +]> + +export type main = [ + SetEvalEnvironment<'test.node'>, + + PutStringLn<'====================='>, + PutStringLn<'=== Running tests ==='>, + PutStringLn<'=====================\n\n'>, + testBind, + testExcpt, + testFFI, +] -- cgit v1.3.1