Skip to content

Commit

Permalink
Merge pull request #1807 from andy840119/implement-base-generator-test
Browse files Browse the repository at this point in the history
Refactor the generate config method.
  • Loading branch information
andy840119 authored Dec 22, 2022
2 parents b4b3893 + 067b920 commit 8b86d64
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 osu.Game.Rulesets.Karaoke.Edit.Generator;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator;

public class BaseGeneratorTest<TConfig> where TConfig : IHasConfig<TConfig>, new()
{
protected static TConfig GeneratorConfig(Action<TConfig>? action = null)
{
var config = new TConfig();
action?.Invoke(config);
return config;
}

protected static TConfig GeneratorDefaultConfig(Action<TConfig>? action = null)
{
var config = new TConfig().CreateDefaultConfig();
action?.Invoke(config);
return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@
using System;
using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Beatmaps;
using osu.Game.Rulesets.Karaoke.Edit.Generator;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Beatmaps;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Beatmaps
{
public abstract class BaseBeatmapDetectorTest<TDetector, TObject, TConfig>
where TDetector : class, IBeatmapPropertyDetector<TObject> where TConfig : new()
: BaseGeneratorTest<TConfig>
where TDetector : class, IBeatmapPropertyDetector<TObject> where TConfig : IHasConfig<TConfig>, new()
{
protected static TConfig GeneratorConfig(params string?[] properties)
{
var config = new TConfig();

foreach (string? propertyName in properties)
{
if (propertyName == null)
continue;

var theMethod = config.GetType().GetProperty(propertyName);
if (theMethod == null)
throw new MissingMethodException("Config is not exist.");

theMethod.SetValue(config, true);
}

return config;
}

protected static TDetector GenerateDetector(TConfig config)
{
if (Activator.CreateInstance(typeof(TDetector), config) is not TDetector detector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@
namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Beatmaps
{
public abstract class BaseBeatmapGeneratorTest<TGenerator, TObject, TConfig>
: BaseGeneratorTest<TConfig>
where TGenerator : class, IBeatmapPropertyGenerator<TObject> where TConfig : IHasConfig<TConfig>, new()
{
protected static TConfig GeneratorConfig(Action<TConfig>? action = null)
{
var config = new TConfig().CreateDefaultConfig();
action?.Invoke(config);
return config;
}

protected static TGenerator GenerateGenerator(TConfig config)
{
if (Activator.CreateInstance(typeof(TGenerator), config) is not TGenerator generator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PageGeneratorTest : BaseBeatmapGeneratorTest<PageGenerator, Page[],
[TestCase(new string[] { }, false)]
public void TestCanGenerate(string[] lyrics, bool canGenerate)
{
var config = GeneratorConfig();
var config = GeneratorDefaultConfig();
var beatmap = new KaraokeBeatmap
{
HitObjects = TestCaseTagHelper.ParseLyrics(lyrics).OfType<KaraokeHitObject>().ToList(),
Expand All @@ -38,7 +38,7 @@ public void TestCanGenerate(string[] lyrics, bool canGenerate)
[Test]
public void TestGenerateWithZeroLyric()
{
var config = GeneratorConfig();
var config = GeneratorDefaultConfig();
var beatmap = new KaraokeBeatmap
{
HitObjects = new List<KaraokeHitObject>()
Expand All @@ -53,7 +53,7 @@ public void TestGenerateWithZeroLyric()
[TestCase("[1000,23000]:karaoke", new[] { 1000, 1000 + max_interval, 1000 + max_interval * 2, 23000 })]
public void TestGenerateWithSingleLyric(string lyric, double[] expectedTimes)
{
var config = GeneratorConfig();
var config = GeneratorDefaultConfig();
var beatmap = new KaraokeBeatmap
{
HitObjects = new List<KaraokeHitObject>
Expand All @@ -77,7 +77,7 @@ public void TestGenerateWithSingleLyric(string lyric, double[] expectedTimes)
[TestCase("[1000,23000]:karaoke", "[3000,4000]:karaoke", new[] { 1000, 1000 + max_interval, 1000 + max_interval * 2, 23000 })] // should ignore second lyric.
public void TestGenerateWithTwoLyrics(string firstLyric, string secondLyric, double[] expectedTimes)
{
var config = GeneratorConfig();
var config = GeneratorDefaultConfig();
var beatmap = new KaraokeBeatmap
{
HitObjects = new List<KaraokeHitObject>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,16 @@

using System;
using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Edit.Generator;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics
{
public abstract class BaseLyricDetectorTest<TDetector, TObject, TConfig>
where TDetector : class, ILyricPropertyDetector<TObject> where TConfig : new()
: BaseGeneratorTest<TConfig>
where TDetector : class, ILyricPropertyDetector<TObject> where TConfig : IHasConfig<TConfig>, new()
{
protected static TConfig GeneratorConfig(params string?[] properties)
{
var config = new TConfig();

foreach (string? propertyName in properties)
{
if (propertyName == null)
continue;

var theMethod = config.GetType().GetProperty(propertyName);
if (theMethod == null)
throw new MissingMethodException("Config is not exist.");

theMethod.SetValue(config, true);
}

return config;
}

protected static TDetector GenerateDetector(TConfig config)
{
if (Activator.CreateInstance(typeof(TDetector), config) is not TDetector detector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,16 @@

using System;
using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Edit.Generator;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics
{
public abstract class BaseLyricGeneratorTest<TGenerator, TObject, TConfig>
where TGenerator : class, ILyricPropertyGenerator<TObject> where TConfig : new()
: BaseGeneratorTest<TConfig>
where TGenerator : class, ILyricPropertyGenerator<TObject> where TConfig : IHasConfig<TConfig>, new()
{
protected static TConfig GeneratorConfig(params string?[] properties)
{
var config = new TConfig();

foreach (string? propertyName in properties)
{
if (propertyName == null)
continue;

var theMethod = config.GetType().GetProperty(propertyName);
if (theMethod == null)
throw new MissingMethodException("Config is not exist.");

theMethod.SetValue(config, true);
}

return config;
}

protected static TGenerator GenerateGenerator(TConfig config)
{
if (Activator.CreateInstance(typeof(TGenerator), config) is not TGenerator generator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void TestCanDetectWithIgnorePrefixAndPostfixSymbol(string lyricText, stri
},
detectedLyric
};
var config = GeneratorConfig("IgnorePrefixAndPostfixSymbol");
var config = GeneratorConfig(x => x.IgnorePrefixAndPostfixSymbol = true);
CheckCanDetect(lyrics, detectedLyric, canDetect, config);
}

Expand Down
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 osu.Game.Rulesets.Karaoke.Edit.Generator;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.RomajiTags;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Asserts;
Expand All @@ -9,7 +10,7 @@
namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics.RomajiTags
{
public abstract class BaseRomajiTagGeneratorTest<TRomajiTagGenerator, TConfig> : BaseLyricGeneratorTest<TRomajiTagGenerator, RomajiTag[], TConfig>
where TRomajiTagGenerator : RomajiTagGenerator<TConfig> where TConfig : RomajiTagGeneratorConfig, new()
where TRomajiTagGenerator : RomajiTagGenerator<TConfig> where TConfig : RomajiTagGeneratorConfig, IHasConfig<TConfig>, new()
{
protected static void CheckCanGenerate(string text, bool canGenerate, TConfig config)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void TestGenerate(string text, string[] expectedRomajies)
[TestCase("はなび", new[] { "[0,3]:HANABI" })]
public void TestGenerateWithUppercase(string text, string[] expectedRomajies)
{
var config = GeneratorConfig(nameof(JaRomajiTagGeneratorConfig.Uppercase));
var config = GeneratorConfig(x => x.Uppercase = true);
CheckGenerateResult(text, expectedRomajies, config);
}
}
Expand Down
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 osu.Game.Rulesets.Karaoke.Edit.Generator;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.RubyTags;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Asserts;
Expand All @@ -9,7 +10,7 @@
namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics.RubyTags
{
public abstract class BaseRubyTagGeneratorTest<TRubyTagGenerator, TConfig> : BaseLyricGeneratorTest<TRubyTagGenerator, RubyTag[], TConfig>
where TRubyTagGenerator : RubyTagGenerator<TConfig> where TConfig : RubyTagGeneratorConfig, new()
where TRubyTagGenerator : RubyTagGenerator<TConfig> where TConfig : RubyTagGeneratorConfig, IHasConfig<TConfig>, new()
{
protected static void CheckCanGenerate(string text, bool canGenerate, TConfig config)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public void TestGenerate(string text, string[] expectedRubies)
[TestCase("ハナビ", new string[] { })]
public void TestGenerateWithRubyAsKatakana(string text, string[] expectedRubies)
{
var config = GeneratorConfig(nameof(JaRubyTagGeneratorConfig.RubyAsKatakana));
var config = GeneratorConfig(x => x.RubyAsKatakana = true);
CheckGenerateResult(text, expectedRubies, config);
}

[TestCase("はなび", new[] { "[0,2]:はな", "[2,3]:び" })]
[TestCase("ハナビ", new[] { "[0,3]:はなび" })]
public void TestGenerateWithEnableDuplicatedRuby(string text, string[] expectedRubies)
{
var config = GeneratorConfig(nameof(JaRubyTagGeneratorConfig.EnableDuplicatedRuby));
var config = GeneratorConfig(x => x.EnableDuplicatedRuby = true);
CheckGenerateResult(text, expectedRubies, config);
}
}
Expand Down
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 osu.Game.Rulesets.Karaoke.Edit.Generator;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.TimeTags;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Asserts;
Expand All @@ -9,7 +10,7 @@
namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Generator.Lyrics.TimeTags
{
public abstract class BaseTimeTagGeneratorTest<TTimeTagGenerator, TConfig> : BaseLyricGeneratorTest<TTimeTagGenerator, TimeTag[], TConfig>
where TTimeTagGenerator : TimeTagGenerator<TConfig> where TConfig : TimeTagGeneratorConfig, new()
where TTimeTagGenerator : TimeTagGenerator<TConfig> where TConfig : TimeTagGeneratorConfig, IHasConfig<TConfig>, new()
{
protected static void CheckCanGenerate(string text, bool canGenerate, TConfig config)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,35 @@ public void TestCanGenerate(string text, bool canGenerate)
[TestCase("か", new[] { "[0,start]:", "[0,end]:" }, true)]
public void TestGenerateWithCheckLineEndKeyUp(string lyric, string[] expectedTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckLineEndKeyUp) : null);
var config = GeneratorConfig(x => x.CheckLineEndKeyUp = applyConfig);
CheckGenerateResult(lyric, expectedTimeTags, config);
}

[TestCase(" ", new string[] { }, false)]
[TestCase(" ", new[] { "[0,start]:" }, true)]
public void TestGenerateWithCheckBlankLine(string lyric, string[] expectedTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckBlankLine) : null);
var config = GeneratorConfig(x => x.CheckBlankLine = applyConfig);
CheckGenerateResult(lyric, expectedTimeTags, config);
}

[TestCase("か ", new[] { "[0,start]:", "[1,start]:", "[2,start]:", "[3,start]:", "[4,start]:", "[5,start]:" }, false)]
[TestCase("か ", new[] { "[0,start]:", "[1,start]:" }, true)]
public void TestGenerateWithCheckWhiteSpace(string lyric, string[] expectedTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace) : null);
var config = GeneratorConfig(x => x.CheckWhiteSpace = applyConfig);
CheckGenerateResult(lyric, expectedTimeTags, config);
}

[TestCase("か ", new[] { "[0,start]:", "[1,start]:" }, false)]
[TestCase("か ", new[] { "[0,start]:", "[0,end]:" }, true)]
public void TestGenerateWithCheckWhiteSpaceKeyUp(string lyric, string[] expectedTimeTags, bool applyConfig)
{
var config = GeneratorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceKeyUp) : null);
var config = GeneratorConfig(x =>
{
x.CheckWhiteSpace = true;
x.CheckWhiteSpaceKeyUp = applyConfig;
});
CheckGenerateResult(lyric, expectedTimeTags, config);
}

Expand All @@ -62,9 +65,12 @@ public void TestGenerateWithCheckWhiteSpaceKeyUp(string lyric, string[] expected
[TestCase("A B C", new[] { "[0,start]:", "[0,end]:", "[2,start]:", "[2,end]:", "[4,start]:" }, true, true)]
public void TestGenerateWithCheckWhiteSpaceAlphabet(string lyric, string[] expectedTimeTags, bool applyConfig, bool keyUp)
{
var config = GeneratorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceAlphabet) : null,
keyUp ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceKeyUp) : null);
var config = GeneratorConfig(x =>
{
x.CheckWhiteSpace = true;
x.CheckWhiteSpaceAlphabet = applyConfig;
x.CheckWhiteSpaceKeyUp = keyUp;
});
CheckGenerateResult(lyric, expectedTimeTags, config);
}

Expand All @@ -76,9 +82,12 @@ public void TestGenerateWithCheckWhiteSpaceAlphabet(string lyric, string[] expec
[TestCase("0 1 2", new[] { "[0,start]:", "[0,end]:", "[2,start]:", "[2,end]:", "[4,start]:" }, true, true)]
public void TestGenerateWithCheckWhiteSpaceDigit(string lyric, string[] expectedTimeTags, bool applyConfig, bool keyUp)
{
var config = GeneratorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceDigit) : null,
keyUp ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceKeyUp) : null);
var config = GeneratorConfig(x =>
{
x.CheckWhiteSpace = true;
x.CheckWhiteSpaceDigit = applyConfig;
x.CheckWhiteSpaceKeyUp = keyUp;
});
CheckGenerateResult(lyric, expectedTimeTags, config);
}

Expand All @@ -87,25 +96,28 @@ public void TestGenerateWithCheckWhiteSpaceDigit(string lyric, string[] expected
[TestCase("! ! !", new[] { "[0,start]:", "[0,end]:", "[2,start]:", "[2,end]:", "[4,start]:" }, true, true)]
public void TestGenerateWitCheckWhiteSpaceAsciiSymbol(string lyric, string[] expectedTimeTags, bool applyConfig, bool keyUp)
{
var config = GeneratorConfig(nameof(JaTimeTagGeneratorConfig.CheckWhiteSpace),
applyConfig ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceAsciiSymbol) : null,
keyUp ? nameof(JaTimeTagGeneratorConfig.CheckWhiteSpaceKeyUp) : null);
var config = GeneratorConfig(x =>
{
x.CheckWhiteSpace = true;
x.CheckWhiteSpaceAsciiSymbol = applyConfig;
x.CheckWhiteSpaceKeyUp = keyUp;
});
CheckGenerateResult(lyric, expectedTimeTags, config);
}

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

[TestCase("買って", new[] { "[0,start]:", "[2,start]:" }, false)]
[TestCase("買って", new[] { "[0,start]:", "[1,start]:", "[2,start]:" }, true)]
public void TestGenerateWithCheckっ(string lyric, string[] expectedTimeTags, bool applyConfig)
{
var config = GeneratorConfig(applyConfig ? nameof(JaTimeTagGeneratorConfig.Checkっ) : null);
var config = GeneratorConfig(x => x.Checkっ = applyConfig);
CheckGenerateResult(lyric, expectedTimeTags, config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void TestCanGenerate(string text, bool canGenerate)
[TestCase("拉~拉~拉~", new[] { "[0,start]:", "[2,start]:", "[4,start]:", "[5,end]:" })]
public void TestGenerateWithCheckLineEndKeyUp(string lyric, string[] expectedTimeTags)
{
var config = GeneratorConfig(nameof(ZhTimeTagGeneratorConfig.CheckLineEndKeyUp));
var config = GeneratorConfig(x => x.CheckLineEndKeyUp = true);
CheckGenerateResult(lyric, expectedTimeTags, config);
}
}
Expand Down

0 comments on commit 8b86d64

Please sign in to comment.