Exemplo n.º 1
0
        /// <summary>
        /// Clears the given Collection.
        /// </summary>
        public void ClearTest(CiccioSet <string> collection)
        {
            INotifyPropertyChanged collectionPropertyChanged = collection;

            collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
            _expectedPropertyChanged = new[]
            {
                new PropertyNameExpected(COUNT),
                //new PropertyNameExpected(ITEMARRAY)
            };

            collection.CollectionChanged += Collection_CollectionChanged;
            ExpectedCollectionChangedFired++;
            ExpectedAction           = NotifyCollectionChangedAction.Reset;
            ExpectedNewItems         = null;
            ExpectedNewStartingIndex = -1;
            ExpectedOldItems         = null;
            ExpectedOldStartingIndex = -1;

            collection.Clear();
            Assert.Equal(0, collection.Count);
            Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);

            foreach (var item in _expectedPropertyChanged)
            {
                Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just cleared the collection");
            }

            collection.CollectionChanged -= Collection_CollectionChanged;
            collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        }
Exemplo n.º 2
0
        public void SerializeDeserialize_Roundtrips(CiccioSet <int> c)
        {
            CiccioSet <int> clone = BinaryFormatterHelpers.Clone(c);

            Assert.NotSame(c, clone);
            Assert.Equal(c, clone);
        }
        public void Clear_INotifyPropertyChangedItems_RemovesPropertyChangedEventHandlers()
        {
            var item1 = new Item();
            var item2 = new Item();
            var list  = new List <Item> {
                item1, item2, null
            };
            var bindingList = new CiccioSet <Item>(list);

            Assert.Equal(1, item1.InvocationList.Length);
            Assert.Equal(1, item2.InvocationList.Length);

            bool calledListChanged = false;

            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                Assert.Equal(ListChangedType.Reset, e.ListChangedType);
                Assert.Equal(-1, e.NewIndex);
            };

            bindingList.Clear();
            Assert.True(calledListChanged);
            Assert.Empty(bindingList);

            Assert.Null(item1.InvocationList);
            Assert.Null(item2.InvocationList);
        }
Exemplo n.º 4
0
        public static void CopyToTest_Negative()
        {
            string[]           anArray    = new string[] { "one", "two", "three", "four" };
            CiccioSet <string> collection = new CiccioSet <string>(anArray);

            int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue };
            foreach (var index in iArrInvalidValues)
            {
                string[] aCopy = new string[collection.Count];
                AssertExtensions.Throws <ArgumentOutOfRangeException>("arrayIndex", "dstIndex", () => collection.CopyTo(aCopy, index));
            }

            int[] iArrLargeValues = new int[] { collection.Count, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 };
            foreach (var index in iArrLargeValues)
            {
                string[] aCopy = new string[collection.Count];
                AssertExtensions.Throws <ArgumentException>(null, null, () => collection.CopyTo(aCopy, index));
            }

            AssertExtensions.Throws <ArgumentNullException>("array", "dest", () => collection.CopyTo(null, 1));

            string[] copy = new string[collection.Count - 1];
            AssertExtensions.Throws <ArgumentException>(null, "", () => collection.CopyTo(copy, 0));

            copy = new string[0];
            AssertExtensions.Throws <ArgumentException>(null, "", () => collection.CopyTo(copy, 0));
        }
Exemplo n.º 5
0
        public static void RemoveTest()
        {
            // trying to remove item in collection.
            string[]           anArray = { "one", "two", "three", "four" };
            CiccioSet <string> col     = new CiccioSet <string>(anArray);
            CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester();

            helper.RemoveItemTest(col, 2, "three", true, hasDuplicates: false);

            // trying to remove item not in collection.
            anArray = new string[] { "one", "two", "three", "four" };
            col     = new CiccioSet <string>(anArray);
            helper  = new CollectionAndPropertyChangedTester();
            helper.RemoveItemTest(col, -1, "three2", false, hasDuplicates: false);

            // removing null
            anArray = new string[] { "one", "two", "three", "four" };
            col     = new CiccioSet <string>(anArray);
            helper  = new CollectionAndPropertyChangedTester();
            helper.RemoveItemTest(col, -1, null, false, hasDuplicates: false);

            //// trying to remove item in collection that has duplicates.
            //anArray = new string[] { "one", "three", "two", "three", "four" };
            //col = new CiccioSet<string>(anArray);
            //helper = new CollectionAndPropertyChangedTester();
            //helper.RemoveItemTest(col, 1, "three", true, hasDuplicates: true);
            //// want to ensure that there is one "three" left in collection and not both were removed.
            //int occurrencesThree = 0;
            //foreach (var item in col)
            //{
            //    if (item.Equals("three"))
            //        occurrencesThree++;
            //}
            //Assert.Equal(1, occurrencesThree);
        }
Exemplo n.º 6
0
        public static void CopyToTest()
        {
            string[]           anArray    = new string[] { "one", "two", "three", "four" };
            CiccioSet <string> collection = new CiccioSet <string>((IEnumerable <string>)anArray);

            string[] aCopy = new string[collection.Count];
            collection.CopyTo(aCopy, 0);
            for (int i = 0; i < anArray.Length; ++i)
            {
                Assert.Equal(anArray[i], aCopy[i]);
            }

            // copy observable collection starting in middle, where array is larger than source.
            aCopy = new string[collection.Count + 2];
            int offsetIndex = 1;

            collection.CopyTo(aCopy, offsetIndex);
            for (int i = 0; i < aCopy.Length; i++)
            {
                string value = aCopy[i];
                if (i == 0)
                {
                    Assert.True(null == value, "Should not have a value since we did not start copying there.");
                }
                else if (i == (aCopy.Length - 1))
                {
                    Assert.True(null == value, "Should not have a value since the collection is shorter than the copy array..");
                }
                else
                {
                    int indexInCollection = i - offsetIndex;
                    Assert.Equal(collection.ToArray()[indexInCollection], aCopy[i]);
                }
            }
        }
Exemplo n.º 7
0
        public static void IEnumerableConstructorTest_Empty()
        {
            var col = new CiccioSet <string>(new string[] { });

            Assert.Equal(0, col.Count);
            Assert.Empty(col);
        }
Exemplo n.º 8
0
        public static void ParameterlessConstructorTest()
        {
            var col = new CiccioSet <string>();

            Assert.Equal(0, col.Count);
            Assert.Empty(col);
        }
