Skip to content

Commit

Permalink
Added ToExpressionString overload for non-generic expression. (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Jun 15, 2024
1 parent ef26c93 commit 0038b7c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Fluxera.Linq.Expressions/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ public static class ExpressionExtensions
/// See Pete Montgomery's post here:
/// http://petemontgomery.wordpress.com/2008/08/07/caching-the-results-of-linq-queries/
/// </remarks>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static string ToExpressionString<T, TResult>(this Expression<Func<T, TResult>> expression)
public static string ToExpressionString(this Expression expression)
{
if(expression is null)
{
Expand All @@ -43,6 +41,24 @@ public static string ToExpressionString<T, TResult>(this Expression<Func<T, TRes
return expr.ToString();
}

/// <summary>
/// Converts the given <see cref="Expression" /> to a string representation.
/// </summary>
/// <remarks>
/// See Pete Montgomery's post here:
/// http://petemontgomery.wordpress.com/2008/08/07/caching-the-results-of-linq-queries/
/// </remarks>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static string ToExpressionString<T, TResult>(this Expression<Func<T, TResult>> expression)
{
Expression expr = expression;

return expr?.ToExpressionString();
}

/// <summary>
/// Creates a <see cref="Expression" /> that represents a bitwise AND operation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,91 @@ public void ShouldCreateExpressionStringForDynamicSelectionExpression()
result.Should().NotBeNullOrWhiteSpace();
result.Should().Be("x => new DynamicAnonymousType0`1() {Name = x.Name}");
}

[Test]
public void ShouldCheckForEmpty_Untyped()
{
// Arrange
Expression<Func<Person, string>> expression = x => x.Name;
Expression expr = expression;

// Act
string result = expr.ToExpressionString();

// Assert
result.Should().NotBeNullOrWhiteSpace();
}

[Test]
public void ShouldCreateExpressionStringForComplexPropertyExpression_Untyped()
{
// Arrange
Expression<Func<Person, bool>> expression = x => x.Name.StartsWith("O") && x.Name.EndsWith("l");
Expression expr = expression;

// Act
string result = expr.ToExpressionString();

// Assert
result.Should().NotBeNullOrWhiteSpace();
result.Should().Be("x => (x.Name.StartsWith(\"O\") AndAlso x.Name.EndsWith(\"l\"))");
}

[Test]
public void ShouldCreateExpressionStringForSimplePropertyExpression_Untyped()
{
// Arrange
Expression<Func<Person, string>> expression = x => x.Name;
Expression expr = expression;

// Act
string result = expr.ToExpressionString();

// Assert
result.Should().NotBeNullOrWhiteSpace();
result.Should().Be("x => x.Name");
}

[Test]
public void ShouldCreateExpressionStringForSelectionExpression_Untyped()
{
// Arrange
Expression<Func<Person, dynamic>> expression = x => new { x.Name };
Expression expr = expression;

// Act
string result = expr.ToExpressionString();

// Assert
result.Should().NotBeNullOrWhiteSpace();
result.Should().Be("x => new <>f__AnonymousType0`1(Name = x.Name)");
}

[Test]
public void ShouldCreateExpressionStringForDynamicSelectionExpression_Untyped()
{
// Arrange
Type type = DynamicFactory.CreateType(("Name", typeof(string)));

PropertyInfo property = typeof(Person).GetProperty("Name");
PropertyInfo dynamicProperty = type.GetProperty("Name");

ParameterExpression parameter = Expression.Parameter(typeof(Person), "x");
MemberExpression propertyExpression = Expression.MakeMemberAccess(parameter, property);
MemberAssignment binding = Expression.Bind(dynamicProperty, propertyExpression);

NewExpression newExpression = Expression.New(type);
MemberInitExpression memberInitExpression = Expression.MemberInit(newExpression, binding);

Expression<Func<Person, dynamic>> expression = Expression.Lambda<Func<Person, dynamic>>(memberInitExpression, parameter);
Expression expr = expression;

// Act
string result = expr.ToExpressionString();

// Assert
result.Should().NotBeNullOrWhiteSpace();
result.Should().Be("x => new DynamicAnonymousType0`1() {Name = x.Name}");
}
}
}

0 comments on commit 0038b7c

Please sign in to comment.