Skip to content

Commit fa0ce6a

Browse files
committed
[T] 添加一些测试样例
1 parent 9284df0 commit fa0ce6a

83 files changed

Lines changed: 33089 additions & 13 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

parser/simai/ErrorStrategy.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public override void SyntaxError(TextWriter output, IRecognizer recognizer, ITok
2020
{
2121
simaiParser.alerts.Add(new Alert(Warning,
2222
string.Format(Locale.RecoverInlineExtraneousToken, GetTokenErrorDisplay(offendingSymbol)),
23-
line: line, relevantNote: RelevantNote(parser.Context)));
23+
line: line, relevantNote: RelevantNote(parser.Context, recognizer, offendingSymbol)));
2424
return;
2525
}
2626
else if (msg.StartsWith("missing"))
2727
{
2828
simaiParser.alerts.Add(new Alert(Warning,
2929
string.Format(Locale.RecoverInlineMissingToken, GetTokenErrorDisplay(offendingSymbol), parser.GetExpectedTokens().ToString(parser.Vocabulary)),
30-
line: line, relevantNote: RelevantNote(parser.Context)));
30+
line: line, relevantNote: RelevantNote(parser.Context, recognizer, offendingSymbol)));
3131
return;
3232
}
3333
}
@@ -50,7 +50,7 @@ public override void SyntaxError(TextWriter output, IRecognizer recognizer, ITok
5050
message = string.Format(Locale.AntlrUnknownError, msg);
5151
break;
5252
}
53-
simaiParser.alerts.Add(new Alert(level, message, line: line, relevantNote: RelevantNote(parser.Context)));
53+
simaiParser.alerts.Add(new Alert(level, message, line: line, relevantNote: RelevantNote(parser.Context, recognizer, offendingSymbol)));
5454
}
5555

5656
// 词法分析的错误报告函数
@@ -75,14 +75,26 @@ public void SyntaxError(TextWriter output, IRecognizer recognizer, int offending
7575
}
7676

7777
// 从context获得为适合放进relevantNote里的形式
78-
private static string? RelevantNote(RuleContext? context)
78+
private static string? RelevantNote(RuleContext? context, IRecognizer? recognizer, IToken? offendingSymbol)
7979
{
80-
while (true)
80+
string? result = null;
81+
while (context != null)
8182
{
82-
if (context == null) return null;
83-
if (context.GetText().Length >= 5) return context.GetText();
83+
if (context.GetText().Length >= 5)
84+
{
85+
result = context.GetText();
86+
break;
87+
}
8488
context = context.Parent;
8589
}
90+
91+
if (recognizer != null && offendingSymbol is { TokenIndex: >= 0 } && result is { Length: > 35 })
92+
{ // 根据offendingToken的位置,前截取5个、后截取三个token
93+
var i = offendingSymbol.TokenIndex;
94+
result = ((ITokenStream)recognizer.InputStream).GetText(
95+
new Interval(Math.Max(i - 5, 0), Math.Min(i + 3, recognizer.InputStream.Size - 1)));
96+
}
97+
return result;
8698
}
8799

88100
# region 用于暴露DefaultErrorStrategy内部的GetTokenErrorDisplay函数

tests/MA2转Simai测试.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ private void TestChart(TestInput c)
3232
var inote = maidata.Levels[c.LevelId].Inote;
3333
var ma2Text = File.ReadAllText(c.MA2, Encoding.UTF8);
3434

35-
var (chart, _) = new MA2Parser().Parse(ma2Text);
36-
var (simai, _) = new SimaiGenerator().Generate(chart);
35+
var (chart, alerts) = new MA2Parser().Parse(ma2Text);
36+
var (simai, alerts2) = new SimaiGenerator().Generate(chart);
37+
_output.WriteLine(string.Join('\n', alerts));
38+
_output.WriteLine(string.Join('\n', alerts2));
3739

3840
Assert.Equal(TestUtils.TryParseMa2ClkDef(ma2Text), chart.ClockCount * 96);
3941
var expectedTimeline = SimaiCommaTimeline.Flatten(inote);

tests/Simai转MA2测试.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ private void TestChart(TestInput input)
2828
var chartInfo = maidata.Levels[input.LevelId];
2929
var expectedMa2 = File.ReadAllText(input.MA2, Encoding.UTF8);
3030

31-
var (chart, _) = new SimaiParser(bigTouch: false, clockCount: maidata.ClockCount).Parse(chartInfo.Inote);
32-
var (ma2, _) = new MA2Generator(isUtage: false).Generate(chart);
31+
var (chart, alerts) = new SimaiParser(bigTouch: false, clockCount: maidata.ClockCount).Parse(chartInfo.Inote);
32+
var (ma2, alerts2) = new MA2Generator(isUtage: false).Generate(chart);
33+
_output.WriteLine(string.Join('\n', alerts));
34+
_output.WriteLine(string.Join('\n', alerts2));
3335

3436
Assert.Equal(maidata.ClockCount * 96, TestUtils.TryParseMa2ClkDef(ma2));
3537
ma2 = KeepNotesOnly(ma2);

0 commit comments

Comments
 (0)