Add unit test project and some early tests

This commit is contained in:
Ske 2020-06-14 18:56:53 +02:00
parent 2d43c17f9e
commit 0f041c2e3c
6 changed files with 119 additions and 3 deletions

View File

@ -15,5 +15,5 @@ jobs:
uses: actions/setup-dotnet@v1 uses: actions/setup-dotnet@v1
with: with:
dotnet-version: 3.1.100 dotnet-version: 3.1.100
- name: Build with dotnet - name: Build and test with dotnet
run: dotnet build --configuration Release run: dotnet test --configuration Release

View File

@ -8,10 +8,13 @@ namespace PluralKit.Bot
{ {
public class ProxyTagParser public class ProxyTagParser
{ {
public bool TryMatch(IEnumerable<ProxyMember> members, string input, out ProxyMatch result) public bool TryMatch(IEnumerable<ProxyMember> members, string? input, out ProxyMatch result)
{ {
result = default; result = default;
// Null input is valid and is equivalent to empty string
if (input == null) return false;
// If the message starts with a @mention, and then proceeds to have proxy tags, // If the message starts with a @mention, and then proceeds to have proxy tags,
// extract the mention and place it inside the inner message // extract the mention and place it inside the inner message
// eg. @Ske [text] => [@Ske text] // eg. @Ske [text] => [@Ske text]

View File

@ -24,5 +24,13 @@ namespace PluralKit.Core
: ServerName ?? DisplayName ?? Name; : ServerName ?? DisplayName ?? Name;
public string? ProxyAvatar(MessageContext ctx) => ServerAvatar ?? Avatar ?? ctx.SystemAvatar; public string? ProxyAvatar(MessageContext ctx) => ServerAvatar ?? Avatar ?? ctx.SystemAvatar;
public ProxyMember() { }
public ProxyMember(string name, params ProxyTag[] tags)
{
Name = name;
ProxyTags = tags;
}
} }
} }

View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PluralKit.API\PluralKit.API.csproj" />
<ProjectReference Include="..\PluralKit.Bot\PluralKit.Bot.csproj" />
<ProjectReference Include="..\PluralKit.Core\PluralKit.Core.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,77 @@
#nullable enable
using PluralKit.Bot;
using PluralKit.Core;
using Xunit;
namespace PluralKit.Tests
{
public class ProxyTagParserTests
{
private ProxyTagParser parser = new ProxyTagParser();
private ProxyMember[] members = {
new ProxyMember("Tagless"),
new ProxyMember("John", new ProxyTag("[", "]")),
new ProxyMember("Curly", new ProxyTag("{", "}")),
new ProxyMember("Specific", new ProxyTag("{{", "}}")),
new ProxyMember("SuperSpecific", new ProxyTag("{{{", "}}}")),
new ProxyMember("Manytags", new ProxyTag("-", "-"), new ProxyTag("<", ">")),
new ProxyMember("Lopsided", new ProxyTag("-", "")),
new ProxyMember("Othersided", new ProxyTag("", "-"))
};
[Fact]
public void EmptyStringMatchesNothing() =>
Assert.False(parser.TryMatch(members, "", out _));
[Fact]
public void NullStringMatchesNothing() =>
Assert.False(parser.TryMatch(members, null, out _));
[Fact]
public void PlainStringMatchesNothing() =>
// Note that we have "Tagless" with no proxy tags
Assert.False(parser.TryMatch(members, "string without any of the tags", out _));
[Fact]
public void StringWithBasicTagsMatch() =>
Assert.True(parser.TryMatch(members, "[these are john's tags]", out _));
[Theory]
[InlineData("[these are john's tags]", "John")]
[InlineData("-lopsided tags on the left", "Lopsided")]
[InlineData("lopsided tags on the right-", "Othersided")]
public void MatchReturnsCorrectMember(string input, string expectedName)
{
parser.TryMatch(members, input, out var result);
Assert.Equal(expectedName, result.Member.Name);
}
[Fact]
public void MatchReturnsCorrectContent()
{
parser.TryMatch(members, "[these are john's tags]", out var result);
Assert.Equal("these are john's tags", result.Content);
}
[Theory]
[InlineData("{just curly}", "Curly", "just curly")]
[InlineData("{{getting deeper}}", "Specific", "getting deeper")]
[InlineData("{{{way too deep}}}", "SuperSpecific", "way too deep")]
[InlineData("{{unmatched brackets}}}", "Specific", "unmatched brackets}")]
[InlineData("{more unmatched brackets}}}}}", "Curly", "more unmatched brackets}}}}")]
public void MostSpecificTagsAreMatched(string input, string expectedName, string expectedContent)
{
Assert.True(parser.TryMatch(members, input, out var result));
Assert.Equal(expectedName, result.Member.Name);
Assert.Equal(expectedContent, result.Content);
}
[Theory]
[InlineData("")]
[InlineData("some text")]
[InlineData("{bogus tags, idk}")]
public void NoMembersMatchNothing(string input) =>
Assert.False(parser.TryMatch(new ProxyMember[]{}, input, out _));
}
}

View File

@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluralKit.Core", "PluralKit
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluralKit.API", "PluralKit.API\PluralKit.API.csproj", "{3420F8A9-125C-4F7F-A444-10DD16945754}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluralKit.API", "PluralKit.API\PluralKit.API.csproj", "{3420F8A9-125C-4F7F-A444-10DD16945754}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluralKit.Tests", "PluralKit.Tests\PluralKit.Tests.csproj", "{752FE725-5EE1-45E9-B721-0CDD28171AC8}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -24,5 +26,9 @@ Global
{3420F8A9-125C-4F7F-A444-10DD16945754}.Debug|Any CPU.Build.0 = Debug|Any CPU {3420F8A9-125C-4F7F-A444-10DD16945754}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3420F8A9-125C-4F7F-A444-10DD16945754}.Release|Any CPU.ActiveCfg = Release|Any CPU {3420F8A9-125C-4F7F-A444-10DD16945754}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3420F8A9-125C-4F7F-A444-10DD16945754}.Release|Any CPU.Build.0 = Release|Any CPU {3420F8A9-125C-4F7F-A444-10DD16945754}.Release|Any CPU.Build.0 = Release|Any CPU
{752FE725-5EE1-45E9-B721-0CDD28171AC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{752FE725-5EE1-45E9-B721-0CDD28171AC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{752FE725-5EE1-45E9-B721-0CDD28171AC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{752FE725-5EE1-45E9-B721-0CDD28171AC8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal