Repository navigation
Expand file tree
/
Copy pathGoldenReportSampleConsistencyTests.cs
More file actions
152 lines (127 loc) · 5.84 KB
/
Copy pathGoldenReportSampleConsistencyTests.cs
File metadata and controls
152 lines (127 loc) · 5.84 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using FolderDiffIL4DotNet.Runner;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Services
{
/// <summary>
/// Verifies that the checked-in report samples reflect the maintainer-managed creator profile.
/// commit 済みレポートサンプルがメンテナー管理の creator profile を反映していることを検証します。
/// </summary>
[Trait("Category", "Unit")]
public sealed class GoldenReportSampleConsistencyTests
{
private const string SAMPLE_ONLY_NORMALIZATION_VALUE = "buildserver1_artifact";
private static readonly string RepositoryRoot = FindRepositoryRoot();
[Fact]
public void MarkdownNormalizationTable_MatchesCreatorDefaultProfile()
{
string markdown = File.ReadAllText(
Path.Combine(RepositoryRoot, "doc", "samples", "diff_report.md"));
Assert.Equal(
GetExpectedNormalizationValues(),
ExtractMarkdownNormalizationValues(markdown));
}
[Fact]
public void HtmlNormalizationTable_MatchesCreatorDefaultProfile()
{
string html = File.ReadAllText(
Path.Combine(RepositoryRoot, "doc", "samples", "diff_report.html"));
Assert.Equal(
GetExpectedNormalizationValues(),
ExtractHtmlNormalizationValues(html));
}
private static string[] GetExpectedNormalizationValues()
{
IReadOnlyList<string> profileValues = CreatorPrivilegeIlIgnoreProfiles.GetStringsOrThrow(
CreatorPrivilegeIlIgnoreProfiles.DefaultProfileName);
var expected = new string[profileValues.Count + 1];
for (int index = 0; index < profileValues.Count; index++)
{
expected[index] = profileValues[index];
}
// The sample appends one user-configured value to demonstrate overlap warnings.
// sample は overlap warning の例示用に、ユーザー設定値を1件追加します。
expected[^1] = SAMPLE_ONLY_NORMALIZATION_VALUE;
return expected;
}
private static string[] ExtractMarkdownNormalizationValues(string markdown)
{
const string header = "| Substring to Normalize (Escaped) |";
int headerIndex = markdown.IndexOf(header, StringComparison.Ordinal);
Assert.True(headerIndex >= 0, "Markdown normalization table header was not found.");
int firstRowIndex = markdown.IndexOf('\n', headerIndex + header.Length);
Assert.True(firstRowIndex >= 0, "Markdown normalization table separator was not found.");
var values = new List<string>();
using var reader = new StringReader(markdown[(firstRowIndex + 1)..]);
// Skip the table separator.
// table separator を読み飛ばします。
_ = reader.ReadLine();
string? line;
while (!string.IsNullOrEmpty(line = reader.ReadLine()))
{
Assert.StartsWith("| ", line, StringComparison.Ordinal);
Assert.EndsWith(" |", line, StringComparison.Ordinal);
values.Add(DecodeMarkdownConfiguredValue(line[2..^2]));
}
return values.ToArray();
}
private static string DecodeMarkdownConfiguredValue(string value)
{
string decoded = WebUtility.HtmlDecode(value);
var original = new StringBuilder(decoded.Length);
for (int index = 0; index < decoded.Length; index++)
{
char current = decoded[index];
if (current != '\\')
{
original.Append(current);
continue;
}
Assert.True(
index + 1 < decoded.Length && decoded[index + 1] == '\\',
"Markdown configured-value backslashes must be doubled.");
original.Append('\\');
index++;
}
return original.ToString();
}
private static string[] ExtractHtmlNormalizationValues(string html)
{
const string tableMarker = "aria-label=\"Substring to Normalize\"";
int markerIndex = html.IndexOf(tableMarker, StringComparison.Ordinal);
Assert.True(markerIndex >= 0, "HTML normalization table was not found.");
int bodyStart = html.IndexOf("<tbody>", markerIndex, StringComparison.Ordinal);
int bodyEnd = html.IndexOf("</tbody>", bodyStart, StringComparison.Ordinal);
Assert.True(bodyStart >= 0 && bodyEnd > bodyStart, "HTML normalization table body was not found.");
string body = html.Substring(bodyStart, bodyEnd - bodyStart);
MatchCollection matches = Regex.Matches(
body,
@"<tr><td><code>(.*?)</code></td></tr>",
RegexOptions.CultureInvariant | RegexOptions.Singleline);
var values = new string[matches.Count];
for (int index = 0; index < matches.Count; index++)
{
values[index] = WebUtility.HtmlDecode(matches[index].Groups[1].Value);
}
return values;
}
private static string FindRepositoryRoot()
{
string? directory = AppContext.BaseDirectory;
while (directory != null)
{
if (File.Exists(Path.Combine(directory, "FolderDiffIL4DotNet.sln")))
{
return directory;
}
directory = Directory.GetParent(directory)?.FullName;
}
throw new DirectoryNotFoundException("Could not locate repository root.");
}
}
}