-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from andy840119/throw-exception-if-lec-format…
…-is-illegal Add try-catch to deal with illegal format.
- Loading branch information
Showing
2 changed files
with
76 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using osu.Game.Beatmaps; | ||
using osu.Game.IO; | ||
using osu.Game.Rulesets.Karaoke.Beatmaps.Formats; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
|
@@ -14,38 +16,57 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Beatmaps.Formats | |
public class LrcDecoderTest | ||
{ | ||
[Test] | ||
public void TestDecodeNote() | ||
public void TestDecodeLyric() | ||
{ | ||
const string lyric_text = "[00:01.00]か[00:02.00]ら[00:03.00]お[00:04.00]け[00:05.00]"; | ||
var beatmap = decodeLrcLine(lyric_text); | ||
|
||
// Get first beatmap | ||
var lyric = beatmap.HitObjects.OfType<LyricLine>().FirstOrDefault(); | ||
|
||
// Check lyric | ||
Assert.AreEqual(lyric?.Text, "からおけ"); | ||
Assert.AreEqual(lyric?.StartTime, 1000); | ||
Assert.AreEqual(lyric?.EndTime, 5000); | ||
|
||
// Check time tag | ||
var tags = lyric?.TimeTags; | ||
var checkedTags = tags.ToArray(); | ||
Assert.AreEqual(tags.Count, 5); | ||
Assert.AreEqual(checkedTags.Length, 5); | ||
Assert.AreEqual(string.Join(',', tags.Select(x => x.Key.Index)), "0,1,2,3,4"); | ||
Assert.AreEqual(string.Join(',', tags.Select(x => x.Value)), "1000,2000,3000,4000,5000"); | ||
} | ||
|
||
[Test] | ||
public void TestDecodeLyricWithDulicatedTimeTag() | ||
{ | ||
const string wrong_lyric_text = "[00:04.00]か[00:04.00]ら[00:05.00]お[00:06.00]け[00:07.00]"; | ||
Assert.Throws<FormatException>(() => decodeLrcLine(wrong_lyric_text)); | ||
} | ||
|
||
[Test] | ||
[Ignore("Waiting for lyric parser update.")] | ||
public void TestDecodeLyricWithTimeTagNotOrder() | ||
{ | ||
const string wrong_lyric_text = "[00:04.00]か[00:03.00]ら[00:02.00]お[00:01.00]け[00:00.00]"; | ||
Assert.Throws<FormatException>(() => decodeLrcLine(wrong_lyric_text)); | ||
} | ||
|
||
private Beatmap decodeLrcLine(string line) | ||
{ | ||
using (var stream = new MemoryStream()) | ||
using (var writer = new StreamWriter(stream)) | ||
using (var reader = new LineBufferedReader(stream)) | ||
{ | ||
// Create stream | ||
writer.Write(lyric_text); | ||
writer.Write(line); | ||
writer.Flush(); | ||
stream.Position = 0; | ||
|
||
// Create karaoke note decoder | ||
var decoder = new LrcDecoder(); | ||
var beatmap = decoder.Decode(reader); | ||
|
||
// Get first beatmap | ||
var lyric = beatmap.HitObjects.OfType<LyricLine>().FirstOrDefault(); | ||
|
||
// Check lyric | ||
Assert.AreEqual(lyric?.Text, "からおけ"); | ||
Assert.AreEqual(lyric?.StartTime, 1000); | ||
Assert.AreEqual(lyric?.EndTime, 5000); | ||
|
||
// Check time tag | ||
var tags = lyric?.TimeTags; | ||
var checkedTags = tags.ToArray(); | ||
Assert.AreEqual(tags.Count, 5); | ||
Assert.AreEqual(checkedTags.Length, 5); | ||
Assert.AreEqual(string.Join(',', tags.Select(x => x.Key.Index)), "0,1,2,3,4"); | ||
Assert.AreEqual(string.Join(',', tags.Select(x => x.Value)), "1000,2000,3000,4000,5000"); | ||
return decoder.Decode(reader); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters