222 lines
8.7 KiB
C#
222 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using NUnit.Framework;
|
|
|
|
public class MiniJsonTests
|
|
{
|
|
[Test]
|
|
public void Parses_nested_values()
|
|
{
|
|
var o = MiniJson.AsObject(MiniJson.Parse("{\"a\":1,\"b\":[true,null,\"x\\n\\u0041\"],\"c\":{\"d\":-2.5e1}}"));
|
|
Assert.AreEqual(1.0, MiniJson.GetNumber(o, "a"));
|
|
var b = MiniJson.AsList(o["b"]);
|
|
Assert.AreEqual(true, b[0]);
|
|
Assert.IsNull(b[1]);
|
|
Assert.AreEqual("x\nA", b[2]);
|
|
Assert.AreEqual(-25.0, MiniJson.GetNumber(MiniJson.AsObject(o["c"]), "d"));
|
|
}
|
|
|
|
[Test]
|
|
public void Missing_keys_are_distinguishable_from_zero()
|
|
{
|
|
var o = MiniJson.AsObject(MiniJson.Parse("{\"pos\":{\"x\":0,\"y\":0}}"));
|
|
Assert.IsNotNull(MiniJson.Get(o, "pos"));
|
|
Assert.IsNull(MiniJson.Get(o, "path"));
|
|
Assert.IsFalse(MiniJson.TryGetNumber(o, "end", out _));
|
|
Assert.AreEqual(7, MiniJson.GetInt(o, "end", 7));
|
|
}
|
|
|
|
[TestCase("{\"a\":}")]
|
|
[TestCase("[1,2")]
|
|
[TestCase("{\"a\":1} x")]
|
|
[TestCase("")]
|
|
public void Rejects_malformed_json(string json)
|
|
{
|
|
Assert.Throws<FormatException>(() => MiniJson.Parse(json));
|
|
}
|
|
}
|
|
|
|
public class ChartLoaderTests
|
|
{
|
|
// Maker 統一格式:tap、hold、slide,加上三顆應該被略過的音符
|
|
const string Chart =
|
|
"{\"format\":1,\"divisor\":4,\"notes\":[" +
|
|
"{\"t\":1000,\"lane\":\"L\",\"type\":\"tap\",\"pos\":{\"x\":0,\"y\":3}}," +
|
|
"{\"t\":2000,\"lane\":\"R\",\"type\":\"hold\",\"end\":2500,\"pos\":{\"x\":3,\"y\":1},\"locked\":true}," +
|
|
"{\"t\":3000,\"lane\":\"L\",\"type\":\"slide\",\"end\":3400,\"path\":[{\"x\":0,\"y\":0},{\"x\":1,\"y\":1},{\"x\":2,\"y\":2}]}," +
|
|
"{\"t\":4000,\"lane\":\"M\",\"type\":\"tap\"}," + // 未配置座標
|
|
"{\"t\":5000,\"type\":\"tap\",\"pos\":{\"x\":9,\"y\":0}}," + // 超出格子
|
|
"{\"t\":6000,\"type\":\"spin\",\"pos\":{\"x\":1,\"y\":1}}]}"; // 不認識的型別
|
|
|
|
[Test]
|
|
public void Parses_unified_format_and_skips_unusable_notes()
|
|
{
|
|
var ev = ChartLoader.Parse(Chart, 0, out int skipped);
|
|
Assert.AreEqual(3, skipped);
|
|
Assert.AreEqual(5, ev.Count);
|
|
|
|
Assert.AreEqual("tap", ev[0].type);
|
|
Assert.AreEqual(1.0, ev[0].hitTime, 1e-9);
|
|
Assert.AreEqual((0, 3), (ev[0].x, ev[0].y));
|
|
|
|
Assert.AreEqual("hold", ev[1].type);
|
|
Assert.AreEqual(500, ev[1].holdMs);
|
|
|
|
// slide:第一格在 t、最後一格在 end,中間平均分布
|
|
Assert.AreEqual("slide", ev[2].type);
|
|
Assert.AreEqual(3.0, ev[2].hitTime, 1e-9);
|
|
Assert.AreEqual(3.2, ev[3].hitTime, 1e-9);
|
|
Assert.AreEqual(3.4, ev[4].hitTime, 1e-9);
|
|
Assert.AreEqual((2, 2), (ev[4].x, ev[4].y));
|
|
}
|
|
|
|
[Test]
|
|
public void Applies_pack_offset()
|
|
{
|
|
var ev = ChartLoader.Parse(Chart, 30, out _);
|
|
Assert.AreEqual(1.03, ev[0].hitTime, 1e-9);
|
|
Assert.AreEqual(3.43, ev[4].hitTime, 1e-9);
|
|
}
|
|
|
|
[Test]
|
|
public void Accepts_bare_note_array()
|
|
{
|
|
var ev = ChartLoader.Parse("[{\"t\":500,\"type\":\"tap\",\"pos\":{\"x\":2,\"y\":2}}]", 0, out int skipped);
|
|
Assert.AreEqual(1, ev.Count);
|
|
Assert.AreEqual(0, skipped);
|
|
}
|
|
}
|
|
|
|
public class SongInfoTests
|
|
{
|
|
[Test]
|
|
public void Parses_config()
|
|
{
|
|
var s = SongInfo.FromConfig(
|
|
"{\"id\":\"my-song\",\"name\":\"N\",\"artist\":\"A\",\"bpm\":174.5,\"beat_offset\":1216,\"offset\":-10," +
|
|
"\"preview_start\":60000,\"easy_num\":4,\"hard_num\":15}", "x");
|
|
Assert.AreEqual("my-song", s.id);
|
|
Assert.AreEqual(174.5, s.bpm);
|
|
Assert.AreEqual(-10, s.offsetMs);
|
|
Assert.AreEqual(60000, s.previewStartMs);
|
|
Assert.AreEqual(15000, s.previewLengthMs); // 預設值
|
|
Assert.AreEqual(4, s.levelNum[1]);
|
|
Assert.AreEqual(0, s.levelNum[2]);
|
|
Assert.AreEqual(15, s.levelNum[3]);
|
|
}
|
|
|
|
[TestCase("{\"id\":\"Bad ID\",\"name\":\"N\"}")]
|
|
[TestCase("{\"id\":\"ok-id\"}")]
|
|
[TestCase("[1,2]")]
|
|
public void Rejects_invalid_config(string json)
|
|
{
|
|
Assert.Throws<FormatException>(() => SongInfo.FromConfig(json, "x"));
|
|
}
|
|
}
|
|
|
|
public class PackImporterTests
|
|
{
|
|
const string Config = "{\"id\":\"test-song\",\"name\":\"Test\",\"artist\":\"A\",\"bpm\":120,\"normal_num\":5}";
|
|
private string tempRoot;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
tempRoot = Path.Combine(Path.GetTempPath(), "rhyculator-test-" + Path.GetRandomFileName());
|
|
Directory.CreateDirectory(tempRoot);
|
|
SongLibrary.rootOverride = tempRoot;
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
SongLibrary.rootOverride = null;
|
|
SongLibrary.Reload();
|
|
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
|
|
}
|
|
|
|
static MemoryStream MakeZip(string prefix, params (string name, string content)[] files)
|
|
{
|
|
var ms = new MemoryStream();
|
|
using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
|
|
{
|
|
foreach (var (name, content) in files)
|
|
{
|
|
var entry = zip.CreateEntry(prefix + name);
|
|
using var w = new StreamWriter(entry.Open(), new UTF8Encoding(false));
|
|
w.Write(content);
|
|
}
|
|
}
|
|
ms.Position = 0;
|
|
return ms;
|
|
}
|
|
|
|
[Test]
|
|
public void Imports_pack_into_library_and_ignores_other_entries()
|
|
{
|
|
var zip = MakeZip("",
|
|
("config.json", Config), ("normal.json", "{\"format\":1,\"notes\":[]}"),
|
|
("music.ogg", "x"), ("image.png", "x"),
|
|
("README.txt", "ignored"), ("../evil.txt", "ignored"), ("sub/easy.json", "ignored"));
|
|
var info = PackImporter.Import(zip);
|
|
|
|
Assert.AreEqual("test-song", info.id);
|
|
string dir = Path.Combine(tempRoot, "test-song");
|
|
Assert.IsTrue(File.Exists(Path.Combine(dir, "normal.json")));
|
|
Assert.IsFalse(File.Exists(Path.Combine(dir, "README.txt")));
|
|
Assert.IsFalse(File.Exists(Path.Combine(dir, "easy.json")));
|
|
Assert.IsFalse(File.Exists(Path.Combine(tempRoot, "evil.txt")));
|
|
Assert.IsTrue(info.HasLevel(2));
|
|
Assert.IsFalse(info.HasLevel(1));
|
|
Assert.AreEqual(2, info.FirstLevel());
|
|
Assert.AreEqual(5, info.levelNum[2]);
|
|
|
|
SongLibrary.Reload();
|
|
Assert.AreEqual(1, SongLibrary.Songs.Count);
|
|
Assert.AreEqual("test-song", SongLibrary.Find("test-song").id);
|
|
}
|
|
|
|
[Test]
|
|
public void Accepts_folder_prefix_and_reimport_replaces_old_files()
|
|
{
|
|
PackImporter.Import(MakeZip("test-song/", ("config.json", Config), ("easy.json", "{\"notes\":[]}"), ("music.wav", "x"), ("image.png", "x")));
|
|
PackImporter.Import(MakeZip("", ("config.json", Config), ("hard.json", "{\"notes\":[]}"), ("music.ogg", "x"), ("image.png", "x")));
|
|
|
|
string dir = Path.Combine(tempRoot, "test-song");
|
|
Assert.IsTrue(File.Exists(Path.Combine(dir, "hard.json")));
|
|
Assert.IsFalse(File.Exists(Path.Combine(dir, "easy.json")), "重新匯入要整個取代舊資料夾");
|
|
Assert.IsFalse(File.Exists(Path.Combine(dir, "music.wav")));
|
|
}
|
|
|
|
[Test]
|
|
public void Rejects_incomplete_packs_without_leaving_files()
|
|
{
|
|
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("easy.json", "{}"))));
|
|
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", Config), ("music.ogg", "x"), ("image.png", "x"))));
|
|
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", Config), ("easy.json", "{}"), ("image.png", "x"))));
|
|
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", Config), ("easy.json", "{}"), ("music.ogg", "x"))));
|
|
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", "{\"id\":\"BAD\",\"name\":\"n\"}"), ("easy.json", "{}"), ("music.ogg", "x"), ("image.png", "x"))));
|
|
Assert.IsFalse(Directory.Exists(Path.Combine(tempRoot, "test-song")));
|
|
}
|
|
|
|
[Test]
|
|
public void Inbox_imports_and_deletes_zips()
|
|
{
|
|
string inbox = SongLibrary.InboxDir;
|
|
Directory.CreateDirectory(inbox);
|
|
File.WriteAllBytes(Path.Combine(inbox, "a.zip"),
|
|
MakeZip("", ("config.json", Config), ("easy.json", "{\"notes\":[]}"), ("music.ogg", "x"), ("image.png", "x")).ToArray());
|
|
File.WriteAllText(Path.Combine(inbox, "bad.zip"), "not a zip");
|
|
|
|
var errors = new List<string>();
|
|
var imported = PackImporter.ImportInbox(errors);
|
|
|
|
Assert.AreEqual(1, imported.Count);
|
|
Assert.AreEqual(1, errors.Count);
|
|
Assert.AreEqual(0, Directory.GetFiles(inbox, "*.zip").Length);
|
|
}
|
|
}
|