discord-bot/Tests/ZalgoTests.cs

103 lines
3.9 KiB
C#
Raw Normal View History

2019-03-14 01:18:06 +05:00
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using CompatBot.EventHandlers;
using NUnit.Framework;
using File = System.IO.File;
namespace Tests
{
[TestFixture]
public class ZalgoTests
{
[Test, Explicit("Requires external data")]
2019-05-06 18:15:09 +05:00
public async Task ZalgoAuditTestAsync()
2019-03-14 01:18:06 +05:00
{
var samplePath = @"C:/Users/13xforever/Downloads/names.txt";
var resultPath = Path.Combine(Path.GetDirectoryName(samplePath), "zalgo.txt");
var names = await File.ReadAllLinesAsync(samplePath, Encoding.UTF8);
using var r = File.Open(resultPath, FileMode.Create, FileAccess.Write, FileShare.Read);
using var w = new StreamWriter(r, new UTF8Encoding(false));
foreach (var line in names)
{
var user = UserInfo.Parse(line);
var isZalgo = UsernameZalgoMonitor.NeedsRename(user.DisplayName);
if (isZalgo)
await w.WriteLineAsync(user.DisplayName).ConfigureAwait(false);
}
2019-03-14 01:18:06 +05:00
}
2019-03-15 04:05:07 +05:00
[Test, Explicit("Requires external data")]
2019-05-06 18:15:09 +05:00
public async Task RoleSortTestAsync()
2019-03-15 04:05:07 +05:00
{
var samplePath = @"C:/Users/13xforever/Downloads/names.txt";
var resultPath = Path.Combine(Path.GetDirectoryName(samplePath), "role_count.txt");
var stats = new int[10];
var names = await File.ReadAllLinesAsync(samplePath, Encoding.UTF8);
using (var r = File.Open(resultPath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using var w = new StreamWriter(r, new UTF8Encoding(false));
2019-03-15 04:05:07 +05:00
foreach (var line in names)
{
var user = UserInfo.Parse(line);
var roleCount = user.Roles?.Length ?? 0;
stats[roleCount]++;
w.Write(roleCount);
2019-05-06 18:15:09 +05:00
await w.WriteAsync('\t').ConfigureAwait(false);
await w.WriteLineAsync(user.DisplayName).ConfigureAwait(false);
2019-03-15 04:05:07 +05:00
}
}
2019-03-15 04:05:07 +05:00
for (var i = 0; i < stats.Length && stats[i] > 0; i++)
{
Console.WriteLine($"{i:#0} roles: {stats[i]} members");
}
}
2019-03-14 01:18:06 +05:00
[TestCase("ᵇᶦᵒˢʰᵒᶜᵏ96", false)]
[TestCase("GodPan กับยูนิตแขนที่หายไป", false)]
[TestCase("⛧Bζ͜͡annerBomb⛧", false)]
[TestCase("(_A_Y_A_Z_) (͡๏̯͡๏)", false)]
[TestCase("🥛🥛", false)]
2019-03-14 01:18:06 +05:00
[TestCase("🎮P̷͙͋a̵̛̳k̶̫̀o̸̿͜ỏ̸̝🎮", true)]
[TestCase("Cindellด้้้", true)]
[TestCase("󠂪󠂪󠂪󠂪 󠂪󠂪󠂪󠂪󠂪󠂪󠂪󠂪 󠂪󠂪󠂪", true)]
[TestCase("󠀀󠀀", true)]
2019-03-15 04:05:07 +05:00
[TestCase("꧁꧂🥴🥴🥴HOJU🥴🥴🥴╲⎝", true)]
[TestCase("", true)]
2019-03-14 01:18:06 +05:00
public void ZalgoDetectionTest(string name, bool isBad)
{
Assert.That(UsernameZalgoMonitor.NeedsRename(name), Is.EqualTo(isBad));
}
}
internal class UserInfo
{
public string Username { get; private set; }
public string Nickname { get; private set; }
public DateTime JoinDate { get; private set; }
public string[] Roles { get; private set; }
public string DisplayName => string.IsNullOrEmpty(Nickname) ? Username : Nickname;
public static UserInfo Parse(string line)
{
var parts = line.Split('\t');
if (parts.Length != 4)
throw new FormatException("Inalid user info line: " + line);
return new UserInfo
{
Username = parts[0],
Nickname = parts[1],
JoinDate = DateTime.Parse(parts[2], CultureInfo.InvariantCulture),
2019-03-15 04:05:07 +05:00
Roles = parts[3]?.Split(',', StringSplitOptions.RemoveEmptyEntries) ?? new string[0],
2019-03-14 01:18:06 +05:00
};
}
}
}