예제 #1
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);
        }
        public void TestToExpressionWithInvalidProperty()
        {
            // GIVEN expressions for parameters
            Mock <IParameterProvider <object> > parameterProvider = new Mock <IParameterProvider <object> >();
            Expression formatProvider = Expression.Constant(null);

            // AND the parameter provider can find the property but it's not IFormattable
            Expression dateTimeExpression = Expression.Constant(new object());

            parameterProvider.Setup(p => p.GetParameter("ObjectProperty")).Returns(dateTimeExpression);

            // WHEN the param segment is converted to an expression with a property that is not IFormattable
            // THEN an exception is thrown
            FormattedParamSegment       paramSegment = new FormattedParamSegment("ObjectProperty", "yyyy-MM-dd");
            FormatStringSyntaxException e            = Assert.ThrowsException <FormatStringSyntaxException>(() => paramSegment.ToExpression(parameterProvider.Object, formatProvider));

            Assert.AreEqual("Property 'ObjectProperty' does not return a type implementing IFormattable hence a format string cannot be applied to it.", e.Message);
        }