Пример #1
0
        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);
            });
        }
Пример #2
0
        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)));
        }
Пример #3
0
 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;
 }
Пример #4
0
        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));
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        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()));
        }
Пример #10
0
        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);
        }
Пример #11
0
        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));
        }
Пример #12
0
        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);
        }
Пример #13
0
 private static Type GetClrType(FunnyType funnyType) =>
 FunnyTypeConverters.GetOutputConverter(funnyType).ClrType;
Пример #14
0
        //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);
        }
Пример #15
0
 public void RequrisiveType_Throws()
 => Assert.Catch(() => FunnyTypeConverters.GetOutputConverter(typeof(NodeMoqRecursiveOutputType)));