Skip to content

Commit ac4c7b4

Browse files
committed
[R] 进一步重构测试脚本,减少冗余代码
1 parent 9e2fa78 commit ac4c7b4

3 files changed

Lines changed: 26 additions & 40 deletions

File tree

tests/MA2转Simai测试.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using MuConvert.parser;
88
using Rationals;
99
using Xunit.Abstractions;
10-
using static MuConvert.Tests.TestUtils;
1110

1211
namespace MuConvert.Tests;
1312

@@ -21,25 +20,10 @@ public class MA2转Simai测试
2120

2221
public MA2转Simai测试(ITestOutputHelper output) => _output = output;
2322

24-
public static IEnumerable<object[]> AllLevels(string dataDir)
25-
{
26-
var repoRoot = FindRepoRoot();
27-
var testsetRoot = Path.Combine(repoRoot.FullName, "tests", "testset", dataDir);
28-
if (!Directory.Exists(testsetRoot))
29-
throw new DirectoryNotFoundException($"Testset root not found: {testsetRoot}");
30-
31-
foreach (var maidataPath in Directory.EnumerateFiles(testsetRoot, "maidata.txt", SearchOption.AllDirectories))
32-
{
33-
var maidataTxt = File.ReadAllText(maidataPath, Encoding.UTF8);
34-
var maidata = new Maidata(maidataTxt);
35-
foreach (var id in maidata.Levels.Keys.OrderBy(k => k))
36-
// if (maidataPath.Contains("系ぎて") && id == 3)
37-
yield return [new TestInput(maidataPath, id)];
38-
}
39-
}
40-
23+
public static IEnumerable<object[]> GetTestInputs(string dataDir) => TestUtils.GetTestInputs(dataDir);
24+
4125
[Theory]
42-
[MemberData(nameof(AllLevels), "官谱")]
26+
[MemberData(nameof(GetTestInputs), "官谱")]
4327
public void 官谱转Simai测试(TestInput c) => TestChart(c);
4428

4529
private void TestChart(TestInput c)

tests/Simai转MA2测试.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using MuConvert.parser;
55
using MuConvert.utils;
66
using Xunit.Abstractions;
7-
using static MuConvert.Tests.TestUtils;
87

98
namespace MuConvert.Tests;
109

@@ -15,28 +14,11 @@ public class Simai转MA2测试
1514

1615
public Simai转MA2测试(ITestOutputHelper output) => _output = output;
1716

18-
/// <summary>
19-
/// 每个元素对应一张谱面:一条 Theory 用例只跑一个 chart。
20-
/// </summary>
21-
public static IEnumerable<object[]> AllCharts(string dataDir)
22-
{
23-
// TODO 再多找一些自制谱加进来吧
24-
var repoRoot = FindRepoRoot();
25-
var testsetRoot = Path.Combine(repoRoot.FullName, "tests", "testset", dataDir);
26-
if (!Directory.Exists(testsetRoot))
27-
throw new DirectoryNotFoundException($"Testset root not found: {testsetRoot}");
28-
29-
foreach (var maidataPath in Directory.EnumerateFiles(testsetRoot, "maidata.txt", SearchOption.AllDirectories))
30-
{
31-
var maidataTxt = File.ReadAllText(maidataPath, Encoding.UTF8);
32-
var maidata = new Maidata(maidataTxt);
33-
foreach (var id in maidata.Levels.Keys.OrderBy(k => k))
34-
yield return [new TestInput(maidataPath, id)];
35-
}
36-
}
17+
public static IEnumerable<object[]> GetTestInputs(string dataDir) => TestUtils.GetTestInputs(dataDir);
3718

19+
// TODO 再多找一些自制谱加进来吧
3820
[Theory]
39-
[MemberData(nameof(AllCharts), "自制谱")]
21+
[MemberData(nameof(GetTestInputs), "自制谱")]
4022
public void 自制谱转MA2测试(TestInput c) => TestChart(c);
4123

4224
private void TestChart(TestInput input)

tests/TestUtils.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ public static Chart LoadOneChart(out List<Alert> alerts)
4040
Assert.DoesNotContain(alerts, a => a.Level >= Alert.LEVEL.Error);
4141
return chart;
4242
}
43+
44+
public static IEnumerable<object[]> GetTestInputs(string dataDir, int? lv = null, string? title = null)
45+
{
46+
var repoRoot = FindRepoRoot();
47+
var testsetRoot = Path.Combine(repoRoot.FullName, "tests", "testset", dataDir);
48+
if (!Directory.Exists(testsetRoot))
49+
throw new DirectoryNotFoundException($"Testset root not found: {testsetRoot}");
50+
51+
foreach (var maidataPath in Directory.EnumerateFiles(testsetRoot, "maidata.txt", SearchOption.AllDirectories))
52+
{
53+
var maidataTxt = File.ReadAllText(maidataPath, Encoding.UTF8);
54+
var maidata = new Maidata(maidataTxt);
55+
foreach (var id in maidata.Levels.Keys.OrderBy(k => k))
56+
{
57+
// 如果指定了lv或title、但与要求不符,则不返回
58+
if ((lv != null && id != lv) || (title != null && !maidataPath.Contains("title"))) continue;
59+
yield return [new TestInput(maidataPath, id)];
60+
}
61+
}
62+
}
4363
}
4464

4565
public record TestInput(string Maidata, int LevelId)

0 commit comments

Comments
 (0)