Пример #1
0
        public void DictionaryChanges()
        {
            var perfectNumbers = new ObservableDictionary <int, int>(Enumerable.Range(1, 10).ToDictionary(i => i, i => i * i));
            var values         = new BlockingCollection <int>();

            using (var expr = ActiveExpression.Create(p1 => p1[5], perfectNumbers))
            {
                var disconnect = expr.OnPropertyChanged(ae => ae.Value, value =>
                {
                    values.Add(value);
                });
                values.Add(expr.Value);
                perfectNumbers.Add(11, 11 * 11);
                perfectNumbers.AddRange(Enumerable.Range(12, 3).ToDictionary(i => i, i => i * i));
                perfectNumbers.Remove(11);
                perfectNumbers.RemoveRange(Enumerable.Range(12, 3));
                perfectNumbers.Remove(5);
                perfectNumbers.Add(5, 30);
                perfectNumbers[5] = 25;
                perfectNumbers.RemoveRange(Enumerable.Range(4, 3));
                perfectNumbers.AddRange(Enumerable.Range(4, 3).ToDictionary(i => i, i => i * i));
                perfectNumbers.Clear();
                disconnect();
            }
            Assert.IsTrue(new int[] { 25, 0, 30, 25, 0, 25, 0 }.SequenceEqual(values));
        }
        public void OperationCollapseTest()
        {
            var argsList   = new List <DictionaryChangedEventArgs <int, string> >();
            var dictionary = new ObservableDictionary <int, string>();

            dictionary.DictionaryChanged += (sender, e) => argsList.Add(e);

            dictionary.BeginBatch();
            dictionary.Add(1, "1");                      //  \
            dictionary.Add(2, "2");                      //   > collapse into [0] add
            dictionary.Add(3, "3");                      //  /
            dictionary.Remove(1);                        //  \
            dictionary.Remove(2);                        //   > collapse into [1] remove
            dictionary.Remove(3);                        //  /
            dictionary.AddRange(MakeTestPairs(1, 2, 3)); //  \
            dictionary.Add(4, "4");                      //   > collapse into [2] add
            dictionary.AddRange(MakeTestPairs(5, 6, 7)); //  /
            dictionary.Remove(7);                        //  \
            dictionary.Remove(6);                        //   > collapse into [3] reset
            dictionary.Clear();                          //  /
            dictionary[1] = "1";                         //  \
            dictionary[2] = "2";                         //   > collapse into [4] add
            dictionary[3] = "3";                         //  /
            dictionary[1] = "1234";                      // no collapse - [5] replace
            dictionary.Remove(1);                        //  \ 
            dictionary.Clear();                          //  / collapse into [6] reset
            Assert.AreEqual(0, argsList.Count);
            dictionary.EndBatch();

            Assert.AreEqual(7, argsList.Count);

            Assert.AreEqual(NotifyCollectionChangedAction.Add, argsList[0].Action);
            Assert.IsTrue(Enumerable.SequenceEqual(MakeTestPairs(1, 2, 3), argsList[0].NewItems));

            Assert.AreEqual(NotifyCollectionChangedAction.Remove, argsList[1].Action);
            Assert.IsTrue(Enumerable.SequenceEqual(MakeTestPairs(1, 2, 3), argsList[1].OldItems));

            Assert.AreEqual(NotifyCollectionChangedAction.Add, argsList[2].Action);
            Assert.IsTrue(Enumerable.SequenceEqual(MakeTestPairs(1, 2, 3, 4, 5, 6, 7), argsList[2].NewItems));

            Assert.AreEqual(NotifyCollectionChangedAction.Reset, argsList[3].Action);

            Assert.AreEqual(NotifyCollectionChangedAction.Add, argsList[4].Action);
            Assert.IsTrue(Enumerable.SequenceEqual(MakeTestPairs(1, 2, 3), argsList[4].NewItems));

            Assert.AreEqual(NotifyCollectionChangedAction.Replace, argsList[5].Action);

            Assert.AreEqual(NotifyCollectionChangedAction.Reset, argsList[6].Action);
        }
