Skip to content

Adds Verify support for Expression Trees using ReadableExpressions

License

Notifications You must be signed in to change notification settings

VerifyTests/Verify.ReadableExpressions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.ReadableExpressions

Discussions Build status NuGet Status

Adds Verify support for Expression Trees using ReadableExpressions.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

NuGet

Usage

Enable

[ModuleInitializer]
public static void Init() =>
    VerifyReadableExpressions.Initialize();

snippet source | anchor

Sample Expression

Given a complex Expression:

// Complex calculation with conditional logic
// Equivalent to: p => p.Age < 30
//                     ? p.Salary * 1.15m
//                     : (p.Age < 50
//                         ? p.Salary * 1.08m
//                         : p.Salary * 1.03m)
public static Expression<Func<Person, decimal>> SalaryCalculation()
{
    var param = Expression.Parameter(typeof(Person));
    var ageProperty = Expression.Property(param, nameof(Person.Age));
    var salaryProperty = Expression.Property(param, nameof(Person.Salary));

    // Nested conditional: age-based salary multipliers
    var youngBonus = Expression.Multiply(
        salaryProperty,
        Expression.Constant(1.15m));
    var midBonus = Expression.Multiply(
        salaryProperty,
        Expression.Constant(1.08m));
    var seniorBonus = Expression.Multiply(
        salaryProperty,
        Expression.Constant(1.03m));

    var ageLessThan30 = Expression.LessThan(
        ageProperty,
        Expression.Constant(30));
    var ageLessThan50 = Expression.LessThan(
        ageProperty,
        Expression.Constant(50));

    var innerConditional = Expression.Condition(
        ageLessThan50,
        midBonus,
        seniorBonus);
    var outerConditional = Expression.Condition(
        ageLessThan30,
        youngBonus,
        innerConditional);

    return Expression.Lambda<Func<Person, decimal>>(outerConditional, param);
}

snippet source | anchor

Test

[Fact]
public Task VerifySalaryCalculation()
{
    var expression = ComplexExpressionTrees.SalaryCalculation();
    return Verify(expression);
}

snippet source | anchor

Resulting

person => (person.Age < 30)
  ? person.Salary * 1.15m
  : (person.Age < 50) ? person.Salary * 1.08m : person.Salary * 1.03m

snippet source | anchor

Icon

Tree designed by BnB Studio from The Noun Project.

About

Adds Verify support for Expression Trees using ReadableExpressions

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors 3

  •  
  •  
  •  

Languages