From 62edbb195c7de7fd20ba0e234322ea0c3ed7a67b Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 16 Apr 2024 19:41:28 +0200 Subject: [PATCH] [tests] Remove MfmTests, rename MfmTetssFParsec to MfmTests --- Iceshrimp.Tests/Parsing/MfmTests.cs | 183 +++++++++++++++++-- Iceshrimp.Tests/Parsing/MfmTestsFParsec.cs | 195 --------------------- 2 files changed, 170 insertions(+), 208 deletions(-) delete mode 100644 Iceshrimp.Tests/Parsing/MfmTestsFParsec.cs diff --git a/Iceshrimp.Tests/Parsing/MfmTests.cs b/Iceshrimp.Tests/Parsing/MfmTests.cs index 68508d50..04f7a357 100644 --- a/Iceshrimp.Tests/Parsing/MfmTests.cs +++ b/Iceshrimp.Tests/Parsing/MfmTests.cs @@ -1,31 +1,72 @@ -using Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion; -using Microsoft.Extensions.DependencyInjection; +using Iceshrimp.Parsing; +using Microsoft.FSharp.Collections; +using static Iceshrimp.Parsing.MfmNodeTypes; namespace Iceshrimp.Tests.Parsing; [TestClass] public class MfmTests { - private const string Mfm = - "*blabla* *test* #example @example @example@invalid @example@example.com @invalid:matrix.org https://hello.com http://test.de javascript://sdfgsdf [test](https://asdfg) ?[test](https://asdfg) `asd`"; + [TestMethod] + public void TestParseBoldItalic() + { + List expected = + [ + new MfmItalicNode(ListModule.OfSeq([ + new MfmTextNode("italic "), + new MfmBoldNode(ListModule.OfSeq([new MfmTextNode("bold")])), + new MfmTextNode(" italic") + ])) + ]; + + var res = Mfm.parse("*italic **bold** italic*").ToList(); + AssertionOptions.FormattingOptions.MaxDepth = 100; + res.Should().Equal(expected, MfmNodeEqual); + } [TestMethod] - public async Task TestToHtml() + public void TestParseCode() { + List expected = + [ + new MfmInlineCodeNode("test"), + new MfmTextNode("\n"), + new MfmCodeBlockNode("test", null), + new MfmTextNode("\n"), + new MfmCodeBlockNode("test", "lang") + ]; + + var res = Mfm.parse(""" + `test` + ``` + test + ``` + ```lang + test + ``` + """); + + AssertionOptions.FormattingOptions.MaxDepth = 100; + res.ToList().Should().Equal(expected, MfmNodeEqual); + } + + [TestMethod] + public void Benchmark() + { + const string mfm = + "*blabla* *test* #example @example @example@invalid @example@example.com @invalid:matrix.org https://hello.com http://test.de javascript://sdfgsdf [test](https://asdfg) ?[test](https://asdfg) `asd`"; + double duration = 100; - for (var i = 0; i < 10; i++) duration = await Benchmark(); + for (var i = 0; i < 4; i++) duration = RunBenchmark(); duration.Should().BeLessThan(2); return; - async Task Benchmark() + double RunBenchmark() { - var provider = MockObjects.ServiceProvider; - var converter = provider.GetRequiredService(); - var pre = DateTime.Now; - await converter.ToHtmlAsync(Mfm, [], null); + Mfm.parse(mfm); var post = DateTime.Now; var ms = (post - pre).TotalMilliseconds; Console.WriteLine($"Took {ms} ms"); @@ -33,6 +74,122 @@ public class MfmTests } } - //TODO: TestFromHtml - //TODO: RoundtripTest + private class MfmNodeEquality : IEqualityComparer + { + public bool Equals(MfmNode? x, MfmNode? y) + { + if (x == null && y == null) return true; + if (x == null && y != null) return false; + if (x != null && y == null) return false; + + return MfmNodeEqual(x!, y!); + } + + public int GetHashCode(MfmNode obj) + { + return obj.GetHashCode(); + } + } + + private static bool MfmNodeEqual(MfmNode a, MfmNode b) + { + if (a.GetType() != b.GetType()) return false; + + if (!a.Children.IsEmpty || !b.Children.IsEmpty) + { + if (!a.Children.IsEmpty && b.Children.IsEmpty || a.Children.IsEmpty && !b.Children.IsEmpty) + return false; + if (!a.Children.SequenceEqual(b.Children, new MfmNodeEquality())) + return false; + } + + switch (a) + { + case MfmTextNode textNode when ((MfmTextNode)b).Text != textNode.Text: + return false; + case MfmMentionNode ax: + { + var bx = (MfmMentionNode)b; + if (bx.Acct != ax.Acct) return false; + if (bx.Username != ax.Username) return false; + if (bx.Host?.Value != ax.Host?.Value) return false; + break; + } + case MfmCodeBlockNode ax: + { + var bx = (MfmCodeBlockNode)b; + if (ax.Code != bx.Code) return false; + if (ax.Language?.Value != bx.Language?.Value) return false; + break; + } + case MfmInlineCodeNode ax: + { + var bx = (MfmInlineCodeNode)b; + if (ax.Code != bx.Code) return false; + break; + } + case MfmMathBlockNode ax: + { + var bx = (MfmMathBlockNode)b; + if (ax.Formula != bx.Formula) return false; + if (ax.Formula != bx.Formula) return false; + break; + } + case MfmMathInlineNode ax: + { + var bx = (MfmMathInlineNode)b; + if (ax.Formula != bx.Formula) return false; + if (ax.Formula != bx.Formula) return false; + break; + } + case MfmSearchNode searchNode: + { + var bx = (MfmSearchNode)b; + if (searchNode.Query != bx.Query) return false; + if (searchNode.Content != bx.Content) return false; + break; + } + case MfmUnicodeEmojiNode ax: + { + var bx = (MfmUnicodeEmojiNode)b; + if (ax.Emoji != bx.Emoji) return false; + break; + } + case MfmEmojiCodeNode ax: + { + var bx = (MfmEmojiCodeNode)b; + if (ax.Name != bx.Name) return false; + break; + } + case MfmHashtagNode ax: + { + var bx = (MfmHashtagNode)b; + if (ax.Hashtag != bx.Hashtag) return false; + break; + } + case MfmUrlNode ax: + { + var bx = (MfmUrlNode)b; + if (ax.Url != bx.Url) return false; + if (ax.Brackets != bx.Brackets) return false; + break; + } + case MfmLinkNode ax: + { + var bx = (MfmLinkNode)b; + if (ax.Url != bx.Url) return false; + if (ax.Silent != bx.Silent) return false; + break; + } + case MfmFnNode ax: + { + var bx = (MfmFnNode)b; + if (ax.Args != bx.Args) return false; + if (ax.Name != bx.Name) return false; + break; + } + } + + return true; + } } \ No newline at end of file diff --git a/Iceshrimp.Tests/Parsing/MfmTestsFParsec.cs b/Iceshrimp.Tests/Parsing/MfmTestsFParsec.cs deleted file mode 100644 index 30a33ff0..00000000 --- a/Iceshrimp.Tests/Parsing/MfmTestsFParsec.cs +++ /dev/null @@ -1,195 +0,0 @@ -using Iceshrimp.Parsing; -using Microsoft.FSharp.Collections; -using static Iceshrimp.Parsing.MfmNodeTypes; - -namespace Iceshrimp.Tests.Parsing; - -[TestClass] -public class MfmTestsFParsec -{ - [TestMethod] - public void TestParseBoldItalic() - { - List expected = - [ - new MfmItalicNode(ListModule.OfSeq([ - new MfmTextNode("italic "), - new MfmBoldNode(ListModule.OfSeq([new MfmTextNode("bold")])), - new MfmTextNode(" italic") - ])) - ]; - - var res = Mfm.parse("*italic **bold** italic*").ToList(); - AssertionOptions.FormattingOptions.MaxDepth = 100; - res.Should().Equal(expected, MfmNodeEqual); - } - - [TestMethod] - public void TestParseCode() - { - List expected = - [ - new MfmInlineCodeNode("test"), - new MfmTextNode("\n"), - new MfmCodeBlockNode("test", null), - new MfmTextNode("\n"), - new MfmCodeBlockNode("test", "lang") - ]; - - var res = Mfm.parse(""" - `test` - ``` - test - ``` - ```lang - test - ``` - """); - - AssertionOptions.FormattingOptions.MaxDepth = 100; - res.ToList().Should().Equal(expected, MfmNodeEqual); - } - - [TestMethod] - public void Benchmark() - { - const string mfm = - "*blabla* *test* #example @example @example@invalid @example@example.com @invalid:matrix.org https://hello.com http://test.de javascript://sdfgsdf [test](https://asdfg) ?[test](https://asdfg) `asd`"; - - double duration = 100; - for (var i = 0; i < 4; i++) duration = RunBenchmark(); - - duration.Should().BeLessThan(2); - - return; - - double RunBenchmark() - { - var pre = DateTime.Now; - Mfm.parse(mfm); - var post = DateTime.Now; - var ms = (post - pre).TotalMilliseconds; - Console.WriteLine($"Took {ms} ms"); - return ms; - } - } - - private class MfmNodeEquality : IEqualityComparer - { - public bool Equals(MfmNode? x, MfmNode? y) - { - if (x == null && y == null) return true; - if (x == null && y != null) return false; - if (x != null && y == null) return false; - - return MfmNodeEqual(x!, y!); - } - - public int GetHashCode(MfmNode obj) - { - return obj.GetHashCode(); - } - } - - private static bool MfmNodeEqual(MfmNode a, MfmNode b) - { - if (a.GetType() != b.GetType()) return false; - - if (!a.Children.IsEmpty || !b.Children.IsEmpty) - { - if (!a.Children.IsEmpty && b.Children.IsEmpty || a.Children.IsEmpty && !b.Children.IsEmpty) - return false; - if (!a.Children.SequenceEqual(b.Children, new MfmNodeEquality())) - return false; - } - - switch (a) - { - case MfmTextNode textNode when ((MfmTextNode)b).Text != textNode.Text: - return false; - case MfmMentionNode ax: - { - var bx = (MfmMentionNode)b; - if (bx.Acct != ax.Acct) return false; - if (bx.Username != ax.Username) return false; - if (bx.Host?.Value != ax.Host?.Value) return false; - break; - } - case MfmCodeBlockNode ax: - { - var bx = (MfmCodeBlockNode)b; - if (ax.Code != bx.Code) return false; - if (ax.Language?.Value != bx.Language?.Value) return false; - break; - } - case MfmInlineCodeNode ax: - { - var bx = (MfmInlineCodeNode)b; - if (ax.Code != bx.Code) return false; - break; - } - case MfmMathBlockNode ax: - { - var bx = (MfmMathBlockNode)b; - if (ax.Formula != bx.Formula) return false; - if (ax.Formula != bx.Formula) return false; - break; - } - case MfmMathInlineNode ax: - { - var bx = (MfmMathInlineNode)b; - if (ax.Formula != bx.Formula) return false; - if (ax.Formula != bx.Formula) return false; - break; - } - case MfmSearchNode searchNode: - { - var bx = (MfmSearchNode)b; - if (searchNode.Query != bx.Query) return false; - if (searchNode.Content != bx.Content) return false; - break; - } - case MfmUnicodeEmojiNode ax: - { - var bx = (MfmUnicodeEmojiNode)b; - if (ax.Emoji != bx.Emoji) return false; - break; - } - case MfmEmojiCodeNode ax: - { - var bx = (MfmEmojiCodeNode)b; - if (ax.Name != bx.Name) return false; - break; - } - case MfmHashtagNode ax: - { - var bx = (MfmHashtagNode)b; - if (ax.Hashtag != bx.Hashtag) return false; - break; - } - case MfmUrlNode ax: - { - var bx = (MfmUrlNode)b; - if (ax.Url != bx.Url) return false; - if (ax.Brackets != bx.Brackets) return false; - break; - } - case MfmLinkNode ax: - { - var bx = (MfmLinkNode)b; - if (ax.Url != bx.Url) return false; - if (ax.Silent != bx.Silent) return false; - break; - } - case MfmFnNode ax: - { - var bx = (MfmFnNode)b; - if (ax.Args != bx.Args) return false; - if (ax.Name != bx.Name) return false; - break; - } - } - - return true; - } -} \ No newline at end of file