private static void AssertDictionaryEquivalence(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) { Type expectationType = config.GetExpectationType(context); string methodName = ExpressionExtensions.GetMethodName( () => AssertDictionaryEquivalence<object, object, object, object>(null, null, null, null, null)); Type subjectType = context.Subject.GetType(); Type[] subjectTypeArguments = GetDictionaryTypeArguments(subjectType); Type[] expectationTypeArguments = GetDictionaryTypeArguments(expectationType); Type[] typeArguments = subjectTypeArguments.Concat(expectationTypeArguments).ToArray(); MethodCallExpression assertDictionaryEquivalence = Expression.Call( typeof(GenericDictionaryEquivalencyStep), methodName, typeArguments, Expression.Constant(context), Expression.Constant(parent), Expression.Constant(config), Expression.Constant(context.Subject, GetIDictionaryInterface(subjectType)), Expression.Constant(context.Expectation, GetIDictionaryInterface(expectationType))); Expression.Lambda(assertDictionaryEquivalence).Compile().DynamicInvoke(); }
/// <summary> /// Applies a step as part of the task to compare two objects for structural equality. /// </summary> /// <value> /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions /// have to be executed. Should return <c>false</c> otherwise. /// </value> /// <remarks> /// May throw when preconditions are not met or if it detects mismatching data. /// </remarks> public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) { if (!(AssertSubjectIsMaybe(context.Subject))) { return(true); } if (!(AssertExpectationIsNotNull(context.Expectation, context.Subject))) { return(true); } var expectationEnclosedType = config.GetExpectationType(context).GetGenericArguments().Single(); var subjectEnclosedType = context.Subject.GetType().GetGenericArguments().Single(); // This gets the runtime type. Declard type config? try { HandleMethod.MakeGenericMethod(subjectEnclosedType, expectationEnclosedType) .Invoke(null, new[] { context.Subject, context.Expectation, context, parent }); } catch (TargetInvocationException e) { throw e;//.Unwrap(); } return(true); }
/// <summary> /// Gets a value indicating whether this step can handle the current subject and/or expectation. /// </summary> public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { Type subjectType = config.GetExpectationType(context); return((subjectType?.IsEnum == true) || (context.Expectation?.GetType().IsEnum == true)); }
public IEnumerable <SelectedMemberInfo> SelectMembers(IEnumerable <SelectedMemberInfo> selectedMembers, IMemberInfo context, IEquivalencyAssertionOptions config) { IEnumerable <SelectedMemberInfo> selectedNonPrivateProperties = config.GetExpectationType(context) .GetNonPrivateProperties() .Select(SelectedMemberInfo.Create); return(selectedMembers.Union(selectedNonPrivateProperties)); }
private static bool PreconditionsAreMet(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { Type expectationType = config.GetExpectationType(context); return(AssertImplementsOnlyOneDictionaryInterface(context.Expectation) && AssertExpectationIsNotNull(context.Subject, context.Expectation) && AssertIsCompatiblyTypedDictionary(expectationType, context.Subject) && AssertSameLength(context.Subject, expectationType, context.Expectation)); }
private static void AssertDictionaryEquivalence(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) { Type expectationType = config.GetExpectationType(context); Type subjectType = context.Subject.GetType(); Type[] subjectTypeArguments = GetDictionaryTypeArguments(subjectType); Type[] expectationTypeArguments = GetDictionaryTypeArguments(expectationType); Type[] typeArguments = subjectTypeArguments.Concat(expectationTypeArguments).ToArray(); AssertDictionaryEquivalenceMethod.MakeGenericMethod(typeArguments).Invoke(null, new[] { context, parent, config, context.Subject, context.Expectation }); }
/// <summary> /// Applies a step as part of the task to compare two objects for structural equality. /// </summary> /// <value> /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions /// have to be executed. Should return <c>false</c> otherwise. /// </value> /// <remarks> /// May throw when preconditions are not met or if it detects mismatching data. /// </remarks> public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) { Type expectedType = config.GetExpectationType(context); var interfaceTypes = GetIEnumerableInterfaces(expectedType) .Select(type => "IEnumerable<" + type.GetGenericArguments().Single() + ">") .ToList(); AssertionScope.Current .ForCondition(interfaceTypes.Count == 1) .FailWith("{context:Expectation} implements {0}, so cannot determine which one " + "to use for asserting the equivalency of the collection. ", interfaceTypes); if (AssertSubjectIsCollection(context.Expectation, context.Subject)) { var validator = new EnumerableEquivalencyValidator(parent, context) { Recursive = context.IsRoot || config.IsRecursive, OrderingRules = config.OrderingRules }; Type typeOfEnumeration = GetTypeOfEnumeration(expectedType); MethodCallExpression expectationAsArray = ToArray(context.Expectation, typeOfEnumeration); ConstantExpression subjectAsArray = Expression.Constant(EnumerableEquivalencyStep.ToArray(context.Subject)); MethodCallExpression executeExpression = Expression.Call( Expression.Constant(validator), ExpressionExtensions.GetMethodName(() => validator.Execute <object>(null, null)), new[] { typeOfEnumeration }, subjectAsArray, expectationAsArray); try { Expression.Lambda(executeExpression).Compile().DynamicInvoke(); } catch (TargetInvocationException e) { throw e.Unwrap(); } } return(true); }
/// <summary> /// Gets a value indicating whether this step can handle the current subject and/or expectation. /// </summary> public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { Type type = config.GetExpectationType(context); EqualityStrategy strategy = config.GetEqualityStrategy(type); bool canHandle = (strategy == EqualityStrategy.Equals) || (strategy == EqualityStrategy.ForceEquals); if (canHandle) { context.TraceSingle(path => { string strategyName = (strategy == EqualityStrategy.Equals) ? "Equals must be used" : "object overrides Equals"; return($"Treating {path} as a value type because {strategyName}."); }); } return(canHandle); }
/// <summary> /// Applies a step as part of the task to compare two objects for structural equality. /// </summary> /// <value> /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions /// have to be executed. Should return <c>false</c> otherwise. /// </value> /// <remarks> /// May throw when preconditions are not met or if it detects mismatching data. /// </remarks> public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) { Type expectedType = config.GetExpectationType(context); Type[] interfaceTypes = GetIEnumerableInterfaces(expectedType); AssertionScope.Current .ForCondition(interfaceTypes.Length == 1) .FailWith(() => new FailReason("{context:Expectation} implements {0}, so cannot determine which one " + "to use for asserting the equivalency of the collection. ", interfaceTypes.Select(type => "IEnumerable<" + type.GetGenericArguments().Single() + ">"))); if (AssertSubjectIsCollection(context.Subject)) { var validator = new EnumerableEquivalencyValidator(parent, context) { Recursive = context.IsRoot || config.IsRecursive, OrderingRules = config.OrderingRules }; Type typeOfEnumeration = GetTypeOfEnumeration(expectedType); var subjectAsArray = EnumerableEquivalencyStep.ToArray(context.Subject); try { HandleMethod.MakeGenericMethod(typeOfEnumeration).Invoke(null, new[] { validator, subjectAsArray, context.Expectation }); } catch (TargetInvocationException e) { throw e.Unwrap(); } } return(true); }
public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { Type expectationType = config.GetExpectationType(context); return(context.Expectation != null && GetIDictionaryInterfaces(expectationType).Any()); }
/// <summary> /// Gets a value indicating whether this step can handle the verificationScope subject and/or expectation. /// </summary> public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { var expectationType = config.GetExpectationType(context); return((context.Expectation != null) && IsGenericCollection(expectationType)); }
/// <summary> /// Gets a value indicating whether this step can handle the current subject and/or expectation. /// </summary> public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { Type expectationType = config.GetExpectationType(context); return((expectationType != null) && (expectationType == typeof(string))); }
public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { return(config.GetExpectationType(context) == typeof(T)); }
public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { var subjectType = config.GetExpectationType(context); return(subjectType != null && subjectType == _enumType && context.Expectation is string); }
/// <summary> /// Gets a value indicating whether this step can handle the verificationScope subject and/or expectation. /// </summary> public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { Type subjectType = config.GetExpectationType(context); return(IsCollection(subjectType)); }
/// <summary> /// Gets a value indicating whether this step can handle the current subject and/or expectation. /// </summary> public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { return(typeof(IDictionary).IsAssignableFrom(config.GetExpectationType(context))); }
public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) { var expectationType = config.GetExpectationType(context);; return(IsMaybe(expectationType)); }