Ejemplo n.º 1
0
        public void TestGetParameter()
        {
            // GIVEN an expression for the parameter
            ParameterExpression parameter = Expression.Parameter(typeof(TestClass));

            // WHEN a param is requested and the null check mode is set to None
            ParameterProvider <TestClass> parameterProvider = new ParameterProvider <TestClass>(parameter, BindingFlags.Instance | BindingFlags.Public, NullCheckMode.None);
            Expression result = parameterProvider.GetParameter("StringProperty");

            // THEN the returned expression is a method call
            Assert.IsInstanceOfType(result, typeof(MethodCallExpression));

            // AND the expression contains the correct parameter
            Assert.AreSame(parameter, ((MethodCallExpression)result).Object);

            // AND the expression contains the getter being accessed
            MethodInfo?methodInfo = typeof(TestClass).GetProperty("StringProperty", BindingFlags.Instance | BindingFlags.Public)?.GetGetMethod();

            Assert.AreSame(methodInfo, ((MethodCallExpression)result).Method);
        }
Ejemplo n.º 2
0
        public void TestGetParameterWithMissingNestedProperty()
        {
            // GIVEN an expression for the parameter
            ParameterExpression parameter = Expression.Parameter(typeof(TestClass));

            // WHEN a param is requested with a nested property name missing from the object
            // THEN an exception is thrown
            ParameterProvider <TestClass> parameterProvider = new ParameterProvider <TestClass>(parameter, BindingFlags.Instance | BindingFlags.Public, NullCheckMode.None);
            FormatStringSyntaxException   e = Assert.ThrowsException <FormatStringSyntaxException>(() => parameterProvider.GetParameter("NestedTestClassProperty.NonExistent"));

            Assert.AreEqual("Property 'NonExistent' not found on type 'FastStringFormat.Parsing.ParameterProviderTests+NestedTestClass'. Does it have a public get accessor?", e.Message);
        }