public void test_ConvertCollection() { var t1 = new IInt(11); var t2 = new IInt(33); var src = new WeakObservableCollection <IInt>() { t1, t2 }; var target = src.ToReadOnly().Convert(x => x.ToString(), false); Assert.AreEqual(2, target.Count); src.Clear(); for (int i = 0; i < 2; i++) { src.Add(t1); Assert.AreEqual(src.Count, target.Count); Assert.AreEqual(t1.ToString(), target[0]); src.Add(t2); Assert.AreEqual(src.Count, target.Count); Assert.AreEqual(t2.ToString(), target.Single(x => x == t2.ToString())); src.Remove(t1); Assert.AreEqual(t2.ToString(), target.Single()); src.Remove(t2); Assert.IsFalse(target.Any()); Assert.AreEqual(0, target.Count); } var target1 = target.Convert(x => x.Length.ToString(), true); src.Add(t1); src.Add(t2); Assert.AreEqual(2, target.Count); Assert.AreEqual(1, target1.Count); src.Remove(t1); Assert.AreEqual(1, target.Count); Assert.AreEqual(1, target1.Count); src.Clear(); Assert.AreEqual(0, target.Count); Assert.AreEqual(0, target1.Count); }
public void test_FilterCollection() { WeakObservableCollection <FilterItem> items = new WeakObservableCollection <FilterItem>() { new FilterItem() { Property = false } }; var manualTrigger = new WeakEvent(items); var filtered = items.ToAutoProcessed().FilteredBy(x => x.Property) .UpdateOn(x => x.PropertyTrigger) .UpdateOn(x => null); var filteredM = items.ToAutoProcessed().FilteredBy(x => x.Property).UpdateOn(manualTrigger); Assert.AreEqual(0, filtered.Count); items[0].Property = true; Assert.AreEqual(1, filtered.Count); Assert.AreEqual(0, filteredM.Count); manualTrigger.Raise(); Assert.AreEqual(filtered.Count, filteredM.Count); items[0].Property = false; Assert.AreEqual(0, filtered.Count); items.Add(new FilterItem() { Property = true }); Assert.AreEqual(1, filtered.Count); items.Add(new FilterItem() { Property = false }); Assert.AreEqual(1, filtered.Count); items.Add(new FilterItem() { Property = true }); Assert.AreEqual(2, filtered.Count); items.Remove(items.First(x => x.Property)); Assert.AreEqual(1, filtered.Count); Assert.AreSame(items.Single(x => x.Property), filtered[0]); items.Remove(items.First(x => !x.Property)); Assert.AreEqual(1, filtered.Count); items.Clear(); Assert.AreEqual(0, filtered.Count); //тестируем работу сортировки var rawColl = new WeakObservableCollection <string>(); //сортируем по первым символам var sorted = rawColl.ToAutoProcessed().SortedOn((x, y) => x.Substring(0, 1).CompareTo(y.Substring(0, 1))); rawColl.Add("5ф"); rawColl.Add("2ф"); rawColl.Add("4ф"); rawColl.Add("3ф"); rawColl.Add("1с"); rawColl.Add("5с"); rawColl.Add("2с"); rawColl.Add("4с"); rawColl.Add("3с"); rawColl.Add("1с"); for (int i = 0; i < 5; i++) { Assert.AreEqual((i + 1).ToString(), sorted[2 * i].Substring(0, 1)); Assert.AreEqual((i + 1).ToString(), sorted[2 * i + 1].Substring(0, 1)); } }
public void test_UnionCollection() { string t11 = "11", t12 = "12", t21 = "21", t22 = "22"; WeakObservableCollection <string> src1 = new WeakObservableCollection <string>() { t11, t12 }; WeakObservableCollection <string> src2 = new WeakObservableCollection <string>() { t21, t22 }; WeakObservableCollection <IReadOnlyCollection <string> > collections = new WeakObservableCollection <IReadOnlyCollection <string> > { src1.ToReadOnly() }; var union = collections.ToReadOnly().UnionAll(x => x); Assert.AreEqual(2, union.Count); Assert.IsTrue(union.Contains(t11)); Assert.IsTrue(union.Contains(t12)); Assert.IsFalse(union.Contains(t21)); Assert.IsFalse(union.Contains(t22)); collections.Add(src2.ToReadOnly()); Assert.AreEqual(4, union.Count); Assert.IsTrue(union.Contains(t11)); Assert.IsTrue(union.Contains(t12)); Assert.IsTrue(union.Contains(t21)); Assert.IsTrue(union.Contains(t22)); src1.Remove(t11); Assert.AreEqual(3, union.Count); Assert.IsFalse(union.Contains(t11)); Assert.IsTrue(union.Contains(t12)); Assert.IsTrue(union.Contains(t21)); Assert.IsTrue(union.Contains(t22)); src1.Clear(); src2.Clear(); Assert.AreEqual(0, union.Count); src2.Add(t21); Assert.AreEqual(1, union.Count); Assert.IsTrue(union.Contains(t21)); Assert.IsFalse(union.Contains(t12)); src1.Add(t11); Assert.AreEqual(2, union.Count); collections.Remove(collections[0]); Assert.AreEqual(1, union.Count); collections.Clear(); Assert.AreEqual(0, union.Count); }
public void test_ConvertCollection() { var t1 = new IInt(11); var t2 = new IInt(33); var src = new WeakObservableCollection<IInt>() { t1, t2}; var target = src.ToReadOnly().Convert(x => x.ToString(), false); Assert.AreEqual(2, target.Count); src.Clear(); for (int i = 0; i < 2; i++) { src.Add(t1); Assert.AreEqual(src.Count, target.Count); Assert.AreEqual(t1.ToString(), target[0]); src.Add(t2); Assert.AreEqual(src.Count, target.Count); Assert.AreEqual(t2.ToString(), target.Single(x => x == t2.ToString())); src.Remove(t1); Assert.AreEqual(t2.ToString(), target.Single()); src.Remove(t2); Assert.IsFalse(target.Any()); Assert.AreEqual(0, target.Count); } var target1 = target.Convert(x => x.Length.ToString(), true); src.Add(t1); src.Add(t2); Assert.AreEqual(2, target.Count); Assert.AreEqual(1, target1.Count); src.Remove(t1); Assert.AreEqual(1, target.Count); Assert.AreEqual(1, target1.Count); src.Clear(); Assert.AreEqual(0, target.Count); Assert.AreEqual(0, target1.Count); }
public void test_UnionCollection() { string t11 = "11", t12 = "12", t21 = "21", t22 = "22"; WeakObservableCollection<string> src1 = new WeakObservableCollection<string>() { t11, t12 }; WeakObservableCollection<string> src2 = new WeakObservableCollection<string>() { t21, t22 }; WeakObservableCollection<IReadOnlyCollection<string>> collections = new WeakObservableCollection<IReadOnlyCollection<string>> { src1.ToReadOnly() }; var union = collections.ToReadOnly().UnionAll(x => x); Assert.AreEqual(2, union.Count); Assert.IsTrue(union.Contains(t11)); Assert.IsTrue(union.Contains(t12)); Assert.IsFalse(union.Contains(t21)); Assert.IsFalse(union.Contains(t22)); collections.Add(src2.ToReadOnly()); Assert.AreEqual(4, union.Count); Assert.IsTrue(union.Contains(t11)); Assert.IsTrue(union.Contains(t12)); Assert.IsTrue(union.Contains(t21)); Assert.IsTrue(union.Contains(t22)); src1.Remove(t11); Assert.AreEqual(3, union.Count); Assert.IsFalse(union.Contains(t11)); Assert.IsTrue(union.Contains(t12)); Assert.IsTrue(union.Contains(t21)); Assert.IsTrue(union.Contains(t22)); src1.Clear(); src2.Clear(); Assert.AreEqual(0, union.Count); src2.Add(t21); Assert.AreEqual(1, union.Count); Assert.IsTrue(union.Contains(t21)); Assert.IsFalse(union.Contains(t12)); src1.Add(t11); Assert.AreEqual(2, union.Count); collections.Remove(collections[0]); Assert.AreEqual(1, union.Count); collections.Clear(); Assert.AreEqual(0, union.Count); }
public void test_FilterCollection() { WeakObservableCollection<FilterItem> items = new WeakObservableCollection<FilterItem>() { new FilterItem() {Property = false}}; var manualTrigger = new WeakEvent(items); var filtered = items.ToAutoProcessed().FilteredBy(x => x.Property) .UpdateOn(x => x.PropertyTrigger) .UpdateOn(x => null); var filteredM = items.ToAutoProcessed().FilteredBy(x => x.Property).UpdateOn(manualTrigger); Assert.AreEqual(0, filtered.Count); items[0].Property = true; Assert.AreEqual(1, filtered.Count); Assert.AreEqual(0, filteredM.Count); manualTrigger.Raise(); Assert.AreEqual(filtered.Count, filteredM.Count); items[0].Property = false; Assert.AreEqual(0, filtered.Count); items.Add(new FilterItem() { Property = true }); Assert.AreEqual(1, filtered.Count); items.Add(new FilterItem() { Property = false }); Assert.AreEqual(1, filtered.Count); items.Add(new FilterItem() { Property = true }); Assert.AreEqual(2, filtered.Count); items.Remove(items.First(x => x.Property)); Assert.AreEqual(1, filtered.Count); Assert.AreSame(items.Single(x => x.Property), filtered[0]); items.Remove(items.First(x => !x.Property)); Assert.AreEqual(1, filtered.Count); items.Clear(); Assert.AreEqual(0, filtered.Count); //тестируем работу сортировки var rawColl = new WeakObservableCollection<string>(); //сортируем по первым символам var sorted = rawColl.ToAutoProcessed().SortedOn((x, y) => x.Substring(0, 1).CompareTo(y.Substring(0, 1))); rawColl.Add("5ф"); rawColl.Add("2ф"); rawColl.Add("4ф"); rawColl.Add("3ф"); rawColl.Add("1с"); rawColl.Add("5с"); rawColl.Add("2с"); rawColl.Add("4с"); rawColl.Add("3с"); rawColl.Add("1с"); for (int i = 0; i < 5; i++) { Assert.AreEqual((i + 1).ToString(), sorted[2 * i].Substring(0, 1)); Assert.AreEqual((i + 1).ToString(), sorted[2 * i + 1].Substring(0, 1)); } }