Example #1
0
        public void Replace()
        {
            var left  = new ListBehaviorSubject <int>();
            var right = new ListBehaviorSubject <int>();

            left.OnAdd(1);
            left.OnAdd(2);
            left.OnAdd(3);

            right.OnAdd(4);
            right.OnAdd(5);
            right.OnAdd(6);

            var list = new List <int>();

            var sub = left.Concat(right).Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list)));

            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, list);

            left.OnReplace(0, 100);
            right.OnReplace(0, 200);

            CollectionAssert.AreEqual(new[] { 100, 2, 3, 200, 5, 6 }, list);

            left.OnReplace(2, 0);
            right.OnReplace(2, 0);

            CollectionAssert.AreEqual(new[] { 100, 2, 0, 200, 5, 0 }, list);

            sub.Dispose();
        }
Example #2
0
        public void Basics()
        {
            var src = new ListBehaviorSubject <int>();

            var list = new List <int>();

            var sub = src.Where(i => i % 2 == 0).Subscribe(change => change.Apply(list));

            CollectionAssert.IsEmpty(list);

            for (var i = 0; i < 6; ++i)
            {
                src.OnAdd(i);
            }

            CollectionAssert.AreEqual(new[] { 0, 2, 4 }, list);

            src.OnRemove(1);

            CollectionAssert.AreEqual(new[] { 0, 2, 4 }, list);

            src.OnReplace(1, 4);

            CollectionAssert.AreEqual(new[] { 0, 4, 4 }, list);

            src.OnInsert(0, -2);

            CollectionAssert.AreEqual(new[] { -2, 0, 4, 4 }, list);

            src.OnClear();

            CollectionAssert.IsEmpty(list);

            sub.Dispose();
        }
Example #3
0
        public void Basics()
        {
            var src1 = new Subject <int>();
            var src2 = new Subject <int>();
            var src3 = new Subject <int>();

            var src = new ListBehaviorSubject <IObservable <int> >();

            var list = new List <int>();

            var sub = src.Switch().Subscribe(change => change.Apply(list));

            CollectionAssert.IsEmpty(list);

            src.OnAdd(src1);
            src.OnAdd(src2);
            src.OnAdd(src3);

            CollectionAssert.IsEmpty(list);

            src1.OnNext(1);

            CollectionAssert.AreEqual(new[] { 1 }, list);

            src3.OnNext(3);

            CollectionAssert.AreEqual(new[] { 1, 3 }, list);

            src2.OnNext(2);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, list);

            src1.OnNext(11);
            src2.OnNext(22);
            src3.OnNext(33);

            CollectionAssert.AreEqual(new[] { 11, 22, 33 }, list);

            src.OnRemove(1);

            CollectionAssert.AreEqual(new[] { 11, 33 }, list);

            src.OnReplace(1, src2);

            CollectionAssert.AreEqual(new[] { 11 }, list);

            src2.OnNext(22);

            CollectionAssert.AreEqual(new[] { 11, 22 }, list);

            src.OnClear();

            CollectionAssert.IsEmpty(list);

            sub.Dispose();
        }
Example #4
0
        public void Basics()
        {
            var lbs1 = new ListBehaviorSubject <int>();
            var lbs2 = new ListBehaviorSubject <int>();

            lbs1.OnAdd(1);
            lbs1.OnAdd(2);

            lbs2.OnAdd(4);
            lbs2.OnAdd(5);

            var source = new BehaviorSubject <IObservableList <int> >(lbs1);

            var list = new List <int>();

            var sub = source.Switch().Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list)));

            CollectionAssert.AreEqual(new[] { 1, 2 }, list);

            lbs1.OnAdd(3);
            lbs1.OnAdd(6);

            CollectionAssert.AreEqual(new[] { 1, 2, 3, 6 }, list);

            source.OnNext(lbs2);

            CollectionAssert.AreEqual(new[] { 4, 5 }, list);

            source.OnNext(lbs2);

            CollectionAssert.AreEqual(new[] { 4, 5 }, list);

            lbs2.OnRemove(0);

            CollectionAssert.AreEqual(new[] { 5 }, list);

            lbs2.OnReplace(0, 41);

            CollectionAssert.AreEqual(new[] { 41 }, list);

            lbs2.OnClear();

            CollectionAssert.IsEmpty(list);

            lbs2.OnAdd(4);
            lbs2.OnAdd(5);

            sub.Dispose();

            lbs1.OnAdd(100);
            lbs2.OnAdd(200);

            CollectionAssert.AreEqual(new[] { 4, 5 }, list);
        }
Example #5
0
        public void DisposeElements_disposes_elements()
        {
            var lbs = new ListBehaviorSubject <MyDisposable>();

            var d1 = new MyDisposable();
            var d2 = new MyDisposable();
            var d3 = new MyDisposable();
            var d4 = new MyDisposable();

            lbs.OnAdd(d1);
            lbs.OnAdd(d2);
            lbs.OnAdd(d3);

            var list = new List <MyDisposable>();

            var sub = lbs.DisposeElements().Subscribe(change => change.Apply(list));

            CollectionAssert.AreEqual(new[] { d1, d2, d3 }, list);

            Assert.AreEqual(0, d1.DisposeCount);
            Assert.AreEqual(0, d2.DisposeCount);
            Assert.AreEqual(0, d3.DisposeCount);
            Assert.AreEqual(0, d4.DisposeCount);

            lbs.OnRemove(1);

            CollectionAssert.AreEqual(new [] { d1, d3 }, list);

            Assert.AreEqual(0, d1.DisposeCount);
            Assert.AreEqual(1, d2.DisposeCount);
            Assert.AreEqual(0, d3.DisposeCount);
            Assert.AreEqual(0, d4.DisposeCount);

            lbs.OnReplace(1, d4);

            CollectionAssert.AreEqual(new [] { d1, d4 }, list);

            Assert.AreEqual(0, d1.DisposeCount);
            Assert.AreEqual(1, d2.DisposeCount);
            Assert.AreEqual(1, d3.DisposeCount);
            Assert.AreEqual(0, d4.DisposeCount);

            lbs.OnClear();

            CollectionAssert.AreEqual(new MyDisposable[] { }, list);

            Assert.AreEqual(1, d1.DisposeCount);
            Assert.AreEqual(1, d2.DisposeCount);
            Assert.AreEqual(1, d3.DisposeCount);
            Assert.AreEqual(1, d4.DisposeCount);

            sub.Dispose();
        }
Example #6
0
        public void Basics()
        {
            var src = new ListBehaviorSubject <int>();

            var list = new List <int>();

            var sub = src.SeparateBy(() => - 1).Subscribe(change => change.Apply(list));

            CollectionAssert.IsEmpty(list);

            for (var i = 0; i < 3; ++i)
            {
                src.OnAdd(i);
            }

            CollectionAssert.AreEqual(new[] { 0, -1, 1, -1, 2 }, list);

            src.OnRemove(0);

            CollectionAssert.AreEqual(new[] { 1, -1, 2 }, list);

            src.OnRemove(1);

            CollectionAssert.AreEqual(new[] { 1 }, list);

            src.OnInsert(0, 4);

            CollectionAssert.AreEqual(new[] { 4, -1, 1 }, list);

            src.OnAdd(5);

            CollectionAssert.AreEqual(new[] { 4, -1, 1, -1, 5 }, list);

            src.OnReplace(1, 11);

            CollectionAssert.AreEqual(new[] { 4, -1, 11, -1, 5 }, list);

            src.OnClear();

            CollectionAssert.IsEmpty(list);

            sub.Dispose();
        }