namespace MeloongCore.Tests; /// /// 所有测试的基类。 /// 会初始化 MeloongCore 并重定向日志输出。 /// public abstract class TestBase { public TestBase() { MeloongCore.Main.Init(new TestLogger()); } private class TestLogger : BaseLogger { public override void HandleBehavior(string? rawMessage, string formattedMessage, LogBehavior behavior, Exception? ex) => TestContext.Current!.OutputWriter.WriteLine(formattedMessage); } } /// /// 用于带文件测试的基类。 /// 会在极端路径下创建测试用的临时文件夹。 /// public abstract class TestWithFolder : TestBase { /// /// 测试用的临时文件夹路径。 /// 这是一个包含特殊字符的长路径,不以 \\?\ 开头,以 \ 结尾。 /// public readonly string tempFolder; public TestWithFolder() : base() { tempFolder = Path.Combine( Path.GetTempPath(), "PCL", "Tests", $"{GetType().Name}-{Guid.NewGuid()}", "文件夹 Dir_!@#$%^&()_+={}[];',_", new string('X', 200), new string('X', 200)) + @"\"; DirectoryUtils.Create(tempFolder); } /// /// 输出指定的测试用文件,返回文件路径。 /// public string GetTestFile(string fileName) { var sourceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", GetType().Name.Replace("Test", ""), fileName); var distPath = Path.Combine(tempFolder, PathUtils.GetLastPart(sourceFilePath)); FileUtils.Copy(sourceFilePath, distPath); return distPath; } }