2019-01-24 16:43:03 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-04-13 17:19:50 +08:00
using NUnit.Framework ;
using osu.Game.Online.Chat ;
namespace osu.Game.Tests.Chat
{
[TestFixture]
public class MessageFormatterTests
{
2022-01-11 16:53:15 +08:00
private string originalWebsiteRootUrl ;
[OneTimeSetUp]
public void OneTimeSetUp ( )
{
originalWebsiteRootUrl = MessageFormatter . WebsiteRootUrl ;
MessageFormatter . WebsiteRootUrl = "dev.ppy.sh" ;
}
[OneTimeTearDown]
public void OneTimeTearDown ( )
{
MessageFormatter . WebsiteRootUrl = originalWebsiteRootUrl ;
}
2022-12-12 17:27:14 +08:00
[Test]
public void TestUnsupportedProtocolLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a gopher://really-old-protocol we don't support." } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "gopher://really-old-protocol" , result . Links [ 0 ] . Url ) ;
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestBareLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a http://www.basic-link.com/?test=test." } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "http://www.basic-link.com/?test=test" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 36 , result . Links [ 0 ] . Length ) ;
}
2021-02-12 20:30:02 +08:00
[TestCase(LinkAction.OpenBeatmap, "456", "https://dev.ppy.sh/beatmapsets/123#osu/456")]
[TestCase(LinkAction.OpenBeatmap, "456", "https://dev.ppy.sh/beatmapsets/123#osu/456?whatever")]
[TestCase(LinkAction.OpenBeatmap, "456", "https://dev.ppy.sh/beatmapsets/123/456")]
2021-06-01 13:09:35 +08:00
[TestCase(LinkAction.External, "https://dev.ppy.sh/beatmapsets/abc/def", "https://dev.ppy.sh/beatmapsets/abc/def")]
2021-02-12 20:30:02 +08:00
[TestCase(LinkAction.OpenBeatmapSet, "123", "https://dev.ppy.sh/beatmapsets/123")]
[TestCase(LinkAction.OpenBeatmapSet, "123", "https://dev.ppy.sh/beatmapsets/123/whatever")]
2021-06-01 13:09:35 +08:00
[TestCase(LinkAction.External, "https://dev.ppy.sh/beatmapsets/abc", "https://dev.ppy.sh/beatmapsets/abc")]
2021-06-21 12:36:08 +08:00
[TestCase(LinkAction.External, "https://dev.ppy.sh/beatmapsets/discussions", "https://dev.ppy.sh/beatmapsets/discussions")]
[TestCase(LinkAction.External, "https://dev.ppy.sh/beatmapsets/discussions/123", "https://dev.ppy.sh/beatmapsets/discussions/123")]
2021-02-12 14:17:39 +08:00
public void TestBeatmapLinks ( LinkAction expectedAction , string expectedArg , string link )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = link } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( expectedAction , result . Links [ 0 ] . Action ) ;
Assert . AreEqual ( expectedArg , result . Links [ 0 ] . Argument ) ;
2021-02-12 14:25:00 +08:00
if ( expectedAction = = LinkAction . External )
Assert . AreEqual ( link , result . Links [ 0 ] . Url ) ;
2021-02-12 14:17:39 +08:00
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestMultipleComplexLinks ( )
{
2022-01-11 16:53:15 +08:00
Message result = MessageFormatter . FormatMessage ( new Message
{
Content = "This is a http://test.io/link#fragment. (see https://twitter.com). Also, This string should not be altered. http://example.com/"
} ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 3 , result . Links . Count ) ;
Assert . AreEqual ( "http://test.io/link#fragment" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 28 , result . Links [ 0 ] . Length ) ;
Assert . AreEqual ( "https://twitter.com" , result . Links [ 1 ] . Url ) ;
Assert . AreEqual ( 45 , result . Links [ 1 ] . Index ) ;
Assert . AreEqual ( 19 , result . Links [ 1 ] . Length ) ;
Assert . AreEqual ( "http://example.com/" , result . Links [ 2 ] . Url ) ;
Assert . AreEqual ( 108 , result . Links [ 2 ] . Index ) ;
Assert . AreEqual ( 19 , result . Links [ 2 ] . Length ) ;
}
[Test]
public void TestAjaxLinks ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "https://twitter.com/#!/hashbanglinks" } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( result . Content , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 0 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 36 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestUnixHomeLinks ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "http://www.chiark.greenend.org.uk/~sgtatham/putty/" } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( result . Content , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 0 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 50 , result . Links [ 0 ] . Length ) ;
}
[Test]
2019-05-15 17:32:29 +08:00
public void TestInsensitiveLinks ( )
2018-04-13 17:19:50 +08:00
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "look: http://puu.sh/7Ggh8xcC6/asf0asd9876.NEF" } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 6 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 39 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestWikiLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [[Wiki Link]]." } ) ;
Assert . AreEqual ( "This is a Wiki Link." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
2022-01-11 16:53:15 +08:00
Assert . AreEqual ( "https://dev.ppy.sh/wiki/Wiki Link" , result . Links [ 0 ] . Url ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 9 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestMultiWikiLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [[Wiki Link]] [[Wiki:Link]][[Wiki.Link]]." } ) ;
Assert . AreEqual ( "This is a Wiki Link Wiki:LinkWiki.Link." , result . DisplayContent ) ;
Assert . AreEqual ( 3 , result . Links . Count ) ;
2022-01-11 16:53:15 +08:00
Assert . AreEqual ( "https://dev.ppy.sh/wiki/Wiki Link" , result . Links [ 0 ] . Url ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 9 , result . Links [ 0 ] . Length ) ;
2022-01-11 16:53:15 +08:00
Assert . AreEqual ( "https://dev.ppy.sh/wiki/Wiki:Link" , result . Links [ 1 ] . Url ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 20 , result . Links [ 1 ] . Index ) ;
Assert . AreEqual ( 9 , result . Links [ 1 ] . Length ) ;
2022-01-11 16:53:15 +08:00
Assert . AreEqual ( "https://dev.ppy.sh/wiki/Wiki.Link" , result . Links [ 2 ] . Url ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 29 , result . Links [ 2 ] . Index ) ;
Assert . AreEqual ( 9 , result . Links [ 2 ] . Length ) ;
}
[Test]
public void TestOldFormatLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a (simple test)[https://osu.ppy.sh] of links." } ) ;
Assert . AreEqual ( "This is a simple test of links." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 11 , result . Links [ 0 ] . Length ) ;
}
2019-09-04 06:17:52 +08:00
[Test]
public void TestOldFormatLinkWithBalancedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a (tricky (one))[https://osu.ppy.sh]!" } ) ;
Assert . AreEqual ( "This is a tricky (one)!" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 12 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestOldFormatLinkWithEscapedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is (another loose bracket \\))[https://osu.ppy.sh]." } ) ;
Assert . AreEqual ( "This is another loose bracket )." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 8 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 23 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestOldFormatWithBackslashes ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This link (should end with a backslash \\)[https://osu.ppy.sh]." } ) ;
Assert . AreEqual ( "This link should end with a backslash \\." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 29 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestOldFormatLinkWithEscapedAndBalancedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a (\\)super\\(\\( tricky (one))[https://osu.ppy.sh]!" } ) ;
Assert . AreEqual ( "This is a )super(( tricky (one)!" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 21 , result . Links [ 0 ] . Length ) ;
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestNewFormatLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [https://osu.ppy.sh simple test]." } ) ;
Assert . AreEqual ( "This is a simple test." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 11 , result . Links [ 0 ] . Length ) ;
}
Add backslash escaping to new link format
For users to be able to add square brackets inside of links using
the new format, the regular expression used for parsing those links
contained a balancing group, which can be used for matching pairs
of tokens (in this case, opening and closing brackets, in that order).
However, this means that users could not post links with unmatched
brackets inside of them (ie. ones that contain single brackets, or
a closing bracket and then an opening one). Allow for escaping opening
and closing brackets using the backslash character.
The change substitutes this old fragment of the regex in the display
text group:
[^\[\]]* // any character other than closing/opening bracket
for this one:
(((?<=\\)[\[\]])|[^\[\]])*
The second pattern in the alternative remains the same; the first one
performs the escaping, as follows:
(
(?<=\\) // positive lookbehind expression:
// this match will succeed, if the next expression
// is preceded by a single backslash
[\[\]] // either an opening or closing brace
)
Since the entire display group is matched, unfortunately the lookbehind
expression does not actually strip the backslashes, so they are
manually stripped in handleMatches.
As demonstrated in the unit tests attached, this also allows balanced
brackets to be mixed with escaped ones.
2019-09-04 05:18:39 +08:00
[Test]
public void TestNewFormatLinkWithEscapedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [https://osu.ppy.sh nasty link with escaped brackets: \\] and \\[]" } ) ;
Assert . AreEqual ( "This is a nasty link with escaped brackets: ] and [" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 41 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestNewFormatLinkWithBackslashesInside ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [https://osu.ppy.sh link \\ with \\ backslashes \\]" } ) ;
Assert . AreEqual ( "This is a link \\ with \\ backslashes \\" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 27 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestNewFormatLinkWithEscapedAndBalancedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [https://osu.ppy.sh [link [with \\] too many brackets \\[ ]]]" } ) ;
Assert . AreEqual ( "This is a [link [with ] too many brackets [ ]]" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 36 , result . Links [ 0 ] . Length ) ;
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestMarkdownFormatLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [simple test](https://osu.ppy.sh)." } ) ;
Assert . AreEqual ( "This is a simple test." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 11 , result . Links [ 0 ] . Length ) ;
}
2019-09-04 06:01:26 +08:00
[Test]
public void TestMarkdownFormatLinkWithBalancedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [tricky [one]](https://osu.ppy.sh)!" } ) ;
Assert . AreEqual ( "This is a tricky [one]!" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 12 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestMarkdownFormatLinkWithEscapedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is [another loose bracket \\]](https://osu.ppy.sh)." } ) ;
Assert . AreEqual ( "This is another loose bracket ]." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 8 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 23 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestMarkdownFormatWithBackslashes ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This link [should end with a backslash \\](https://osu.ppy.sh)." } ) ;
Assert . AreEqual ( "This link should end with a backslash \\." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 29 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestMarkdownFormatLinkWithEscapedAndBalancedBrackets ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [\\]super\\[\\[ tricky [one]](https://osu.ppy.sh)!" } ) ;
Assert . AreEqual ( "This is a ]super[[ tricky [one]!" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 21 , result . Links [ 0 ] . Length ) ;
}
2019-10-22 06:34:33 +08:00
[Test]
public void TestMarkdownFormatLinkWithInlineTitle ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "I haven't seen [this link format](https://osu.ppy.sh \"osu!\") before..." } ) ;
Assert . AreEqual ( "I haven't seen this link format before..." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 15 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 16 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestMarkdownFormatLinkWithInlineTitleAndEscapedQuotes ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "I haven't seen [this link format](https://osu.ppy.sh \"inner quote \\\" just to confuse \") before..." } ) ;
Assert . AreEqual ( "I haven't seen this link format before..." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 15 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 16 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestMarkdownFormatLinkWithUrlInTextAndInlineTitle ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "I haven't seen [https://osu.ppy.sh](https://osu.ppy.sh \"https://osu.ppy.sh\") before..." } ) ;
Assert . AreEqual ( "I haven't seen https://osu.ppy.sh before..." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 15 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 18 , result . Links [ 0 ] . Length ) ;
}
2019-10-22 07:01:37 +08:00
[Test]
public void TestMarkdownFormatLinkWithUrlAndTextInTitle ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "I haven't seen [oh no, text here! https://osu.ppy.sh](https://osu.ppy.sh) before..." } ) ;
Assert . AreEqual ( "I haven't seen oh no, text here! https://osu.ppy.sh before..." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 15 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 36 , result . Links [ 0 ] . Length ) ;
}
2019-10-22 06:34:33 +08:00
[Test]
public void TestMarkdownFormatLinkWithMisleadingUrlInText ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "I haven't seen [https://google.com](https://osu.ppy.sh) before..." } ) ;
Assert . AreEqual ( "I haven't seen https://google.com before..." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 15 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 18 , result . Links [ 0 ] . Length ) ;
}
2019-10-25 06:20:44 +08:00
[Test]
public void TestMarkdownFormatLinkThatContractsIntoLargerLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "super broken https://[osu.ppy](https://reddit.com).sh/" } ) ;
Assert . AreEqual ( "super broken https://osu.ppy.sh/" , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://reddit.com" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 21 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 7 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestMarkdownFormatLinkDirectlyNextToRawLink ( )
{
// the raw link has a port at the end of it, so that the raw link regex terminates at the port and doesn't consume display text from the formatted one
Message result = MessageFormatter . FormatMessage ( new Message { Content = "https://localhost:8080[https://osu.ppy.sh](https://osu.ppy.sh) should be two links" } ) ;
Assert . AreEqual ( "https://localhost:8080https://osu.ppy.sh should be two links" , result . DisplayContent ) ;
Assert . AreEqual ( 2 , result . Links . Count ) ;
Assert . AreEqual ( "https://localhost:8080" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 0 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 22 , result . Links [ 0 ] . Length ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 1 ] . Url ) ;
Assert . AreEqual ( 22 , result . Links [ 1 ] . Index ) ;
Assert . AreEqual ( 18 , result . Links [ 1 ] . Length ) ;
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestChannelLink ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is an #english and #japanese." } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 2 , result . Links . Count ) ;
2022-02-18 14:57:37 +08:00
Assert . AreEqual ( $"{OsuGameBase.OSU_PROTOCOL}chan/#english" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( $"{OsuGameBase.OSU_PROTOCOL}chan/#japanese" , result . Links [ 1 ] . Url ) ;
2018-04-13 17:19:50 +08:00
}
[Test]
public void TestOsuProtocol ( )
{
2022-02-18 14:57:37 +08:00
Message result = MessageFormatter . FormatMessage ( new Message { Content = $"This is a custom protocol {OsuGameBase.OSU_PROTOCOL}chan/#english." } ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
2022-02-18 14:57:37 +08:00
Assert . AreEqual ( $"{OsuGameBase.OSU_PROTOCOL}chan/#english" , result . Links [ 0 ] . Url ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 26 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 19 , result . Links [ 0 ] . Length ) ;
2022-02-18 14:57:37 +08:00
result = MessageFormatter . FormatMessage ( new Message { Content = $"This is a [custom protocol]({OsuGameBase.OSU_PROTOCOL}chan/#english)." } ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( "This is a custom protocol." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
2022-02-18 14:57:37 +08:00
Assert . AreEqual ( $"{OsuGameBase.OSU_PROTOCOL}chan/#english" , result . Links [ 0 ] . Url ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( "#english" , result . Links [ 0 ] . Argument ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 15 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestOsuMpProtocol ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "Join my multiplayer game osump://12346." } ) ;
Assert . AreEqual ( result . Content , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "osump://12346" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 25 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 13 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestRecursiveBreaking ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "This is a [https://osu.ppy.sh [[simple test]]]." } ) ;
Assert . AreEqual ( "This is a [[simple test]]." , result . DisplayContent ) ;
Assert . AreEqual ( 1 , result . Links . Count ) ;
Assert . AreEqual ( "https://osu.ppy.sh" , result . Links [ 0 ] . Url ) ;
Assert . AreEqual ( 10 , result . Links [ 0 ] . Index ) ;
Assert . AreEqual ( 15 , result . Links [ 0 ] . Length ) ;
}
[Test]
public void TestLinkComplex ( )
{
2022-01-11 16:53:15 +08:00
Message result = MessageFormatter . FormatMessage ( new Message
{
Content = "This is a [http://www.simple-test.com simple test] with some [traps] and [[wiki links]]. Don't forget to visit https://osu.ppy.sh (now!)[http://google.com]\uD83D\uDE12"
} ) ;
2018-04-13 17:19:50 +08:00
2023-07-13 09:13:10 +08:00
Assert . AreEqual ( "This is a simple test with some [traps] and wiki links. Don't forget to visit https://osu.ppy.sh now!" , result . DisplayContent ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 5 , result . Links . Count ) ;
2022-01-11 16:53:15 +08:00
Link f = result . Links . Find ( l = > l . Url = = "https://dev.ppy.sh/wiki/wiki links" ) ;
2020-06-09 21:13:48 +08:00
Assert . That ( f , Is . Not . Null ) ;
Assert . AreEqual ( 44 , f . Index ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 10 , f . Length ) ;
f = result . Links . Find ( l = > l . Url = = "http://www.simple-test.com" ) ;
2020-06-09 21:13:48 +08:00
Assert . That ( f , Is . Not . Null ) ;
Assert . AreEqual ( 10 , f . Index ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 11 , f . Length ) ;
f = result . Links . Find ( l = > l . Url = = "http://google.com" ) ;
2020-06-09 21:13:48 +08:00
Assert . That ( f , Is . Not . Null ) ;
Assert . AreEqual ( 97 , f . Index ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 4 , f . Length ) ;
f = result . Links . Find ( l = > l . Url = = "https://osu.ppy.sh" ) ;
2020-06-09 21:13:48 +08:00
Assert . That ( f , Is . Not . Null ) ;
Assert . AreEqual ( 78 , f . Index ) ;
2018-04-13 17:19:50 +08:00
Assert . AreEqual ( 18 , f . Length ) ;
}
[Test]
2023-07-13 09:13:10 +08:00
[Ignore("https://github.com/ppy/osu/pull/24190")]
2018-04-13 17:19:50 +08:00
public void TestEmoji ( )
{
Message result = MessageFormatter . FormatMessage ( new Message { Content = "Hello world\uD83D\uDE12<--This is an emoji,There are more:\uD83D\uDE10\uD83D\uDE00,\uD83D\uDE20" } ) ;
Assert . AreEqual ( "Hello world\0\0\0<--This is an emoji,There are more:\0\0\0\0\0\0,\0\0\0" , result . DisplayContent ) ;
Assert . AreEqual ( result . Links . Count , 4 ) ;
Assert . AreEqual ( result . Links [ 0 ] . Index , 11 ) ;
Assert . AreEqual ( result . Links [ 1 ] . Index , 49 ) ;
Assert . AreEqual ( result . Links [ 2 ] . Index , 52 ) ;
Assert . AreEqual ( result . Links [ 3 ] . Index , 56 ) ;
Assert . AreEqual ( result . Links [ 0 ] . Url , "\uD83D\uDE12" ) ;
Assert . AreEqual ( result . Links [ 1 ] . Url , "\uD83D\uDE10" ) ;
Assert . AreEqual ( result . Links [ 2 ] . Url , "\uD83D\uDE00" ) ;
Assert . AreEqual ( result . Links [ 3 ] . Url , "\uD83D\uDE20" ) ;
}
2021-06-01 12:50:04 +08:00
2021-06-01 13:09:35 +08:00
[Test]
public void TestAbsoluteExternalLinks ( )
{
LinkDetails result = MessageFormatter . GetLinkDetails ( "https://google.com" ) ;
Assert . AreEqual ( LinkAction . External , result . Action ) ;
Assert . AreEqual ( "https://google.com" , result . Argument ) ;
}
2021-06-01 12:50:04 +08:00
[Test]
public void TestRelativeExternalLinks ( )
{
LinkDetails result = MessageFormatter . GetLinkDetails ( "/relative" ) ;
Assert . AreEqual ( LinkAction . External , result . Action ) ;
Assert . AreEqual ( "/relative" , result . Argument ) ;
}
2021-10-12 10:37:42 +08:00
[TestCase("https://dev.ppy.sh/home/changelog", "")]
[TestCase("https://dev.ppy.sh/home/changelog/lazer/2021.1012", "lazer/2021.1012")]
public void TestChangelogLinks ( string link , string expectedArg )
{
LinkDetails result = MessageFormatter . GetLinkDetails ( link ) ;
Assert . AreEqual ( LinkAction . OpenChangelog , result . Action ) ;
Assert . AreEqual ( expectedArg , result . Argument ) ;
}
2018-04-13 17:19:50 +08:00
}
}