add tests for reflection hacks and remove custom compiler warnings

This commit is contained in:
13xforever
2022-12-05 14:18:32 +05:00
parent 6342fcb529
commit 6dd0c024b1
4 changed files with 49 additions and 4 deletions

View File

@@ -13,7 +13,6 @@ public static class DiscordComponentsExtensions
public static DiscordButtonComponent SetEmoji(this DiscordButtonComponent button, DiscordComponentEmoji emoji)
{
#warning Ugly hack, needs builder method or better yet making the setter public like before
var property = button.GetType().GetProperty(nameof(button.Emoji));
property?.SetValue(button, emoji, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, null, null);
return button;

View File

@@ -24,7 +24,6 @@ public static class DiscordMessageExtensions
{
if (messageBuilder.ReplyId is not null)
{
#warning Ugly hack, needs property reset method in the builder
var property = messageBuilder.GetType().GetProperty(nameof(messageBuilder.ReplyId));
property?.SetValue(messageBuilder, null, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, null, null);
}
@@ -32,7 +31,6 @@ public static class DiscordMessageExtensions
task = botMsg.ModifyAsync(messageBuilder, suppressEmbeds: forceRemoveEmbed);
}
var newMsg = await task.ConfigureAwait(false);
#warning Ugly hack, needs proper fix in upstream, but they are not enthused to do so
if (newMsg.Channel is null)
{
Config.Log.Warn("New message in DM from the bot still has no channel");

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using CompatBot.Utils;
using Microsoft.Extensions.Caching.Memory;
using NUnit.Framework;

View File

@@ -0,0 +1,49 @@
using CompatBot.Utils;
using CompatBot.Utils.Extensions;
using DSharpPlus;
using DSharpPlus.Entities;
using NUnit.Framework;
namespace Tests;
[TestFixture]
public class ReflectionHacksTests
{
[Test]
public void DiscordButtonComponentEmojiSetterTest()
{
var button = new DiscordButtonComponent(ButtonStyle.Primary, "test", "Test");
var property = button.GetType().GetProperty(nameof(button.Emoji));
Assert.That(property, Is.Not.Null);
Assert.That(property.GetMethod?.IsPublic, Is.True);
var setter = property.SetMethod;
Assert.That(setter, Is.Not.Null);
Assert.That(setter.IsPublic, Is.False, $"{nameof(DiscordButtonComponent)}.{nameof(DiscordButtonComponent.Emoji)} setter is now public, please remove hack in {nameof(DiscordComponentsExtensions)}.{nameof(DiscordComponentsExtensions.SetEmoji)}");
}
[Test]
public void DiscordMessageBuilderReplyIdSetterTest()
{
var messageBuilder = new DiscordMessageBuilder();
var property = messageBuilder.GetType().GetProperty(nameof(messageBuilder.ReplyId));
Assert.That(property, Is.Not.Null);
Assert.That(property.GetMethod?.IsPublic, Is.True);
var setter = property.SetMethod;
Assert.That(setter, Is.Not.Null);
Assert.That(setter.IsPublic, Is.False, $"{nameof(DiscordMessageBuilder)}.{nameof(DiscordMessageBuilder.ReplyId)} setter is now public, please remove hack in {nameof(DiscordMessageExtensions)}.{nameof(DiscordMessageExtensions.UpdateOrCreateMessageAsync)}");
}
[Test]
public void DiscordMessageChannelSetterTest()
{
var property = typeof(DiscordMessage).GetProperty(nameof(DiscordMessage.Channel));
Assert.That(property, Is.Not.Null);
Assert.That(property.GetMethod?.IsPublic, Is.True);
var setter = property.SetMethod;
Assert.That(setter, Is.Not.Null);
Assert.That(setter.IsPublic, Is.False, $"{nameof(DiscordMessage)}.{nameof(DiscordMessage.Channel)} setter is now public, please remove hack in {nameof(DiscordMessageExtensions)}.{nameof(DiscordMessageExtensions.UpdateOrCreateMessageAsync)}");
}
}