-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestResultHelper.cs
248 lines (216 loc) · 9.26 KB
/
TestResultHelper.cs
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
namespace Atc.XUnit;
/// <summary>
/// TestResultHelper.
/// </summary>
public static class TestResultHelper
{
/// <summary>
/// Asserts the on test results.
/// </summary>
/// <param name="testResults">The test results.</param>
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "OK.")]
public static void AssertOnTestResults(IReadOnlyCollection<TestResult> testResults)
{
Assert.NotNull(testResults);
if (!testResults.Any(x => x.IsError))
{
return;
}
var lines = new List<string>();
foreach (var testResult in testResults)
{
if (testResult.IsError)
{
var indentSpaces = testResult.IndentLevel * 3;
lines.Add(testResult.Text.PadLeft(testResult.Text.Length + indentSpaces));
if (testResult.SubLines is not null)
{
lines.AddRange(testResult.SubLines
.Select(subLine => "- " + subLine)
.Select(s => s.PadLeft(s.Length + indentSpaces)));
}
}
else
{
if (testResult.IndentLevel == default)
{
lines.Add(testResult.Text);
}
else
{
var indentSpaces = testResult.IndentLevel * 3;
lines.Add(testResult.Text.PadLeft(testResult.Text.Length + indentSpaces));
}
}
}
var sb = new StringBuilder();
for (var i = 0; i < lines.Count; i++)
{
if (i < lines.Count - 1
&& lines[i].EndsWith(':')
&& lines[i + 1].EndsWith(':'))
{
continue;
}
if (i == lines.Count - 1 && lines[i].EndsWith(':'))
{
continue;
}
sb.AppendLine(lines[i]);
}
Assert.True(false, sb.ToString());
}
/// <summary>
/// Asserts the on test results from methods with missing tests.
/// </summary>
/// <param name="assemblyName">Name of the assembly.</param>
/// <param name="methodsWithMissingTests">The methods with missing tests.</param>
/// <param name="useFullName">if set to <c>true</c> [use full name].</param>
public static void AssertOnTestResultsFromMethodsWithMissingTests(
string assemblyName,
MethodInfo[] methodsWithMissingTests,
bool useFullName = false)
{
if (methodsWithMissingTests is null)
{
throw new ArgumentNullException(nameof(methodsWithMissingTests));
}
var methodsWithMissingTestsGroups = methodsWithMissingTests
.OrderBy(x => x.DeclaringType?.FullName, StringComparer.Ordinal)
.GroupBy(x => x.DeclaringType?.BeautifyName(useFullName: true), StringComparer.Ordinal)
.ToArray();
var testResults = new List<TestResult>
{
new($"Assembly: {assemblyName} ({methodsWithMissingTests.Length})"),
};
foreach (var item in methodsWithMissingTestsGroups)
{
testResults.Add(new TestResult(isError: false, 1, $"Type: {item.Key} ({item.Count()})"));
testResults.AddRange(item.Select(methodInfo => new TestResult(isError: true, 2, methodInfo.BeautifyName(useFullName))));
}
AssertOnTestResults(testResults);
}
/// <summary>
/// To Excel with the test results from methods with missing tests.
/// </summary>
/// <param name="reportDirectory">The report directory.</param>
/// <param name="assemblyName">Name of the assembly.</param>
/// <param name="methodsWithMissingTests">The methods with missing tests.</param>
public static void ToExcelTestResultsFromMethodsWithMissingTests(
DirectoryInfo reportDirectory,
string assemblyName,
MethodInfo[] methodsWithMissingTests)
{
if (reportDirectory is null)
{
throw new ArgumentNullException(nameof(reportDirectory));
}
var methodsWithMissingTestsGroups = methodsWithMissingTests
.OrderBy(x => x.ReflectedType?.FullName, StringComparer.Ordinal)
.GroupBy(x => x.ReflectedType?.BeautifyName(useFullName: true), StringComparer.Ordinal)
.ToArray();
var file = new FileInfo(Path.Combine(reportDirectory.FullName, $"TestResultsFromMethodsWithMissingTestsFor_{assemblyName}.xlsx"));
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using var p = new ExcelPackage();
var ws = p.Workbook.Worksheets.Add("Methods with missing tests");
ws.Cells["A1"].Value = "Class";
ws.Cells["B1"].Value = "Method";
var row = 2;
foreach (var item in methodsWithMissingTestsGroups)
{
ws.Cells["A" + row].Value = item.Key;
foreach (var methodInfo in item)
{
ws.Cells["B" + row].Value = methodInfo.BeautifyName(useFullName: true);
row++;
}
}
ws.Column(1).AutoFit();
ws.Column(2).AutoFit();
p.SaveAs(file);
}
/// <summary>
/// Asserts the on test results from methods with wrong definitions.
/// </summary>
/// <param name="assemblyName">Name of the assembly.</param>
/// <param name="methodsWithWrongNaming">The methods with wrong naming.</param>
/// <param name="useFullName">if set to <c>true</c> [use full name].</param>
public static void AssertOnTestResultsFromMethodsWithWrongDefinitions(
string assemblyName,
IDictionary<MethodInfo, string> methodsWithWrongNaming,
bool useFullName = false)
{
if (methodsWithWrongNaming is null)
{
throw new ArgumentNullException(nameof(methodsWithWrongNaming));
}
var methodsWithWrongNamingGroups = methodsWithWrongNaming
.OrderBy(x => x.Key.ReflectedType?.FullName, StringComparer.Ordinal)
.GroupBy(x => x.Key.ReflectedType?.BeautifyName(useFullName: true), StringComparer.Ordinal)
.ToArray();
var testResults = new List<TestResult>
{
new($"Assembly: {assemblyName} ({methodsWithWrongNaming.Count})"),
};
foreach (var item in methodsWithWrongNamingGroups)
{
testResults.Add(new TestResult(isError: false, 1, $"Type: {item.Key} ({item.Count()})"));
testResults.AddRange(item.Select(x => new TestResult(isError: true, 2, $"{x.Key.BeautifyName(useFullName)} # {x.Value}")));
}
AssertOnTestResults(testResults);
}
public static void AssertOnTestResultsFromMissingTranslationsAndInvalidKeysSuffixWithPlaceholders(
string assemblyName,
IDictionary<string, Dictionary<string, List<string>>>? missingTranslations,
IDictionary<string, Dictionary<string, List<string>>>? invalidKeysSuffixWithPlaceholders)
{
var assemblyTotalCount = 0;
if (missingTranslations is not null)
{
assemblyTotalCount += missingTranslations.Sum(item => missingTranslations[item.Key].Values.Sum(x => x.Count));
}
if (invalidKeysSuffixWithPlaceholders is not null)
{
assemblyTotalCount += invalidKeysSuffixWithPlaceholders.Sum(item => invalidKeysSuffixWithPlaceholders[item.Key].Values.Sum(x => x.Count));
}
var testResults = new List<TestResult>
{
new($"Assembly: {assemblyName} ({assemblyTotalCount})"),
};
if (missingTranslations is not null)
{
foreach (var item in missingTranslations)
{
var resourceTotalCount = missingTranslations[item.Key].Values.Sum(x => x.Count);
if (resourceTotalCount == 0)
{
continue;
}
testResults.Add(new TestResult(isError: false, 1, $"Resource: {item.Key} ({resourceTotalCount})"));
foreach (var itemForResource in item.Value)
{
testResults.Add(new TestResult(isError: false, 2, $"Missing translation values for '{itemForResource.Key}' ({itemForResource.Value.Count})"));
testResults.AddRange(itemForResource.Value.Select(itemValue => new TestResult(isError: true, 3, $"Key: {itemValue}")));
}
}
}
if (invalidKeysSuffixWithPlaceholders is not null)
{
foreach (var item in invalidKeysSuffixWithPlaceholders)
{
var totalCount = invalidKeysSuffixWithPlaceholders[item.Key].Values.Sum(x => x.Count);
if (totalCount == 0)
{
continue;
}
testResults.Add(new TestResult(isError: false, 1, $"Resource: {item.Key} ({totalCount})"));
foreach (var itemForResource in item.Value)
{
testResults.Add(new TestResult(isError: false, 2, $"Invalid placeholder values for '{itemForResource.Key}' ({itemForResource.Value.Count})"));
testResults.AddRange(itemForResource.Value.Select(itemValue => new TestResult(isError: true, 3, $"Key: {itemValue}")));
}
}
}
AssertOnTestResults(testResults);
}
}