Exemplo n.º 9
0
        ///// <summary>
        ///// Given a collection, will move an item from the oldIndex to the newIndex.
        ///// </summary>
        //public void MoveItemTest(CiccioSet<string> collection, int oldIndex, int newIndex)
        //{
        //    INotifyPropertyChanged collectionPropertyChanged = collection;
        //    collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
        //    _expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) };

        //    collection.CollectionChanged += Collection_CollectionChanged;

        //    string itemAtOldIndex = collection[oldIndex];

        //    ExpectedCollectionChangedFired++;
        //    ExpectedAction = NotifyCollectionChangedAction.Move;
        //    ExpectedNewItems = new string[] { itemAtOldIndex };
        //    ExpectedNewStartingIndex = newIndex;
        //    ExpectedOldItems = new string[] { itemAtOldIndex };
        //    ExpectedOldStartingIndex = oldIndex;

        //    int expectedCount = collection.Count;

        //    collection.Move(oldIndex, newIndex);
        //    Assert.Equal(expectedCount, collection.Count);
        //    Assert.Equal(itemAtOldIndex, collection[newIndex]);
        //    Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);

        //    foreach (var item in _expectedPropertyChanged)
        //        Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just moved an item");

        //    collection.CollectionChanged -= Collection_CollectionChanged;
        //    collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        //}

        ///// <summary>
        ///// Will set that new item at the specified index in the given collection.
        ///// </summary>
        //public void ReplaceItemTest(CiccioSet<string> collection, int index, string newItem)
        //{
        //    INotifyPropertyChanged collectionPropertyChanged = collection;
        //    collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
        //    _expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) };

        //    collection.CollectionChanged += Collection_CollectionChanged;

        //    string itemAtOldIndex = collection.ToArray()[index];

        //    ExpectedCollectionChangedFired++;
        //    ExpectedAction = NotifyCollectionChangedAction.Replace;
        //    ExpectedNewItems = new string[] { newItem };
        //    ExpectedNewStartingIndex = index;
        //    ExpectedOldItems = new string[] { itemAtOldIndex };
        //    ExpectedOldStartingIndex = index;

        //    int expectedCount = collection.Count;

        //    collection[index] = newItem;
        //    Assert.Equal(expectedCount, collection.Count);
        //    Assert.Equal(newItem, collection[index]);
        //    Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);

        //    foreach (var item in _expectedPropertyChanged)
        //        Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just replaced an item");

        //    collection.CollectionChanged -= Collection_CollectionChanged;
        //    collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        //}

        /// <summary>
        /// Given a collection, index and item to remove, will try to remove that item
        /// from the index. If the item has duplicates, will verify that only the first
        /// instance was removed.
        /// </summary>
        public void RemoveItemTest(CiccioSet <string> collection, int itemIndex, string itemToRemove, bool isSuccessfulRemove, bool hasDuplicates)
        {
            INotifyPropertyChanged collectionPropertyChanged = collection;

            collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
            _expectedPropertyChanged = new[]
            {
                new PropertyNameExpected(COUNT),
                //new PropertyNameExpected(ITEMARRAY)
            };

            collection.CollectionChanged += Collection_CollectionChanged;

            if (isSuccessfulRemove)
            {
                ExpectedCollectionChangedFired++;
            }

            ExpectedAction           = NotifyCollectionChangedAction.Remove;
            ExpectedNewItems         = null;
            ExpectedNewStartingIndex = -1;
            ExpectedOldItems         = new string[] { itemToRemove };
            ExpectedOldStartingIndex = itemIndex;

            int expectedCount = isSuccessfulRemove ? collection.Count - 1 : collection.Count;

            bool removedItem = collection.Remove(itemToRemove);

            Assert.Equal(expectedCount, collection.Count);
            Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);

            if (isSuccessfulRemove)
            {
                foreach (var item in _expectedPropertyChanged)
                {
                    Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were items removed.");
                }

                Assert.True(removedItem, "Should have been successful in removing the item.");
            }
            else
            {
                foreach (var item in _expectedPropertyChanged)
                {
                    Assert.False(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were no items removed.");
                }

                Assert.False(removedItem, "Should not have been successful in removing the item.");
            }
            if (hasDuplicates)
            {
                return;
            }

            Assert.DoesNotContain(itemToRemove, collection);

            collection.CollectionChanged -= Collection_CollectionChanged;
            collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        }
Exemplo n.º 10
0
        public static void AddTest()
        {
            string[]           anArray = { "one", "two", "three" };
            CiccioSet <string> col     = new CiccioSet <string>(anArray);
            CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester();

            helper.AddOrInsertItemTest(col, "four");
        }
Exemplo n.º 11
0
        public static void ListConstructorTest()
        {
            List <string> collection = new List <string> {
                "one", "two", "three"
            };
            var actual = new CiccioSet <string>(collection);

            Assert.Equal(collection, actual);
        }
