|
| 1 | +using System.Globalization; |
| 2 | +using System.Text; |
| 3 | +using MuConvert.generator; |
| 4 | +using MuConvert.parser; |
| 5 | +using MuConvert.utils; |
| 6 | +using static MuConvert.Tests.TestUtils; |
| 7 | + |
| 8 | +namespace MuConvert.Tests; |
| 9 | + |
| 10 | +public class Simai片段测试 |
| 11 | +{ |
| 12 | + public static IEnumerable<object[]> FragmentYamlFiles() |
| 13 | + { |
| 14 | + var root = Path.Combine(FindRepoRoot().FullName, "tests", "testset", "片段"); |
| 15 | + if (!Directory.Exists(root)) |
| 16 | + throw new DirectoryNotFoundException($"片段测例目录不存在: {root}"); |
| 17 | + |
| 18 | + foreach (var path in Directory.EnumerateFiles(root, "*.yaml", SearchOption.TopDirectoryOnly) |
| 19 | + .OrderBy(p => p, StringComparer.Ordinal)) |
| 20 | + yield return [TestSegment.Load(path)]; |
| 21 | + } |
| 22 | + |
| 23 | + [Theory] |
| 24 | + [MemberData(nameof(FragmentYamlFiles))] |
| 25 | + public void Simai片段转MA2(TestSegment c) |
| 26 | + { |
| 27 | + var (chart, parseAlerts) = new SimaiParser().Parse(c.Simai); |
| 28 | + var (ma2Full, genAlerts) = new MA2Generator(isUtage: false).Generate(chart); |
| 29 | + |
| 30 | + var actual = KeepNotesOnly(ma2Full); |
| 31 | + var expected = NormalizeMa2Block(c.Ma2); |
| 32 | + AssertMa2NotesEqual(expected, actual, c.ToString()); |
| 33 | + } |
| 34 | + |
| 35 | + private static string NormalizeMa2Block(string text) |
| 36 | + { |
| 37 | + // 与生成结果一致:统一换行、去掉文末空行 |
| 38 | + var sb = new StringBuilder(); |
| 39 | + foreach (var l in text.EnumerateLines()) |
| 40 | + { |
| 41 | + var line = l.ToString().TrimEnd('\r'); |
| 42 | + if (string.IsNullOrWhiteSpace(line) && sb.Length == 0) |
| 43 | + continue; |
| 44 | + sb.Append(line).Append('\n'); |
| 45 | + } |
| 46 | + while (sb.Length > 0 && sb[^1] == '\n' && (sb.Length == 1 || sb[^2] == '\n')) |
| 47 | + sb.Length--; |
| 48 | + return sb.ToString().TrimEnd(); |
| 49 | + } |
| 50 | + |
| 51 | + private static (int TimeTick, int Len, string Extra) GetSlideTime(string slide) |
| 52 | + { |
| 53 | + var values = slide.Split('\t'); |
| 54 | + return (int.Parse(values[1], CultureInfo.InvariantCulture) * 384 + int.Parse(values[2], CultureInfo.InvariantCulture), |
| 55 | + int.Parse(values[5], CultureInfo.InvariantCulture), |
| 56 | + string.Join("\t", values[0], values[3], values[4], values[6])); |
| 57 | + } |
| 58 | + |
| 59 | + private static bool CompareLine(string exp, string act) |
| 60 | + { |
| 61 | + var result = string.Equals(exp, act, StringComparison.Ordinal); |
| 62 | + if (!result && exp.Length >= 5 && act.Length >= 5 && exp[..5] == act[..5] && SlideTypeTool.IsSlide(exp[2..5])) |
| 63 | + { |
| 64 | + var (expTime, expLen, expExtra) = GetSlideTime(exp); |
| 65 | + var (actTime, actLen, actExtra) = GetSlideTime(act); |
| 66 | + if (expExtra != actExtra) return result; |
| 67 | + if (exp[..2] == "CN") |
| 68 | + { |
| 69 | + if (expTime + expLen == actTime + actLen || Math.Abs(expLen - actLen) <= 1) result = true; |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + if (expTime == actTime && Math.Abs(expLen - actLen) <= 1) result = true; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + private static void AssertMa2NotesEqual(string expected, string actual, string context) |
| 81 | + { |
| 82 | + var expectedLines = expected.Split('\n'); |
| 83 | + var actualLines = actual.Split('\n'); |
| 84 | + var max = Math.Max(expectedLines.Length, actualLines.Length); |
| 85 | + |
| 86 | + for (var i = 0; i < max; i++) |
| 87 | + { |
| 88 | + var exp = i < expectedLines.Length ? expectedLines[i] : "<EOF>"; |
| 89 | + var act = i < actualLines.Length ? actualLines[i] : "<EOF>"; |
| 90 | + var result = CompareLine(exp, act); |
| 91 | + if (!result) |
| 92 | + { |
| 93 | + for (var j = 1; j < Math.Min(expectedLines.Length, i + 5); j++) |
| 94 | + { |
| 95 | + if (CompareLine(expectedLines[j], act)) |
| 96 | + { |
| 97 | + (expectedLines[j], expectedLines[i]) = (expectedLines[i], expectedLines[j]); |
| 98 | + result = true; |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!result) |
| 105 | + { |
| 106 | + Assert.Fail( |
| 107 | + $"{context}: first difference at line {i + 1}:{Environment.NewLine}" + |
| 108 | + $"EXPECTED: {exp}{Environment.NewLine}" + |
| 109 | + $"ACTUAL : {act}"); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments