diff --git a/packages/squiggle-lang/__tests__/FunctionRegistry_test.res b/packages/squiggle-lang/__tests__/FunctionRegistry_test.res index 376f8c86..bfb4ab9f 100644 --- a/packages/squiggle-lang/__tests__/FunctionRegistry_test.res +++ b/packages/squiggle-lang/__tests__/FunctionRegistry_test.res @@ -15,15 +15,15 @@ module FooImplementation = { open FunctionRegistry_Core open FunctionRegistry_Helpers - let fn = Function.make( + let fn1 = Function.make( ~name="add", ~nameSpace="Foo", ~requiresNamespace=false, - ~examples=["Foo.add(1, 2)", "Foo.add(1, 2, 3)"], + ~examples=["Foo.add2(1, 2)", "Foo.add3(1, 2, 3)"], ~output=EvtNumber, ~definitions=[ FnDefinition.make( - ~name="add", + ~name="add2", ~inputs=[FRTypeNumber, FRTypeNumber], ~run=(_, inputs, _) => switch inputs { @@ -33,7 +33,7 @@ module FooImplementation = { (), ), FnDefinition.make( - ~name="add", + ~name="add3", ~inputs=[FRTypeNumber, FRTypeNumber, FRTypeNumber], ~run=(_, inputs, _) => switch inputs { @@ -47,7 +47,7 @@ module FooImplementation = { (), ) - let library = [fn] + let library = [fn1] } let makeBindings = FunctionRegistry_Core.Registry.makeBindings(_, FooImplementation.library) @@ -78,7 +78,7 @@ describe("Module", () => { }) describe("Fn auto-testing", () => { - let items = FooImplementation.fn.examples->E.A.to_list + let items = FooImplementation.fn1.examples->E.A.to_list testAll("tests of validity", items, r => { expect(r->evalWithFoo->E.R.isOk)->toEqual(true) @@ -87,7 +87,7 @@ describe("Fn auto-testing", () => { testAll("tests of type", items, r => { let responseType = r->evalWithFoo->E.R2.fmap(ReducerInterface_InternalExpressionValue.valueToValueType) - let expectedOutputType = FooImplementation.fn.output |> E.O.toExn("") + let expectedOutputType = FooImplementation.fn1.output |> E.O.toExn("") expect(responseType)->toEqual(Ok(expectedOutputType)) }) }) diff --git a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res new file mode 100644 index 00000000..23e83a45 --- /dev/null +++ b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res @@ -0,0 +1,27 @@ +open Jest +open Expect + +let expectEvalToBeOk = (expr: string) => + Reducer.evaluate(expr)->Reducer_Helpers.rRemoveDefaultsExternal->E.R.isOk->expect->toBe(true) + +let registry = FunctionRegistry_Library.registry +let examples = E.A.to_list(FunctionRegistry_Core.Registry.allExamples(registry)) + +describe("Fn auto-testing", () => { + testAll("tests of validity", examples, r => { + expectEvalToBeOk(r) + }) + + // testAll( + // "tests of type", + // E.A.to_list(FunctionRegistry_Core.Registry.allExamplesWithFns(registry)), + // ((fn, example)) => { + // let responseType = + // example + // ->Reducer.evaluate + // ->E.R2.fmap(ReducerInterface_InternalExpressionValue.externalValueToValueType) + // let expectedOutputType = fn.output |> E.O.toExn("") + // expect(responseType)->toEqual(Ok(expectedOutputType)) + // }, + // ) +}) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index 1af459f9..f35bbb5e 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -429,6 +429,9 @@ module NameSpace = { module Registry = { let toJson = (r: registry) => r->E.A2.fmap(Function.toJson) + let allExamples = (r: registry) => r->E.A2.fmap(r => r.examples)->E.A.concatMany + let allExamplesWithFns = (r: registry) => r->E.A2.fmap(fn => (fn.examples->E.A2.fmap(example => (fn, example))))->E.A.concatMany + let _exportedSubset = (r: registry): registry => r |> E.A.filter(r => !r.requiresNamespace) let definitionsWithFunctions = (r: registry) => @@ -463,7 +466,11 @@ module Registry = { } } - let dispatch = (registry, (fnName, args): ReducerInterface_InternalExpressionValue.functionCall, env) => { + let dispatch = ( + registry, + (fnName, args): ReducerInterface_InternalExpressionValue.functionCall, + env, + ) => { _matchAndRun(~registry=_exportedSubset(registry), ~fnName, ~args, ~env)->E.O2.fmap( E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)), ) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res index a6766071..3beea889 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res @@ -160,6 +160,26 @@ let valueToValueType = value => | IEvTypeIdentifier(_) => EvtTypeIdentifier } +let externalValueToValueType = (value: ExternalExpressionValue.t) => + switch value { + | EvArray(_) => EvtArray + | EvArrayString(_) => EvtArrayString + | EvBool(_) => EvtBool + | EvCall(_) => EvtCall + | EvDate(_) => EvtDate + | EvDeclaration(_) => EvtDeclaration + | EvDistribution(_) => EvtDistribution + | EvLambda(_) => EvtLambda + | EvModule(_) => EvtModule + | EvNumber(_) => EvtNumber + | EvRecord(_) => EvtRecord + | EvString(_) => EvtString + | EvSymbol(_) => EvtSymbol + | EvTimeDuration(_) => EvtTimeDuration + | EvType(_) => EvtType + | EvTypeIdentifier(_) => EvtTypeIdentifier + } + let functionCallToCallSignature = (functionCall: functionCall): functionCallSignature => { let (fn, args) = functionCall CallSignature(fn, args->Js.Array2.map(valueToValueType))