Exemplo n.º 12
0
        public void OnDeserialized_MonitorNotInitialized_ExpectSuccess()
        {
            var        observableCollection     = new CiccioSet <int>();
            MethodInfo onDeserializedMethodInfo = observableCollection.GetType().GetMethod("OnDeserialized",
                                                                                           BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

            Assert.NotNull(onDeserializedMethodInfo);
            onDeserializedMethodInfo.Invoke(observableCollection, new object[] { null });
        }
Exemplo n.º 13
0
        public static void DebuggerAttributeTests()
        {
            CiccioSet <int> col = new CiccioSet <int>(new[] { 1, 2, 3, 4 });

            DebuggerAttributes.ValidateDebuggerDisplayReferences(col);
            DebuggerAttributeInfo info         = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(col);
            PropertyInfo          itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute <DebuggerBrowsableAttribute>().State == DebuggerBrowsableState.RootHidden);

            int[] items = itemProperty.GetValue(info.Instance) as int[];
            Assert.Equal(col, items);
        }
        protected override IList NonGenericIListFactory(int count)
        {
            CiccioSet <string> collection = new CiccioSet <string>();
            int seed = 9600;

            while (collection.Count < count)
            {
                object toAdd = CreateT(seed++);
                collection.Add((string)toAdd);
            }
            return(collection);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Will perform an Add or Insert on the given Collection depending on whether the
        /// insertIndex is null or not. If it is null, will Add, otherwise, will Insert.
        /// </summary>
        public void AddOrInsertItemTest(CiccioSet <string> collection, string itemToAdd, int?insertIndex = null)
        {
            INotifyPropertyChanged collectionPropertyChanged = collection;

            collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
            _expectedPropertyChanged = new[]
            {
                new PropertyNameExpected(COUNT),
                //new PropertyNameExpected(ITEMARRAY)
            };

            collection.CollectionChanged += Collection_CollectionChanged;

            ExpectedCollectionChangedFired++;
            ExpectedAction   = NotifyCollectionChangedAction.Add;
            ExpectedNewItems = new string[] { itemToAdd };
            if (insertIndex.HasValue)
            {
                ExpectedNewStartingIndex = insertIndex.Value;
            }
            else
            {
                ExpectedNewStartingIndex = collection.Count;
            }
            ExpectedOldItems         = null;
            ExpectedOldStartingIndex = -1;

            int expectedCount = collection.Count + 1;

            if (insertIndex.HasValue)
            {
                //collection.Insert(insertIndex.Value, itemToAdd);
                //Assert.Equal(itemToAdd, collection[insertIndex.Value]);
            }
            else
            {
                collection.Add(itemToAdd);
                Assert.Equal(itemToAdd, collection.ToArray()[collection.Count - 1]);
            }

            Assert.Equal(expectedCount, collection.Count);
            Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);


            foreach (var item in _expectedPropertyChanged)
            {
                Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just added an item");
            }

            collection.CollectionChanged -= Collection_CollectionChanged;
            collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        }
        public void Ctor_Default()
        {
            var          list         = new CiccioSet <string>();
            IBindingList iBindingList = list;

            Assert.True(iBindingList.AllowEdit);
            Assert.False(iBindingList.AllowNew);
            Assert.True(iBindingList.AllowRemove);
            Assert.Equal(ListSortDirection.Ascending, iBindingList.SortDirection);
            Assert.True(iBindingList.SupportsChangeNotification);
            Assert.False(iBindingList.SupportsSearching);
            Assert.False(iBindingList.SupportsSorting);
            Assert.False(((IRaiseItemChangedEvents)list).RaisesItemChangedEvents);
        }
Exemplo n.º 17
0
        public static void GetEnumeratorTest()
        {
            Guid[]           anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
            CiccioSet <Guid> col     = new CiccioSet <Guid>((IEnumerable <Guid>)anArray);

            int i = 0;
            IEnumerator <Guid> e;

            for (e = col.GetEnumerator(); e.MoveNext(); ++i)
            {
                Assert.Equal(anArray[i], e.Current);
            }
            Assert.Equal(col.Count, i);
            e.Dispose();
        }
        public void Ctor_FixedSizeIList()
        {
            var          array        = new string[10];
            var          bindingList  = new CiccioSet <string>(array);
            IBindingList iBindingList = bindingList;

            Assert.True(iBindingList.AllowEdit);
            Assert.False(iBindingList.AllowNew);
            Assert.True(iBindingList.AllowRemove);
            Assert.False(iBindingList.IsSorted);
            Assert.Equal(ListSortDirection.Ascending, iBindingList.SortDirection);
            Assert.True(iBindingList.SupportsChangeNotification);
            Assert.False(iBindingList.SupportsSearching);
            Assert.False(iBindingList.SupportsSorting);
            Assert.False(((IRaiseItemChangedEvents)bindingList).RaisesItemChangedEvents);
        }
Exemplo n.º 19
0
        public static void ClearTest()
        {
            string[]           anArray = { "one", "two", "three", "four" };
            CiccioSet <string> col     = new CiccioSet <string>(anArray);

            col.Clear();
            Assert.Equal(0, col.Count);
            Assert.Empty(col);

            //AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => col.ToArray()[1]);

            //tests that the collectionChanged events are fired.
            CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester();

            col = new CiccioSet <string>(anArray);
            helper.ClearTest(col);
        }
        public void Clear_Invoke_Success()
        {
            var bindingList = new CiccioSet <object> {
                new object(), new object()
            };

            bool calledListChanged = false;

            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                Assert.Equal(ListChangedType.Reset, e.ListChangedType);
                Assert.Equal(-1, e.NewIndex);
            };

            bindingList.Clear();
            Assert.True(calledListChanged);
            Assert.Empty(bindingList);
        }
