public override void DescribeTo(IDescription description)
        {
            description.AppendText("a dictionary consisting of:");

            var first = true;

            using (description.IndentBy(4))
            {
                foreach (var entryDescriptor in _entryDescriptors)
                {
                    if (!first)
                    {
                        description.AppendText(",");
                    }

                    description.AppendNewLine()
                    .AppendText("an entry where:");

                    using (description.IndentBy(4))
                    {
                        description.AppendNewLine()
                        .AppendText("key: ")
                        .AppendDescriptionOf(entryDescriptor.KeyMatcher)
                        .AppendNewLine()
                        .AppendText("value: ")
                        .AppendDescriptionOf(entryDescriptor.ValueMatcher);
                    }

                    first = false;
                }
            }
        }
Пример #2
0
        public override void DescribeTo(IDescription description)
        {
            var matcherArray = _matcherCollection.ToArray();

            if (matcherArray.Length == 0)
            {
                description.AppendText("an empty list");
                return;
            }

            description.AppendText("a list containing:");

            using (description.IndentBy(4))
            {
                description.AppendNewLine();

                var first = true;

                foreach (var matcher in matcherArray)
                {
                    if (!first)
                    {
                        description.AppendText(",").AppendNewLine();
                    }
                    description.AppendDescriptionOf(matcher);
                    first = false;
                }
            }
        }
Пример #3
0
 private void DescribeMatchers(IDescription description, IEnumerable <IMatcher <T> > matchers, Action <IDescription, IMatcher <T> > matcherRenderer)
 {
     using (description.IndentBy(Indent))
     {
         foreach (var matcher in matchers)
         {
             description.AppendNewLine();
             matcherRenderer(description, matcher);
         }
     }
 }
Пример #4
0
        protected override bool MatchesSafely(IEnumerable <T> collection, IDescription mismatchDescription)
        {
            var collectionArray = collection.ToArray();
            var matcherArray    = _matcherCollection.ToArray();

            for (var i = 0; i < Math.Max(collectionArray.Length, matcherArray.Length); i++)
            {
                if (i >= collectionArray.Length)
                {
                    mismatchDescription.AppendText("was too short (expected to be of length {0}, was {1})", matcherArray.Length, collectionArray.Length);
                    return(false);
                }

                if (i >= matcherArray.Length)
                {
                    mismatchDescription.AppendText("was too long (expected to be of length {0}, was {1})", matcherArray.Length, collectionArray.Length);
                    return(false);
                }

                if (!matcherArray[i].Matches(collectionArray[i]))
                {
                    mismatchDescription.AppendText("was not matched at position {0}:", i);

                    using (mismatchDescription.IndentBy(4))
                    {
                        mismatchDescription.AppendNewLine()
                        .AppendText("expected: ")
                        .AppendDescriptionOf(matcherArray[i])
                        .AppendNewLine()
                        .AppendText("but: ");

                        matcherArray[i].DescribeMismatch(collectionArray[i], mismatchDescription);
                    }

                    return(false);
                }
            }

            return(true);
        }
        protected override bool MatchesSafely(IDictionary <TKey, TValue> dictionary, IDescription mismatchDescription)
        {
            var descriptorList = _entryDescriptors.ToList();
            var entriesList    = dictionary.AsEnumerable().Cast <KeyValuePair <TKey, TValue>?>().ToList();

            var differentEntriesList   = new List <Tuple <KeyValuePair <TKey, TValue>, EntryDescriptor <TKey, TValue> > >();
            var missingDescriptorsList = new List <EntryDescriptor <TKey, TValue> >();

            for (var i = 0; i < descriptorList.Count; i++)
            {
                var descriptor       = descriptorList[i];
                var matchingKeyEntry = entriesList.FirstOrDefault(x => descriptor.KeyMatcher.Matches(x.Value.Key));

                if (matchingKeyEntry == null)
                {
                    missingDescriptorsList.Add(descriptor);
                    continue;
                }

                entriesList.Remove(matchingKeyEntry);

                if (!descriptor.ValueMatcher.Matches(matchingKeyEntry.Value.Value))
                {
                    differentEntriesList.Add(Tuple.Create(matchingKeyEntry.Value, descriptor));
                }
            }

            if (entriesList.Count == 0 && differentEntriesList.Count == 0 && missingDescriptorsList.Count == 0)
            {
                return(true);
            }

            mismatchDescription.AppendText("was a dictionary that:");

            using (mismatchDescription.IndentBy(4))
            {
                var first = true;

                foreach (var differentEntry in differentEntriesList)
                {
                    if (!first)
                    {
                        mismatchDescription.AppendText(",");
                    }
                    mismatchDescription.AppendNewLine();
                    mismatchDescription.AppendText("had a different entry where:");

                    using (mismatchDescription.IndentBy(4))
                    {
                        mismatchDescription.AppendNewLine()
                        .AppendText("key: ")
                        .AppendDescriptionOf(differentEntry.Item2.KeyMatcher)
                        .AppendNewLine()
                        .AppendText("value: ");

                        differentEntry.Item2.ValueMatcher.DescribeMismatch(differentEntry.Item1.Value, mismatchDescription);
                    }

                    first = false;
                }

                foreach (var entryDescriptor in missingDescriptorsList)
                {
                    if (!first)
                    {
                        mismatchDescription.AppendText(",");
                    }

                    mismatchDescription.AppendNewLine();
                    mismatchDescription.AppendText("did not have an entry where:");

                    using (mismatchDescription.IndentBy(4))
                    {
                        mismatchDescription.AppendNewLine()
                        .AppendText("key: ")
                        .AppendDescriptionOf(entryDescriptor.KeyMatcher)
                        .AppendNewLine()
                        .AppendText("value: ")
                        .AppendDescriptionOf(entryDescriptor.ValueMatcher);
                    }

                    first = false;
                }

                foreach (var additionalEntry in entriesList)
                {
                    if (!first)
                    {
                        mismatchDescription.AppendText(",");
                    }

                    mismatchDescription.AppendNewLine();
                    mismatchDescription.AppendText("had an additional entry where:");

                    using (mismatchDescription.IndentBy(4))
                    {
                        mismatchDescription.AppendNewLine()
                        .AppendText("key: ")
                        .AppendValue(additionalEntry.Value.Key)
                        .AppendNewLine()
                        .AppendText("value: ")
                        .AppendValue(additionalEntry.Value.Value);
                    }

                    first = false;
                }

                return(false);
            }
        }