Skip to content

Commit

Permalink
Merge pull request #1809 from andy840119/implement-base-class-for-gen…
Browse files Browse the repository at this point in the history
…erator

Implement base class for generator and detector.
  • Loading branch information
andy840119 authored Dec 24, 2022
2 parents 9f95526 + 480f58f commit 69d4a98
Show file tree
Hide file tree
Showing 41 changed files with 551 additions and 531 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Configuration;
using osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers.Lyrics;
using osu.Game.Rulesets.Karaoke.Edit.Generator;
using osu.Game.Rulesets.Karaoke.Edit.Utils;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Objects.Properties;
Expand Down Expand Up @@ -61,13 +62,7 @@ public void TestDetectReferenceLyricWithNonSupportedLyric()
Text = "???"
});

TriggerHandlerChanged(c => c.AutoGenerate(LyricAutoGenerateProperty.DetectReferenceLyric));

AssertSelectedHitObject(h =>
{
Assert.IsNull(h.ReferenceLyric);
Assert.IsNull(h.ReferenceLyricConfig);
});
TriggerHandlerChangedWithException<NotDetectatableException>(c => c.AutoGenerate(LyricAutoGenerateProperty.DetectReferenceLyric));
}

#endregion
Expand Down Expand Up @@ -149,13 +144,7 @@ public void TestAutoGenerateRubyTagsWithNonSupportedLyric()
},
});

TriggerHandlerChanged(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateRubyTags));

AssertSelectedHitObject(h =>
{
// should not able to generate time-tag if lyric text is empty, or did not have language.
Assert.IsEmpty(h.RubyTags);
});
TriggerHandlerChangedWithException<NotGeneratableException>(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateRubyTags));
}

#endregion
Expand Down Expand Up @@ -201,13 +190,7 @@ public void TestAutoGenerateRomajiTagsWithNonSupportedLyric()
},
});

TriggerHandlerChanged(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateRomajiTags));

AssertSelectedHitObject(h =>
{
// should not able to generate time-tag if lyric text is empty, or did not have language.
Assert.IsEmpty(h.RomajiTags);
});
TriggerHandlerChangedWithException<NotGeneratableException>(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateRomajiTags));
}

#endregion
Expand Down Expand Up @@ -251,13 +234,7 @@ public void TestAutoGenerateTimeTagsWithNonSupportedLyric()
},
});

TriggerHandlerChanged(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateTimeTags));

AssertSelectedHitObject(h =>
{
// should not able to generate time-tag if lyric text is empty, or did not have language.
Assert.IsEmpty(h.TimeTags);
});
TriggerHandlerChangedWithException<NotGeneratableException>(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateTimeTags));
}

#endregion
Expand Down Expand Up @@ -302,12 +279,7 @@ public void TestAutoGenerateNotesWithNonSupportedLyric()
Text = "カラオケ",
});

TriggerHandlerChanged(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateNotes));

AssertSelectedHitObject(h =>
{
Assert.IsEmpty(getMatchedNotes(h));
});
TriggerHandlerChangedWithException<NotGeneratableException>(c => c.AutoGenerate(LyricAutoGenerateProperty.AutoGenerateNotes));
}

private Note[] getMatchedNotes(Lyric lyric)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Edit.Generator;

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

public abstract class BasePropertyDetectorTest<TDetector, TItem, TProperty, TConfig>
: BasePropertyDetectorTest<TDetector, TItem, TProperty>
where TDetector : PropertyDetector<TItem, TProperty>
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;
}

protected static TDetector GenerateDetector(TConfig config)
{
if (Activator.CreateInstance(typeof(TDetector), config) is not TDetector detector)
throw new ArgumentNullException(nameof(detector));

return detector;
}

protected static void CheckCanDetect(TItem item, bool canDetect, TConfig config)
{
var detector = GenerateDetector(config);

CheckCanDetect(item, canDetect, detector);
}

protected void CheckDetectResult(TItem item, TProperty expected, TConfig config)
{
var detector = GenerateDetector(config);

CheckDetectResult(item, expected, detector);
}
}

public abstract class BasePropertyDetectorTest<TDetector, TItem, TProperty>
where TDetector : PropertyDetector<TItem, TProperty>
{
protected static void CheckCanDetect(TItem item, bool canDetect, TDetector detector)
{
bool actual = detector.CanDetect(item);
Assert.AreEqual(canDetect, actual);
}

protected void CheckDetectResult(TItem item, TProperty expected, TDetector detector)
{
// create time tag and actually time tag.
var actual = detector.Detect(item);
AssertEqual(expected, actual);
}

protected abstract void AssertEqual(TProperty expected, TProperty actual);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Edit.Generator;

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

public abstract class BasePropertyGeneratorTest<TGenerator, TItem, TProperty, TConfig>
: BasePropertyGeneratorTest<TGenerator, TItem, TProperty>
where TGenerator : PropertyGenerator<TItem, TProperty>
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;
}

protected static TGenerator GenerateGenerator(TConfig config)
{
if (Activator.CreateInstance(typeof(TGenerator), config) is not TGenerator generator)
throw new ArgumentNullException(nameof(generator));

return generator;
}

protected static void CheckCanGenerate(TItem item, bool canGenerate, TConfig config)
{
var generator = GenerateGenerator(config);

CheckCanGenerate(item, canGenerate, generator);
}

protected void CheckGenerateResult(TItem item, TProperty expected, TConfig config)
{
var generator = GenerateGenerator(config);

CheckGenerateResult(item, expected, generator);
}
}

