Skip to content

Commit

Permalink
Expand Graft helpers up to 4 parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jcracknell committed Nov 3, 2024
1 parent 81f987e commit 3f54454
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/Arborist/src/ExpressionOn_2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ public static Expression<Func<A, B, R>> Of<R>(Expression<Func<A, B, R>> expressi
/// </remarks>
public static Expression<Action<A, B>> Of(Expression<Action<A, B>> expression) =>
expression;

/// <summary>
/// Grafts the provided <paramref name="branch"/> expression onto the <paramref name="root"/> expression,
/// replacing references to its parameter with the body of the <paramref name="root"/> expression.
/// </summary>
public static Expression<Func<A, B, RR>> Graft<R, RR>(
Expression<Func<A, B, R>> root,
Expression<Func<R, RR>> branch
) =>
Expression.Lambda<Func<A, B, RR>>(
body: ExpressionHelper.Replace(branch.Body, branch.Parameters[0], root.Body),
parameters: root.Parameters
);
}
13 changes: 13 additions & 0 deletions src/Arborist/src/ExpressionOn_3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ public static Expression<Func<A, B, C, R>> Of<R>(Expression<Func<A, B, C, R>> ex
/// </remarks>
public static Expression<Action<A, B, C>> Of(Expression<Action<A, B, C>> expression) =>
expression;

/// <summary>
/// Grafts the provided <paramref name="branch"/> expression onto the <paramref name="root"/> expression,
/// replacing references to its parameter with the body of the <paramref name="root"/> expression.
/// </summary>
public static Expression<Func<A, B, C, RR>> Graft<R, RR>(
Expression<Func<A, B, C, R>> root,
Expression<Func<R, RR>> branch
) =>
Expression.Lambda<Func<A, B, C, RR>>(
body: ExpressionHelper.Replace(branch.Body, branch.Parameters[0], root.Body),
parameters: root.Parameters
);
}
13 changes: 13 additions & 0 deletions src/Arborist/src/ExpressionOn_4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ public static Expression<Func<A, B, C, D, R>> Of<R>(Expression<Func<A, B, C, D,
/// </remarks>
public static Expression<Action<A, B, C, D>> Of(Expression<Action<A, B, C, D>> expression) =>
expression;

/// <summary>
/// Grafts the provided <paramref name="branch"/> expression onto the <paramref name="root"/> expression,
/// replacing references to its parameter with the body of the <paramref name="root"/> expression.
/// </summary>
public static Expression<Func<A, B, C, D, RR>> Graft<R, RR>(
Expression<Func<A, B, C, D, R>> root,
Expression<Func<R, RR>> branch
) =>
Expression.Lambda<Func<A, B, C, D, RR>>(
body: ExpressionHelper.Replace(branch.Body, branch.Parameters[0], root.Body),
parameters: root.Parameters
);
}
48 changes: 28 additions & 20 deletions src/Arborist/test/GraftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,40 @@ namespace Arborist;
public class GraftTests {
[Fact]
public void Graft0_works_as_expected() {
var expected = Expression.Lambda<Func<int>>(
Expression.Property(
Expression.Constant("foo"),
typeof(string).GetProperty(nameof(string.Length))!
)
);

var actual = ExpressionOnNone.Graft(() => "foo", str => str.Length);
var expected = ExpressionOnNone.Of(() => "foo".Length);
var actual = ExpressionOnNone.Graft(() => "foo", v => v.Length);

Assert.Equivalent(expected, actual);
}

[Fact]
public void Graft1_works_as_expected() {
var actual = ExpressionOn<Cat>.Graft(c => c.Name, str => str.Length);

var expected = Expression.Lambda<Func<Cat, int>>(
Expression.Property(
Expression.Property(
actual.Parameters[0],
typeof(Cat).GetProperty(nameof(Cat.Name))!
),
typeof(string).GetProperty(nameof(string.Length))!
),
actual.Parameters
);
var expected = ExpressionOn<Cat>.Of(a => a.Name.Length);
var actual = ExpressionOn<Cat>.Graft(a => a.Name, v => v.Length);

Assert.Equivalent(expected, actual);
}

[Fact]
public void Graft2_works_as_expected() {
var expected = ExpressionOn<Cat, Cat>.Of((a, b) => a.Name.Length);
var actual = ExpressionOn<Cat, Cat>.Graft((a, b) => a.Name, v => v.Length);

Assert.Equivalent(expected, actual);
}

[Fact]
public void Graft3_works_as_expected() {
var expected = ExpressionOn<Cat, Cat, Cat>.Of((a, b, c) => a.Name.Length);
var actual = ExpressionOn<Cat, Cat, Cat>.Graft((a, b, c) => a.Name, v => v.Length);

Assert.Equivalent(expected, actual);
}

[Fact]
public void Graft4_works_as_expected() {
var expected = ExpressionOn<Cat, Cat, Cat, Cat>.Of((a, b, c, d) => a.Name.Length);
var actual = ExpressionOn<Cat, Cat, Cat, Cat>.Graft((a, b, c, d) => a.Name, v => v.Length);

Assert.Equivalent(expected, actual);
}
Expand Down

0 comments on commit 3f54454

Please sign in to comment.