public void Can_union_with()
        {
            var hashSet = new ObservableHashSet <string> {
                "Palmer", "Carmack"
            };
            var countChanging     = 0;
            var countChanged      = 0;
            var collectionChanged = 0;
            var currentCount      = 2;
            var countChange       = 2;
            var adding            = new[] { "Brendan", "Nate" };

            hashSet.PropertyChanging  += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
            hashSet.PropertyChanged   += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
            hashSet.CollectionChanged += (s, a) =>
            {
                Assert.Equal(NotifyCollectionChangedAction.Replace, a.Action);
                Assert.Empty(a.OldItems);
                Assert.Equal(adding, a.NewItems.OfType <string>().OrderBy(i => i));
                collectionChanged++;
            };

            hashSet.UnionWith(new[] { "Carmack", "Nate", "Brendan" });

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Brendan", "Carmack", "Nate", "Palmer" }, hashSet.OrderBy(i => i));

            hashSet.UnionWith(new[] { "Brendan" });

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Brendan", "Carmack", "Nate", "Palmer" }, hashSet.OrderBy(i => i));
        }
        public void WhenUnionWithAndOtherEqual_ThenDoesNotRaiseCountPropertyChanged() {
            // Arrange
            var target = new ObservableHashSet<int>();

            target.Add(1);
            target.Add(2);
            target.Add(3);
            target.Add(4);

            var other = new int[] { 1, 2, 3, 4 };

            bool wasEventRaised = false;
            target.PropertyChanged += (s, e) => {
                if (e.PropertyName == ObservableHashSet<int>.PropertyNames.Count) {
                    wasEventRaised = true;
                }
            };

            // Act
            target.UnionWith(other);

            // Assert
            Assert.IsFalse(wasEventRaised);
        }
        public void WhenUnionWith_ThenRaisesCollectionChanged() {
            // Arrange
            var target = new ObservableHashSet<int>();

            target.Add(1);
            target.Add(2);
            target.Add(3);
            target.Add(4);

            var other = new int[] { 1, 4, 5, 6 };

            bool wasEventRaised = false;
            target.CollectionChanged += (s, e) => {
                if (e.Action == NotifyCollectionChangedAction.Add &&
                    e.NewItems.Count == 2 &&
                    e.NewItems.Contains(5) &&
                    e.NewItems.Contains(6)) {
                    wasEventRaised = true;
                }
            };

            // Act
            target.UnionWith(other);

            // Assert            
            Assert.IsTrue(wasEventRaised);
        }
        public void WhenUnionWithAndOtherEqual_ThenDoesNotRaiseCollectionChanged() {
            // Arrange
            var target = new ObservableHashSet<int>();

            target.Add(1);
            target.Add(2);
            target.Add(3);
            target.Add(4);

            var other = new int[] { 1, 2, 3, 4 };

            bool wasEventRaised = false;
            target.CollectionChanged += (s, e) => {
                if (e.Action == NotifyCollectionChangedAction.Add) {
                    wasEventRaised = true;
                }
            };

            // Act
            target.UnionWith(other);

            // Assert            
            Assert.IsFalse(wasEventRaised);
        }
예제 #5
0
 public void LoadXml(XElement xml)
 {
     _collection.Clear();
     _collection.UnionWith(xml.Elements().Select(e => e.LoadMetadata()));
 }