feat: upgrade to .NET 6, refactor everything

This commit is contained in:
spiral
2021-11-26 21:10:56 -05:00
parent d28e99ba43
commit 1918c56937
314 changed files with 27954 additions and 27966 deletions

View File

@@ -1,185 +1,181 @@
using System;
using PluralKit.Bot;
using PluralKit.Core;
using Xunit;
namespace PluralKit.Tests
namespace PluralKit.Tests;
public class GroupAddRemoveResponseTests
{
public class GroupAddRemoveResponseTests
private static readonly Func<Groups.AddRemoveOperation, int, int, int, int, string>
func = GroupMemberUtils.GenerateResponse;
private static readonly Groups.AddRemoveOperation addOp = Groups.AddRemoveOperation.Add;
private static readonly Groups.AddRemoveOperation removeOp = Groups.AddRemoveOperation.Remove;
private static readonly string success = Emojis.Success;
private static readonly string failure = Emojis.Error;
public class AddOp
{
private static Func<Groups.AddRemoveOperation, int, int, int, int, string>
func = GroupMemberUtils.GenerateResponse;
private static Groups.AddRemoveOperation addOp = Groups.AddRemoveOperation.Add;
private static Groups.AddRemoveOperation removeOp = Groups.AddRemoveOperation.Remove;
private static string success = Emojis.Success;
private static string failure = Emojis.Error;
public class AddOp
public class MemberGroup
{
public class MemberGroup
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member added to group.",
func(addOp, 1, 1, 1, 0)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not added to group (member already in group).",
func(addOp, 1, 1, 0, 1)
);
}
public class MemberGroups
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member added to groups.",
func(addOp, 1, 2, 2, 0)
);
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} Member added to 2 groups (member already in 1 group).",
func(addOp, 1, 3, 2, 1)
);
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} Member added to 1 group (member already in 2 groups).",
func(addOp, 1, 3, 1, 2)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not added to groups (member already in groups).",
func(addOp, 1, 2, 0, 2)
);
}
public class MembersGroup
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Members added to group.",
func(addOp, 2, 1, 2, 0)
);
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} 2 members added to group (1 member already in group).",
func(addOp, 3, 1, 2, 1)
);
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} 1 member added to group (2 members already in group).",
func(addOp, 3, 1, 1, 2)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Members not added to group (members already in group).",
func(addOp, 2, 1, 0, 2)
);
}
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member added to group.",
func(addOp, 1, 1, 1, 0)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not added to group (member already in group).",
func(addOp, 1, 1, 0, 1)
);
}
public class RemoveOp
public class MemberGroups
{
public class MemberGroup
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member removed from group.",
func(removeOp, 1, 1, 1, 0)
);
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member added to groups.",
func(addOp, 1, 2, 2, 0)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not removed from group (member already not in group).",
func(removeOp, 1, 1, 0, 1)
);
}
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} Member added to 2 groups (member already in 1 group).",
func(addOp, 1, 3, 2, 1)
);
public class MemberGroups
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member removed from groups.",
func(removeOp, 1, 3, 3, 0)
);
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} Member added to 1 group (member already in 2 groups).",
func(addOp, 1, 3, 1, 2)
);
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} Member removed from 1 group (member already not in 2 groups).",
func(removeOp, 1, 3, 1, 2)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not added to groups (member already in groups).",
func(addOp, 1, 2, 0, 2)
);
}
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} Member removed from 2 groups (member already not in 1 group).",
func(removeOp, 1, 3, 2, 1)
);
public class MembersGroup
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Members added to group.",
func(addOp, 2, 1, 2, 0)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not removed from groups (member already not in groups).",
func(removeOp, 1, 3, 0, 3)
);
}
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} 2 members added to group (1 member already in group).",
func(addOp, 3, 1, 2, 1)
);
public class MembersGroup
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Members removed from group.",
func(removeOp, 2, 1, 2, 0)
);
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} 1 member added to group (2 members already in group).",
func(addOp, 3, 1, 1, 2)
);
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} 1 member removed from group (2 members already not in group).",
func(removeOp, 3, 1, 1, 2)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Members not added to group (members already in group).",
func(addOp, 2, 1, 0, 2)
);
}
}
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} 2 members removed from group (1 member already not in group).",
func(removeOp, 3, 1, 2, 1)
);
public class RemoveOp
{
public class MemberGroup
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member removed from group.",
func(removeOp, 1, 1, 1, 0)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Members not removed from group (members already not in group).",
func(removeOp, 2, 1, 0, 2)
);
}
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not removed from group (member already not in group).",
func(removeOp, 1, 1, 0, 1)
);
}
public class MemberGroups
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Member removed from groups.",
func(removeOp, 1, 3, 3, 0)
);
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} Member removed from 1 group (member already not in 2 groups).",
func(removeOp, 1, 3, 1, 2)
);
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} Member removed from 2 groups (member already not in 1 group).",
func(removeOp, 1, 3, 2, 1)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Member not removed from groups (member already not in groups).",
func(removeOp, 1, 3, 0, 3)
);
}
public class MembersGroup
{
[Fact]
public void Success()
=> Assert.Equal(
$"{success} Members removed from group.",
func(removeOp, 2, 1, 2, 0)
);
[Fact]
public void PartialSuccess1()
=> Assert.Equal(
$"{success} 1 member removed from group (2 members already not in group).",
func(removeOp, 3, 1, 1, 2)
);
[Fact]
public void PartialSuccess2()
=> Assert.Equal(
$"{success} 2 members removed from group (1 member already not in group).",
func(removeOp, 3, 1, 2, 1)
);
[Fact]
public void Failure()
=> Assert.Equal(
$"{failure} Members not removed from group (members already not in group).",
func(removeOp, 2, 1, 0, 2)
);
}
}
}

View File

@@ -1,22 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugType>full</DebugType>
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0"/>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
@@ -25,13 +26,13 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PluralKit.API\PluralKit.API.csproj" />
<ProjectReference Include="..\PluralKit.Bot\PluralKit.Bot.csproj" />
<ProjectReference Include="..\PluralKit.Core\PluralKit.Core.csproj" />
<ProjectReference Include="..\PluralKit.API\PluralKit.API.csproj"/>
<ProjectReference Include="..\PluralKit.Bot\PluralKit.Bot.csproj"/>
<ProjectReference Include="..\PluralKit.Core\PluralKit.Core.csproj"/>
</ItemGroup>
<ItemGroup>
<Folder Include="API" />
<Folder Include="API"/>
</ItemGroup>
</Project>

View File

@@ -1,186 +1,186 @@
#nullable enable
using System.Collections.Generic;
using PluralKit.Bot;
using PluralKit.Core;
using Xunit;
namespace PluralKit.Tests
namespace PluralKit.Tests;
public class ProxyTagParserTests
{
public class ProxyTagParserTests
internal static ProxyMatch AssertMatch(IEnumerable<ProxyMember> members, string input, string? name = null,
string? prefix = null, string? suffix = null, string? content = null)
{
public class Basics
Assert.True(new ProxyTagParser().TryMatch(members, input, out var result));
if (name != null) Assert.Equal(name, result.Member.Name);
if (prefix != null) Assert.Equal(prefix, result.ProxyTags?.Prefix);
if (suffix != null) Assert.Equal(suffix, result.ProxyTags?.Suffix);
if (content != null) Assert.Equal(content, result.Content);
return result;
}
internal static void AssertNoMatch(IEnumerable<ProxyMember> members, string? input)
{
Assert.False(new ProxyTagParser().TryMatch(members, input, out _));
}
public class Basics
{
private readonly ProxyMember[] members =
{
private ProxyMember[] members = {
new ProxyMember("John", new ProxyTag("[", "]")),
new ProxyMember("Bob", new ProxyTag("{", "}"), new ProxyTag("<", ">")),
new ProxyMember("Prefixed", new ProxyTag("A:", "")),
new ProxyMember("Tagless")
};
new("John", new ProxyTag("[", "]")),
new("Bob", new ProxyTag("{", "}"), new ProxyTag("<", ">")),
new("Prefixed", new ProxyTag("A:", "")),
new("Tagless")
};
[Fact]
public void StringWithoutAnyTagsMatchesNothing() =>
// Note that we have "Tagless" with no proxy tags
AssertNoMatch(members, "string without any tags");
[Fact]
public void StringWithoutAnyTagsMatchesNothing() =>
// Note that we have "Tagless" with no proxy tags
AssertNoMatch(members, "string without any tags");
[Theory]
[InlineData("[john's tags]")]
[InlineData("{bob's tags}")]
[InlineData("A:tag with prefix")]
public void StringWithTagsMatch(string input) =>
AssertMatch(members, input);
[Theory]
[InlineData("[john's tags]")]
[InlineData("{bob's tags}")]
[InlineData("A:tag with prefix")]
public void StringWithTagsMatch(string input) =>
AssertMatch(members, input);
[Theory]
[InlineData("[john's tags]", "John")]
[InlineData("{bob's tags}", "Bob")]
[InlineData("A:tag with prefix", "Prefixed")]
public void MatchReturnsCorrespondingMember(string input, string expectedName) =>
AssertMatch(members, input, name: expectedName);
[Theory]
[InlineData("[john's tags]", "John")]
[InlineData("{bob's tags}", "Bob")]
[InlineData("A:tag with prefix", "Prefixed")]
public void MatchReturnsCorrespondingMember(string input, string expectedName) =>
AssertMatch(members, input, expectedName);
[Theory]
[InlineData("[text inside]", "text inside")]
[InlineData("A:text after", "text after")]
public void ContentBetweenTagsIsExtracted(string input, string expectedContent) =>
AssertMatch(members, input, content: expectedContent);
[Theory]
[InlineData("[text inside]", "text inside")]
[InlineData("A:text after", "text after")]
public void ContentBetweenTagsIsExtracted(string input, string expectedContent) =>
AssertMatch(members, input, content: expectedContent);
[Theory]
[InlineData("[john's tags]", "[", "]")]
[InlineData("{bob's tags}", "{", "}")]
[InlineData("<also bob's tags>", "<", ">")]
public void ReturnedTagMatchesInput(string input, string expectedPrefix, string expectedSuffix) =>
AssertMatch(members, input, prefix: expectedPrefix, suffix: expectedSuffix);
[Theory]
[InlineData("[john's tags]", "[", "]")]
[InlineData("{bob's tags}", "{", "}")]
[InlineData("<also bob's tags>", "<", ">")]
public void ReturnedTagMatchesInput(string input, string expectedPrefix, string expectedSuffix) =>
AssertMatch(members, input, prefix: expectedPrefix, suffix: expectedSuffix);
[Theory]
[InlineData("[tags at the start] but more text here")]
[InlineData("text at the start [but tags after]")]
[InlineData("something A:prefix")]
public void TagsOnlyMatchAtTheStartAndEnd(string input) =>
AssertNoMatch(members, input);
[Theory]
[InlineData("[tags at the start] but more text here")]
[InlineData("text at the start [but tags after]")]
[InlineData("something A:prefix")]
public void TagsOnlyMatchAtTheStartAndEnd(string input) =>
AssertNoMatch(members, input);
[Theory]
[InlineData("[ text ]", " text ")]
[InlineData("A: text", " text")]
public void WhitespaceInContentShouldNotBeTrimmed(string input, string expectedContent) =>
AssertMatch(members, input, content: expectedContent);
[Theory]
[InlineData("[ text ]", " text ")]
[InlineData("A: text", " text")]
public void WhitespaceInContentShouldNotBeTrimmed(string input, string expectedContent) =>
AssertMatch(members, input, content: expectedContent);
}
public class MentionPrefix
{
private readonly ProxyMember[] members =
{
new("John", new ProxyTag("[", "]")),
new("Suffix only", new ProxyTag("", "-Q"))
};
public void MentionAtStartGetsMovedIntoTags() =>
AssertMatch(members, "<@466378653216014359>[some text]", content: "some text");
public void SpacesBetweenMentionAndTagsAreAllowed() =>
AssertMatch(members, "<@466378653216014359> [some text]", content: "some text");
public void MentionMovingTakesPrecedenceOverTagMatching() =>
// (as opposed to content: "<@466378653216014359> some text")
// which would also be valid, but the tags should be moved first
AssertMatch(members, "<@466378653216014359> some text -Q", content: "some text");
public void AlternateMentionSyntaxAlsoAccepted() =>
AssertMatch(members, "<@466378653216014359> [some text]", content: "some text");
}
public class Specificity
{
private readonly ProxyMember[] members =
{
new("Level One", new ProxyTag("[", "]")),
new("Level Two", new ProxyTag("[[", "]]")),
new("Level Three", new ProxyTag("[[[", "]]]"))
};
[Theory]
[InlineData("[just one]", "Level One")]
[InlineData("[[getting deeper]]", "Level Two")]
[InlineData("[[[way too deep]]]", "Level Three")]
[InlineData("[[unmatched brackets]]]", "Level Two")]
[InlineData("[more unmatched brackets]]]]]]", "Level One")]
public void MostSpecificTagsAreMatched(string input, string expectedName) =>
AssertMatch(members, input, expectedName);
}
public class EmptyInput
{
private readonly ProxyMember[] members = { new("Something", new ProxyTag("[", "]")) };
[Theory]
[InlineData("")]
[InlineData("some text")]
[InlineData("{bogus tags, idk}")]
public void NoMembersMatchNothing(string input) =>
AssertNoMatch(new ProxyMember[] { }, input);
[Fact]
public void EmptyStringMatchesNothing() =>
AssertNoMatch(members, "");
[Fact]
public void NullStringMatchesNothing() =>
AssertNoMatch(members, null);
}
public class TagSpaceHandling
{
private readonly ProxyMember[] members =
{
new("Tags without spaces", new ProxyTag("[", "]")),
new("Tags with spaces", new ProxyTag("{ ", " }")),
new("Spaced prefix tag", new ProxyTag("A: ", ""))
};
[Fact]
public void TagsWithoutSpacesAlwaysMatch()
{
AssertMatch(members, "[no spaces inside tags]");
AssertMatch(members, "[ spaces inside tags ]");
}
public class MentionPrefix
[Fact]
public void TagsWithSpacesOnlyMatchWithSpaces()
{
private ProxyMember[] members = {
new ProxyMember("John", new ProxyTag("[", "]")),
new ProxyMember("Suffix only", new ProxyTag("", "-Q")),
};
AssertMatch(members, "{ spaces in here }");
AssertNoMatch(members, "{no spaces}");
public void MentionAtStartGetsMovedIntoTags() =>
AssertMatch(members, "<@466378653216014359>[some text]", content: "some text");
public void SpacesBetweenMentionAndTagsAreAllowed() =>
AssertMatch(members, "<@466378653216014359> [some text]", content: "some text");
public void MentionMovingTakesPrecedenceOverTagMatching() =>
// (as opposed to content: "<@466378653216014359> some text")
// which would also be valid, but the tags should be moved first
AssertMatch(members, "<@466378653216014359> some text -Q", content: "some text");
public void AlternateMentionSyntaxAlsoAccepted() =>
AssertMatch(members, "<@466378653216014359> [some text]", content: "some text");
AssertMatch(members, "A: text here");
AssertNoMatch(members, "A:same text without spaces");
}
public class Specificity
[Fact]
public void SpacesBeforePrefixOrAfterSuffixAlsoCount()
{
private ProxyMember[] members =
{
new ProxyMember("Level One", new ProxyTag("[", "]")),
new ProxyMember("Level Two", new ProxyTag("[[", "]]")),
new ProxyMember("Level Three", new ProxyTag("[[[", "]]]")),
};
[Theory]
[InlineData("[just one]", "Level One")]
[InlineData("[[getting deeper]]", "Level Two")]
[InlineData("[[[way too deep]]]", "Level Three")]
[InlineData("[[unmatched brackets]]]", "Level Two")]
[InlineData("[more unmatched brackets]]]]]]", "Level One")]
public void MostSpecificTagsAreMatched(string input, string expectedName) =>
AssertMatch(members, input, name: expectedName);
AssertNoMatch(members, " A: text here");
AssertNoMatch(members, "{ something something } ");
}
public class EmptyInput
[Fact]
public void TagsWithSpacesStillMatchWithoutSpacesIfTheContentIsEmpty()
{
private ProxyMember[] members = { new ProxyMember("Something", new ProxyTag("[", "]")) };
[Theory]
[InlineData("")]
[InlineData("some text")]
[InlineData("{bogus tags, idk}")]
public void NoMembersMatchNothing(string input) =>
AssertNoMatch(new ProxyMember[] { }, input);
[Fact]
public void EmptyStringMatchesNothing() =>
AssertNoMatch(members, "");
[Fact]
public void NullStringMatchesNothing() =>
AssertNoMatch(members, null);
}
public class TagSpaceHandling
{
private ProxyMember[] members =
{
new ProxyMember("Tags without spaces", new ProxyTag("[", "]")),
new ProxyMember("Tags with spaces", new ProxyTag("{ ", " }")),
new ProxyMember("Spaced prefix tag", new ProxyTag("A: ", ""))
};
[Fact]
public void TagsWithoutSpacesAlwaysMatch()
{
AssertMatch(members, "[no spaces inside tags]");
AssertMatch(members, "[ spaces inside tags ]");
}
[Fact]
public void TagsWithSpacesOnlyMatchWithSpaces()
{
AssertMatch(members, "{ spaces in here }");
AssertNoMatch(members, "{no spaces}");
AssertMatch(members, "A: text here");
AssertNoMatch(members, "A:same text without spaces");
}
[Fact]
public void SpacesBeforePrefixOrAfterSuffixAlsoCount()
{
AssertNoMatch(members, " A: text here");
AssertNoMatch(members, "{ something something } ");
}
[Fact]
public void TagsWithSpacesStillMatchWithoutSpacesIfTheContentIsEmpty()
{
AssertMatch(members, "A:");
AssertMatch(members, "{}");
}
}
internal static ProxyMatch AssertMatch(IEnumerable<ProxyMember> members, string input, string? name = null, string? prefix = null, string? suffix = null, string? content = null)
{
Assert.True(new ProxyTagParser().TryMatch(members, input, out var result));
if (name != null) Assert.Equal(name, result.Member.Name);
if (prefix != null) Assert.Equal(prefix, result.ProxyTags?.Prefix);
if (suffix != null) Assert.Equal(suffix, result.ProxyTags?.Suffix);
if (content != null) Assert.Equal(content, result.Content);
return result;
}
internal static void AssertNoMatch(IEnumerable<ProxyMember> members, string? input)
{
Assert.False(new ProxyTagParser().TryMatch(members, input, out _));
AssertMatch(members, "A:");
AssertMatch(members, "{}");
}
}
}

File diff suppressed because it is too large Load Diff