2020-06-14 16:56:53 +00:00
|
|
|
#nullable enable
|
2020-06-14 18:22:29 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2020-06-14 16:56:53 +00:00
|
|
|
using PluralKit.Bot;
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace PluralKit.Tests
|
|
|
|
{
|
|
|
|
public class ProxyTagParserTests
|
|
|
|
{
|
2020-06-14 18:22:29 +00:00
|
|
|
public class Basics
|
|
|
|
{
|
|
|
|
private ProxyMember[] members = {
|
|
|
|
new ProxyMember("John", new ProxyTag("[", "]")),
|
|
|
|
new ProxyMember("Bob", new ProxyTag("{", "}"), new ProxyTag("<", ">")),
|
|
|
|
new ProxyMember("Prefixed", new ProxyTag("A:", "")),
|
2021-08-27 15:03:47 +00:00
|
|
|
new ProxyMember("Tagless")
|
2020-06-14 18:22:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
[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]", "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("[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("[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);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-24 15:19:01 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData("[ text ]", " text ")]
|
|
|
|
[InlineData("A: text", " text")]
|
|
|
|
public void WhitespaceInContentShouldNotBeTrimmed(string input, string expectedContent) =>
|
|
|
|
AssertMatch(members, input, content: expectedContent);
|
2020-06-14 18:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class MentionPrefix
|
2020-06-14 16:56:53 +00:00
|
|
|
{
|
2020-06-14 18:22:29 +00:00
|
|
|
private ProxyMember[] members = {
|
|
|
|
new ProxyMember("John", new ProxyTag("[", "]")),
|
2021-08-27 15:03:47 +00:00
|
|
|
new ProxyMember("Suffix only", new ProxyTag("", "-Q")),
|
2020-06-14 18:22:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public void MentionAtStartGetsMovedIntoTags() =>
|
|
|
|
AssertMatch(members, "<@466378653216014359>[some text]", content: "some text");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
public void SpacesBetweenMentionAndTagsAreAllowed() =>
|
|
|
|
AssertMatch(members, "<@466378653216014359> [some text]", content: "some text");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
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");
|
2020-06-14 16:56:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
public class Specificity
|
|
|
|
{
|
|
|
|
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")]
|
2021-08-27 15:03:47 +00:00
|
|
|
public void MostSpecificTagsAreMatched(string input, string expectedName) =>
|
2020-06-14 18:22:29 +00:00
|
|
|
AssertMatch(members, input, name: expectedName);
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
public class EmptyInput
|
2020-06-14 16:56:53 +00:00
|
|
|
{
|
2021-08-27 15:03:47 +00:00
|
|
|
private ProxyMember[] members = { new ProxyMember("Something", new ProxyTag("[", "]")) };
|
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData("")]
|
|
|
|
[InlineData("some text")]
|
|
|
|
[InlineData("{bogus tags, idk}")]
|
2021-08-27 15:03:47 +00:00
|
|
|
public void NoMembersMatchNothing(string input) =>
|
|
|
|
AssertNoMatch(new ProxyMember[] { }, input);
|
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
[Fact]
|
|
|
|
public void EmptyStringMatchesNothing() =>
|
|
|
|
AssertNoMatch(members, "");
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void NullStringMatchesNothing() =>
|
|
|
|
AssertNoMatch(members, null);
|
2020-06-14 16:56:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
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}");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
AssertMatch(members, "A: text here");
|
|
|
|
AssertNoMatch(members, "A:same text without spaces");
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
[Fact]
|
|
|
|
public void SpacesBeforePrefixOrAfterSuffixAlsoCount()
|
|
|
|
{
|
|
|
|
AssertNoMatch(members, " A: text here");
|
|
|
|
AssertNoMatch(members, "{ something something } ");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TagsWithSpacesStillMatchWithoutSpacesIfTheContentIsEmpty()
|
|
|
|
{
|
|
|
|
AssertMatch(members, "A:");
|
|
|
|
AssertMatch(members, "{}");
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
internal static ProxyMatch AssertMatch(IEnumerable<ProxyMember> members, string input, string? name = null, string? prefix = null, string? suffix = null, string? content = null)
|
2020-06-14 16:56:53 +00:00
|
|
|
{
|
2020-06-14 18:22:29 +00:00
|
|
|
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;
|
2020-06-14 16:56:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 18:22:29 +00:00
|
|
|
internal static void AssertNoMatch(IEnumerable<ProxyMember> members, string? input)
|
2020-06-14 17:20:02 +00:00
|
|
|
{
|
2020-06-14 18:22:29 +00:00
|
|
|
Assert.False(new ProxyTagParser().TryMatch(members, input, out _));
|
2020-06-14 17:20:02 +00:00
|
|
|
}
|
2020-06-14 16:56:53 +00:00
|
|
|
}
|
|
|
|
}
|