public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator) { Array expectationAsArray = comparands.Expectation as Array; if (expectationAsArray is null || expectationAsArray?.Rank == 1) { return(EquivalencyResult.ContinueWithNext); } if (AreComparable(comparands, expectationAsArray)) { if (expectationAsArray.Length == 0) { return(EquivalencyResult.AssertionCompleted); } Digit digit = BuildDigitsRepresentingAllIndices(expectationAsArray); do { int[] indices = digit.GetIndices(); object subject = ((Array)comparands.Subject).GetValue(indices); string listOfIndices = string.Join(",", indices); object expectation = expectationAsArray.GetValue(indices); IEquivalencyValidationContext itemContext = context.AsCollectionItem <object>(listOfIndices); nestedValidator.RecursivelyAssertEquality(new Comparands(subject, expectation, typeof(object)), itemContext); }while (digit.Increment()); } return(EquivalencyResult.AssertionCompleted); }
private string[] TryToMatch <T>(object subject, T expectation, int expectationIndex) { using var scope = new AssertionScope(); parent.RecursivelyAssertEquality(new Comparands(subject, expectation, typeof(T)), context.AsCollectionItem <T>(expectationIndex)); return(scope.Discard()); }
private static void MatchRowsByIndexAndCompare(IEquivalencyValidationContext context, IEquivalencyValidator parent, DataRowCollection subject, DataRowCollection expectation) { for (int index = 0; index < expectation.Count; index++) { IEquivalencyValidationContext nestedContext = context.AsCollectionItem <DataRow>(index); parent.RecursivelyAssertEquality(new Comparands(subject[index], expectation[index], typeof(DataRow)), nestedContext); } }
private static void CompareFieldValue(IEquivalencyValidationContext context, IEquivalencyValidator parent, DataRow subject, DataRow expectation, DataColumn subjectColumn, DataRowVersion subjectVersion, DataColumn expectationColumn, DataRowVersion expectationVersion) { IEquivalencyValidationContext nestedContext = context.AsCollectionItem <object>( subjectVersion == DataRowVersion.Current ? subjectColumn.ColumnName : $"{subjectColumn.ColumnName}, DataRowVersion.Original"); if (nestedContext is not null) { parent.RecursivelyAssertEquality( new Comparands(subject[subjectColumn, subjectVersion], expectation[expectationColumn, expectationVersion], typeof(object)), nestedContext); } }
protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator) { if (comparands.Subject is not ConstraintCollection) { AssertionScope.Current .FailWith("Expected a value of type ConstraintCollection at {context:Constraints}, but found {0}", comparands.Subject.GetType()); } else { var subject = (ConstraintCollection)comparands.Subject; var expectation = (ConstraintCollection)comparands.Expectation; var subjectConstraints = subject.Cast <Constraint>().ToDictionary(constraint => constraint.ConstraintName); var expectationConstraints = expectation.Cast <Constraint>().ToDictionary(constraint => constraint.ConstraintName); IEnumerable <string> constraintNames = subjectConstraints.Keys.Union(expectationConstraints.Keys); foreach (var constraintName in constraintNames) { AssertionScope.Current .ForCondition(subjectConstraints.TryGetValue(constraintName, out Constraint subjectConstraint)) .FailWith("Expected constraint named {0} in {context:Constraints collection}{reason}, but did not find one", constraintName); AssertionScope.Current .ForCondition(expectationConstraints.TryGetValue(constraintName, out Constraint expectationConstraint)) .FailWith("Found unexpected constraint named {0} in {context:Constraints collection}", constraintName); if ((subjectConstraint is not null) && (expectationConstraint is not null)) { Comparands newComparands = new() { Subject = subjectConstraint, Expectation = expectationConstraint, CompileTimeType = typeof(Constraint) }; IEquivalencyValidationContext nestedContext = context.AsCollectionItem <Constraint>(constraintName); nestedValidator.RecursivelyAssertEquality(newComparands, nestedContext); } } } return(EquivalencyResult.AssertionCompleted); } }
private static void CompareTable(IEquivalencyValidationContext context, IEquivalencyValidator parent, DataSet subject, DataSet expectation, string tableName) { DataTable expectationTable = expectation.Tables[tableName]; DataTable subjectTable = subject.Tables[tableName]; bool success = AssertionScope.Current .ForCondition(subjectTable is not null) .FailWith("Expected {context:DataSet} to contain table '{0}'{reason}, but did not find it", tableName) .Then .ForCondition(expectationTable is not null) .FailWith("Found unexpected table '{0}' in DataSet", tableName); if (success) { IEquivalencyValidationContext nestedContext = context.AsCollectionItem <DataTable>(tableName); parent.RecursivelyAssertEquality(new Comparands(subjectTable, expectationTable, typeof(DataTable)), nestedContext); } }
public static IEquivalencyValidationContext AsCollectionItem <TItem>(this IEquivalencyValidationContext context, int index) => context.AsCollectionItem <TItem>(index.ToString(CultureInfo.InvariantCulture));