Skip to content

Commit 864342e

Browse files
committed
[Test] 给Maidata模块写测试
1 parent cedb4e5 commit 864342e

4 files changed

Lines changed: 92 additions & 9 deletions

File tree

maidata/Maidata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public override string ToString()
9898
string[] fixedKeys = ["title", "artist", "first", "des", "wholebpm"]; // 对这些键,优先、按这里指定的顺序输出。
9999
foreach (var k in fixedKeys)
100100
{
101-
if (TryGetValue(k, out var v)) result.AppendLine($"&{k}=v");
101+
if (TryGetValue(k, out var v)) result.AppendLine($"&{k}={v}");
102102
}
103103

104104
var (levels, infos) = _splitLevels();

tests/Maidata模块测试.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.Text;
2+
using MuConvert.maidata;
3+
using static MuConvert.Tests.TestUtils;
4+
5+
namespace MuConvert.Tests;
6+
7+
/// <summary>
8+
/// Maidata 读写与 ToString 往返:testset 中的 maidata.txt 为输入。
9+
/// 说明:ToString 会追加 ChartConvertTool 元数据,且键顺序与原始文件可能不同,故不做整文件逐字相等;
10+
/// 往返后应得到与原字典相同的条目(另加工具键)。
11+
/// </summary>
12+
public class Maidata模块测试
13+
{
14+
private const string ChartConvertToolKey = "ChartConvertTool";
15+
private const string ChartConvertToolVersionKey = "ChartConvertToolVersion";
16+
17+
public static IEnumerable<object[]> MaidataFiles()
18+
{
19+
var root = FindRepoRoot();
20+
var dir = Path.Combine(root.FullName, "tests", "testset", "自制谱");
21+
string[] TO_TEST_CHARTS = ["Pre-STAR", "蝴蝶", "流光(Light Me Up)"];
22+
foreach (var chart in TO_TEST_CHARTS)
23+
{
24+
var path = Path.Combine(dir, chart, "maidata.txt");
25+
if (!File.Exists(path)) throw new FileNotFoundException($"Testset not found: {path}");
26+
yield return [path];
27+
}
28+
}
29+
30+
[Theory]
31+
[MemberData(nameof(MaidataFiles))]
32+
public void 读入为Maidata_再ToString写出(string path)
33+
{
34+
var originalText = File.ReadAllText(path, Encoding.UTF8);
35+
var m = new Maidata(originalText);
36+
37+
Assert.False(string.IsNullOrWhiteSpace(m.Title));
38+
Assert.False(string.IsNullOrWhiteSpace(m.Artist));
39+
Assert.NotEmpty(m.Levels);
40+
41+
foreach (var (id, chart) in m.Levels.OrderBy(x => x.Key))
42+
{
43+
Assert.False(string.IsNullOrWhiteSpace(chart.Level), $"lv_{id} should be set in {path}");
44+
Assert.False(string.IsNullOrWhiteSpace(chart.Inote), $"inote_{id} should be non-empty in {path}");
45+
}
46+
47+
Assert.True(m.First >= -10f && m.First <= 10f, $"sanity: first in reasonable range for {path}");
48+
Assert.True(m.ClockCount >= 1 && m.ClockCount <= 192, $"sanity: clock_count for {path}");
49+
50+
var folder = Path.GetFileName(Path.GetDirectoryName(path))!;
51+
Assert.Equal(folder, m.Title);
52+
53+
var serialized = m.ToString();
54+
var m2 = new Maidata(serialized);
55+
AssertMaidataDictionaryEquivalent(m, m2);
56+
57+
Assert.True(m2.ContainsKey(ChartConvertToolKey));
58+
Assert.Equal("MuConvert", m2[ChartConvertToolKey]);
59+
Assert.True(m2.ContainsKey(ChartConvertToolVersionKey));
60+
Assert.False(string.IsNullOrWhiteSpace(m2[ChartConvertToolVersionKey]));
61+
}
62+
63+
private static void AssertMaidataDictionaryEquivalent(Maidata original, Maidata roundTrip)
64+
{
65+
foreach (var kv in original)
66+
Assert.Equal(kv.Value, roundTrip[kv.Key]);
67+
68+
foreach (var k in roundTrip.Keys)
69+
{
70+
if (k is ChartConvertToolKey or ChartConvertToolVersionKey)
71+
continue;
72+
Assert.True(original.ContainsKey(k), $"Unexpected key in round-trip: {k}");
73+
}
74+
}
75+
}

tests/TestUtils.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace MuConvert.Tests;
2+
3+
internal static class TestUtils
4+
{
5+
/// <summary>
6+
/// 从测试运行目录向上查找包含 MuConvert.csproj 的仓库根目录。
7+
/// </summary>
8+
public static DirectoryInfo FindRepoRoot()
9+
{
10+
var dir = new DirectoryInfo(AppContext.BaseDirectory);
11+
while (dir != null && !File.Exists(Path.Combine(dir.FullName, "MuConvert.csproj")))
12+
dir = dir.Parent;
13+
return dir ?? throw new DirectoryNotFoundException("Could not locate repo root (MuConvert.csproj).");
14+
}
15+
}

tests/自制谱测试.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using MuConvert.parser.simai;
55
using MuConvert.utils;
66
using Xunit.Abstractions;
7+
using static MuConvert.Tests.TestUtils;
78

89
namespace MuConvert.Tests;
910

@@ -145,12 +146,4 @@ private static void AssertTextEqual(string expected, string actual)
145146
);
146147
}
147148
}
148-
149-
private static DirectoryInfo FindRepoRoot()
150-
{
151-
var dir = new DirectoryInfo(AppContext.BaseDirectory);
152-
while (dir != null && !File.Exists(Path.Combine(dir.FullName, "MuConvert.csproj")))
153-
dir = dir.Parent;
154-
return dir ?? throw new DirectoryNotFoundException("Could not locate repo root (MuConvert.csproj).");
155-
}
156149
}

0 commit comments

Comments
 (0)