public abstract class BasePropertyGeneratorTest<TGenerator, TItem, TProperty>
where TGenerator : PropertyGenerator<TItem, TProperty>
{
protected static void CheckCanGenerate(TItem item, bool canGenerate, TGenerator generator)
{
bool actual = generator.CanGenerate(item);
Assert.AreEqual(canGenerate, actual);
}

protected void CheckGenerateResult(TItem item, TProperty expected, TGenerator generator)
{
// create time tag and actually time tag.
var actual = generator.Generate(item);
AssertEqual(expected, actual);
}

protected abstract void AssertEqual(TProperty expected, TProperty actual);
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
// 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 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>
: BaseGeneratorTest<TConfig>
where TDetector : class, IBeatmapPropertyDetector<TObject> where TConfig : IHasConfig<TConfig>, new()
: BasePropertyDetectorTest<TDetector, KaraokeBeatmap, TObject, TConfig>
where TDetector : BeatmapPropertyDetector<TObject, TConfig>
where TConfig : IHasConfig<TConfig>, new()
{
protected static TDetector GenerateDetector(TConfig config)
{
if (Activator.CreateInstance(typeof(TDetector), config) is not TDetector detector)
throw new ArgumentNullException(nameof(detector));

return detector;
}

protected static void CheckCanDetect(KaraokeBeatmap beatmap, bool canDetect, TConfig config)
{
var detector = GenerateDetector(config);

CheckCanDetect(beatmap, canDetect, detector);
}

protected static void CheckCanDetect(KaraokeBeatmap beatmap, bool canDetect, TDetector detector)
{
bool actual = detector.CanDetect(beatmap);
Assert.AreEqual(canDetect, actual);
}

protected void CheckDetectResult(KaraokeBeatmap beatmap, TObject expected, TConfig config)
{
var detector = GenerateDetector(config);

CheckDetectResult(beatmap, expected, detector);
}

protected void CheckDetectResult(KaraokeBeatmap beatmap, TObject expected, TDetector detector)
{
// create time tag and actually time tag.
var actual = detector.Detect(beatmap);
AssertEqual(expected, actual);
}

protected abstract void AssertEqual(TObject expected, TObject actual);
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
// 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 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 BaseBeatmapGeneratorTest<TGenerator, TObject, TConfig>
: BaseGeneratorTest<TConfig>
where TGenerator : class, IBeatmapPropertyGenerator<TObject> where TConfig : IHasConfig<TConfig>, new()
: BasePropertyGeneratorTest<TGenerator, KaraokeBeatmap, TObject, TConfig>
where TGenerator : BeatmapPropertyGenerator<TObject, TConfig>
where TConfig : IHasConfig<TConfig>, new()
{
protected static TGenerator GenerateGenerator(TConfig config)
{
if (Activator.CreateInstance(typeof(TGenerator), config) is not TGenerator generator)
throw new ArgumentNullException(nameof(generator));

return generator;
}

protected static void CheckCanGenerate(KaraokeBeatmap beatmap, bool canGenerate, TConfig config)
{
var generator = GenerateGenerator(config);

CheckCanGenerate(beatmap, canGenerate, generator);
}

protected static void CheckCanGenerate(KaraokeBeatmap beatmap, bool canGenerate, TGenerator generator)
{
bool actual = generator.CanGenerate(beatmap);
Assert.AreEqual(canGenerate, actual);
}

protected void CheckGenerateResult(KaraokeBeatmap beatmap, TObject expected, TConfig config)
{
var generator = GenerateGenerator(config);

CheckGenerateResult(beatmap, expected, generator);
}

protected void CheckGenerateResult(KaraokeBeatmap beatmap, TObject expected, TGenerator generator)
{
// create time tag and actually time tag.
var actual = generator.Generate(beatmap);
AssertEqual(expected, actual);
}

protected abstract void AssertEqual(TObject expected, TObject actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PageGeneratorTest : BaseBeatmapGeneratorTest<PageGenerator, Page[],
[TestCase(new[] { "[1000,3000]:karaoke" }, true)]
[TestCase(new[] { "[1000,3000]:karaoke", "[4000,6000]:karaoke" }, true)]
[TestCase(new[] { "[1000,3000]:karaoke", "[1000,3000]:karaoke" }, true)] // should still runnable even if lyric is overlapping.
[TestCase(new[] { "" }, false)] // should not be able to generate if lyric is empty.
[TestCase(new string[] { }, false)]
public void TestCanGenerate(string[] lyrics, bool canGenerate)
{
Expand All @@ -35,20 +36,6 @@ public void TestCanGenerate(string[] lyrics, bool canGenerate)
CheckCanGenerate(beatmap, canGenerate, config);
}

[Test]
public void TestGenerateWithZeroLyric()
{
var config = GeneratorDefaultConfig();
var beatmap = new KaraokeBeatmap
{
HitObjects = new List<KaraokeHitObject>()
};

var expectedPages = Array.Empty<Page>();

CheckGenerateResult(beatmap, expectedPages, config);
}

[TestCase("[1000,3000]:karaoke", new double[] { 1000, 3000 })]
[TestCase("[1000,23000]:karaoke", new[] { 1000, 1000 + max_interval, 1000 + max_interval * 2, 23000 })]
public void TestGenerateWithSingleLyric(string lyric, double[] expectedTimes)
Expand Down
Loading

0 comments on commit 69d4a98

Please sign in to comment.