-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathAggregateExceptionTests.cs
More file actions
130 lines (108 loc) · 5.28 KB
/
AggregateExceptionTests.cs
File metadata and controls
130 lines (108 loc) · 5.28 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using Xunit;
namespace System.Threading.Tasks.Tests
{
public class AggregateExceptionTests
{
[Fact]
public static void ConstructorBasic()
{
AggregateException ex = new AggregateException();
Assert.Equal(0, ex.InnerExceptions.Count);
Assert.True(ex.Message != null, "RunAggregateException_Constructor: FAILED. Message property is null when the default constructor is used, expected a default message");
ex = new AggregateException("message");
Assert.Equal(0, ex.InnerExceptions.Count);
Assert.True(ex.Message != null, "RunAggregateException_Constructor: FAILED. Message property is null when the default constructor(string) is used");
ex = new AggregateException("message", new Exception());
Assert.Equal(1, ex.InnerExceptions.Count);
Assert.True(ex.Message != null, "RunAggregateException_Constructor: FAILED. Message property is null when the default constructor(string, Exception) is used");
}
[Fact]
public static void ConstructorInvalidArguments()
{
AggregateException ex = new AggregateException();
Assert.Throws<ArgumentNullException>(() => new AggregateException("message", (Exception)null));
Assert.Throws<ArgumentNullException>(() => new AggregateException("message", (IEnumerable<Exception>)null));
AssertExtensions.Throws<ArgumentException>(null, () => ex = new AggregateException("message", new[] { new Exception(), null }));
}
[Fact]
public static void BaseExceptions()
{
AggregateException ex = new AggregateException();
Assert.Equal(ex.GetBaseException(), ex);
Exception[] innerExceptions = new Exception[0];
ex = new AggregateException(innerExceptions);
Assert.Equal(ex.GetBaseException(), ex);
innerExceptions = new Exception[1] { new AggregateException() };
ex = new AggregateException(innerExceptions);
Assert.Equal(ex.GetBaseException(), innerExceptions[0]);
innerExceptions = new Exception[2] { new AggregateException(), new AggregateException() };
ex = new AggregateException(innerExceptions);
Assert.Equal(ex.GetBaseException(), ex);
}
[Fact]
public static void Handle()
{
AggregateException ex = new AggregateException();
ex = new AggregateException(new[] { new ArgumentException(), new ArgumentException(), new ArgumentException() });
int handledCount = 0;
ex.Handle((e) =>
{
if (e is ArgumentException)
{
handledCount++;
return true;
}
return false;
});
Assert.Equal(handledCount, ex.InnerExceptions.Count);
}
[Fact]
public static void HandleInvalidCases()
{
AggregateException ex = new AggregateException();
Assert.Throws<ArgumentNullException>(() => ex.Handle(null));
ex = new AggregateException(new[] { new Exception(), new ArgumentException(), new ArgumentException() });
int handledCount = 0;
Assert.Throws<AggregateException>(
() => ex.Handle((e) =>
{
if (e is ArgumentException)
{
handledCount++;
return true;
}
return false;
}));
}
// Validates that flattening (including recursive) works.
[Fact]
public static void Flatten()
{
Exception exceptionA = new Exception("A");
Exception exceptionB = new Exception("B");
Exception exceptionC = new Exception("C");
AggregateException aggExceptionBase = new AggregateException("message", exceptionA, exceptionB, exceptionC);
Assert.Equal("message (A) (B) (C)", aggExceptionBase.Message);
// Verify flattening one with another.
// > Flattening (no recursion)...
AggregateException flattened1 = aggExceptionBase.Flatten();
Exception[] expected1 = new Exception[] {
exceptionA, exceptionB, exceptionC
};
Assert.Equal(expected1, flattened1.InnerExceptions);
Assert.Equal("message (A) (B) (C)", flattened1.Message);
// Verify flattening one with another, accounting for recursion.
AggregateException aggExceptionRecurse = new AggregateException("message", aggExceptionBase, aggExceptionBase);
AggregateException flattened2 = aggExceptionRecurse.Flatten();
Exception[] expected2 = new Exception[] {
exceptionA, exceptionB, exceptionC, exceptionA, exceptionB, exceptionC,
};
Assert.Equal(expected2, flattened2.InnerExceptions);
Assert.Equal("message (A) (B) (C) (A) (B) (C)", flattened2.Message);
}
}
}