Skip to content

Commit

Permalink
Fixed expression evaluator for a special selector case. (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Jun 8, 2024
1 parent 59543ff commit ef26c93
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
next-version: 8.0.0
next-version: 8.2.0
4 changes: 3 additions & 1 deletion src/Fluxera.ComponentModel.Annotations/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
using System.Runtime.CompilerServices;
using JetBrains.Annotations;

Expand All @@ -28,7 +30,7 @@ public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][
return argument;
}

public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null)
public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
argument = ThrowIfNull(argument, parameterName);

Expand Down
8 changes: 7 additions & 1 deletion src/Fluxera.Linq.Expressions/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/// Copyright notice http://msdn.microsoft.com/en-gb/cc300389.aspx#O
/// </remarks>
[PublicAPI]
public static class Evaluator
internal static class Evaluator
{
/// <summary>
/// Performs evaluation and replacement of independent sub-trees.
Expand Down Expand Up @@ -76,6 +76,12 @@ public override Expression Visit(Expression exp)
return base.Visit(exp);
}

/// <inheritdoc />
protected override Expression VisitMemberInit(MemberInitExpression node)
{
return node;
}

private Expression Evaluate(Expression e)
{
if(e.NodeType == ExpressionType.Constant)
Expand Down
2 changes: 1 addition & 1 deletion src/Fluxera.Linq.Expressions/LocalCollectionExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/// See: http://petemontgomery.wordpress.com/2008/08/07/caching-the-results-of-linq-queries/
/// </remarks>
[PublicAPI]
public sealed class LocalCollectionExpander : ExpressionVisitor
internal sealed class LocalCollectionExpander : ExpressionVisitor
{
/// <summary>
/// Rewrites the given expression by expanding local collections.
Expand Down
2 changes: 1 addition & 1 deletion src/Fluxera.Linq.Expressions/ParameterRebinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx.
/// </summary>
[PublicAPI]
public sealed class ParameterRebinder : ExpressionVisitor
internal sealed class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary<ParameterExpression, ParameterExpression> map;

Expand Down
3 changes: 1 addition & 2 deletions src/Fluxera.Linq.Expressions/UtilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

internal static class UtilExtensions
Expand All @@ -28,7 +27,7 @@ public static string ToConcatenatedString<T>(this IEnumerable<T> source, Func<T,

public static LinkedList<T> ToLinkedList<T>(this IEnumerable<T> enumerable)
{
enumerable ??= Enumerable.Empty<T>();
enumerable ??= [];

return new LinkedList<T>(enumerable);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Fluxera.Utilities/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
using System.Runtime.CompilerServices;

internal static class Guard
Expand All @@ -28,7 +30,7 @@ public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][
return argument;
}

public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null)
public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
argument = Guard.ThrowIfNull(argument, parameterName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
using System;
using System.Linq.Expressions;
using System.Reflection;
using DynamicAnonymousType;
using FluentAssertions;
using NUnit.Framework;

Expand Down Expand Up @@ -48,5 +50,45 @@ public void ShouldCreateExpressionStringForSimplePropertyExpression()
result.Should().NotBeNullOrWhiteSpace();
result.Should().Be("x => x.Name");
}

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

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

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

[Test]
public void ShouldCreateExpressionStringForDynamicSelectionExpression()
{
// 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);

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

// Assert
result.Should().NotBeNullOrWhiteSpace();
result.Should().Be("x => new DynamicAnonymousType0`1() {Name = x.Name}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DynamicAnonymousType" Version="1.0.4" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
Expand Down
2 changes: 2 additions & 0 deletions tests/Fluxera.Linq.Expressions.UnitTests/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
public class Person
{
public string Name { get; set; }

public int Age { get; set; }
}
}

0 comments on commit ef26c93

Please sign in to comment.