Skip to content

Commit

Permalink
Ensure that Split isn't too lenient
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Sep 30, 2020
1 parent 5ec20c9 commit 5c87090
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/Moq.Tests/ExpressionSplitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,52 @@ public void Split_one_indexer_get_access_and_one_property_set_access()
A((IB b) => b.C, D<IC>()));
}

[Fact]
public void Non_lenient_split_fails_if_rightmost_part_is_non_overridable_property()
{
var expression = E((U u) => u.V.SealedW);

AssertSplitFails(expression, allowNonOverridableLastProperty: false);
}

[Fact]
public void Lenient_split_succeeds_if_rightmost_part_is_non_overridable_property()
{
var expression = E((U u) => u.V.SealedW);

AssertSplitYields(expression, allowNonOverridableLastProperty: true,
E((U u) => u.V),
E((V v) => v.SealedW));
}

[Fact]
public void Lenient_split_fails_if_any_part_other_than_the_rightmost_one_is_non_overridable_property()
{
var expression = E((U u) => u.SealedV.W);

AssertSplitFails(expression, allowNonOverridableLastProperty: true);
}

private void AssertSplitFails(LambdaExpression expression, params LambdaExpression[] expected)
{
Assert.Throws<ArgumentException>(() => expression.Split());
}

private void AssertSplitFails(LambdaExpression expression, bool allowNonOverridableLastProperty)
{
Assert.ThrowsAny<Exception>(() => expression.Split(allowNonOverridableLastProperty));
}

private void AssertSplitYields(LambdaExpression expression, params LambdaExpression[] expected)
{
Assert.Equal(expected, expression.Split().Select(e => e.Expression), ExpressionComparer.Default);
}

private void AssertSplitYields(LambdaExpression expression, bool allowNonOverridableLastProperty, params LambdaExpression[] expected)
{
Assert.Equal(expected, expression.Split(allowNonOverridableLastProperty).Select(e => e.Expression), ExpressionComparer.Default);
}

/// <summary>
/// Helper method producing an assignment <see cref="BinaryExpression"/>.
/// Useful because C# does not allow assignments in literal expressions.
Expand Down Expand Up @@ -353,5 +389,21 @@ public interface IC
}

public delegate IB ADelegate();

public abstract class U
{
public abstract V V { get; }
public V SealedV { get => throw new NotImplementedException(); }
}

public abstract class V
{
public abstract W W { get; }
public W SealedW { get => throw new NotImplementedException(); }
}

public abstract class W
{
}
}
}

0 comments on commit 5c87090

Please sign in to comment.