public Func <TInput, TOutput> Build(string expression) { var apriories = AprioriTypesMap.Empty; var inputsMap = FluentApiTools.SetupAprioriInputs <TInput>(apriories); var outputConverter = FunnyTypeConverters.GetOutputConverter(typeof(TOutput)); apriories.Add(Parser.AnonymousEquationId, outputConverter.FunnyType); var runtime = _builder.CreateRuntime(expression, apriories); FluentApiTools.ThrowIfHasUnknownInputs(runtime, inputsMap); if (!runtime.HasDefaultOutput) { throw ErrorFactory.OutputIsUnset(outputConverter.FunnyType); } var outVariable = runtime.GetVariable(Parser.AnonymousEquationId); return(input => { var inputValues = FluentApiTools.GetInputValues(inputsMap, input); runtime.CalculateSafe(inputValues); return (TOutput)outputConverter.ToClrObject(outVariable.FunnyValue); }); }
public void ConvertString(string value) { var converter = FunnyTypeConverters.GetOutputConverter(typeof(string)); Assert.AreEqual(FunnyType.Text, converter.FunnyType); Assert.AreEqual(value, converter.ToClrObject(new TextFunArray(value))); }
public ConcreteLambdaWrapperFunction(string id, Func <Tin, Tout> function) { Name = id; _function = function; _argConverter = FunnyTypeConverters.GetOutputConverter(typeof(Tin)); _resultConverter = FunnyTypeConverters.GetInputConverter(typeof(Tout)); ArgTypes = new[] { _argConverter.FunnyType }; ReturnType = _resultConverter.FunnyType; }
private void AssertFunnyConvert(object originClrObject) { var inputConverter = FunnyTypeConverters.GetInputConverter(originClrObject.GetType()); var outputConverter = FunnyTypeConverters.GetOutputConverter(originClrObject.GetType()); var funObject = inputConverter.ToFunObject(originClrObject); var clrObject = outputConverter.ToClrObject(funObject); Assert.IsTrue(TestHelper.AreSame(originClrObject, clrObject)); }
public void TwinEquations_Runtime_OutputTypesCalculateCorrect(string expr, BaseFunnyType ytype, BaseFunnyType ztype) { var res = expr.Calc(); var y = res.Get("y"); Assert.AreEqual(y.GetType(), FunnyTypeConverters.GetOutputConverter(FunnyType.PrimitiveOf(ytype)).ClrType); var z = res.Get("z"); Assert.AreEqual(z.GetType(), FunnyTypeConverters.GetOutputConverter(FunnyType.PrimitiveOf(ztype)).ClrType); }
public void StructType() { var inputUser = new UserMoqOutputType("vasa", 42, 17.1); var converter = FunnyTypeConverters.GetOutputConverter(inputUser.GetType()); Assert.AreEqual(FunnyType.StructOf( ("name", FunnyType.Text), ("age", FunnyType.Int32), ("size", FunnyType.Real)), converter.FunnyType); }
public void ConvertPrimitiveType(object primitiveValue, BaseFunnyType expectedTypeName) { var clrType = primitiveValue.GetType(); var converter = FunnyTypeConverters.GetOutputConverter(clrType); Assert.AreEqual(expectedTypeName, converter.FunnyType.BaseType); var convertedValue = converter.ToClrObject(primitiveValue); Assert.AreEqual(primitiveValue, convertedValue); }
public void ConvertPrimitiveTypeArrays(object primitiveValue, BaseFunnyType expectedTypeName) { var clrType = primitiveValue.GetType(); var converter = FunnyTypeConverters.GetOutputConverter(clrType); Assert.AreEqual(FunnyType.ArrayOf(FunnyType.PrimitiveOf(expectedTypeName)), converter.FunnyType); var convertedValue = converter.ToClrObject( new ImmutableFunArray(primitiveValue as Array, FunnyType.PrimitiveOf(expectedTypeName))); Assert.AreEqual(primitiveValue, convertedValue); }
public void ArrayOfStructTypesWithoutNewContructor() { var inputUsers = new[] { new UserMoqType("vasa", 42, 17.1), new UserMoqType("peta", 41, 17.0), new UserMoqType("kata", 40, -17.1) }; Assert.Catch(() => FunnyTypeConverters.GetOutputConverter(inputUsers.GetType())); }
public void ArrayOfArrayOfAnything() { object[][] inputValue = { new object[] { 1, 2, "kate" }, new object[] { 2, 1, "kate" }, new object[] { } }; var converter = FunnyTypeConverters.GetOutputConverter(inputValue.GetType()); Assert.AreEqual(FunnyType.ArrayOf(FunnyType.ArrayOf(FunnyType.Anything)), converter.FunnyType); }
public void ConvertArrayOfStrings() { string[] clrValue = { "vasa", "kata", "" }; var converter = FunnyTypeConverters.GetOutputConverter(clrValue.GetType()); Assert.AreEqual(FunnyType.ArrayOf(FunnyType.Text), converter.FunnyType); var funValue = new ImmutableFunArray( clrValue.Select(i => new TextFunArray(i)).ToArray(), FunnyType.Text); Assert.AreEqual(clrValue, converter.ToClrObject(funValue)); }
public void ArrayOfStructTypes() { var inputUsers = new[] { new UserMoqOutputType("vasa", 42, 17.1), new UserMoqOutputType("peta", 41, 17.0), new UserMoqOutputType("kata", 40, -17.1) }; var converter = FunnyTypeConverters.GetOutputConverter(inputUsers.GetType()); Assert.AreEqual( FunnyType.ArrayOf(FunnyType.StructOf( ("name", FunnyType.Text), ("age", FunnyType.Int32), ("size", FunnyType.Real))), converter.FunnyType); }
private static Type GetClrType(FunnyType funnyType) => FunnyTypeConverters.GetOutputConverter(funnyType).ClrType;
//todo //[TestCase( // @"div12(x) = 2600/x // supsum(n) = [1..n].map(div12).sum() // y = [1..20].map(supsum).sum().round()", BaseVarType.Int32)] public void SingleEquation_Runtime_OutputTypeCalculatesCorrect(string expr, BaseFunnyType type) { var clrtype = FunnyTypeConverters.GetOutputConverter(FunnyType.PrimitiveOf(type)).ClrType; expr.Calc().AssertResultIs(clrtype); }
public void RequrisiveType_Throws() => Assert.Catch(() => FunnyTypeConverters.GetOutputConverter(typeof(NodeMoqRecursiveOutputType)));