Exemplo n.º 21
0
        public static void ContainsTest()
        {
            string[]           anArray          = new string[] { "one", "two", "three", "four" };
            CiccioSet <string> collection       = new CiccioSet <string>((IEnumerable <string>)anArray);
            string             collectionString = "";

            foreach (var item in collection)
            {
                collectionString += item + ", ";
            }

            for (int i = 0; i < collection.Count; ++i)
            {
                Assert.True(collection.Contains(anArray[i]), "ObservableCollection did not contain the item: " + anArray[i] + " Collection: " + collectionString);
            }

            string g = "six";

            Assert.False(collection.Contains(g), "Collection contained an item that should not have been there. guid: " + g + " Collection: " + collectionString);
            Assert.False(collection.Contains(null), "Collection should not have contained null. Collection: " + collectionString);
        }
Exemplo n.º 22
0
        public void Reentrancy_MultipleListeners_Throws()
        {
            bool handler1Called = false;
            bool handler2Called = false;

            var collection = new CiccioSet <int>();

            collection.CollectionChanged += (sender, e) => { handler1Called = true; };
            collection.CollectionChanged += (sender, e) =>
            {
                handler2Called = true;

                // More than one listener; throws.
                Assert.Throws <InvalidOperationException>(() => collection.Add(2));
            };
            collection.Add(1);

            Assert.True(handler1Called);
            Assert.True(handler2Called);
            Assert.Equal(1, collection.Count);
            Assert.Equal(1, collection.ToArray()[0]);
        }
Exemplo n.º 23
0
        public void Reentrancy_SingleListener_DoesNotThrow()
        {
            bool handlerCalled = false;

            var collection = new CiccioSet <int>();

            collection.CollectionChanged += (sender, e) =>
            {
                if (!handlerCalled)
                {
                    handlerCalled = true;

                    // Single listener; does not throw.
                    collection.Add(2);
                }
            };
            collection.Add(1);

            Assert.True(handlerCalled);
            Assert.Equal(2, collection.Count);
            Assert.Equal(1, collection.ToArray()[0]);
            Assert.Equal(2, collection.ToArray()[1]);
        }
        public void Remove_INotifyPropertyChangedItems_RemovesPropertyChangedEventHandlers()
        {
            var item        = new Item();
            var bindingList = new CiccioSet <Item> {
                item
            };

            Assert.Equal(1, item.InvocationList.Length);

            bool calledListChanged = false;

            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                Assert.Equal(ListChangedType.ItemDeleted, e.ListChangedType);
                Assert.Equal(0, e.NewIndex);
            };

            bindingList.Remove(item);
            Assert.True(calledListChanged);
            Assert.Empty(bindingList);
            Assert.Null(item.InvocationList);
        }
        public void Remove_Invoke_CallsListChanged()
        {
            var obj  = new object();
            var list = new List <object> {
                obj
            };
            var bindingList = new CiccioSet <object>(list);

            bool calledListChanged = false;

            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                Assert.Equal(0, e.NewIndex);
                Assert.Equal(ListChangedType.ItemDeleted, e.ListChangedType);

                // The event is raised after the removal.
                Assert.Equal(0, bindingList.Count);
            };
            bindingList.Remove(obj);

            Assert.True(calledListChanged);
        }
        public void SortProperty_Get_ReturnsNull()
        {
            IBindingList bindingList = new CiccioSet <object>();

            Assert.Null(bindingList.SortProperty);
        }
Exemplo n.º 27
0
        public static void IsReadOnlyTest()
        {
            var col = new CiccioSet <Guid>(new[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() });

            Assert.False(((ICollection <Guid>)col).IsReadOnly);
        }
Exemplo n.º 28
0
        public static void IEnumerableConstructorTest(IEnumerable <string> collection)
        {
            var actual = new CiccioSet <string>(collection);

            Assert.Equal(collection, actual);
        }