Пример #3
0
        public void ExpressionlessSourceManipulation()
        {
            var numbers = new ObservableDictionary <string, decimal>();

            using (var aggregate = numbers.ActiveAverage())
            {
                Assert.IsNotNull(aggregate.OperationFault);
                Assert.AreEqual(0, aggregate.Value);
                numbers.Add("1", 1m);
                Assert.IsNull(aggregate.OperationFault);
                Assert.AreEqual(1m, aggregate.Value);
                numbers.AddRange(System.Linq.Enumerable.Range(2, 3).ToDictionary(i => i.ToString(), i => Convert.ToDecimal(i)));
                Assert.AreEqual(2.5m, aggregate.Value);
                numbers.RemoveRange(new string[] { "1", "2" });
                Assert.AreEqual(3.5m, aggregate.Value);
                numbers.Remove("3");
                Assert.AreEqual(4m, aggregate.Value);
                numbers.Remove("4");
                Assert.IsNotNull(aggregate.OperationFault);
                Assert.AreEqual(0m, aggregate.Value);
                numbers.Reset(System.Linq.Enumerable.Range(2, 3).ToDictionary(i => i.ToString(), i => Convert.ToDecimal(i)));
                Assert.IsNull(aggregate.OperationFault);
                Assert.AreEqual(3m, aggregate.Value);
            }
        }
        public void BeginEndBulkOperationTest()
        {
            DictionaryChangedEventArgs <int, string> args = null;

            var dictionary = new ObservableDictionary <int, string>();

            dictionary.DictionaryChanged += (sender, e) => args = e;

            dictionary.BeginBatch();
            dictionary.Add(1, "1");
            Assert.IsNull(args);
            dictionary.Add(2, "2");
            Assert.IsNull(args);
            dictionary.EndBatch();
            Assert.IsNotNull(args);

            args = null;
            dictionary.BeginBatch();
            dictionary.AddRange(MakeTestPairs(4, 5, 6));
            Assert.IsNull(args);
            dictionary.Add(10, "10");
            Assert.IsNull(args);
            dictionary.Remove(4);
            Assert.IsNull(args);
            dictionary[4] = "44";
            Assert.IsNull(args);
            dictionary[44] = "44";
            Assert.IsNull(args);
            dictionary.Clear();
            Assert.IsNull(args);
            dictionary.EndBatch();
            Assert.IsNotNull(args);
        }
        public void AddRangeTest()
        {
            DictionaryChangedEventArgs <int, string> args = null;

            var dictionary = new ObservableDictionary <int, string>();

            dictionary.DictionaryChanged += (sender, e) => args = e;

            dictionary.Add(1, "1");
            Assert.IsNotNull(args);
            Assert.AreEqual(NotifyCollectionChangedAction.Add, args.Action);
            Assert.AreEqual(1, args.NewItems.Count);
            Assert.AreEqual(1, args.NewItems[0].Key);
            Assert.AreEqual("1", args.NewItems[0].Value);

            args = null;
            dictionary.AddRange(MakeTestPairs(2, 3, 4, 5));
            Assert.IsNotNull(args);
            Assert.AreEqual(NotifyCollectionChangedAction.Add, args.Action);
            Assert.AreEqual(4, args.NewItems.Count);
            Assert.AreEqual(2, args.NewItems[0].Key);
            Assert.AreEqual("2", args.NewItems[0].Value);
            Assert.AreEqual(5, args.NewItems[3].Key);
            Assert.AreEqual("5", args.NewItems[3].Value);
            Assert.AreEqual(5, dictionary.Count);

            args = null;
            dictionary.AddRange(MakeTestPairs(6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
            Assert.IsNotNull(args);
            Assert.AreEqual(NotifyCollectionChangedAction.Add, args.Action);
            Assert.AreEqual(10, args.NewItems.Count);
            Assert.AreEqual(6, args.NewItems[0].Key);
            Assert.AreEqual("6", args.NewItems[0].Value);
            Assert.AreEqual(15, args.NewItems[9].Key);
            Assert.AreEqual("15", args.NewItems[9].Value);
            Assert.AreEqual(15, dictionary.Count);
        }
        public void AddRangeTest()
        {
            var dic = new ObservableDictionary <int, string>();
            List <NotifyCollectionChangedEventArgs> collectionsEvents = new List <NotifyCollectionChangedEventArgs>();
            List <PropertyChangedEventArgs>         propertyEvents    = new List <PropertyChangedEventArgs>();

            ((INotifyCollectionChanged)dic).CollectionChanged += (s, e) => collectionsEvents.Add(e);
            ((INotifyPropertyChanged)dic).PropertyChanged     += (s, e) => propertyEvents.Add(e);

            dic.AddRange(new Dictionary <int, string>()
            {
                { 1, "1" }, { 2, "2" }
            });

            collectionsEvents[0].Action.Should().Be(NotifyCollectionChangedAction.Add);
            propertyEvents.First(e => e.PropertyName == "Count").Should().NotBeNull();
            dic.Count.Should().Be(2);
        }
Пример #7
0
        public void ExpressionlessSourceManipulation()
        {
            var numbers = new ObservableDictionary <int, int>();

            using var aggregate = numbers.ActiveMin();
            Assert.IsNotNull(aggregate.OperationFault);
            Assert.AreEqual(0, aggregate.Value);
            numbers.Add(1, 1);
            Assert.IsNull(aggregate.OperationFault);
            Assert.AreEqual(1, aggregate.Value);
            numbers.AddRange(Enumerable.Range(2, 3).Select(i => new KeyValuePair <int, int>(i, i)));
            Assert.AreEqual(1, aggregate.Value);
            numbers.RemoveRange(new int[] { 1, 2 });
            Assert.AreEqual(3, aggregate.Value);
            numbers.Remove(3);
            Assert.AreEqual(4, aggregate.Value);
            numbers.Reset(Enumerable.Range(1, 3).ToDictionary(i => i));
            Assert.AreEqual(1, aggregate.Value);
        }
Пример #8
0
        public void ExpressionlessSourceManipulation()
        {
            var numbers = new ObservableDictionary <int, int>();

            using var aggregate = numbers.ActiveSum();
            Assert.IsNull(aggregate.OperationFault);
            Assert.AreEqual(0, aggregate.Value);
            numbers.Add(1, 1);
            Assert.IsNull(aggregate.OperationFault);
            Assert.AreEqual(1, aggregate.Value);
            numbers.AddRange(System.Linq.Enumerable.Range(2, 3).ToDictionary(i => i));
            Assert.AreEqual(10, aggregate.Value);
            numbers.RemoveRange(new int[] { 1, 2 });
            Assert.AreEqual(7, aggregate.Value);
            numbers.Remove(3);
            Assert.AreEqual(4, aggregate.Value);
            numbers.Reset(System.Linq.Enumerable.Range(1, 3).ToDictionary(i => i));
            Assert.AreEqual(6, aggregate.Value);
        }
Пример #9
0
 public void Load()
 {
     _elements.AddRange(DataManager.DeserializeDataFromFile <ObservableDictionary <Guid, IBaseObject> >(SceneConfiguration.Settings.ObjectsList));
 }
        public void AddRangeRaisesPropertyChangeEventForItemIndexerAndCount()
        {
            // given
            using (var observableDictionary = new ObservableDictionary<int, string>())
            {
                observableDictionary.MonitorEvents();

                var items = new List<KeyValuePair<int, string>>()
                {
                    new KeyValuePair<int, string>(1, "One"),
                    new KeyValuePair<int, string>(2, "Two"),
                    new KeyValuePair<int, string>(3, "Three"),
                };

                // when
                observableDictionary.AddRange(items);

                // then
                observableDictionary
                    .ShouldRaise(nameof(observableDictionary.PropertyChanged))
                    .WithSender(observableDictionary)
                    .WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "Item[]");

                observableDictionary
                    .ShouldRaise(nameof(observableDictionary.PropertyChanged))
                    .WithSender(observableDictionary)
                    .WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == nameof(observableDictionary.Count));
            }
        }
        public void AddRangeNotifiesCountChangeForEveryAddedItemIfAddedItemsAreLessThanThreshold()
        {
            // given
            var testScheduler = new TestScheduler();
            var countChangesObserver = testScheduler.CreateObserver<int>();

            int itemsAmountToAdd = 100;
            using (var observableDictionary = new ObservableDictionary<int, string>())
            {
                observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = itemsAmountToAdd + 1;
                observableDictionary.CountChanges.Subscribe(countChangesObserver);

                var items = Enumerable.Range(0, itemsAmountToAdd).ToDictionary(i => i, i => i.ToString());

                // when
                observableDictionary.AddRange(items);

                // then
                countChangesObserver.Messages.Count.Should().Be(itemsAmountToAdd);
            }
        }
        public void AddRangeRaisesPropertyChangeEventForItemIndexerdAndCountOnlyOnceWhenThresholdAmountWhenItemChangesAreNotifiedAsResetIsSetAccordingly()
        {
            // given
            using (var observableDictionary = new ObservableDictionary<int, string>())
            {
                observableDictionary.ThresholdAmountWhenChangesAreNotifiedAsReset = 0;
                observableDictionary.MonitorEvents();

                var items = new List<KeyValuePair<int, string>>()
                {
                    new KeyValuePair<int, string>(1, "One"),
                    new KeyValuePair<int, string>(2, "Two"),
                    new KeyValuePair<int, string>(3, "Three"),
                };

                // when
                observableDictionary.AddRange(items);

                // then
                observableDictionary
                    .ShouldRaise(nameof(observableDictionary.PropertyChanged))
                    .WithSender(observableDictionary)
                    .WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "Item[]")
                    .Should().HaveCount(1); // ToDo: re-enable test if/when https://github.com/dennisdoomen/fluentassertions/issues/337 has been fixed

                observableDictionary
                    .ShouldRaise(nameof(observableDictionary.PropertyChanged))
                    .WithSender(observableDictionary)
                    .WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == nameof(observableDictionary.Count))
                    .Should().HaveCount(1); // ToDo: re-enable test if/when https://github.com/dennisdoomen/fluentassertions/issues/337 has been fixed
            }
        }
        public void AddRangeOfKeyValuePairsThrowsOnNullItems()
        {
            // given
            using (var observableDictionary = new ObservableDictionary<int, string>())
            {
                // when
                Action action = () => observableDictionary.AddRange(null);

                // then
                action
                    .ShouldThrow<ArgumentNullException>()
                    .WithMessage("Value cannot be null.\r\nParameter name: items");

                observableDictionary.Count.Should().Be(0);
            }
        }
        public void AddRangeOfKeyValuePairsThrowsOnNonExistingKeys()
        {
            // given
            var keyValuePairs = Enumerable.Range(0, 2)
                .Select(i => new KeyValuePair<int, string>(i, $"#{i}"))
                .ToList();

            using (var observableDictionary = new ObservableDictionary<int, string>(keyValuePairs))
            {
                // when
                Action invalidRemoveRangeForNonExistingKey = ()
                    => observableDictionary.AddRange(
                        new List<KeyValuePair<int, string>>
                        {
                            new KeyValuePair<int, string>(0, "#0"),
                            new KeyValuePair<int, string>(1, "One"),
                            new KeyValuePair<int, string>(2, "Two")
                        });

                // then
                invalidRemoveRangeForNonExistingKey
                    .ShouldThrow<ArgumentOutOfRangeException>()
                    .WithMessage("The following key(s) are already in this dictionary and cannot be added to it: 0, 1\r\nParameter name: items");

                observableDictionary.Count.Should().Be(3);

                observableDictionary.Should().Contain(0, "#0");
                observableDictionary.Should().Contain(1, "#1");
                observableDictionary.Should().Contain(2, "Two");
            }
        }
        public void AddRangeOfKeyValuePairsAddsItems(int amountOfItemsToAdd)
        {
            // given
            var keyValuePairs = Enumerable.Range(0, amountOfItemsToAdd)
                .Select(i => new KeyValuePair<int, string>(i, $"#{i}"))
                .ToList();

            using (var observableDictionary = new ObservableDictionary<int, string>())
            {
                // when
                observableDictionary.AddRange(keyValuePairs);

                // then check whether all items have been accounted for
                observableDictionary.Count.Should().Be(amountOfItemsToAdd);

                foreach (var keyValuePair in keyValuePairs)
                {
                    observableDictionary.Should().Contain(keyValuePair);
                }
            }
        }