PropertiesComparerConfiguration<T>
Generic version of PropertiesComparerConfiguration to allow
specifiying properties using expression syntax instead of strings.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class PropertiesComparerConfiguration<[System.Runtime.CompilerServices.Nullable(2)] T> : PropertiesComparerConfiguration
{
public PropertiesComparerConfiguration<T> AllowDifferentTypes()
{
base.AllowComparingDifferentTypes = true;
return this;
}
public PropertiesComparerConfiguration<T> CompareOnlyCommonProperties()
{
base.OnlyCompareCommonProperties = true;
return AllowDifferentTypes();
}
public PropertiesComparerConfiguration<T> Using([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
1,
2
})] params Expression<Func<T, object>>[] properties)
{
return this.Using<T>(properties);
}
public PropertiesComparerConfiguration<T> Using<[System.Runtime.CompilerServices.Nullable(2)] TFrom>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
1,
2
})] params Expression<Func<TFrom, object>>[] properties)
{
if (base.PropertyNamesToUseForType == null) {
Dictionary<Type, HashSet<string>> dictionary2 = base.PropertyNamesToUseForType = new Dictionary<Type, HashSet<string>>();
}
base.PropertyNamesToUseForType[typeof(TFrom)] = new HashSet<string>(Enumerable.Select<Expression<Func<TFrom, object>>, string>((IEnumerable<Expression<Func<TFrom, object>>>)properties, (Func<Expression<Func<TFrom, object>>, string>)GetNameFromExpression));
return this.AllowDifferentTypes();
}
public PropertiesComparerConfiguration<T> Excluding([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
1,
2
})] params Expression<Func<T, object>>[] properties)
{
return this.Excluding<T>(properties);
}
public PropertiesComparerConfiguration<T> Excluding<[System.Runtime.CompilerServices.Nullable(2)] TFrom>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
1,
2
})] params Expression<Func<TFrom, object>>[] properties)
{
if (base.PropertyNamesToExcludeForType == null) {
Dictionary<Type, HashSet<string>> dictionary2 = base.PropertyNamesToExcludeForType = new Dictionary<Type, HashSet<string>>();
}
base.PropertyNamesToExcludeForType[typeof(TFrom)] = new HashSet<string>(Enumerable.Select<Expression<Func<TFrom, object>>, string>((IEnumerable<Expression<Func<TFrom, object>>>)properties, (Func<Expression<Func<TFrom, object>>, string>)GetNameFromExpression));
return this.AllowDifferentTypes();
}
public PropertiesComparerConfiguration<T> Map<[System.Runtime.CompilerServices.Nullable(2)] TTo>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
2
})] Expression<Func<T, object>> from, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
2
})] Expression<Func<TTo, object>> to)
{
return this.Map<T, TTo>(from, to);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public PropertiesComparerConfiguration<T> Map<TFrom, TTo>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
2
})] Expression<Func<TFrom, object>> from, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
2
})] Expression<Func<TTo, object>> to)
{
if (base.PropertyNameMapForType == null) {
Dictionary<Type, Dictionary<string, string>> dictionary2 = base.PropertyNameMapForType = new Dictionary<Type, Dictionary<string, string>>();
}
if (!base.PropertyNameMapForType.TryGetValue(typeof(TFrom), out Dictionary<string, string> value)) {
value = new Dictionary<string, string>();
base.PropertyNameMapForType.Add(typeof(TFrom), value);
}
value.Add(GetNameFromExpression(from), GetNameFromExpression(to));
return this.AllowDifferentTypes();
}
public PropertiesComparerConfiguration<T> Map<[System.Runtime.CompilerServices.Nullable(2)] TTo>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
1,
1,
2,
1,
1,
1,
2
})] params(Expression<Func<T, object>> From, Expression<Func<TTo, object>> To)[] properties)
{
return this.Map<T, TTo>(properties);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public PropertiesComparerConfiguration<T> Map<TFrom, TTo>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
1,
1,
2,
1,
1,
1,
2
})] params(Expression<Func<TFrom, object>> From, Expression<Func<TTo, object>> To)[] properties)
{
if (base.PropertyNameMapForType == null) {
Dictionary<Type, Dictionary<string, string>> dictionary2 = base.PropertyNameMapForType = new Dictionary<Type, Dictionary<string, string>>();
}
base.PropertyNameMapForType[typeof(TFrom)] = Enumerable.ToDictionary<(Expression<Func<TFrom, object>>, Expression<Func<TTo, object>>), string, string>((IEnumerable<(Expression<Func<TFrom, object>>, Expression<Func<TTo, object>>)>)properties, (Func<(Expression<Func<TFrom, object>>, Expression<Func<TTo, object>>), string>)(((Expression<Func<TFrom, object>> From, Expression<Func<TTo, object>> To) x) => GetNameFromExpression(x.From)), (Func<(Expression<Func<TFrom, object>>, Expression<Func<TTo, object>>), string>)(((Expression<Func<TFrom, object>> From, Expression<Func<TTo, object>> To) x) => GetNameFromExpression(x.To)));
return this.AllowDifferentTypes();
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public PropertiesComparerConfiguration<T> Map<TFrom>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
2
})] Expression<Func<TFrom, object>> from, object value)
{
if (base.PropertyNameToValueMapForType == null) {
Dictionary<Type, Dictionary<string, object>> dictionary2 = base.PropertyNameToValueMapForType = new Dictionary<Type, Dictionary<string, object>>();
}
if (!base.PropertyNameToValueMapForType.TryGetValue(typeof(TFrom), out Dictionary<string, object> value2)) {
value2 = new Dictionary<string, object>();
base.PropertyNameToValueMapForType.Add(typeof(TFrom), value2);
}
value2.Add(GetNameFromExpression(from), value);
return this.AllowDifferentTypes();
}
public PropertiesComparerConfiguration<T> Within(object amount)
{
SetTolerance(amount);
return this;
}
private static string GetNameFromExpression<[System.Runtime.CompilerServices.Nullable(2)] TExpression>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
2
})] Expression<Func<TExpression, object>> expression)
{
Expression expression2 = expression.Body;
UnaryExpression unaryExpression = expression2 as UnaryExpression;
if (unaryExpression != null && unaryExpression.NodeType == ExpressionType.Convert && unaryExpression.Type == typeof(object))
expression2 = unaryExpression.Operand;
MemberExpression memberExpression = expression2 as MemberExpression;
if (memberExpression != null)
return memberExpression.Member.Name;
throw new ArgumentException("Expression must be a member expression", "expression");
}
}
}