Ejemplo n.º 1
0
        public void Catch3()
        {
            var ex = new InvalidOperationException("Bang!");

            var err = false;
            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw<int>(ex));
            var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();

            var res = xs.Catch<int, Exception>(ex_ => { err = true; return ys; });

            var e = res.GetEnumerator();
            HasNext(e, 1);
            HasNext(e, 2);
            HasNext(e, 3);

            Assert.False(err);

            HasNext(e, 4);

            Assert.True(err);

            HasNext(e, 5);
            HasNext(e, 6);
            NoNext(e);
        }
Ejemplo n.º 2
0
        public static void TestCatch_OnlySwitchOnError()
        {
            IObservable<int> stream1 = new[] { 1, 2 }.ToObservable();
            IObservable<int> stream2 = new[] { 3, 4 }.ToObservable();

            IObservable<int> errored = stream1.Concat(Observable.Throw<int>(new Exception("test"))).Catch(stream2);
            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 }, errored.ToEnumerable());

            IObservable<int> noerror = stream1.Catch(stream2);
            CollectionAssert.AreEqual(new[] { 1, 2 }, noerror.ToEnumerable());
        }
Ejemplo n.º 3
0
        public void Catch1()
        {
            var err = false;
            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
            var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();

            var res = xs.Catch<int, Exception>(ex_ => { err = true; return ys; });

            var e = res.GetEnumerator();
            HasNext(e, 1);
            HasNext(e, 2);
            HasNext(e, 3);
            NoNext(e);

            Assert.False(err);
        }
Ejemplo n.º 4
0
        public void Catch6()
        {
            var ex = new InvalidOperationException("Bang!");

            var err = false;
            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw<int>(ex));

            var res = xs.Catch<int, InvalidOperationException>(ex_ => { err = true; return xs; });

            var e = res.GetEnumerator();
            HasNext(e, 1);
            HasNext(e, 2);
            HasNext(e, 3);

            Assert.False(err);

            HasNext(e, 1);

            Assert.True(err);

            HasNext(e, 2);
            HasNext(e, 3);

            AssertThrows<Exception>(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
        }
Ejemplo n.º 5
0
        public void Catch5()
        {
            var ex = new InvalidOperationException("Bang!");
            var ex2 = new Exception("Oops!");

            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw<int>(ex));
            var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();

            var res = xs.Catch<int, InvalidOperationException>(ex_ => { if (ex_.Message == "Bang!") throw ex2; return ys; });

            var e = res.GetEnumerator();
            HasNext(e, 1);
            HasNext(e, 2);
            HasNext(e, 3);

            AssertThrows<Exception>(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex2);
        }
Ejemplo n.º 6
0
 public void Catch6()
 {
     var xss = new[] { Enumerable.Range(0, 5), Enumerable.Range(5, 5) };
     var res = xss.Catch();
     Assert.True(res.SequenceEqual(Enumerable.Range(0, 5)));
 }
Ejemplo n.º 7
0
 public void Catch9()
 {
     var xss = new[] { Enumerable.Range(0, 5).Concat(EnumerableEx.Throw<int>(new MyException())), Enumerable.Range(5, 5) };
     var res = xss.Catch();
     Assert.True(res.SequenceEqual(Enumerable.Range(0, 10)));
 }
Ejemplo n.º 8
0
        public void Catch11()
        {
            var e1 = new MyException();
            var ex1 = EnumerableEx.Throw<int>(e1);

            var e2 = new MyException();
            var ex2 = EnumerableEx.Throw<int>(e2);

            var e3 = new MyException();
            var ex3 = EnumerableEx.Throw<int>(e3);

            var xss = new[] { Enumerable.Range(0, 2).Concat(ex1), Enumerable.Range(2, 2).Concat(ex2), ex3 };
            var res = xss.Catch();

            var e = res.GetEnumerator();
            HasNext(e, 0);
            HasNext(e, 1);
            HasNext(e, 2);
            HasNext(e, 3);
            AssertThrows<MyException>(() => e.MoveNext(), ex => ex == e3);
        }
Ejemplo n.º 9
0
        public async Task Catch14()
        {
            var err = false;
            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
            var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();

            var res = xs.Catch<int, Exception>(ex_ => { err = true; return ys; });

            await SequenceIdentity(res);

            Assert.False(err);
        }
Ejemplo n.º 10
0
 public void Catch9_Array()
 {
     var xss = new[] { new[] { 0, 1, 2, 3, 4 }.Concat(EnumerableEx.Throw<int>(new MyException())), new[] { 5, 6, 7, 8, 9 } };
     var res = xss.Catch();
     Assert.True(res.SequenceEqual(Enumerable.Range(0, 10)));
 }
Ejemplo n.º 11
0
 public void Catch6_Array()
 {
     var xss = new[] { new[] { 0, 1, 2, 3, 4 }, new[] { 5, 6, 7, 8, 9 } };
     var res = xss.Catch();
     Assert.True(res.SequenceEqual(Enumerable.Range(0, 5)));
 }
Ejemplo n.º 12
0
 public void Catch4_Array()
 {
     var xs = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     var res = xs.Catch<int, MyException>(e => { Assert.False(true); return new[] { 42 }; });
     Assert.True(xs.SequenceEqual(res));
 }