public void ObservableSelect_ChangeItemsTest()
        {
            var collection = new ObservableCollection<TestObject>();
            var values = collection.ObservableSelect(item => item.Value);
            var receivedEvents = new ObservableIndexer<NotifyCollectionChangedAction, int>(_ => 0);

            var expectedValues = new[] { 2, 3, 4 };
            var expectedEvents = new[] { KeyValuePair(NotifyCollectionChangedAction.Replace, 3) };

            var testObject1 = new TestObject { Value = 1 };
            var testObject2 = new TestObject { Value = 2 };
            var testObject3 = new TestObject { Value = 3 };

            NotifyCollectionChangedEventHandler collectionChangedEventHandler = (sender, e) =>
            {
                receivedEvents[e.Action] += 1;
            };

            collection.AddRange(testObject1, testObject2, testObject3);

            values.CollectionChanged += collectionChangedEventHandler;

            foreach (var item in collection)
            {
                item.Value += 1;
            }

            Assert.IsTrue(receivedEvents.SequenceEqual(expectedEvents));
            Assert.IsTrue(values.SequenceEqual(expectedValues));
        }
        public void ObservableIndexer_RemoveTest()
        {
            var target = new ObservableIndexer <int, string>(i => (i + 1).ToString());

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 3, 6 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "4", "7" }));

            target.Remove(3);

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 6 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "7" }));

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);
        }
        public void ObservableIndexer_RemoveTest()
        {
            var target = new ObservableIndexer<int, string>(i => (i + 1).ToString());

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 3, 6 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "4", "7" }));

            target.Remove(3);

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 6 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "7" }));

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);
        }
        public void ObservableIndexer_SetTest()
        {
            var target = new ObservableIndexer <int, string>(i => (i + 1).ToString());

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 3, 6 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "4", "7" }));

            target[5] = AnotherString;

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 3, 6, 5 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "4", "7", AnotherString }));

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);
            Assert.AreEqual(AnotherString, target[5]);
        }
        public void ObservableIndexer_SetTest()
        {
            var target = new ObservableIndexer<int, string>(i => (i + 1).ToString());

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 3, 6 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "4", "7" }));

            target[5] = AnotherString;

            Assert.IsTrue(target.Select(item => item.Key).SequenceEqual(new[] { 1, 3, 6, 5 }));
            Assert.IsTrue(target.Select(item => item.Value).SequenceEqual(new[] { "2", "4", "7", AnotherString }));

            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual("7", target[6]);
            Assert.AreEqual(AnotherString, target[5]);
        }
        public void RelayedEventAttribute_MultipleGoverningClassTest()
        {
            var receivedEvents = new ObservableIndexer<string, int>(_ => 0);
            var governing1 = new GoverningClass1();
            var governing2 = new GoverningClass2();
            var relaying = new RelayingClass(governing1, governing2);

            relaying.PropertyChanged += (sender, e) => receivedEvents[e.PropertyName] += 1;

            governing1.Value = 5;

            Assert.AreEqual(5, relaying.Value);
            Assert.AreEqual(0, relaying.MyOtherValue);
            Assert.AreEqual(1, receivedEvents.Count);
            Assert.AreEqual(1, receivedEvents["Value"]);

            governing1.Value = 7;

            Assert.AreEqual(7, relaying.Value);
            Assert.AreEqual(0, relaying.MyOtherValue);
            Assert.AreEqual(1, receivedEvents.Count);
            Assert.AreEqual(2, receivedEvents["Value"]);

            // Governing2.Value is not relayed, changes should not generate relayed events
            governing2.Value = 8;

            Assert.AreEqual(7, relaying.Value);
            Assert.AreEqual(0, relaying.MyOtherValue);
            Assert.AreEqual(1, receivedEvents.Count);
            Assert.AreEqual(2, receivedEvents["Value"]);

            governing2.OtherValue = 8;

            Assert.AreEqual(7, relaying.Value);
            Assert.AreEqual(8, relaying.MyOtherValue);
            Assert.AreEqual(2, receivedEvents.Count);
            Assert.AreEqual(2, receivedEvents["Value"]);
            Assert.AreEqual(1, receivedEvents["MyOtherValue"]);
        }
        public void RelayedEventAttribute_SingleGoverningClassTest()
        {
            var receivedEvents = new ObservableIndexer<string, int>(_ => 0);
            var governing = new GoverningClass1();
            var relaying = new RelayingClass(governing);

            relaying.PropertyChanged += (sender, e) => receivedEvents[e.PropertyName] += 1;

            governing.Value = 5;

            Assert.AreEqual(5, relaying.Value);
            Assert.AreEqual(1, receivedEvents.Count);
            Assert.AreEqual(1, receivedEvents["Value"]);

            governing.Value = 7;

            Assert.AreEqual(7, relaying.Value);
            Assert.AreEqual(1, receivedEvents.Count);
            Assert.AreEqual(2, receivedEvents["Value"]);
        }
        public void ObservableSelectMany_RemoveValuesTest()
        {
            var collection = new ObservableCollection<TestObject>();
            var values = collection.ObservableSelectMany(item => item.Inner);
            var receivedEvents = new ObservableIndexer<NotifyCollectionChangedAction, int>(_ => 0);

            var expectedInitialValues = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var expectedValues = new[] { 1, 3, 4, 6, 7, 9 };
            var expectedEvents = new[] { KeyValuePair(NotifyCollectionChangedAction.Remove, 3) };

            var testObject1 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };
            var testObject2 = new TestObject { Inner = new ObservableCollection<int>(new[] { 4, 5, 6 }) };
            var testObject3 = new TestObject { Inner = new ObservableCollection<int>(new[] { 7, 8, 9 }) };

            NotifyCollectionChangedEventHandler collectionChangedEventHandler = (sender, e) =>
            {
                receivedEvents[e.Action] += 1;
            };

            collection.AddRange(testObject1, testObject2, testObject3);
            Assert.IsTrue(values.SequenceEqual(expectedInitialValues));

            values.CollectionChanged += collectionChangedEventHandler;

            foreach (var item in collection)
            {
                item.Inner.RemoveAt(1);
            }

            Assert.IsTrue(receivedEvents.SequenceEqual(expectedEvents));
            Assert.IsTrue(values.SequenceEqual(expectedValues));
        }
        public void ObservableSelectMany_ChangeItemValuesTest()
        {
            var collection = new ObservableCollection<TestObject>();
            var values = collection.ObservableSelectMany(item => item.Inner);
            var receivedEvents = new ObservableIndexer<NotifyCollectionChangedAction, int>(_ => 0);

            var expectedInitialValues = new[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
            var expectedValues = new[] { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var expectedEvents = new[] { KeyValuePair(NotifyCollectionChangedAction.Replace, 9) };

            var testObject1 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };
            var testObject2 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };
            var testObject3 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };

            NotifyCollectionChangedEventHandler collectionChangedEventHandler = (sender, e) =>
            {
                receivedEvents[e.Action] += 1;
            };

            collection.AddRange(testObject1, testObject2, testObject3);
            Assert.IsTrue(values.SequenceEqual(expectedInitialValues));

            values.CollectionChanged += collectionChangedEventHandler;

            var innerValue = 1;

            foreach (var item in collection)
            {
                var subItems = item.Inner;
                for (var i = 0; i < subItems.Count; i++)
                {
                    subItems[i] = ++innerValue;
                }
            }

            Assert.IsTrue(receivedEvents.SequenceEqual(expectedEvents));
            Assert.IsTrue(values.SequenceEqual(expectedValues));
        }
        public void ObservableSelectMany_AddItemsTest()
        {
            var collection = new ObservableCollection<TestObject>();
            var values = collection.ObservableSelectMany(item => item.Inner);
            var receivedEvents = new ObservableIndexer<NotifyCollectionChangedAction, int>(_ => 0);

            var expectedValues = new[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
            var expectedEvents = new[] { KeyValuePair(NotifyCollectionChangedAction.Add, 9) };

            var testObject1 = new TestObject { Inner = new ObservableCollection<int>() };
            var testObject2 = new TestObject { Inner = new ObservableCollection<int>() };
            var testObject3 = new TestObject { Inner = new ObservableCollection<int>() };

            NotifyCollectionChangedEventHandler collectionChangedEventHandler = (sender, e) =>
            {
                receivedEvents[e.Action] += 1;
            };

            values.CollectionChanged += collectionChangedEventHandler;

            Assert.AreEqual(0, values.Count);

            collection.AddRange(testObject1, testObject2, testObject3);

            foreach (var item in collection)
            {
                item.Inner.AddRange(1, 2, 3);
            }

            Assert.IsTrue(receivedEvents.SequenceEqual(expectedEvents));
            Assert.IsTrue(values.SequenceEqual(expectedValues));
        }