Beispiel #1
0
 private static bool AreComparable(Comparands comparands, Array expectationAsArray)
 {
     return
         (IsArray(comparands.Subject) &&
          HaveSameRank(comparands.Subject, expectationAsArray) &&
          HaveSameDimensions(comparands.Subject, expectationAsArray));
 }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public void RecursivelyAssertEquality(Comparands comparands, IEquivalencyValidationContext context)
        {
            var scope = AssertionScope.Current;

            if (ShouldCompareMembersThisDeep(context.CurrentNode, context.Options, scope))
            {
                UpdateScopeWithReportableContext(scope, comparands, context.CurrentNode);

                if (!context.IsCyclicReference(comparands.Expectation))
                {
                    RunStepsUntilEquivalencyIsProven(comparands, context);
                }
            }
        }
Beispiel #4
0
        public void AssertEquality(Comparands comparands, EquivalencyValidationContext context)
        {
            using var scope = new AssertionScope();

            scope.AssumeSingleCaller();
            scope.AddReportable("configuration", context.Options.ToString());
            scope.BecauseOf(context.Reason);

            RecursivelyAssertEquality(comparands, context);

            if (context.TraceWriter is not null)
            {
                scope.AddReportable("trace", context.TraceWriter.ToString());
            }
        }
Beispiel #5
0
        private void RunStepsUntilEquivalencyIsProven(Comparands comparands, IEquivalencyValidationContext context)
        {
            foreach (IEquivalencyStep step in AssertionOptions.EquivalencyPlan)
            {
                var result = step.Handle(comparands, context, this);
                context.Tracer.WriteLine(_ => $"Step {step.GetType().Name} returned {result}");

                if (result == EquivalencyResult.AssertionCompleted)
                {
                    return;
                }
            }

            throw new NotImplementedException(
                      $"Do not know how to compare {comparands.Subject} and {comparands.Expectation}. Please report an issue through https://www.fluentassertions.com.");
        }
Beispiel #6
0
        private static void UpdateScopeWithReportableContext(AssertionScope scope, Comparands comparands, INode currentNode)
        {
            scope.Context = new Lazy <string>(() => currentNode.Description);

            scope.TrackComparands(comparands.Subject, comparands.Expectation);
        }