Skip to content

Commit

Permalink
Merge pull request #337 from andy840119/re-write-generator-test-case
Browse files Browse the repository at this point in the history
Re write generator test case
  • Loading branch information
andy840119 authored Dec 21, 2020
2 parents 25764f2 + 951f136 commit a70820c
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 194 deletions.
25 changes: 25 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Asserts/TimeTagAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Asserts
{
public class TimeTagAssert : Assert
{
public static void AreEqual(IReadOnlyList<TimeTag> expect, IReadOnlyList<TimeTag> actually)
{
AreEqual(expect?.Count, actually?.Count);
if (expect == null || actually == null)
return;

for (int i = 0; i < expect.Count; i++)
{
AreEqual(expect[i].Index, actually[i].Index);
AreEqual(expect[i].Time, actually[i].Time);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,51 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Edit.Generator.RubyTags.Ja;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Helper;

namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.RubyTags.Ja
{
[TestFixture]
public class JaRubyTagGeneratorTest
{
[TestCase("花火大会", new[] { "はなび", "たいかい" })]
[TestCase("花火大会", new[] { "[0,2]:はなび", "[2,4]:たいかい" })]
[TestCase("はなび", new string[] { })]
public void TestCreateRubyTags(string text, string[] ruby)
public void TestCreateRubyTags(string text, string[] actualRuby)
{
var config = generatorConfig(null);
RunRubyCheckTest(text, ruby, config);
RunRubyCheckTest(text, actualRuby, config);
}

[TestCase("花火大会", new[] { "ハナビ", "タイカイ" })]
[TestCase("花火大会", new[] { "[0,2]:ハナビ", "[2,4]:タイカイ" })]
[TestCase("ハナビ", new string[] { })]
public void TestCreateRubyTagsWithRubyAsKatakana(string text, string[] ruby)
public void TestCreateRubyTagsWithRubyAsKatakana(string text, string[] actualRuby)
{
var config = generatorConfig(nameof(JaRubyTagGeneratorConfig.RubyAsKatakana));
RunRubyCheckTest(text, ruby, config);
RunRubyCheckTest(text, actualRuby, config);
}

[TestCase("はなび", new[] { "はな", "び" })]
[TestCase("ハナビ", new[] { "はなび" })]
public void TestCreateRubyTagsWithEnableDuplicatedRuby(string text, string[] ruby)
[TestCase("はなび", new[] { "[0,2]:はな", "[2,3]:び" })]
[TestCase("ハナビ", new[] { "[0,3]:はなび" })]
public void TestCreateRubyTagsWithEnableDuplicatedRuby(string text, string[] actualRuby)
{
var config = generatorConfig(nameof(JaRubyTagGeneratorConfig.EnableDuplicatedRuby));
RunRubyCheckTest(text, ruby, config);
RunRubyCheckTest(text, actualRuby, config);
}

#region test helper

protected void RunRubyCheckTest(string text, string[] ruby, JaRubyTagGeneratorConfig config)
protected void RunRubyCheckTest(string text, string[] actualRuby, JaRubyTagGeneratorConfig config)
{
var generator = new JaRubyTagGenerator(config);

var lyric = new Lyric { Text = text };
var rubyTags = generator.CreateRubyTags(lyric);
var actualRubyTags = TestCaseTagHelper.ParseRubyTags(actualRuby);

Assert.AreEqual(rubyTags.Length, ruby.Length);

foreach (var rubyTag in rubyTags)
{
Assert.IsTrue(ruby.Contains(rubyTag.Text));
}
Assert.AreEqual(rubyTags, actualRubyTags);
}

private JaRubyTagGeneratorConfig generatorConfig(params string[] properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Edit.Generator.TimeTags;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Asserts;
using osu.Game.Rulesets.Karaoke.Tests.Helper;

namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.TimeTags
{
public abstract class BaseTimeTagGeneratorTest<TTimeTagGenerator, TConfig>
where TTimeTagGenerator : TimeTagGenerator<TConfig> where TConfig : TimeTagGeneratorConfig, new()
{
protected void RunTimeTagCheckTest(string lyricText, double[] index, TConfig config)
protected void RunTimeTagCheckTest(string text, string[] actualTimeTags, TConfig config)
{
var lyric = generateLyric(lyricText);
RunTimeTagCheckTest(lyric, index, config);
var lyric = new Lyric { Text = text };
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

protected void RunTimeTagCheckTest(Lyric lyric, double[] index, TConfig config)
protected void RunTimeTagCheckTest(Lyric lyric, string[] actualTimeTags, TConfig config)
{
var generator = Activator.CreateInstance(typeof(TTimeTagGenerator), config) as TTimeTagGenerator;

// create time tag and actually time tag.
var timeTags = getTimeTagIndex(generator?.CreateTimeTags(lyric));
var actualIndexed = getTimeTagIndexByArray(index);
var timeTags = generator?.CreateTimeTags(lyric);
var actualIndexed = TestCaseTagHelper.ParseTimeTags(actualTimeTags);

// check should be equal
Assert.AreEqual(timeTags, actualIndexed);
TimeTagAssert.AreEqual(timeTags, actualIndexed);
}

protected TConfig GeneratorConfig(params string[] properties)
Expand All @@ -51,23 +50,5 @@ protected TConfig GeneratorConfig(params string[] properties)

return config;
}

#region test helper

private TimeTagIndex[] getTimeTagIndex(TimeTag[] timeTags)
=> timeTags.Select((v, i) => v.Index).ToArray();

private TimeTagIndex[] getTimeTagIndexByArray(double[] timeTags)
=> timeTags.Select(timeTag =>
{
var state = Math.Abs(timeTag) % 1 == 0.5 ? TimeTagIndex.IndexState.End : TimeTagIndex.IndexState.Start;
var index = (int)timeTag;
return new TimeTagIndex(index, state);
}).ToArray();

private Lyric generateLyric(string text)
=> new Lyric { Text = text };

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,87 +11,87 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.TimeTags.Ja
public class JaTimeTagGeneratorTest : BaseTimeTagGeneratorTest<JaTimeTagGenerator, JaTimeTagGeneratorConfig>
{
[Ignore("This feature has not been implemented")]
public void TestLyricWithCheckLineEnd(string lyric, double[] index, bool applyConfig)
public void TestLyricWithCheckLineEnd(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckLineEnd) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[TestCase("か", new double[] { 0 }, false)]
[TestCase("か", new[] { 0, 0.5 }, true)]
public void TestLyricWithCheckLineEndKeyUp(string lyric, double[] index, bool applyConfig)
[TestCase("か", new[] { "[0,start]:" }, false)]
[TestCase("か", new[] { "[0,start]:", "[0,end]:" }, true)]
public void TestLyricWithCheckLineEndKeyUp(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckLineEndKeyUp) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[Ignore("This feature has not been implemented")]
public void TestLyricWithCheckBlankLine(string lyric, double[] index, bool applyConfig)
public void TestLyricWithCheckBlankLine(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckBlankLine) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[TestCase(" ", new double[] { 0, 1, 2, 3, 4 }, false)]
[TestCase(" ", new double[] { 0 }, true)]
public void TestLyricWithCheckWhiteSpace(string lyric, double[] index, bool applyConfig)
[TestCase(" ", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:" }, false)]
[TestCase(" ", new[] { "[0,start]:" }, true)]
public void TestLyricWithCheckWhiteSpace(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[Ignore("This feature has not been implemented")]
public void TestLyricWithCheckWhiteSpaceKeyUp(string lyric, double[] index, bool applyConfig)
public void TestLyricWithCheckWhiteSpaceKeyUp(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceKeyUp) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[TestCase("a b c d e", new double[] { 0, 2, 4, 6, 8 }, false)]
[TestCase("a b c d e", new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, true)]
[TestCase("A B C D E", new double[] { 0, 2, 4, 6, 8 }, false)]
[TestCase("A B C D E", new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, true)]
public void TestLyricWithCheckWhiteSpaceAlphabet(string lyric, double[] index, bool applyConfig)
[TestCase("a b c d e", new[] { "[0,start]:", "[2,start]:", "[4,start]:", "[6,start]:", "[8,start]:" }, false)]
[TestCase("a b c d e", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:", "[5,start]:", "[6,start]:", "[7,start]:", "[8,start]:" }, true)]
[TestCase("A B C D E", new[] { "[0,start]:", "[2,start]:", "[4,start]:", "[6,start]:", "[8,start]:" }, false)]
[TestCase("A B C D E", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:", "[5,start]:", "[6,start]:", "[7,start]:", "[8,start]:" }, true)]
public void TestLyricWithCheckWhiteSpaceAlphabet(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceAlphabet) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[TestCase("0 1 2 3 4", new double[] { 0, 2, 4, 6, 8 }, false)]
[TestCase("0 1 2 3 4", new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, true)]
[TestCase("0 1 2 3 4", new double[] { 0, 2, 4, 6, 8 }, false)]
[TestCase("0 1 2 3 4", new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, true)]
public void TestLyricWithCheckWhiteSpaceDigit(string lyric, double[] index, bool applyConfig)
[TestCase("0 1 2 3 4", new[] { "[0,start]:", "[2,start]:", "[4,start]:", "[6,start]:", "[8,start]:" }, false)]
[TestCase("0 1 2 3 4", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:", "[5,start]:", "[6,start]:", "[7,start]:", "[8,start]:" }, true)]
[TestCase("0 1 2 3 4", new[] { "[0,start]:", "[2,start]:", "[4,start]:", "[6,start]:", "[8,start]:" }, false)]
[TestCase("0 1 2 3 4", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:", "[5,start]:", "[6,start]:", "[7,start]:", "[8,start]:" }, true)]
public void TestLyricWithCheckWhiteSpaceDigit(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceDigit) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[TestCase("! ! ! ! !", new double[] { 0, 2, 4, 6, 8 }, false)]
[TestCase("! ! ! ! !", new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, true)]
public void TestLyricWitCheckWhiteSpaceAsciiSymbol(string lyric, double[] index, bool applyConfig)
[TestCase("! ! ! ! !", new[] { "[0,start]:", "[2,start]:", "[4,start]:", "[6,start]:", "[8,start]:" }, false)]
[TestCase("! ! ! ! !", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:", "[5,start]:", "[6,start]:", "[7,start]:", "[8,start]:" }, true)]
public void TestLyricWitCheckWhiteSpaceAsciiSymbol(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceAsciiSymbol) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[TestCase("がんばって", new double[] { 0, 2, 4 }, false)]
[TestCase("がんばって", new double[] { 0, 1, 2, 4 }, true)]
public void TestLyricWithCheckWhiteCheckん(string lyric, double[] index, bool applyConfig)
[TestCase("がんばって", new[] { "[0,start]:", "[2,start]:", "[4,start]:" }, false)]
[TestCase("がんばって", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[4,start]:" }, true)]
public void TestLyricWithCheckWhiteCheckん(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.Checkん) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[TestCase("買って", new double[] { 0, 2 }, false)]
[TestCase("買って", new double[] { 0, 1, 2 }, true)]
public void TestLyricWithCheckっ(string lyric, double[] index, bool applyConfig)
[TestCase("買って", new[] { "[0,start]:", "[2,start]:" }, false)]
[TestCase("買って", new[] { "[0,start]:", "[1,start]:", "[2,start]:" }, true)]
public void TestLyricWithCheckっ(string lyric, string[] actualTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.Checkっ) : null);
RunTimeTagCheckTest(lyric, index, config);
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}

[Test]
Expand All @@ -118,8 +118,23 @@ public void TestTagWithRubyLyric()
}
};

var result = new double[] { 0, 0, 0, 2, 4, 6, 7, 7, 8, 9, 10, 12, 13 };
RunTimeTagCheckTest(lyric, result, config);
var actualTimeTags = new[]
{
"[0,start]:",
"[0,start]:",
"[0,start]:",
"[2,start]:",
"[4,start]:",
"[6,start]:",
"[7,start]:",
"[7,start]:",
"[8,start]:",
"[9,start]:",
"[10,start]:",
"[12,start]:",
"[13,start]:"
};
RunTimeTagCheckTest(lyric, actualTimeTags, config);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Edit.Generator.TimeTags.Zh
[TestFixture]
public class ZhTimeTagGeneratorTest : BaseTimeTagGeneratorTest<ZhTimeTagGenerator, ZhTimeTagGeneratorConfig>
{
[TestCase("測試一些歌詞", new[] { 0, 1, 2, 3, 4, 5, 5.5 })]
[TestCase("拉拉拉~~~", new[] { 0, 1, 2, 5.5 })]
[TestCase("拉~拉~拉~", new[] { 0, 2, 4, 5.5 })]
public void TestLyricWithCheckLineEndKeyUp(string lyric, double[] index)
[TestCase("測試一些歌詞", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:", "[5,start]:", "[5,end]:" })]
[TestCase("拉拉拉~~~", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[5,end]:" })]
[TestCase("拉~拉~拉~", new[] { "[0,start]:", "[2,start]:", "[4,start]:", "[5,end]:" })]
public void TestLyricWithCheckLineEndKeyUp(string lyric, string[] index)
{
var config = GeneratorConfig(nameof(ZhTimeTagGeneratorConfig.CheckLineEndKeyUp));
RunTimeTagCheckTest(lyric, index, config);
Expand Down
7 changes: 4 additions & 3 deletions osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseTagHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Linq;
using System.Text.RegularExpressions;
using osu.Framework.Graphics.Sprites;
Expand All @@ -26,7 +27,7 @@ public static RubyTag ParseRubyTag(string str)
var regex = new Regex("(?<start>[-0-9]+),(?<end>[-0-9]+)]:(?<ruby>.*$)");
var result = regex.Match(str);
if (!result.Success)
return new RubyTag();
throw new ArgumentException(nameof(str));

var startIndex = int.Parse(result.Groups["start"]?.Value);
var endIndex = int.Parse(result.Groups["end"]?.Value);
Expand Down Expand Up @@ -56,7 +57,7 @@ public static RomajiTag ParseRomajiTag(string str)
var regex = new Regex("(?<start>[-0-9]+),(?<end>[-0-9]+)]:(?<romaji>.*$)");
var result = regex.Match(str);
if (!result.Success)
return new RomajiTag();
throw new ArgumentException(nameof(str));

var startIndex = int.Parse(result.Groups["start"]?.Value);
var endIndex = int.Parse(result.Groups["end"]?.Value);
Expand Down Expand Up @@ -86,7 +87,7 @@ public static TimeTag ParseTimeTag(string str)
var regex = new Regex("(?<index>[-0-9]+),(?<state>start|end)]:(?<time>[-0-9]+|s*|)");
var result = regex.Match(str);
if (!result.Success)
return new TimeTag(new TimeTagIndex());
throw new ArgumentException(nameof(str));

var index = int.Parse(result.Groups["index"]?.Value);
var state = result.Groups["state"]?.Value == "start" ? TimeTagIndex.IndexState.Start : TimeTagIndex.IndexState.End;
Expand Down
Loading

0 comments on commit a70820c

Please sign in to comment.