Exemplo n.º 1
0
        public void BoolPropertyIdTest()
        {
            foreach (var entry in new Dictionary <string, object>
            {
                { "Bool", PropertyIdTestType.Bool },
                { "String", PropertyIdTestType.String },
                { "Char", PropertyIdTestType.Char },
                { "Guid", PropertyIdTestType.Guid },
                { "Byte", PropertyIdTestType.Byte },
                { "SByte", PropertyIdTestType.SByte },
                { "Int16", PropertyIdTestType.Int16 },
                { "UInt16", PropertyIdTestType.UInt16 },
                { "Int32", PropertyIdTestType.Int32 },
                { "UInt32", PropertyIdTestType.UInt32 },
                { "Int64", PropertyIdTestType.Int64 },
                { "UInt64", PropertyIdTestType.UInt64 },
                { "Float", PropertyIdTestType.Float },
                { "Double", PropertyIdTestType.Double },
                { "Decimal", PropertyIdTestType.Decimal },
            }
                     .Select(entry =>
            {
                var untypedExpr = ParseAndVisit($"Nesp.NespExpressionTests.PropertyIdTestType.{entry.Key}");

                var context = new NespMetadataResolverContext();
                context.AddCandidate(typeof(PropertyIdTestType));
                var typedExprs = untypedExpr.ResolveMetadata(context);

                var propertyExpr = (NespPropertyExpression)typedExprs.Single();
                return(new { entry.Key, propertyExpr.Property });
            }))
            {
                Assert.AreSame(typeof(PropertyIdTestType).GetProperty(entry.Key), entry.Property);
            }
        }
Exemplo n.º 2
0
        public void DefineOneArgumentLambdaWithOverloadsTest()
        {
            var untypedExpr = ParseAndVisit("define getString1 (value) (Nesp.NespExpressionTests.LambdaTestType.GetString1 value)");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(LambdaTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            Assert.AreEqual(2, typedExprs.Length);

            var lambdaExpr0 = (NespDefineLambdaExpression)typedExprs[0];

            Assert.AreEqual("getString1", lambdaExpr0.Name);
            Assert.AreEqual(typeof(string), lambdaExpr0.FixedType);
            Assert.AreEqual(1, lambdaExpr0.Parameters.Length);
            Assert.AreEqual("value", lambdaExpr0.Parameters[0].Symbol);
            // TODO: How deriving inferered type from NespReferenceSymbolExpression?
            //    Assert.AreEqual(typeof(bool), lambdaExpr0.Parameters[0].FixedType);
            var bodyExpr0 = (NespApplyFunctionExpression)lambdaExpr0.Body;

            Assert.AreEqual(typeof(LambdaTestType).GetMethods().First(method => method.Name == "GetString1" && method.GetParameters()[0].ParameterType == typeof(bool)), bodyExpr0.Method);

            var lambdaExpr1 = (NespDefineLambdaExpression)typedExprs[1];

            Assert.AreEqual("getString1", lambdaExpr1.Name);
            Assert.AreEqual(typeof(string), lambdaExpr1.FixedType);
            Assert.AreEqual(1, lambdaExpr1.Parameters.Length);
            Assert.AreEqual("value", lambdaExpr1.Parameters[0].Symbol);
            // TODO: How deriving inferered type from NespReferenceSymbolExpression?
            //    Assert.AreEqual(typeof(int), lambdaExpr1.Parameters[0].FixedType);
            var bodyExpr1 = (NespApplyFunctionExpression)lambdaExpr1.Body;

            Assert.AreEqual(typeof(LambdaTestType).GetMethods().First(method => method.Name == "GetString1" && method.GetParameters()[0].ParameterType == typeof(int)), bodyExpr1.Method);
        }
Exemplo n.º 3
0
        public void EnumIdTest()
        {
            foreach (var entry in new Dictionary <string, object>
            {
                { "AAA", EnumIdTestType.AAA },
                { "BBB", EnumIdTestType.BBB },
                { "CCC", EnumIdTestType.CCC },
                { "DDD", EnumIdTestType.DDD },
                { "EEE", EnumIdTestType.EEE },
            }
                     .Select(entry =>
            {
                var untypedExpr = ParseAndVisit($"Nesp.NespExpressionTests.EnumIdTestType.{entry.Key}");

                var context = new NespMetadataResolverContext();
                context.AddCandidate(typeof(EnumIdTestType));
                var typedExprs = untypedExpr.ResolveMetadata(context);

                var enumExpr = (NespEnumExpression)typedExprs.Single();
                return(new { entry.Value, ExprValue = enumExpr.Value });
            }))
            {
                Assert.AreEqual(entry.Value, entry.ExprValue);
            }
        }
Exemplo n.º 4
0
        public void NumericFieldIdTest()
        {
            foreach (var entry in new Dictionary <string, object>
            {
                { "ByteField", FieldIdTestType.ByteField },
                { "SByteField", FieldIdTestType.SByteField },
                { "Int16Field", FieldIdTestType.Int16Field },
                { "UInt16Field", FieldIdTestType.UInt16Field },
                { "Int32Field", FieldIdTestType.Int32Field },
                { "UInt32Field", FieldIdTestType.UInt32Field },
                { "Int64Field", FieldIdTestType.Int64Field },
                { "UInt64Field", FieldIdTestType.UInt64Field },
                { "FloatField", FieldIdTestType.FloatField },
                { "DoubleField", FieldIdTestType.DoubleField },
                { "DecimalField", FieldIdTestType.DecimalField },
            }
                     .Select(entry =>
            {
                var untypedExpr = ParseAndVisit($"Nesp.NespExpressionTests.FieldIdTestType.{entry.Key}");

                var context = new NespMetadataResolverContext();
                context.AddCandidate(typeof(FieldIdTestType));
                var typedExprs = untypedExpr.ResolveMetadata(context);

                var fieldExpr = (NespFieldExpression)typedExprs.Single();
                return(new { entry.Key, fieldExpr.Field });
            }))
            {
                Assert.AreSame(typeof(FieldIdTestType).GetField(entry.Key), entry.Field);
            }
        }
Exemplo n.º 5
0
        public void LiteralNumericFieldIdTest()
        {
            foreach (var entry in new Dictionary <string, object>
            {
                { "ByteLiteral", FieldIdTestType.ByteLiteral },
                { "SByteLiteral", FieldIdTestType.SByteLiteral },
                { "Int16Literal", FieldIdTestType.Int16Literal },
                { "UInt16Literal", FieldIdTestType.UInt16Literal },
                { "Int32Literal", FieldIdTestType.Int32Literal },
                { "UInt32Literal", FieldIdTestType.UInt32Literal },
                { "Int64Literal", FieldIdTestType.Int64Literal },
                { "UInt64Literal", FieldIdTestType.UInt64Literal },
                { "FloatLiteral", FieldIdTestType.FloatLiteral },
                { "DoubleLiteral", FieldIdTestType.DoubleLiteral },
                { "DecimalLiteral", FieldIdTestType.DecimalLiteral },
            }
                     .Select(entry =>
            {
                var untypedExpr = ParseAndVisit($"Nesp.NespExpressionTests.FieldIdTestType.{entry.Key}");

                var context = new NespMetadataResolverContext();
                context.AddCandidate(typeof(FieldIdTestType));
                var typedExprs = untypedExpr.ResolveMetadata(context);

                var numericExpr = (NespNumericExpression)typedExprs.Single();
                return(new { entry.Value, Result = numericExpr.Value });
            }))
            {
                Assert.AreEqual(entry.Value, entry.Result);
            }
        }
Exemplo n.º 6
0
        public void FieldIdTest()
        {
            var untypedExpr = ParseAndVisit("Nesp.NespExpressionTests.FieldIdTestType.GuidField");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(FieldIdTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var fieldExpr = (NespFieldExpression)typedExprs.Single();

            Assert.AreSame(typeof(FieldIdTestType).GetField("GuidField"), fieldExpr.Field);
        }
Exemplo n.º 7
0
        public void LiteralCharFieldIdTest()
        {
            var untypedExpr = ParseAndVisit("Nesp.NespExpressionTests.FieldIdTestType.CharLiteral");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(FieldIdTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var charExpr = (NespCharExpression)typedExprs.Single();

            Assert.AreEqual(FieldIdTestType.CharLiteral, charExpr.Value);
        }
Exemplo n.º 8
0
        public void InitOnlyStringFieldIdTest()
        {
            var untypedExpr = ParseAndVisit("Nesp.NespExpressionTests.FieldIdTestType.StringInitOnly");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(FieldIdTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var stringExpr = (NespStringExpression)typedExprs.Single();

            Assert.AreEqual(FieldIdTestType.StringInitOnly, stringExpr.Value);
        }
Exemplo n.º 9
0
        public void MethodArgument0IdTest()
        {
            var untypedExpr = ParseAndVisit("Nesp.NespExpressionTests.SimpleFunctionIdTestType.GetString0");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(SimpleFunctionIdTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var functionExpr = (NespApplyFunctionExpression)typedExprs.Single();
            var expected     = typeof(SimpleFunctionIdTestType).GetMethod("GetString0");

            Assert.AreEqual(expected, functionExpr.Method);
        }
Exemplo n.º 10
0
        public void MethodArgument0ParamsOverloadedIdTest()
        {
            var untypedExpr = ParseAndVisit("Nesp.NespExpressionTests.SimpleFunctionIdTestType.GetString2");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(SimpleFunctionIdTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var functionExpr = (NespApplyFunctionExpression)typedExprs.Single();
            var expected     = typeof(SimpleFunctionIdTestType)
                               .GetMethods()
                               .First(method => (method.Name == "GetString2") && (method.GetParameters().Length == 0));

            Assert.AreEqual(expected, functionExpr.Method);
        }
Exemplo n.º 11
0
        public void DefineEmptyArgumentListLambdaTest()
        {
            var untypedExpr = ParseAndVisit("define getString0 () Nesp.NespExpressionTests.LambdaTestType.GetString0");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(LambdaTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var lambdaExpr = (NespDefineLambdaExpression)typedExprs.Single();

            Assert.AreEqual("getString0", lambdaExpr.Name);
            Assert.AreEqual(typeof(string), lambdaExpr.FixedType);
            Assert.AreEqual(0, lambdaExpr.Parameters.Length);
            var bodyExpr = (NespApplyFunctionExpression)lambdaExpr.Body;

            Assert.AreEqual(typeof(LambdaTestType).GetMethod("GetString0"), bodyExpr.Method);
        }
Exemplo n.º 12
0
        public void DefineOneArgumentLambdaTest()
        {
            var untypedExpr = ParseAndVisit("define parseInt32 (value) (Nesp.NespExpressionTests.LambdaTestType.ParseInt32 value)");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(LambdaTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var lambdaExpr = (NespDefineLambdaExpression)typedExprs.Single();

            Assert.AreEqual("parseInt32", lambdaExpr.Name);
            Assert.AreEqual(typeof(int), lambdaExpr.FixedType);
            Assert.AreEqual(1, lambdaExpr.Parameters.Length);
            Assert.AreEqual("value", lambdaExpr.Parameters[0].Symbol);
            // TODO: How deriving inferered type from NespReferenceSymbolExpression?
            //    Assert.AreEqual(typeof(string), lambdaExpr.Parameters[0].FixedType);
            var bodyExpr = (NespApplyFunctionExpression)lambdaExpr.Body;

            Assert.AreEqual(typeof(LambdaTestType).GetMethod("ParseInt32"), bodyExpr.Method);
        }
Exemplo n.º 13
0
        public void MethodParamArgumentsInt32AndLongOverloadedCompletedIdTest()
        {
            var untypedExpr = ParseAndVisit("Nesp.NespExpressionTests.SimpleFunctionIdTestType.GetString2 12345678 2345678901234567");

            var context = new NespMetadataResolverContext();

            context.AddCandidate(typeof(SimpleFunctionIdTestType));
            var typedExprs = untypedExpr.ResolveMetadata(context);

            var functionExpr = (NespApplyFunctionExpression)typedExprs.Single();
            var expected     = typeof(SimpleFunctionIdTestType)
                               .GetMethods()
                               .First(method =>
                                      (method.Name == "GetString2") &&
                                      (method.GetParameters().Select(parameter => parameter.ParameterType).SequenceEqual(new[] { typeof(int), typeof(long[]) })));

            Assert.AreEqual(expected, functionExpr.Method);
            var argExprs = functionExpr.Arguments
                           .Select(iexpr => (NespNumericExpression)iexpr)
                           .ToArray();

            Assert.IsTrue(argExprs.Select(iexpr => iexpr.FixedType).SequenceEqual(new[] { typeof(int), typeof(long) }));
            Assert.IsTrue(argExprs.Select(iexpr => iexpr.Value).SequenceEqual(new object[] { 12345678, 2345678901234567 }));
        }