From bee788674621efe17950eaf37f234c67c50c69e5 Mon Sep 17 00:00:00 2001 From: d4n Date: Thu, 27 Jun 2024 20:20:20 -0500 Subject: [PATCH] Refactoring --- src/Apis/Dictionary/DictionaryFormatter.cs | 2 +- src/Apis/Genius/LyricsConverter.cs | 1 + src/Apis/WolframAlpha/ArrayOrObjectConverter.cs | 2 +- tests/Fergun.Tests/Apis/WolframAlphaTests.cs | 2 +- tests/Fergun.Tests/Extensions/MessageExtensionsTests.cs | 3 ++- tests/Fergun.Tests/Utils.cs | 1 + 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Apis/Dictionary/DictionaryFormatter.cs b/src/Apis/Dictionary/DictionaryFormatter.cs index 5e29aea..06e8645 100644 --- a/src/Apis/Dictionary/DictionaryFormatter.cs +++ b/src/Apis/Dictionary/DictionaryFormatter.cs @@ -173,7 +173,7 @@ private static string FormatHtml(string htmlText, bool newLineExample = false) { builder.Append($"\n> *{content}*"); } // Sometimes there's text instead of numbers in a superscript class (e.g., satire) - else if (className == "superscript" && content.Length == 1 && content[0] >= '0' && content[0] <= '9') + else if (className == "superscript" && content is [>= '0' and <= '9']) { builder.Append(SuperscriptDigits[content[0] - '0']); } diff --git a/src/Apis/Genius/LyricsConverter.cs b/src/Apis/Genius/LyricsConverter.cs index 02fe489..e46935d 100644 --- a/src/Apis/Genius/LyricsConverter.cs +++ b/src/Apis/Genius/LyricsConverter.cs @@ -64,6 +64,7 @@ private static void IterateContent(in JsonElement element, StringBuilder builder { builder.Append('\u200b'); // Append zero-width space to prevent markdown from breaking } + builder.Append(markDownStart); if (element.TryGetProperty("children", out var children)) diff --git a/src/Apis/WolframAlpha/ArrayOrObjectConverter.cs b/src/Apis/WolframAlpha/ArrayOrObjectConverter.cs index 35a941b..d5d88b6 100644 --- a/src/Apis/WolframAlpha/ArrayOrObjectConverter.cs +++ b/src/Apis/WolframAlpha/ArrayOrObjectConverter.cs @@ -17,7 +17,7 @@ public override IReadOnlyList Read(ref Utf8JsonReader reader, Type typeToConv return reader.TokenType switch { JsonTokenType.StartArray => JsonSerializer.Deserialize>(ref reader)!, - JsonTokenType.StartObject => new[] { JsonSerializer.Deserialize(ref reader)! }, + JsonTokenType.StartObject => [JsonSerializer.Deserialize(ref reader)!], _ => throw new JsonException("Token type must be either array or object.") }; } diff --git a/tests/Fergun.Tests/Apis/WolframAlphaTests.cs b/tests/Fergun.Tests/Apis/WolframAlphaTests.cs index 72b42c6..4ef8b4f 100644 --- a/tests/Fergun.Tests/Apis/WolframAlphaTests.cs +++ b/tests/Fergun.Tests/Apis/WolframAlphaTests.cs @@ -169,7 +169,7 @@ public void ArrayOrObjectConverter_Throws_Exceptions() options.Converters.Add(new ArrayOrObjectConverter()); Assert.Throws(() => JsonSerializer.Deserialize>("true", options)); - Assert.Throws(() => JsonSerializer.Serialize>(new[] { "test" }, options)); + Assert.Throws(() => JsonSerializer.Serialize>(["test"], options)); } public static TheoryData GetWolframAlphaErrorInfoConverterData() diff --git a/tests/Fergun.Tests/Extensions/MessageExtensionsTests.cs b/tests/Fergun.Tests/Extensions/MessageExtensionsTests.cs index 245c8ec..98e7730 100644 --- a/tests/Fergun.Tests/Extensions/MessageExtensionsTests.cs +++ b/tests/Fergun.Tests/Extensions/MessageExtensionsTests.cs @@ -30,7 +30,7 @@ public void IMessage_GetText_Should_Return_Text_From_Content_And_Embed(string co { var messageMock = new Mock(); messageMock.SetupGet(x => x.Content).Returns(content); - messageMock.SetupGet(x => x.Embeds).Returns(new[] { embed }); + messageMock.SetupGet(x => x.Embeds).Returns([embed]); string[] parts = messageMock.Object.GetText().Split('\n', StringSplitOptions.RemoveEmptyEntries); @@ -44,6 +44,7 @@ public void IMessage_GetText_Should_Return_Text_From_Content_And_Embed(string co { Assert.Equal(embed.Author.Value.Name, parts[index++]); } + Assert.Equal(embed.Title, parts[index++]); Assert.Equal(embed.Description, parts[index]); if (embed.Footer is not null) diff --git a/tests/Fergun.Tests/Utils.cs b/tests/Fergun.Tests/Utils.cs index 7981031..df1fc53 100644 --- a/tests/Fergun.Tests/Utils.cs +++ b/tests/Fergun.Tests/Utils.cs @@ -55,6 +55,7 @@ public static IUser CreateMockedUser(Faker? faker = null, bool pomelo = false) return userMock.Object; } + public static IGuildUser CreateMockedGuildUser(Faker? faker = null, bool pomelo = false) { faker ??= new Faker();