private void LooselyMatchAgainst <T>(IList <object> subjects, T expectation, int expectationIndex)
        {
            var results = new AssertionResultSet();

            foreach (int index in Enumerable.Range(0, subjects.Count))
            {
                if (!matchedSubjectIndexes.Contains(index))
                {
                    object subject = subjects[index];

                    using (context.TraceBlock(path => $"Comparing subject at {path}[{index}] with the expectation at {path}[{expectationIndex}]"))
                    {
                        string[] failures = TryToMatch(subject, expectation, expectationIndex);

                        results.AddSet(index, failures);
                        if (results.ContainsSuccessfulSet())
                        {
                            context.TraceSingle(_ => $"It's a match");
                            matchedSubjectIndexes.Add(index);
                            break;
                        }
                        else
                        {
                            context.TraceSingle(_ => $"Contained {failures.Length} failures");
                        }
                    }
                }
            }

            foreach (string failure in results.SelectClosestMatchFor(expectationIndex))
            {
                AssertionScope.Current.AddPreFormattedFailure(failure);
            }
        }
Esempio n. 2
0
        private bool LooselyMatchAgainst <T>(IList <object> subjects, T expectation, int expectationIndex)
        {
            var             results          = new AssertionResultSet();
            int             index            = 0;
            GetTraceMessage getMessage       = path => $"Comparing subject at {path}[{index}] with the expectation at {path}[{expectationIndex}]";
            int             indexToBeRemoved = -1;

            for (var metaIndex = 0; metaIndex < unmatchedSubjectIndexes.Count; metaIndex++)
            {
                index = unmatchedSubjectIndexes[metaIndex];
                object subject = subjects[index];

                using (context.TraceBlock(getMessage))
                {
                    string[] failures = TryToMatch(subject, expectation, expectationIndex);

                    results.AddSet(index, failures);
                    if (results.ContainsSuccessfulSet())
                    {
                        context.TraceSingle(_ => "It's a match");
                        indexToBeRemoved = metaIndex;
                        break;
                    }
                    else
                    {
                        context.TraceSingle(_ => $"Contained {failures.Length} failures");
                    }
                }
            }

            if (indexToBeRemoved != -1)
            {
                unmatchedSubjectIndexes.RemoveAt(indexToBeRemoved);
            }

            foreach (string failure in results.SelectClosestMatchFor(expectationIndex))
            {
                AssertionScope.Current.AddPreFormattedFailure(failure);
            }

            return(indexToBeRemoved != -1);
        }