1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 07:32:55 +08:00

Refactor beatmap encoder test to be a bit easier to understand

This commit is contained in:
Dean Herbert 2020-05-11 16:30:54 +09:00
parent 7072207c7c
commit 93440874db

View File

@ -28,14 +28,15 @@ namespace osu.Game.Tests.Beatmaps.Formats
private static IEnumerable<string> allBeatmaps => TestResources.GetStore().GetAvailableResources().Where(res => res.EndsWith(".osu"));
[TestCaseSource(nameof(allBeatmaps))]
public void TestBeatmap(string name)
public void TestEncodeDecodeStability(string name)
{
var decoded = decode(name, out var encoded);
var decoded = decodeFromLegacy(TestResources.GetStore().GetStream(name));
var decodedAfterEncode = decodeFromLegacy(encodeToLegacy(decoded));
sort(decoded);
sort(encoded);
sort(decodedAfterEncode);
Assert.That(encoded.Serialize(), Is.EqualTo(decoded.Serialize()));
Assert.That(decodedAfterEncode.Serialize(), Is.EqualTo(decoded.Serialize()));
}
private void sort(IBeatmap beatmap)
@ -48,27 +49,22 @@ namespace osu.Game.Tests.Beatmaps.Formats
}
}
private IBeatmap decode(string filename, out IBeatmap encoded)
private IBeatmap decodeFromLegacy(Stream stream)
{
using (var stream = TestResources.GetStore().GetStream(filename))
using (var sr = new LineBufferedReader(stream))
{
var legacyDecoded = convert(new LegacyBeatmapDecoder { ApplyOffsets = false }.Decode(sr));
using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms))
using (var sr2 = new LineBufferedReader(ms, true))
{
new LegacyBeatmapEncoder(legacyDecoded).Encode(sw);
sw.Flush();
ms.Position = 0;
encoded = convert(new LegacyBeatmapDecoder { ApplyOffsets = false }.Decode(sr2));
return legacyDecoded;
}
using (var reader = new LineBufferedReader(stream))
return convert(new LegacyBeatmapDecoder { ApplyOffsets = false }.Decode(reader));
}
private Stream encodeToLegacy(IBeatmap beatmap)
{
var stream = new MemoryStream();
using (var writer = new StreamWriter(stream, leaveOpen: true))
new LegacyBeatmapEncoder(beatmap).Encode(writer);
stream.Position = 0;
return stream;
}
private IBeatmap convert(IBeatmap beatmap)