|
2 | 2 | using MuConvert.generator; |
3 | 3 | using MuConvert.maidata; |
4 | 4 | using MuConvert.parser.simai; |
| 5 | +using MuConvert.utils; |
5 | 6 | using Xunit.Abstractions; |
6 | 7 |
|
7 | 8 | namespace MuConvert.Tests; |
@@ -85,33 +86,64 @@ private static string keepNotesOnly(string text) |
85 | 86 | return result.ToString(); |
86 | 87 | } |
87 | 88 |
|
88 | | - private static void AssertTextEqual(string expected, string actual) |
| 89 | + private static (int, int, string) GetSlideTime(string slide) |
89 | 90 | { |
90 | | - if (string.Equals(expected, actual, StringComparison.Ordinal)) return; |
| 91 | + var values = slide.Split("\t"); |
| 92 | + return (int.Parse(values[1]) * 384 + int.Parse(values[2]), int.Parse(values[5]), |
| 93 | + string.Join("\t", values[0], values[3], values[4], values[6])); |
| 94 | + } |
91 | 95 |
|
| 96 | + private static bool CompareLine(string exp, string act) |
| 97 | + { |
| 98 | + var result = string.Equals(exp, act, StringComparison.Ordinal); |
| 99 | + if (!result && exp[..5] == act[..5] && SlideTypeTool.IsSlide(exp)) |
| 100 | + { // 如果是星星,则允许一定范围的误差。具体而言: |
| 101 | + var (expTime, expLen, expExtra) = GetSlideTime(exp); |
| 102 | + var (actTime, actLen, actExtra) = GetSlideTime(act); |
| 103 | + if (expExtra != actExtra) return result; // 首先任何情况下,waitTime和按键等信息必须相等 |
| 104 | + if (exp[..2] == "CN") |
| 105 | + { // CN星星则要么尾时刻完全对,要么长度至多差1。 |
| 106 | + if (expTime + expLen == actTime + actLen || Math.Abs(expLen - actLen) <= 1) result = true; |
| 107 | + } |
| 108 | + else |
| 109 | + { // 第一段星星则开始时刻必须对且长度至多差1 |
| 110 | + if (expTime == actTime && Math.Abs(expLen - actLen) <= 1) result = true; |
| 111 | + } |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + private static void AssertTextEqual(string expected, string actual) |
| 117 | + { |
92 | 118 | var expectedLines = expected.Split('\n'); |
93 | 119 | var actualLines = actual.Split('\n'); |
94 | | - var min = Math.Min(expectedLines.Length, actualLines.Length); |
| 120 | + var max = Math.Max(expectedLines.Length, actualLines.Length); |
95 | 121 |
|
96 | | - var firstDiff = -1; |
97 | | - for (var i = 0; i < min; i++) |
| 122 | + for (var i = 0; i < max; i++) |
98 | 123 | { |
99 | | - if (!string.Equals(expectedLines[i], actualLines[i], StringComparison.Ordinal)) |
| 124 | + var exp = i < expectedLines.Length ? expectedLines[i] : "<EOF>"; |
| 125 | + var act = i < actualLines.Length ? actualLines[i] : "<EOF>"; |
| 126 | + var result = CompareLine(exp, act); |
| 127 | + if (!result) |
100 | 128 | { |
101 | | - firstDiff = i; |
102 | | - break; |
| 129 | + // 尝试下面五行之内有无相同的,如果有,交换之 |
| 130 | + for (int j = 1; j < Math.Min(expectedLines.Length, i+5); j++) |
| 131 | + { |
| 132 | + if (CompareLine(expectedLines[j], act)) |
| 133 | + { |
| 134 | + (expectedLines[j], expectedLines[i]) = (expectedLines[i], expectedLines[j]); |
| 135 | + result = true; |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
103 | 139 | } |
104 | | - } |
105 | | - if (firstDiff == -1) firstDiff = min; |
106 | | - |
107 | | - var exp = firstDiff < expectedLines.Length ? expectedLines[firstDiff] : "<EOF>"; |
108 | | - var act = firstDiff < actualLines.Length ? actualLines[firstDiff] : "<EOF>"; |
109 | 140 |
|
110 | | - Assert.Fail( |
111 | | - $"First difference at line {firstDiff + 1}:{Environment.NewLine}" + |
112 | | - $"EXPECTED: {exp}{Environment.NewLine}" + |
113 | | - $"ACTUAL : {act}" |
114 | | - ); |
| 141 | + if (!result) Assert.Fail( |
| 142 | + $"First difference at line {i + 1}:{Environment.NewLine}" + |
| 143 | + $"EXPECTED: {exp}{Environment.NewLine}" + |
| 144 | + $"ACTUAL : {act}" |
| 145 | + ); |
| 146 | + } |
115 | 147 | } |
116 | 148 |
|
117 | 149 | private static DirectoryInfo FindRepoRoot() |
|
0 commit comments