Пример #1
0
        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());
            }
        }
Пример #2
0
        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));
        }
Пример #3
0
        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"));
        }
Пример #4
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;
 }
Пример #5
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));
        }
Пример #6
0
        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);
        }
Пример #7
0
        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));
        }
Пример #8
0
        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);
        }
Пример #9
0
        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"));
        }
Пример #10
0
        public object ToFunObject(object clrObject)
        {
            var converter = FunnyTypeConverters.GetInputConverter(clrObject.GetType());

            return(converter.ToFunObject(clrObject));
        }
Пример #11
0
        public void RequrisiveType_Throws()
        {
            NodeMoqType obj = new NodeMoqType("vasa", new NodeMoqType("peta"));

            Assert.Throws <ArgumentException>(() => FunnyTypeConverters.GetInputConverter(obj.GetType()));
        }