public void Transform_WithTwoArgument_TypeString() { var method = typeof(string).GetMethod("IndexOf", new[] { typeof(string), typeof(int) }); var objectExpression = Expression.Constant("Test"); var argument1 = Expression.Constant("es"); var argument2 = Expression.Constant(2); var expression = Expression.Call(objectExpression, method, argument1, argument2); var transformer = new IndexOfMethodCallTransformer(); var result = transformer.Transform(expression); var startIndexExpression = Expression.Add(argument2, new SqlLiteralExpression(1)); var lenArgExpression = new SqlLengthExpression(argument1); var leftTestPredicate = Expression.Equal(lenArgExpression, new SqlLiteralExpression(0)); var lenObjectExpression = new SqlLengthExpression(objectExpression); var rightTestpredicate = Expression.LessThanOrEqual(startIndexExpression, lenObjectExpression); var testPredicate = Expression.AndAlso(leftTestPredicate, rightTestpredicate); var charIndexExpression = new SqlFunctionExpression( expression.Type, "CHARINDEX", argument1, objectExpression, startIndexExpression); var elseValue = Expression.Subtract(charIndexExpression, new SqlLiteralExpression(1)); var expectedResult = Expression.Condition(testPredicate, argument2, elseValue); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result); }
public void Transform_WithOneArgument_TypeChar() { var method = typeof(string).GetMethod("IndexOf", new[] { typeof(char) }); var objectExpression = Expression.Constant("Test"); var argument1 = Expression.Constant('e'); var expression = Expression.Call(objectExpression, method, argument1); var transformer = new IndexOfMethodCallTransformer(); var result = transformer.Transform(expression); var lenExpression = new SqlLengthExpression(argument1); var testPredicate = Expression.Equal(lenExpression, new SqlLiteralExpression(0)); var charIndexExpression = new SqlFunctionExpression( expression.Type, "CHARINDEX", argument1, objectExpression); var elseValue = Expression.Subtract(charIndexExpression, new SqlLiteralExpression(1)); var expectedResult = Expression.Condition(testPredicate, new SqlLiteralExpression(0), elseValue); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result); }