Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#

Visual Studio Unit Testing Extensions

Rate me:
Please Sign up or sign in to vote.
2.75/5 (4 votes)
23 Dec 2007CPOL6 min read 21.1K   70   10  
An article on creating extensions for unit testing.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace CodeProject.VisualStudio.QualityTools.UnitTestFramework
{
    public sealed class CollectionChangedWatcher
    {
        private INotifyCollectionChanged watchedObject;
        private List<NotifyCollectionChangedEventArgs> events = new List<NotifyCollectionChangedEventArgs>();

        public CollectionChangedWatcher(INotifyCollectionChanged watchedObject)
        {
            this.watchedObject = watchedObject;
            this.watchedObject.CollectionChanged += watchedObject_CollectionChanged;
        }

        public void Reset()
        {
            events.Clear();
        }

        public IEnumerator<NotifyCollectionChangedEventArgs> Find(NotifyCollectionChangedAction action)
        {
            foreach (NotifyCollectionChangedEventArgs e in events)
            {
                if (e.Action.Equals(action))
                {
                    yield return e;
                }
            }
        }

        public IEnumerator<NotifyCollectionChangedEventArgs> Find(NotifyCollectionChangedAction action, ICollection items)
        {
            foreach (NotifyCollectionChangedEventArgs e in events)
            {
                if (e.Action.Equals(action))
                {
                    ICollection eventItems = null;
                    switch (action)
                    {
                        case NotifyCollectionChangedAction.Add:
                            eventItems = e.NewItems;
                            break;

                        case NotifyCollectionChangedAction.Remove:
                            eventItems = e.OldItems;
                            break;

                        default:
                            continue;
                    }
                    if (items.Count != eventItems.Count)
                        continue;

                    int actualOccurrences;
                    int expectedOccurrences;
                    object mismatchedObject;
                    if (Specify.FindMismatchedElement(eventItems, items, out actualOccurrences,
                        out expectedOccurrences, out mismatchedObject))
                    {
                        continue;
                    }

                    yield return e;
                }
            }
        }

        public IEnumerator<NotifyCollectionChangedEventArgs> Find(
            NotifyCollectionChangedAction action, ICollection newItems, ICollection oldItems)
        {
            foreach (NotifyCollectionChangedEventArgs e in events)
            {
                if (e.Action.Equals(action))
                {
                    if (newItems.Count != e.NewItems.Count || oldItems.Count != e.OldItems.Count)
                        continue;

                    int actualOccurrences;
                    int expectedOccurrences;
                    object mismatchedObject;

                    if (Specify.FindMismatchedElement(e.NewItems, newItems, out actualOccurrences,
                        out expectedOccurrences, out mismatchedObject))
                    {
                        continue;
                    }

                    if (Specify.FindMismatchedElement(e.OldItems, oldItems, out actualOccurrences,
                        out expectedOccurrences, out mismatchedObject))
                    {
                        continue;
                    }

                    yield return e;
                }
            }
        }

        private void watchedObject_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            events.Add(e);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Windows developer with 10+ years experience working in the banking industry.

Comments and Discussions