-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathAlertBoxTests.cs
66 lines (55 loc) · 2.34 KB
/
AlertBoxTests.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
namespace IntegrityTests;
internal partial class AlertBoxTests
{
[Test]
public void NoLegacyAlertBoxes()
{
new TestRunner("*.md", "Old-style alert boxes no longer supported, use GitHub style alert boxes")
.Run(path =>
{
var matches = LegacyAlertRegex().Matches(File.ReadAllText(path))
.OfType<Match>()
.Select(match => match.Groups[2].Value)
.Where(val => val is not "info" and not "warn") // This can be in .NET logging output with exactly this casing
.ToArray();
if (matches.Any())
{
var msg = "Found legacy alert(s) of type " + string.Join(" & ", matches);
return (false, msg);
}
return (true, null);
});
}
[GeneratedRegex(@"^(\{\{)?(SUCCESS|NOTE|INFO|WARN|WARNING|DANGER):", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase)]
private partial Regex LegacyAlertRegex();
[Test]
public void AlertBoxesAreValid()
{
var only = string.Join(" ", ValidGitHubAlertTypes.Select(s => $"![{s}]"));
new TestRunner("*.md", $"Invalid alert boxes found, only {only} are valid")
.Run(path =>
{
var invalidTypes = GitHubAlertBoxHeaderRegex().Matches(File.ReadAllText(path))
.OfType<Match>()
.Select(m => m.Groups[1].Value)
.Where(type => !ValidGitHubAlertTypes.Contains(type))
.Distinct()
.ToArray();
if (invalidTypes.Any())
{
var typesStr = string.Join(" & ", invalidTypes.Select(s => $"![{s}]"));
var msg = $"Alert box of type {typesStr} is invalid";
return (false, msg);
}
return (true, null);
});
}
[GeneratedRegex(@"^> \[!(\w+)\]\s*\r?\n", RegexOptions.Compiled | RegexOptions.Multiline)]
private partial Regex GitHubAlertBoxHeaderRegex();
static readonly HashSet<string> ValidGitHubAlertTypes = ["NOTE", "TIP", "IMPORTANT", "WARNING", "CAUTION"];
}