From f4ef8419729f4c80ccd8b1a666c180b2267a73f0 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 10 Nov 2021 18:27:23 +0900 Subject: [PATCH] Add fallback for cases where beatmap has no author/title/artist specified --- osu.Game.Tests/Models/DisplayStringTest.cs | 22 +++++++++++++++++++ .../Beatmaps/BeatmapMetadataInfoExtensions.cs | 8 +++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/osu.Game.Tests/Models/DisplayStringTest.cs b/osu.Game.Tests/Models/DisplayStringTest.cs index 9a3c32bfb2..754a849ac8 100644 --- a/osu.Game.Tests/Models/DisplayStringTest.cs +++ b/osu.Game.Tests/Models/DisplayStringTest.cs @@ -17,6 +17,8 @@ namespace osu.Game.Tests.Models { private static readonly object[][] test_cases = { + new object[] { makeNoMetadataMockBeatmapSet(), "unknown artist - unknown title" }, + new object[] { makeNoAuthorMockBeatmapSet(), "artist - title" }, new object[] { makeMockBeatmapSet(), "artist - title (author)" }, new object[] { makeMockBeatmap(), "artist - title (author) [difficulty]" }, new object[] { makeMockMetadata(), "artist - title (author)" }, @@ -29,6 +31,26 @@ namespace osu.Game.Tests.Models [TestCaseSource(nameof(test_cases))] public void TestDisplayString(object model, string expected) => Assert.That(model.GetDisplayString(), Is.EqualTo(expected)); + private static IBeatmapSetInfo makeNoAuthorMockBeatmapSet() + { + var mock = new Mock(); + + mock.Setup(m => m.Metadata.Artist).Returns("artist"); + mock.Setup(m => m.Metadata.Title).Returns("title"); + mock.Setup(m => m.Metadata.Author.Username).Returns(string.Empty); + + return mock.Object; + } + + private static IBeatmapSetInfo makeNoMetadataMockBeatmapSet() + { + var mock = new Mock(); + + mock.Setup(m => m.Metadata).Returns(new BeatmapMetadata()); + + return mock.Object; + } + private static IBeatmapSetInfo makeMockBeatmapSet() { var mock = new Mock(); diff --git a/osu.Game/Beatmaps/BeatmapMetadataInfoExtensions.cs b/osu.Game/Beatmaps/BeatmapMetadataInfoExtensions.cs index 32fb389e9a..7aab6a7a9b 100644 --- a/osu.Game/Beatmaps/BeatmapMetadataInfoExtensions.cs +++ b/osu.Game/Beatmaps/BeatmapMetadataInfoExtensions.cs @@ -27,8 +27,12 @@ namespace osu.Game.Beatmaps /// public static string GetDisplayTitle(this IBeatmapMetadataInfo metadataInfo) { - string author = string.IsNullOrEmpty(metadataInfo.Author.Username) ? string.Empty : $"({metadataInfo.Author.Username})"; - return $"{metadataInfo.Artist} - {metadataInfo.Title} {author}".Trim(); + string author = string.IsNullOrEmpty(metadataInfo.Author.Username) ? string.Empty : $" ({metadataInfo.Author.Username})"; + + string artist = string.IsNullOrEmpty(metadataInfo.Artist) ? "unknown artist" : metadataInfo.Artist; + string title = string.IsNullOrEmpty(metadataInfo.Title) ? "unknown title" : metadataInfo.Title; + + return $"{artist} - {title}{author}".Trim(); } ///