From f31caa5baad20553cac0330ae9ff279b8f88f340 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 14 Jan 2023 17:38:24 +0530 Subject: feat: adds test cases for do and seq + fixes behavior with empty results --- src/context.ts | 4 +- src/eval-env/builtins.ts | 14 ++- src/util.ts | 7 +- stdlib/effect.ts | 2 + tests/builtins.spec.ts | 291 ++++++++++++++++++++++++++++------------------- 5 files changed, 190 insertions(+), 128 deletions(-) diff --git a/src/context.ts b/src/context.ts index fab6653..f70a17d 100644 --- a/src/context.ts +++ b/src/context.ts @@ -39,7 +39,9 @@ export const createContext = (options: CtxOptions): Ctx => { ) const getResultExpr = (resultKey?: string) => - `(${RESULT_TYPE_NAME}[${JSON.stringify(resultKey)})` + resultKey + ? `((${RESULT_TYPE_NAME}[${JSON.stringify(resultKey)})['output'])` + : 'undefined' const addResult = (name: string, ty: string): Node | undefined => resultTypeNode diff --git a/src/eval-env/builtins.ts b/src/eval-env/builtins.ts index c5b0c82..d90bc2e 100644 --- a/src/eval-env/builtins.ts +++ b/src/eval-env/builtins.ts @@ -49,6 +49,8 @@ export default (ctx: Ctx, args: Type[]) => ({ return [resultKey] }, + Noop: async () => [], + Print: async () => { console.log(...args.map(ctx.typeToString)) return [] @@ -77,7 +79,7 @@ export default (ctx: Ctx, args: Type[]) => ({ const resultType = applyFunc( ctx, chainToKind, - resultKey ? `(${ctx.getResultExpr(resultKey)})['output']` : 'never' + resultKey ? `${ctx.getResultExpr(resultKey)}` : 'never' ) return ctx.evaluateType(ctx, resultType) }), @@ -102,7 +104,7 @@ export default (ctx: Ctx, args: Type[]) => ({ return [value] }, - Try: async () => { + Try: ctx.withScope(async () => { const [effTyp, catchK] = args try { @@ -113,7 +115,7 @@ export default (ctx: Ctx, args: Type[]) => ({ const resultType = applyFunc(ctx, catchK, error) return ctx.evaluateType(ctx, resultType) } - }, + }), Throw: async () => { throw args[0] && ctx.getTypeValue(args[0]) @@ -134,8 +136,8 @@ export default (ctx: Ctx, args: Type[]) => ({ effectTyps?.getTupleElements() ?? [] ) const [resultKey, _] = ctx.createResult(`[ - ${effectResults.map(ctx.getResultExpr).join(', ')} - ]`) + ${effectResults.map(ctx.getResultExpr).join(', ')} + ]`) return [resultKey] }), @@ -148,7 +150,7 @@ export default (ctx: Ctx, args: Type[]) => ({ // TODO: Use last type's result instead of last result key const lastResKey = effectResults[effectResults.length - 1] const [resultKey, _] = ctx.createResult( - `(${ctx.getResultExpr(lastResKey)})['output']` + `${ctx.getResultExpr(lastResKey)}` ) return [resultKey] }), diff --git a/src/util.ts b/src/util.ts index a486294..b7d4efe 100644 --- a/src/util.ts +++ b/src/util.ts @@ -7,9 +7,10 @@ export const match = ( ) => (k && pattern[k] ? pattern[k]() : pattern._()) export const evalList = async (ctx: Ctx, effectTyps: Type[]) => { - const effectResults: string[] = [] + const effectResults: (string | undefined)[] = [] for (const item of effectTyps ?? []) { - effectResults.push(...(await ctx.evaluateType(ctx, item))) + const [resultKey] = await ctx.evaluateType(ctx, item) + effectResults.push(resultKey ? resultKey : undefined) } return effectResults } @@ -69,8 +70,6 @@ export const applyFunc = ( return undefined })() - // TODO: Cleanup unwanted result node values - if (!resultType) { throw new Error('Couldnt get result for function application') } diff --git a/stdlib/effect.ts b/stdlib/effect.ts index d53b6b6..9b19197 100644 --- a/stdlib/effect.ts +++ b/stdlib/effect.ts @@ -22,3 +22,5 @@ export interface Seq<_Effs extends Effect[]> extends Effect {} export interface Do<_Effs extends Effect[]> extends Effect {} export interface Pure extends Effect {} + +export interface Noop extends Effect {} diff --git a/tests/builtins.spec.ts b/tests/builtins.spec.ts index 31e2cc0..1c833c0 100644 --- a/tests/builtins.spec.ts +++ b/tests/builtins.spec.ts @@ -1,6 +1,15 @@ -import { Bind, BindTo, Do, Effect, Label, Pure } from '../stdlib/effect' +import { + Bind, + BindTo, + Do, + Effect, + Label, + Noop, + Pure, + Seq, +} from '../stdlib/effect' import { Throw, Try } from '../stdlib/exception' -import { PutStringLn } from '../stdlib/stdio' +import { Print, PutStringLn } from '../stdlib/stdio' import { DefineEffect, JsExpr, SetEvalEnvironment } from '../stdlib/sys' import { Test, AssertEqualsK, AssertEquals } from '../stdlib/test' @@ -14,121 +23,169 @@ export type main = [ PutStringLn<'=== Running tests ==='>, PutStringLn<'=====================\n\n'>, - Do< - [ - PutStringLn<'Bind & BindTo'>, - - Test< - 'should bind value to function and kind', - [ - Bind< - Pure<[1, 2, 3]>, - () => 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'> - > - ] - > - ] - >, - - 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< - Try, Pure<20>]>, <_>() => Pure<0>>, - AssertEqualsK<0> - >, - - Bind< - Try, Pure<20>]>, () => Pure>, - AssertEqualsK<'wow'> - > - ] - > - ] - >, - - 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< - MyEffect<4, 'hey', [1, 2]>, - AssertEqualsK<[8, 'hey!', [1, 2, 'wow']]> - > - ] - > - ] - >, + testDo, + testSeq, + testBind, + testException, + testFFI, PutStringLn<''> ] + +type testSeq = Do< + [ + PutStringLn<'Seq'>, + + Test< + 'should run a sequence of effects and all results', + [ + BindTo<'result', Seq<[Pure<1>, Pure<2>, Pure<3>, Pure<4>]>>, + + Bind, AssertEqualsK<[1, 2, 3, 4]>> + ] + >, + + Test< + 'should produce undefined for effects with no result', + [ + BindTo<'result', Seq<[Pure<1>, Noop, Pure<3>, Noop]>>, + + Bind, AssertEqualsK<[1, undefined, 3, undefined]>> + ] + > + ] +> + +type testDo = Do< + [ + PutStringLn<'Do'>, + + Test< + 'should run a sequence of effects and return the last effect', + [ + BindTo<'result', Do<[Pure<1>, Pure<2>, Pure<3>, Pure<4>]>>, + + Bind, AssertEqualsK<4>> + ] + >, + + Test< + 'should return undefined if last effect doesnt produce a result', + [ + BindTo<'result', Do<[Pure<1>, Noop, Pure<3>, Noop]>>, + + Bind, AssertEqualsK> + ] + > + ] +> + +type testBind = Do< + [ + PutStringLn<'Bind & BindTo'>, + + Test< + 'should bind value to function and kind', + [ + Bind< + Pure<[1, 2, 3]>, + () => 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, <_>() => Pure<'none'>>, AssertEqualsK<'none'>> + ] + >, + + Test< + 'should bind only inside Bind scope', + [ + Bind< + BindTo<'value', Pure<'some value'>>, + <_>() => Bind, AssertEqualsK<'some value'>> + >, + + Bind, <_>() => Pure<'none'>>, AssertEqualsK<'none'>> + ] + > + ] +> + +type testException = 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< + Try, Pure<20>]>, <_>() => Pure<0>>, + AssertEqualsK<0> + >, + + Bind< + Try, Pure<20>]>, () => Pure>, + AssertEqualsK<'wow'> + > + ] + > + ] +> + +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< + MyEffect<4, 'hey', [1, 2]>, + AssertEqualsK<[8, 'hey!', [1, 2, 'wow']]> + > + ] + > + ] +> -- cgit v1.3.1