Initial testing of FunctionRegistry Library

This commit is contained in:
Ozzie Gooen 2022-07-16 19:50:18 -07:00
parent 5e8b46b7df
commit 395477ed12
4 changed files with 62 additions and 8 deletions

View File

@ -15,15 +15,15 @@ module FooImplementation = {
open FunctionRegistry_Core open FunctionRegistry_Core
open FunctionRegistry_Helpers open FunctionRegistry_Helpers
let fn = Function.make( let fn1 = Function.make(
~name="add", ~name="add",
~nameSpace="Foo", ~nameSpace="Foo",
~requiresNamespace=false, ~requiresNamespace=false,
~examples=["Foo.add(1, 2)", "Foo.add(1, 2, 3)"], ~examples=["Foo.add2(1, 2)", "Foo.add3(1, 2, 3)"],
~output=EvtNumber, ~output=EvtNumber,
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~name="add", ~name="add2",
~inputs=[FRTypeNumber, FRTypeNumber], ~inputs=[FRTypeNumber, FRTypeNumber],
~run=(_, inputs, _) => ~run=(_, inputs, _) =>
switch inputs { switch inputs {
@ -33,7 +33,7 @@ module FooImplementation = {
(), (),
), ),
FnDefinition.make( FnDefinition.make(
~name="add", ~name="add3",
~inputs=[FRTypeNumber, FRTypeNumber, FRTypeNumber], ~inputs=[FRTypeNumber, FRTypeNumber, FRTypeNumber],
~run=(_, inputs, _) => ~run=(_, inputs, _) =>
switch inputs { switch inputs {
@ -47,7 +47,7 @@ module FooImplementation = {
(), (),
) )
let library = [fn] let library = [fn1]
} }
let makeBindings = FunctionRegistry_Core.Registry.makeBindings(_, FooImplementation.library) let makeBindings = FunctionRegistry_Core.Registry.makeBindings(_, FooImplementation.library)
@ -78,7 +78,7 @@ describe("Module", () => {
}) })
describe("Fn auto-testing", () => { 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 => { testAll("tests of validity", items, r => {
expect(r->evalWithFoo->E.R.isOk)->toEqual(true) expect(r->evalWithFoo->E.R.isOk)->toEqual(true)
@ -87,7 +87,7 @@ describe("Fn auto-testing", () => {
testAll("tests of type", items, r => { testAll("tests of type", items, r => {
let responseType = let responseType =
r->evalWithFoo->E.R2.fmap(ReducerInterface_InternalExpressionValue.valueToValueType) 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)) expect(responseType)->toEqual(Ok(expectedOutputType))
}) })
}) })

View File

@ -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))
// },
// )
})

View File

@ -429,6 +429,9 @@ module NameSpace = {
module Registry = { module Registry = {
let toJson = (r: registry) => r->E.A2.fmap(Function.toJson) 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 _exportedSubset = (r: registry): registry => r |> E.A.filter(r => !r.requiresNamespace)
let definitionsWithFunctions = (r: registry) => 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( _matchAndRun(~registry=_exportedSubset(registry), ~fnName, ~args, ~env)->E.O2.fmap(
E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)), E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)),
) )

View File

@ -160,6 +160,26 @@ let valueToValueType = value =>
| IEvTypeIdentifier(_) => EvtTypeIdentifier | 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 functionCallToCallSignature = (functionCall: functionCall): functionCallSignature => {
let (fn, args) = functionCall let (fn, args) = functionCall
CallSignature(fn, args->Js.Array2.map(valueToValueType)) CallSignature(fn, args->Js.Array2.map(valueToValueType))