public void ConvertString(string value) { var converter = FunnyTypeConverters.GetInputConverter(typeof(string)); Assert.AreEqual(FunnyType.Text, converter.FunnyType); Assert.AreEqual(new TextFunArray(value), converter.ToFunObject(value)); }
public void ArrayOfStructTypes() { var inputUsers = new[] { new UserMoqType("vasa", 42, 17.1), new UserMoqType("peta", 41, 17.0), new UserMoqType("kata", 40, -17.1) }; var converter = FunnyTypeConverters.GetInputConverter(inputUsers.GetType()); Assert.AreEqual( FunnyType.ArrayOf(FunnyType.StructOf( ("name", FunnyType.Text), ("age", FunnyType.Int32), ("size", FunnyType.Real))), converter.FunnyType); var value = converter.ToFunObject(inputUsers); Assert.IsInstanceOf <ImmutableFunArray>(value); var secondElememt = ((ImmutableFunArray)value).GetElementOrNull(1) as FunnyStruct; Assert.IsNotNull(secondElememt, ((ImmutableFunArray)value).GetElementOrNull(1).GetType().Name + " is wrong type"); Assert.AreEqual(inputUsers[1].Name, (secondElememt.GetValue("name") as TextFunArray).ToText()); Assert.AreEqual(inputUsers[1].Age, secondElememt.GetValue("age")); Assert.AreEqual(inputUsers[1].Size, secondElememt.GetValue("size")); }
public object this[string key] { set { var usage = _variables.GetUsages(key); if (usage == null) { throw new KeyNotFoundException($"Variable '{key}' not found in scope"); } var converter = FunnyTypeConverters.GetInputConverter(value.GetType()); usage.Source.InternalFunnyValue = converter.ToFunObject(value); } get { var usage = _variables.GetUsages(key); if (usage == null) { throw new KeyNotFoundException($"Variable '{key}' not found in scope"); } var output = usage.Source; if (output.IsReadonly) { throw new KeyNotFoundException($"Variable '{key}' is input and cannot be read"); } return(output.GetClrValue()); } }
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 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 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 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 ConvertPrimitiveTypeArrays(object primitiveValue, BaseFunnyType expectedTypeName) { var clrType = primitiveValue.GetType(); var converter = FunnyTypeConverters.GetInputConverter(clrType); Assert.AreEqual(FunnyType.ArrayOf(FunnyType.PrimitiveOf(expectedTypeName)), converter.FunnyType); var convertedValue = converter.ToFunObject(primitiveValue); 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 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 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 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[] inputValue = { "vasa", "kata", "" }; var converter = FunnyTypeConverters.GetInputConverter(inputValue.GetType()); Assert.AreEqual(FunnyType.ArrayOf(FunnyType.Text), converter.FunnyType); Assert.AreEqual(new ImmutableFunArray( inputValue.Select(i => new TextFunArray(i)).ToArray(), FunnyType.Text), converter.ToFunObject(inputValue)); }
public void ArrayOfArrayOfAnything() { object[][] inputValue = { new object[] { 1, 2, "kate" }, new object[] { 2, 1, "kate" }, new object[] { } }; var converter = FunnyTypeConverters.GetInputConverter(inputValue.GetType()); Assert.AreEqual(FunnyType.ArrayOf(FunnyType.ArrayOf(FunnyType.Anything)), converter.FunnyType); var value = converter.ToFunObject(inputValue); Assert.IsInstanceOf <ImmutableFunArray>(value); Assert.AreEqual(3, ((ImmutableFunArray)value).Count); }
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); }
public void StructType() { var inputUser = new UserMoqType("vasa", 42, 17.1); var converter = FunnyTypeConverters.GetInputConverter(inputUser.GetType()); Assert.AreEqual(FunnyType.StructOf( ("name", FunnyType.Text), ("age", FunnyType.Int32), ("size", FunnyType.Real)), converter.FunnyType); var value = converter.ToFunObject(inputUser); Assert.IsInstanceOf <FunnyStruct>(value); var converted = (FunnyStruct)value; Assert.AreEqual(inputUser.Name, (converted.GetValue("name") as TextFunArray).ToText()); Assert.AreEqual(inputUser.Age, converted.GetValue("age")); Assert.AreEqual(inputUser.Size, converted.GetValue("size")); }
private static Type GetClrType(FunnyType funnyType) => FunnyTypeConverters.GetOutputConverter(funnyType).ClrType;
public object ToFunObject(object clrObject) { var converter = FunnyTypeConverters.GetInputConverter(clrObject.GetType()); return(converter.ToFunObject(clrObject)); }
public void RequrisiveType_Throws() => Assert.Catch(() => FunnyTypeConverters.GetOutputConverter(typeof(NodeMoqRecursiveOutputType)));
public void RequrisiveType_Throws() { NodeMoqType obj = new NodeMoqType("vasa", new NodeMoqType("peta")); Assert.Throws <ArgumentException>(() => FunnyTypeConverters.GetInputConverter(obj.GetType())); }
//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); }