File: Tests.Exceptions.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (318 lines) | stat: -rw-r--r-- 10,467 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
#if NUNIT
using NUnit.Framework;
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
using TestMethodAttribute = NUnit.Framework.TestAttribute;
using TestInitializeAttribute = NUnit.Framework.SetUpAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

namespace Tests
{
    public partial class Tests
    {
        [TestMethod]
        public void Catch_Arguments()
        {
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Catch<int>(null, new[] { 1 }));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Catch<int>(new[] { 1 }, null));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Catch<int>(default(IEnumerable<int>[])));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Catch<int>(default(IEnumerable<IEnumerable<int>>)));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Catch<int, Exception>(null, ex => new[] { 1 }));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Catch<int, Exception>(new[] { 1 }, null));
        }

        [TestMethod]
        public void Catch1()
        {
            var ex = new MyException();
            var res = EnumerableEx.Throw<int>(ex).Catch<int, MyException>(e => { Assert.AreSame(ex, e); return new[] { 42 }; }).Single();
            Assert.AreEqual(42, res);
        }

        [TestMethod]
        public void Catch2()
        {
            var ex = new MyException();
            var res = EnumerableEx.Throw<int>(ex).Catch<int, Exception>(e => { Assert.AreSame(ex, e); return new[] { 42 }; }).Single();
            Assert.AreEqual(42, res);
        }

        [TestMethod]
        public void Catch3()
        {
            var ex = new MyException();
            AssertThrows<MyException>(() =>
            {
                EnumerableEx.Throw<int>(ex).Catch<int, InvalidOperationException>(e => { Assert.Fail(); return new[] { 42 }; }).Single();
            });
        }

        [TestMethod]
        public void Catch4()
        {
            var xs = Enumerable.Range(0, 10);
            var res = xs.Catch<int, MyException>(e => { Assert.Fail(); return new[] { 42 }; });
            Assert.IsTrue(xs.SequenceEqual(res));
        }

        [TestMethod]
        public void Catch5()
        {
            var xss = new[] { Enumerable.Range(0, 5), Enumerable.Range(5, 5) };
            var res = EnumerableEx.Catch(xss);
            Assert.IsTrue(res.SequenceEqual(Enumerable.Range(0, 5)));
        }

        [TestMethod]
        public void Catch6()
        {
            var xss = new[] { Enumerable.Range(0, 5), Enumerable.Range(5, 5) };
            var res = xss.Catch();
            Assert.IsTrue(res.SequenceEqual(Enumerable.Range(0, 5)));
        }

        [TestMethod]
        public void Catch7()
        {
            var xss = new[] { Enumerable.Range(0, 5), Enumerable.Range(5, 5) };
            var res = xss[0].Catch(xss[1]);
            Assert.IsTrue(res.SequenceEqual(Enumerable.Range(0, 5)));
        }

        [TestMethod]
        public void Catch8()
        {
            var xss = new[] { Enumerable.Range(0, 5).Concat(EnumerableEx.Throw<int>(new MyException())), Enumerable.Range(5, 5) };
            var res = EnumerableEx.Catch(xss);
            Assert.IsTrue(res.SequenceEqual(Enumerable.Range(0, 10)));
        }

        [TestMethod]
        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.IsTrue(res.SequenceEqual(Enumerable.Range(0, 10)));
        }

        [TestMethod]
        public void Catch10()
        {
            var xss = new[] { Enumerable.Range(0, 5).Concat(EnumerableEx.Throw<int>(new MyException())), Enumerable.Range(5, 5) };
            var res = xss[0].Catch(xss[1]);
            Assert.IsTrue(res.SequenceEqual(Enumerable.Range(0, 10)));
        }

        [TestMethod]
        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);
        }

        [TestMethod]
        public void Finally_Arguments()
        {
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Finally<int>(null, () => { }));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Finally<int>(new[] { 1 }, null));
        }

        [TestMethod]
        public void Finally1()
        {
            var done = false;

            var xs = Enumerable.Range(0, 2).Finally(() => done = true);
            Assert.IsFalse(done);

            var e = xs.GetEnumerator();
            Assert.IsFalse(done);

            HasNext(e, 0);
            Assert.IsFalse(done);

            HasNext(e, 1);
            Assert.IsFalse(done);

            NoNext(e);
            Assert.IsTrue(done);
        }

        [TestMethod]
        public void Finally2()
        {
            var done = false;

            var xs = Enumerable.Range(0, 2).Finally(() => done = true);
            Assert.IsFalse(done);

            var e = xs.GetEnumerator();
            Assert.IsFalse(done);

            HasNext(e, 0);
            Assert.IsFalse(done);

            e.Dispose();
            Assert.IsTrue(done);
        }

        [TestMethod]
        public void Finally3()
        {
            var done = false;

            var ex = new MyException();
            var xs = EnumerableEx.Throw<int>(ex).Finally(() => done = true);
            Assert.IsFalse(done);

            var e = xs.GetEnumerator();
            Assert.IsFalse(done);

            try
            {
                HasNext(e, 0);
                Assert.Fail();
            }
            catch (MyException ex_)
            {
                Assert.AreSame(ex, ex_);
            }

            Assert.IsTrue(done);
        }

        [TestMethod]
        public void OnErrorResumeNext_Arguments()
        {
            AssertThrows<ArgumentNullException>(() => EnumerableEx.OnErrorResumeNext<int>(null, new[] { 1 }));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.OnErrorResumeNext<int>(new[] { 1 }, null));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.OnErrorResumeNext<int>(default(IEnumerable<int>[])));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.OnErrorResumeNext<int>(default(IEnumerable<IEnumerable<int>>)));
        }

        [TestMethod]
        public void OnErrorResumeNext1()
        {
            var xs = new[] { 1, 2 };
            var ys = new[] { 3, 4 };

            var res = xs.OnErrorResumeNext(ys);
            Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 1, 2, 3, 4 }));
        }

        [TestMethod]
        public void OnErrorResumeNext2()
        {
            var xs = new[] { 1, 2 }.Concat(EnumerableEx.Throw<int>(new MyException()));
            var ys = new[] { 3, 4 };

            var res = xs.OnErrorResumeNext(ys);
            Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 1, 2, 3, 4 }));
        }

        [TestMethod]
        public void OnErrorResumeNext3()
        {
            var xs = new[] { 1, 2 };
            var ys = new[] { 3, 4 };
            var zs = new[] { 5, 6 };

            var res = EnumerableEx.OnErrorResumeNext(xs, ys, zs);
            Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 1, 2, 3, 4, 5, 6 }));
        }

        [TestMethod]
        public void OnErrorResumeNext4()
        {
            var xs = new[] { 1, 2 }.Concat(EnumerableEx.Throw<int>(new MyException()));
            var ys = new[] { 3, 4 };
            var zs = new[] { 5, 6 };

            var res = EnumerableEx.OnErrorResumeNext(xs, ys, zs);
            Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 1, 2, 3, 4, 5, 6 }));
        }

        [TestMethod]
        public void OnErrorResumeNext5()
        {
            var xs = new[] { 1, 2 };
            var ys = new[] { 3, 4 };

            var res = new[] { xs, ys }.OnErrorResumeNext();
            Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 1, 2, 3, 4 }));
        }

        [TestMethod]
        public void OnErrorResumeNext6()
        {
            var xs = new[] { 1, 2 }.Concat(EnumerableEx.Throw<int>(new MyException()));
            var ys = new[] { 3, 4 };

            var res = new[] { xs, ys }.OnErrorResumeNext();
            Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 1, 2, 3, 4 }));
        }

        [TestMethod]
        public void Retry_Arguments()
        {
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Retry<int>(null));
            AssertThrows<ArgumentNullException>(() => EnumerableEx.Retry<int>(null, 5));
            AssertThrows<ArgumentOutOfRangeException>(() => EnumerableEx.Retry<int>(new[] { 1 }, -1));
        }

        [TestMethod]
        public void Retry1()
        {
            var xs = Enumerable.Range(0, 10);

            var res = xs.Retry();
            Assert.IsTrue(Enumerable.SequenceEqual(res, xs));
        }

        [TestMethod]
        public void Retry2()
        {
            var xs = Enumerable.Range(0, 10);

            var res = xs.Retry(2);
            Assert.IsTrue(Enumerable.SequenceEqual(res, xs));
        }

        [TestMethod]
        public void Retry3()
        {
            var ex = new MyException();
            var xs = Enumerable.Range(0, 2).Concat(EnumerableEx.Throw<int>(ex));

            var res = xs.Retry(2);
            var e = res.GetEnumerator();
            HasNext(e, 0);
            HasNext(e, 1);
            HasNext(e, 0);
            HasNext(e, 1);
            AssertThrows<MyException>(() => e.MoveNext(), ex_ => ex == ex_);
        }
    }
}