https://foo.bar/path/\#m4mv5W0GYKZpGvfA.97
"; - - TestParser.TestSpec($"<{input}>", expected); - TestParser.TestSpec(input, expected, "autolinks|advanced"); + test(); } + } - [Test] - public void IsIssue365Corrected() - { - // The scheme must be escaped too... - string input = "![image](\"onclick=\"alert('click')\"://)"; - string expected = ""; + [Test] + public void IsIssue356Corrected() + { + string input = @"https://foo.bar/path/\#m4mv5W0GYKZpGvfA.97"; + string expected = @"https://foo.bar/path/\#m4mv5W0GYKZpGvfA.97
"; - TestParser.TestSpec(input, expected); - } + TestParser.TestSpec($"<{input}>", expected); + TestParser.TestSpec(input, expected, "autolinks|advanced"); + } - [Test] - public void TestAltTextIsCorrectlyEscaped() - { - TestParser.TestSpec( - @"![This is image alt text with quotation ' and double quotation ""hello"" world](girl.png)", - @""); - } + [Test] + public void IsIssue365Corrected() + { + // The scheme must be escaped too... + string input = "![image](\"onclick=\"alert('click')\"://)"; + string expected = ""; - [Test] - public void TestChangelogPRLinksMatchDescription() - { - string solutionFolder = Path.GetFullPath(Path.Combine(TestParser.TestsDirectory, "../..")); - string changelogPath = Path.Combine(solutionFolder, "changelog.md"); - string changelog = File.ReadAllText(changelogPath); - var matches = Regex.Matches(changelog, @"\(\[\(PR #(\d+)\)\]\(.*?pull\/(\d+)\)\)"); - Assert.Greater(matches.Count, 0); - foreach (Match match in matches) - { - Assert.True(int.TryParse(match.Groups[1].Value, out int textNr)); - Assert.True(int.TryParse(match.Groups[2].Value, out int linkNr)); - Assert.AreEqual(textNr, linkNr); - } - } + TestParser.TestSpec(input, expected); + } - [Test] - public void TestFixHang() - { - var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "hang.md")); - _ = Markdown.ToHtml(input); - } + [Test] + public void TestAltTextIsCorrectlyEscaped() + { + TestParser.TestSpec( + @"![This is image alt text with quotation ' and double quotation ""hello"" world](girl.png)", + @""); + } - [Test] - public void TestInvalidHtmlEntity() + [Test] + public void TestChangelogPRLinksMatchDescription() + { + string solutionFolder = Path.GetFullPath(Path.Combine(TestParser.TestsDirectory, "../..")); + string changelogPath = Path.Combine(solutionFolder, "changelog.md"); + string changelog = File.ReadAllText(changelogPath); + var matches = Regex.Matches(changelog, @"\(\[\(PR #(\d+)\)\]\(.*?pull\/(\d+)\)\)"); + Assert.Greater(matches.Count, 0); + foreach (Match match in matches) { - var input = "9&ddr;&*&ddr;&de��__"; - TestParser.TestSpec(input, "9&ddr;&*&ddr;&de��__
"); + Assert.True(int.TryParse(match.Groups[1].Value, out int textNr)); + Assert.True(int.TryParse(match.Groups[2].Value, out int linkNr)); + Assert.AreEqual(textNr, linkNr); } + } - [Test] - public void TestInvalidCharacterHandling() - { - var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "ArgumentOutOfRangeException.md")); - _ = Markdown.ToHtml(input); - } + [Test] + public void TestFixHang() + { + var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "hang.md")); + _ = Markdown.ToHtml(input); + } - [Test] - public void TestInvalidCodeEscape() - { - var input = "```**Header** "; - _ = Markdown.ToHtml(input); - } + [Test] + public void TestInvalidHtmlEntity() + { + var input = "9&ddr;&*&ddr;&de��__"; + TestParser.TestSpec(input, "9&ddr;&*&ddr;&de��__
"); + } - [Test] - public void TestEmphasisAndHtmlEntity() - { - var markdownText = "*Unlimited-Fun®*®"; - TestParser.TestSpec(markdownText, "Unlimited-Fun®®
"); - } + [Test] + public void TestInvalidCharacterHandling() + { + var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "ArgumentOutOfRangeException.md")); + _ = Markdown.ToHtml(input); + } - [Test] - public void TestThematicInsideCodeBlockInsideList() - { - var input = @"1. In the : + [Test] + public void TestInvalidCodeEscape() + { + var input = "```**Header** "; + _ = Markdown.ToHtml(input); + } + + [Test] + public void TestEmphasisAndHtmlEntity() + { + var markdownText = "*Unlimited-Fun®*®"; + TestParser.TestSpec(markdownText, "Unlimited-Fun®®
"); + } + + [Test] + public void TestThematicInsideCodeBlockInsideList() + { + var input = @"1. In the : ``` Id DisplayName Description -- ----------- ----------- 62375ab9-6b52-47ed-826b-58e47e0e304b Group.Unified ... ```"; - TestParser.TestSpec(input, @"In the :
Id DisplayName Description
-- ----------- -----------
62375ab9-6b52-47ed-826b-58e47e0e304b Group.Unified ...
\\("); - var test2 = html.Contains("\\)
"); - if (!test1 || !test2) - { - Console.WriteLine(html); - } + var html = Markdown.ToHtml(math, pl); - Assert.IsTrue(test1, "Leading bracket missing"); - Assert.IsTrue(test2, "Trailing bracket missing"); + var test1 = html.Contains("\\("); + var test2 = html.Contains("\\)
"); + if (!test1 || !test2) + { + Console.WriteLine(html); } - [Test] - public void BlockMathExpression() - { - string math = @"Math expressions + Assert.IsTrue(test1, "Leading bracket missing"); + Assert.IsTrue(test2, "Trailing bracket missing"); + } + + [Test] + public void BlockMathExpression() + { + string math = @"Math expressions $$ \frac{n!}{k!(n-k)!} = \binom{n}{k} $$ "; - var pl = new MarkdownPipelineBuilder().UseMathematics().Build(); // UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build() - - var html = Markdown.ToHtml(math, pl); - var test1 = html.Contains("Foo\n===
", noHeadingsPipeline); + Assert.IsTrue(test1, "Leading bracket missing"); + Assert.IsTrue(test2, "Trailing bracket missing"); + } - TestParser.TestSpec("# Heading 1", "# Heading 1
", noHeadingsPipeline); + [Test] + public void CanDisableParsingHeadings() + { + var noHeadingsPipeline = new MarkdownPipelineBuilder().DisableHeadings().Build(); - // Does not also disable link reference definitions - TestParser.TestSpec("[Foo]\n\n[Foo]: bar", ""); - TestParser.TestSpec("[Foo]\n\n[Foo]: bar", "", noHeadingsPipeline); - } + TestParser.TestSpec("Foo\n===", "Foo\n===
", noHeadingsPipeline); - [Test] - public void CanOpenAutoLinksInNewWindow() - { - var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); - var newWindowPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { OpenInNewWindow = true }).Build(); + TestParser.TestSpec("# Heading 1", "# Heading 1
", noHeadingsPipeline); - TestParser.TestSpec("www.foo.bar", "", pipeline); - TestParser.TestSpec("www.foo.bar", "", newWindowPipeline); - } + // Does not also disable link reference definitions + TestParser.TestSpec("[Foo]\n\n[Foo]: bar", ""); + TestParser.TestSpec("[Foo]\n\n[Foo]: bar", "", noHeadingsPipeline); + } - [Test] - public void CanUseHttpsPrefixForWWWAutoLinks() - { - var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); - var httpsPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { UseHttpsForWWWLinks = true }).Build(); + [Test] + public void CanOpenAutoLinksInNewWindow() + { + var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); + var newWindowPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { OpenInNewWindow = true }).Build(); - TestParser.TestSpec("www.foo.bar", "", pipeline); - TestParser.TestSpec("www.foo.bar", "", httpsPipeline); - } + TestParser.TestSpec("www.foo.bar", "", pipeline); + TestParser.TestSpec("www.foo.bar", "", newWindowPipeline); + } + + [Test] + public void CanUseHttpsPrefixForWWWAutoLinks() + { + var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); + var httpsPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { UseHttpsForWWWLinks = true }).Build(); + + TestParser.TestSpec("www.foo.bar", "", pipeline); + TestParser.TestSpec("www.foo.bar", "", httpsPipeline); } } diff --git a/src/Markdig.Tests/Program.cs b/src/Markdig.Tests/Program.cs index 564e9a9ea..dc47be724 100644 --- a/src/Markdig.Tests/Program.cs +++ b/src/Markdig.Tests/Program.cs @@ -2,18 +2,15 @@ // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. -using System; +namespace Markdig.Tests; -namespace Markdig.Tests +class Program { - class Program + public static void Main(string[] args) { - public static void Main(string[] args) - { - Console.WriteLine("Run NUnit tests runner with this"); - // Uncomment the following line to debug a specific tests more easily - //var tests = new Specs.CommonMarkV_0_29.TestLeafBlocksSetextHeadings(); - //tests.LeafBlocksSetextHeadings_Example063(); - } + Console.WriteLine("Run NUnit tests runner with this"); + // Uncomment the following line to debug a specific tests more easily + //var tests = new Specs.CommonMarkV_0_29.TestLeafBlocksSetextHeadings(); + //tests.LeafBlocksSetextHeadings_Example063(); } } \ No newline at end of file diff --git a/src/Markdig.Tests/RoundtripSpecs/TestAtxHeading.cs b/src/Markdig.Tests/RoundtripSpecs/TestAtxHeading.cs index 4b6607952..51110f4f3 100644 --- a/src/Markdig.Tests/RoundtripSpecs/TestAtxHeading.cs +++ b/src/Markdig.Tests/RoundtripSpecs/TestAtxHeading.cs @@ -1,55 +1,53 @@ -using NUnit.Framework; using static Markdig.Tests.TestRoundtrip; -namespace Markdig.Tests.RoundtripSpecs +namespace Markdig.Tests.RoundtripSpecs; + +[TestFixture] +public class TestAtxHeading { - [TestFixture] - public class TestAtxHeading + [TestCase("# h")] + [TestCase("# h ")] + [TestCase("# h\n#h")] + [TestCase("# h\n #h")] + [TestCase("# h\n # h")] + [TestCase("# h\n # h ")] + [TestCase(" # h \n # h ")] + public void Test(string value) { - [TestCase("# h")] - [TestCase("# h ")] - [TestCase("# h\n#h")] - [TestCase("# h\n #h")] - [TestCase("# h\n # h")] - [TestCase("# h\n # h ")] - [TestCase(" # h \n # h ")] - public void Test(string value) - { - RoundTrip(value); - } + RoundTrip(value); + } - [TestCase("\n# h\n\np")] - [TestCase("\n# h\n\np\n")] - [TestCase("\n# h\n\np\n\n")] - [TestCase("\n\n# h\n\np\n\n")] - [TestCase("\n\n# h\np\n\n")] - [TestCase("\n\n# h\np\n\n")] - public void TestParagraph(string value) - { - RoundTrip(value); - } + [TestCase("\n# h\n\np")] + [TestCase("\n# h\n\np\n")] + [TestCase("\n# h\n\np\n\n")] + [TestCase("\n\n# h\n\np\n\n")] + [TestCase("\n\n# h\np\n\n")] + [TestCase("\n\n# h\np\n\n")] + public void TestParagraph(string value) + { + RoundTrip(value); + } - [TestCase("\n# h")] - [TestCase("\n# h\n")] - [TestCase("\n# h\r")] - [TestCase("\n# h\r\n")] + [TestCase("\n# h")] + [TestCase("\n# h\n")] + [TestCase("\n# h\r")] + [TestCase("\n# h\r\n")] - [TestCase("\r# h")] - [TestCase("\r# h\n")] - [TestCase("\r# h\r")] - [TestCase("\r# h\r\n")] + [TestCase("\r# h")] + [TestCase("\r# h\n")] + [TestCase("\r# h\r")] + [TestCase("\r# h\r\n")] - [TestCase("\r\n# h")] - [TestCase("\r\n# h\n")] - [TestCase("\r\n# h\r")] - [TestCase("\r\n# h\r\n")] + [TestCase("\r\n# h")] + [TestCase("\r\n# h\n")] + [TestCase("\r\n# h\r")] + [TestCase("\r\n# h\r\n")] - [TestCase("# h\n\n ")] - [TestCase("# h\n\n ")] - [TestCase("# h\n\n ")] - public void TestNewline(string value) - { - RoundTrip(value); - } + [TestCase("# h\n\n ")] + [TestCase("# h\n\n ")] + [TestCase("# h\n\n ")] + public void TestNewline(string value) + { + RoundTrip(value); } } diff --git a/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs b/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs index b49fcc633..5361928af 100644 --- a/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs +++ b/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs @@ -1,107 +1,105 @@ -using NUnit.Framework; using static Markdig.Tests.TestRoundtrip; -namespace Markdig.Tests.RoundtripSpecs +namespace Markdig.Tests.RoundtripSpecs; + +[TestFixture] +public class TestFencedCodeBlock { - [TestFixture] - public class TestFencedCodeBlock - { - [TestCase("```\nc\n```")] - [TestCase("```\nc\n```\n")] - [TestCase("\n```\nc\n```")] - [TestCase("\n\n```\nc\n```")] - [TestCase("```\nc\n```\n")] - [TestCase("```\nc\n```\n\n")] - [TestCase("\n```\nc\n```\n")] - [TestCase("\n```\nc\n```\n\n")] - [TestCase("\n\n```\nc\n```\n")] - [TestCase("\n\n```\nc\n```\n\n")] + [TestCase("```\nc\n```")] + [TestCase("```\nc\n```\n")] + [TestCase("\n```\nc\n```")] + [TestCase("\n\n```\nc\n```")] + [TestCase("```\nc\n```\n")] + [TestCase("```\nc\n```\n\n")] + [TestCase("\n```\nc\n```\n")] + [TestCase("\n```\nc\n```\n\n")] + [TestCase("\n\n```\nc\n```\n")] + [TestCase("\n\n```\nc\n```\n\n")] - [TestCase(" ```\nc\n````")] - [TestCase("```\nc\n````")] - [TestCase("p\n\n```\nc\n```")] + [TestCase(" ```\nc\n````")] + [TestCase("```\nc\n````")] + [TestCase("p\n\n```\nc\n```")] - [TestCase("```\n c\n```")] - [TestCase("```\nc \n```")] - [TestCase("```\n c \n```")] + [TestCase("```\n c\n```")] + [TestCase("```\nc \n```")] + [TestCase("```\n c \n```")] - [TestCase(" ``` \n c \n ``` ")] - [TestCase("\t```\t\n\tc\t\n\t```\t")] - [TestCase("\v```\v\n\vc\v\n\v```\v")] - [TestCase("\f```\f\n\fc\f\n\f```\f")] - public void Test(string value) - { - RoundTrip(value); - } + [TestCase(" ``` \n c \n ``` ")] + [TestCase("\t```\t\n\tc\t\n\t```\t")] + [TestCase("\v```\v\n\vc\v\n\v```\v")] + [TestCase("\f```\f\n\fc\f\n\f```\f")] + public void Test(string value) + { + RoundTrip(value); + } - [TestCase("~~~ aa ``` ~~~\nfoo\n~~~")] - [TestCase("~~~ aa ``` ~~~\nfoo\n~~~ ")] - public void TestTilde(string value) - { - RoundTrip(value); - } + [TestCase("~~~ aa ``` ~~~\nfoo\n~~~")] + [TestCase("~~~ aa ``` ~~~\nfoo\n~~~ ")] + public void TestTilde(string value) + { + RoundTrip(value); + } - [TestCase("```\n c \n```")] - [TestCase("```\n c \r```")] - [TestCase("```\n c \r\n```")] - [TestCase("```\r c \n```")] - [TestCase("```\r c \r```")] - [TestCase("```\r c \r\n```")] - [TestCase("```\r\n c \n```")] - [TestCase("```\r\n c \r```")] - [TestCase("```\r\n c \r\n```")] + [TestCase("```\n c \n```")] + [TestCase("```\n c \r```")] + [TestCase("```\n c \r\n```")] + [TestCase("```\r c \n```")] + [TestCase("```\r c \r```")] + [TestCase("```\r c \r\n```")] + [TestCase("```\r\n c \n```")] + [TestCase("```\r\n c \r```")] + [TestCase("```\r\n c \r\n```")] - [TestCase("```\n c \n```\n")] - [TestCase("```\n c \r```\n")] - [TestCase("```\n c \r\n```\n")] - [TestCase("```\r c \n```\n")] - [TestCase("```\r c \r```\n")] - [TestCase("```\r c \r\n```\n")] - [TestCase("```\r\n c \n```\n")] - [TestCase("```\r\n c \r```\n")] - [TestCase("```\r\n c \r\n```\n")] + [TestCase("```\n c \n```\n")] + [TestCase("```\n c \r```\n")] + [TestCase("```\n c \r\n```\n")] + [TestCase("```\r c \n```\n")] + [TestCase("```\r c \r```\n")] + [TestCase("```\r c \r\n```\n")] + [TestCase("```\r\n c \n```\n")] + [TestCase("```\r\n c \r```\n")] + [TestCase("```\r\n c \r\n```\n")] - [TestCase("```\n c \n```\r")] - [TestCase("```\n c \r```\r")] - [TestCase("```\n c \r\n```\r")] - [TestCase("```\r c \n```\r")] - [TestCase("```\r c \r```\r")] - [TestCase("```\r c \r\n```\r")] - [TestCase("```\r\n c \n```\r")] - [TestCase("```\r\n c \r```\r")] - [TestCase("```\r\n c \r\n```\r")] + [TestCase("```\n c \n```\r")] + [TestCase("```\n c \r```\r")] + [TestCase("```\n c \r\n```\r")] + [TestCase("```\r c \n```\r")] + [TestCase("```\r c \r```\r")] + [TestCase("```\r c \r\n```\r")] + [TestCase("```\r\n c \n```\r")] + [TestCase("```\r\n c \r```\r")] + [TestCase("```\r\n c \r\n```\r")] - [TestCase("```\n c \n```\r\n")] - [TestCase("```\n c \r```\r\n")] - [TestCase("```\n c \r\n```\r\n")] - [TestCase("```\r c \n```\r\n")] - [TestCase("```\r c \r```\r\n")] - [TestCase("```\r c \r\n```\r\n")] - [TestCase("```\r\n c \n```\r\n")] - [TestCase("```\r\n c \r```\r\n")] - [TestCase("```\r\n c \r\n```\r\n")] - public void TestNewline(string value) - { - RoundTrip(value); - } + [TestCase("```\n c \n```\r\n")] + [TestCase("```\n c \r```\r\n")] + [TestCase("```\n c \r\n```\r\n")] + [TestCase("```\r c \n```\r\n")] + [TestCase("```\r c \r```\r\n")] + [TestCase("```\r c \r\n```\r\n")] + [TestCase("```\r\n c \n```\r\n")] + [TestCase("```\r\n c \r```\r\n")] + [TestCase("```\r\n c \r\n```\r\n")] + public void TestNewline(string value) + { + RoundTrip(value); + } - [TestCase("```i a\n```")] - [TestCase("```i a a2\n```")] - [TestCase("```i a a2 a3\n```")] - [TestCase("```i a a2 a3 a4\n```")] + [TestCase("```i a\n```")] + [TestCase("```i a a2\n```")] + [TestCase("```i a a2 a3\n```")] + [TestCase("```i a a2 a3 a4\n```")] - [TestCase("```i\ta\n```")] - [TestCase("```i\ta a2\n```")] - [TestCase("```i\ta a2 a3\n```")] - [TestCase("```i\ta a2 a3 a4\n```")] + [TestCase("```i\ta\n```")] + [TestCase("```i\ta a2\n```")] + [TestCase("```i\ta a2 a3\n```")] + [TestCase("```i\ta a2 a3 a4\n```")] - [TestCase("```i\ta \n```")] - [TestCase("```i\ta a2 \n```")] - [TestCase("```i\ta a2 a3 \n```")] - [TestCase("```i\ta a2 a3 a4 \n```")] - public void TestInfoArguments(string value) - { - RoundTrip(value); - } + [TestCase("```i\ta \n```")] + [TestCase("```i\ta a2 \n```")] + [TestCase("```i\ta a2 a3 \n```")] + [TestCase("```i\ta a2 a3 a4 \n```")] + public void TestInfoArguments(string value) + { + RoundTrip(value); } } diff --git a/src/Markdig.Tests/RoundtripSpecs/TestHtmlBlock.cs b/src/Markdig.Tests/RoundtripSpecs/TestHtmlBlock.cs index d40217cf7..9f2860b85 100644 --- a/src/Markdig.Tests/RoundtripSpecs/TestHtmlBlock.cs +++ b/src/Markdig.Tests/RoundtripSpecs/TestHtmlBlock.cs @@ -1,20 +1,18 @@ -using NUnit.Framework; using static Markdig.Tests.TestRoundtrip; -namespace Markdig.Tests.RoundtripSpecs +namespace Markdig.Tests.RoundtripSpecs; + +[TestFixture] +public class TestHtmlBlock { - [TestFixture] - public class TestHtmlBlock + [TestCase("1\n2
\n")] + [TestCase(/* newLineForWriting: */ "\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\n2
\n")] + [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1\r\n2
\r\n")] + [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\r\n2
\r\n")] + [TestCase(/* newLineForWriting: */ "!!!" , /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1!!!2
!!!")] + [TestCase(/* newLineForWriting: */ "!!!" , /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1!!!2
!!!")] + public void TestHtmlOutputWhenConfiguringNewLine(string newLineForWriting, string markdownText, string expected) { - [Test] - [TestCase(/* newLineForWriting: */ "\n", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1\n2
\n")] - [TestCase(/* newLineForWriting: */ "\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\n2
\n")] - [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1\r\n2
\r\n")] - [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\r\n2
\r\n")] - [TestCase(/* newLineForWriting: */ "!!!" , /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1!!!2
!!!")] - [TestCase(/* newLineForWriting: */ "!!!" , /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1!!!2
!!!")] - public void TestHtmlOutputWhenConfiguringNewLine(string newLineForWriting, string markdownText, string expected) - { - var pipeline = new MarkdownPipelineBuilder() - .ConfigureNewLine(newLineForWriting) - .Build(); + var pipeline = new MarkdownPipelineBuilder() + .ConfigureNewLine(newLineForWriting) + .Build(); - var actual = Markdown.ToHtml(markdownText, pipeline); - Assert.AreEqual(expected, actual); - } + var actual = Markdown.ToHtml(markdownText, pipeline); + Assert.AreEqual(expected, actual); + } - [Test] - [TestCase(/* newLineForWriting: */ "\n", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1\n2\n")] - [TestCase(/* newLineForWriting: */ "\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\n2\n")] - [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1\r\n2\r\n")] - [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\r\n2\r\n")] - [TestCase(/* newLineForWriting: */ "!!!", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1!!!2!!!")] - [TestCase(/* newLineForWriting: */ "!!!", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1!!!2!!!")] - public void TestPlainOutputWhenConfiguringNewLine(string newLineForWriting, string markdownText, string expected) - { - var pipeline = new MarkdownPipelineBuilder() - .ConfigureNewLine(newLineForWriting) - .Build(); + [Test] + [TestCase(/* newLineForWriting: */ "\n", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1\n2\n")] + [TestCase(/* newLineForWriting: */ "\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\n2\n")] + [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1\r\n2\r\n")] + [TestCase(/* newLineForWriting: */ "\r\n", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1\r\n2\r\n")] + [TestCase(/* newLineForWriting: */ "!!!", /* markdownText: */ "*1*\n*2*\n", /* expected: */ "1!!!2!!!")] + [TestCase(/* newLineForWriting: */ "!!!", /* markdownText: */ "*1*\r\n*2*\r\n", /* expected: */ "1!!!2!!!")] + public void TestPlainOutputWhenConfiguringNewLine(string newLineForWriting, string markdownText, string expected) + { + var pipeline = new MarkdownPipelineBuilder() + .ConfigureNewLine(newLineForWriting) + .Build(); - var actual = Markdown.ToPlainText(markdownText, pipeline); - Assert.AreEqual(expected, actual); - } + var actual = Markdown.ToPlainText(markdownText, pipeline); + Assert.AreEqual(expected, actual); } } \ No newline at end of file diff --git a/src/Markdig.Tests/TestContainerBlocks.cs b/src/Markdig.Tests/TestContainerBlocks.cs index a7405189d..abffe84fe 100644 --- a/src/Markdig.Tests/TestContainerBlocks.cs +++ b/src/Markdig.Tests/TestContainerBlocks.cs @@ -1,189 +1,186 @@ -using System; using Markdig.Syntax; -using NUnit.Framework; -namespace Markdig.Tests +namespace Markdig.Tests; + +public class TestContainerBlocks { - public class TestContainerBlocks + private class MockContainerBlock : ContainerBlock { - private class MockContainerBlock : ContainerBlock + public MockContainerBlock() + : base(null) { - public MockContainerBlock() - : base(null) - { - } } + } - [Test] - public void CanBeCleared() - { - ContainerBlock container = new MockContainerBlock(); - Assert.AreEqual(0, container.Count); - Assert.Null(container.LastChild); + [Test] + public void CanBeCleared() + { + ContainerBlock container = new MockContainerBlock(); + Assert.AreEqual(0, container.Count); + Assert.Null(container.LastChild); - var paragraph = new ParagraphBlock(); - Assert.Null(paragraph.Parent); + var paragraph = new ParagraphBlock(); + Assert.Null(paragraph.Parent); - container.Add(paragraph); + container.Add(paragraph); - Assert.AreEqual(1, container.Count); - Assert.AreSame(container, paragraph.Parent); - Assert.AreSame(paragraph, container.LastChild); + Assert.AreEqual(1, container.Count); + Assert.AreSame(container, paragraph.Parent); + Assert.AreSame(paragraph, container.LastChild); - container.Clear(); + container.Clear(); - Assert.AreEqual(0, container.Count); - Assert.Null(container.LastChild); - Assert.Null(paragraph.Parent); - } + Assert.AreEqual(0, container.Count); + Assert.Null(container.LastChild); + Assert.Null(paragraph.Parent); + } - [Test] - public void CanBeInsertedInto() - { - ContainerBlock container = new MockContainerBlock(); - - var one = new ParagraphBlock(); - container.Insert(0, one); - Assert.AreEqual(1, container.Count); - Assert.AreSame(container[0], one); - Assert.AreSame(container, one.Parent); - - var two = new ParagraphBlock(); - container.Insert(1, two); - Assert.AreEqual(2, container.Count); - Assert.AreSame(container[0], one); - Assert.AreSame(container[1], two); - Assert.AreSame(container, two.Parent); - - var three = new ParagraphBlock(); - container.Insert(0, three); - Assert.AreEqual(3, container.Count); - Assert.AreSame(container[0], three); - Assert.AreSame(container[1], one); - Assert.AreSame(container[2], two); - Assert.AreSame(container, three.Parent); - - Assert.Throws♥
\n")] + [TestCase(":confused:", ":confused:
\n")] // default emoji does not work + [TestCase(":/", ":/
\n")] // default smiley does not work + public void TestCustomEmoji(string input, string expected) + { + var emojiToUnicode = new Dictionary♥
\n")] + [TestCase("hello", "♥
\n")] + [TestCase(":confused:", ":confused:
\n")] // default emoji does not work + [TestCase(":/", ":/
\n")] // default smiley does not work + public void TestCustomSmiley(string input, string expected) + { + var emojiToUnicode = new Dictionary♥
\n")] + [TestCase(":)", "♥
\n")] + [TestCase(":confused:", "😕
\n")] // default emoji still works + [TestCase(":/", "😕
\n")] // default smiley still works + public void TestOverrideDefaultWithCustomEmoji(string input, string expected) + { + var emojiToUnicode = EmojiMapping.GetDefaultEmojiShortcodeToUnicode(); + var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmojiShortcode(); + + emojiToUnicode[":smiley:"] = "♥"; + + var customMapping = new EmojiMapping(emojiToUnicode, smileyToEmoji); + + var pipeline = new MarkdownPipelineBuilder() + .UseEmojiAndSmiley(customEmojiMapping: customMapping) + .Build(); + + var actual = Markdown.ToHtml(input, pipeline); + Assert.AreEqual(expected, actual); + } + + [Test] + [TestCase(":testheart:", "♥
\n")] + [TestCase("hello", "♥
\n")] + [TestCase(":confused:", "😕
\n")] // default emoji still works + [TestCase(":/", "😕
\n")] // default smiley still works + public void TestOverrideDefaultWithCustomSmiley(string input, string expected) { - [Test] - [TestCase(":smiley:", "♥
\n")] - [TestCase(":confused:", ":confused:
\n")] // default emoji does not work - [TestCase(":/", ":/
\n")] // default smiley does not work - public void TestCustomEmoji(string input, string expected) - { - var emojiToUnicode = new Dictionary♥
\n")] - [TestCase("hello", "♥
\n")] - [TestCase(":confused:", ":confused:
\n")] // default emoji does not work - [TestCase(":/", ":/
\n")] // default smiley does not work - public void TestCustomSmiley(string input, string expected) - { - var emojiToUnicode = new Dictionary♥
\n")] - [TestCase(":)", "♥
\n")] - [TestCase(":confused:", "😕
\n")] // default emoji still works - [TestCase(":/", "😕
\n")] // default smiley still works - public void TestOverrideDefaultWithCustomEmoji(string input, string expected) - { - var emojiToUnicode = EmojiMapping.GetDefaultEmojiShortcodeToUnicode(); - var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmojiShortcode(); - - emojiToUnicode[":smiley:"] = "♥"; - - var customMapping = new EmojiMapping(emojiToUnicode, smileyToEmoji); - - var pipeline = new MarkdownPipelineBuilder() - .UseEmojiAndSmiley(customEmojiMapping: customMapping) - .Build(); - - var actual = Markdown.ToHtml(input, pipeline); - Assert.AreEqual(expected, actual); - } - - [Test] - [TestCase(":testheart:", "♥
\n")] - [TestCase("hello", "♥
\n")] - [TestCase(":confused:", "😕
\n")] // default emoji still works - [TestCase(":/", "😕
\n")] // default smiley still works - public void TestOverrideDefaultWithCustomSmiley(string input, string expected) - { - var emojiToUnicode = EmojiMapping.GetDefaultEmojiShortcodeToUnicode(); - var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmojiShortcode(); - - emojiToUnicode[":testheart:"] = "♥"; - smileyToEmoji["hello"] = ":testheart:"; - - var customMapping = new EmojiMapping(emojiToUnicode, smileyToEmoji); - - var pipeline = new MarkdownPipelineBuilder() - .UseEmojiAndSmiley(customEmojiMapping: customMapping) - .Build(); - - var actual = Markdown.ToHtml(input, pipeline); - Assert.AreEqual(expected, actual); - } - - [Test] - public void TestCustomEmojiValidation() - { - var emojiToUnicode = new Dictionary" + expectedHtml + "
", Pipeline); - } + [Test] + [TestCase("*foo**", "foo*")] + [TestCase("**foo*", "*foo")] + [TestCase("***foo***", "foo")] + [TestCase("**_foo_**", "foo")] + [TestCase("_**foo**_", "foo")] + [TestCase("\"foo\"", "" + expectedHtml + "
", Pipeline); } } diff --git a/src/Markdig.Tests/TestEmphasisExtraOptions.cs b/src/Markdig.Tests/TestEmphasisExtraOptions.cs index 7da3fec7d..217987f3a 100644 --- a/src/Markdig.Tests/TestEmphasisExtraOptions.cs +++ b/src/Markdig.Tests/TestEmphasisExtraOptions.cs @@ -1,45 +1,43 @@ -using NUnit.Framework; using Markdig.Extensions.EmphasisExtras; -namespace Markdig.Tests +namespace Markdig.Tests; + +[TestFixture] +public class TestEmphasisExtraOptions { - [TestFixture] - public class TestEmphasisExtraOptions + [Test] + public void OnlyStrikethrough_Single() { - [Test] - public void OnlyStrikethrough_Single() - { - TestParser.TestSpec("~foo~", "~foo~
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).Build()); - } + TestParser.TestSpec("~foo~", "~foo~
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).Build()); + } - [Test] - public void OnlyStrikethrough_Double() - { - TestParser.TestSpec("~~foo~~", "foo
foo
foo
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); - } + [Test] + public void OnlySubscript_Single() + { + TestParser.TestSpec("~foo~", "foo
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); + } - [Test] - public void OnlySubscript_Double() - { - TestParser.TestSpec("~~foo~~", "foo
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); - } + [Test] + public void OnlySubscript_Double() + { + TestParser.TestSpec("~~foo~~", "foo
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); + } - [Test] - public void SubscriptAndStrikethrough_Single() - { - TestParser.TestSpec("~foo~", "foo
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough | EmphasisExtraOptions.Subscript).Build()); - } + [Test] + public void SubscriptAndStrikethrough_Single() + { + TestParser.TestSpec("~foo~", "foo
", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough | EmphasisExtraOptions.Subscript).Build()); + } - [Test] - public void SubscriptAndStrikethrough_Double() - { - TestParser.TestSpec("~~foo~~", "foo
foo
Strong emphasis normal
", ""); - } + TestParser.TestSpec("***Strong emphasis*** normal", "Strong emphasis normal
", ""); + } - [Test] - public void NormalStrongNormal() - { - TestParser.TestSpec("normal ***Strong emphasis*** normal", "normal Strong emphasis normal
", ""); - } + [Test] + public void NormalStrongNormal() + { + TestParser.TestSpec("normal ***Strong emphasis*** normal", "normal Strong emphasis normal
", ""); } } \ No newline at end of file diff --git a/src/Markdig.Tests/TestExceptionNotThrown.cs b/src/Markdig.Tests/TestExceptionNotThrown.cs index 034d2620c..0663017c1 100644 --- a/src/Markdig.Tests/TestExceptionNotThrown.cs +++ b/src/Markdig.Tests/TestExceptionNotThrown.cs @@ -1,48 +1,45 @@ -using NUnit.Framework; +namespace Markdig.Tests; -namespace Markdig.Tests +[TestFixture] +public class TestExceptionNotThrown { - [TestFixture] - public class TestExceptionNotThrown + [Test] + public void DoesNotThrowIndexOutOfRangeException1() { - [Test] - public void DoesNotThrowIndexOutOfRangeException1() + Assert.DoesNotThrow(() => { - Assert.DoesNotThrow(() => - { - var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); - Markdown.ToHtml("+-\n|\n+", pipeline); - }); - } + var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); + Markdown.ToHtml("+-\n|\n+", pipeline); + }); + } - [Test] - public void DoesNotThrowIndexOutOfRangeException2() + [Test] + public void DoesNotThrowIndexOutOfRangeException2() + { + Assert.DoesNotThrow(() => { - Assert.DoesNotThrow(() => - { - var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); - Markdown.ToHtml("+--\n|\n+0", pipeline); - }); - } + var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); + Markdown.ToHtml("+--\n|\n+0", pipeline); + }); + } - [Test] - public void DoesNotThrowIndexOutOfRangeException3() + [Test] + public void DoesNotThrowIndexOutOfRangeException3() + { + Assert.DoesNotThrow(() => { - Assert.DoesNotThrow(() => - { - var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); - Markdown.ToHtml("+-\n|\n+\n0", pipeline); - }); - } + var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); + Markdown.ToHtml("+-\n|\n+\n0", pipeline); + }); + } - [Test] - public void DoesNotThrowIndexOutOfRangeException4() + [Test] + public void DoesNotThrowIndexOutOfRangeException4() + { + Assert.DoesNotThrow(() => { - Assert.DoesNotThrow(() => - { - var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); - Markdown.ToHtml("+-\n|\n+0", pipeline); - }); - } + var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); + Markdown.ToHtml("+-\n|\n+0", pipeline); + }); } } diff --git a/src/Markdig.Tests/TestFastStringWriter.cs b/src/Markdig.Tests/TestFastStringWriter.cs index d43dd3a3d..e28b0a60b 100644 --- a/src/Markdig.Tests/TestFastStringWriter.cs +++ b/src/Markdig.Tests/TestFastStringWriter.cs @@ -1,189 +1,186 @@ -using Markdig.Helpers; -using NUnit.Framework; -using System; using System.Text; -using System.Threading.Tasks; -namespace Markdig.Tests +using Markdig.Helpers; + +namespace Markdig.Tests; + +[TestFixture] +public class TestFastStringWriter { - [TestFixture] - public class TestFastStringWriter - { - private const string NewLineReplacement = "~~NEW_LINE~~"; + private const string NewLineReplacement = "~~NEW_LINE~~"; - private FastStringWriter _writer = new(); + private FastStringWriter _writer = new(); - [SetUp] - public void Setup() + [SetUp] + public void Setup() + { + _writer = new FastStringWriter { - _writer = new FastStringWriter - { - NewLine = NewLineReplacement - }; - } + NewLine = NewLineReplacement + }; + } - public void AssertToString(string value) - { - value = value.Replace("\n", NewLineReplacement); - Assert.AreEqual(value, _writer.ToString()); - Assert.AreEqual(value, _writer.ToString()); - } + public void AssertToString(string value) + { + value = value.Replace("\n", NewLineReplacement); + Assert.AreEqual(value, _writer.ToString()); + Assert.AreEqual(value, _writer.ToString()); + } - [Test] - public async Task NewLine() - { - Assert.AreEqual("\n", new FastStringWriter().NewLine); + [Test] + public async Task NewLine() + { + Assert.AreEqual("\n", new FastStringWriter().NewLine); - _writer.NewLine = "\r"; - Assert.AreEqual("\r", _writer.NewLine); + _writer.NewLine = "\r"; + Assert.AreEqual("\r", _writer.NewLine); - _writer.NewLine = "foo"; - Assert.AreEqual("foo", _writer.NewLine); + _writer.NewLine = "foo"; + Assert.AreEqual("foo", _writer.NewLine); - _writer.WriteLine(); - await _writer.WriteLineAsync(); - _writer.WriteLine("bar"); - Assert.AreEqual("foofoobarfoo", _writer.ToString()); - } + _writer.WriteLine(); + await _writer.WriteLineAsync(); + _writer.WriteLine("bar"); + Assert.AreEqual("foofoobarfoo", _writer.ToString()); + } - [Test] - public async Task FlushCloseDispose() - { - _writer.Write('a'); + [Test] + public async Task FlushCloseDispose() + { + _writer.Write('a'); - // Nops - _writer.Close(); - _writer.Dispose(); - await _writer.DisposeAsync(); - _writer.Flush(); - await _writer.FlushAsync(); + // Nops + _writer.Close(); + _writer.Dispose(); + await _writer.DisposeAsync(); + _writer.Flush(); + await _writer.FlushAsync(); - _writer.Write('b'); - AssertToString("ab"); - } + _writer.Write('b'); + AssertToString("ab"); + } - [Test] - public async Task Write_Char() - { - _writer.Write('a'); - AssertToString("a"); + [Test] + public async Task Write_Char() + { + _writer.Write('a'); + AssertToString("a"); - _writer.Write('b'); - AssertToString("ab"); + _writer.Write('b'); + AssertToString("ab"); - _writer.Write('\0'); - _writer.Write('\r'); - _writer.Write('\u1234'); - AssertToString("ab\0\r\u1234"); + _writer.Write('\0'); + _writer.Write('\r'); + _writer.Write('\u1234'); + AssertToString("ab\0\r\u1234"); - _writer.Reset(); - AssertToString(""); + _writer.Reset(); + AssertToString(""); + _writer.Write('a'); + _writer.WriteLine('b'); + _writer.Write('c'); + _writer.Write('d'); + _writer.WriteLine('e'); + AssertToString("ab\ncde\n"); + + await _writer.WriteAsync('f'); + await _writer.WriteLineAsync('g'); + AssertToString("ab\ncde\nfg\n"); + + _writer.Reset(); + + for (int i = 0; i < 2050; i++) + { _writer.Write('a'); - _writer.WriteLine('b'); - _writer.Write('c'); - _writer.Write('d'); - _writer.WriteLine('e'); - AssertToString("ab\ncde\n"); - - await _writer.WriteAsync('f'); - await _writer.WriteLineAsync('g'); - AssertToString("ab\ncde\nfg\n"); - - _writer.Reset(); - - for (int i = 0; i < 2050; i++) - { - _writer.Write('a'); - AssertToString(new string('a', i + 1)); - } + AssertToString(new string('a', i + 1)); } + } - [Test] - public async Task Write_String() - { - _writer.Write("foo"); - AssertToString("foo"); + [Test] + public async Task Write_String() + { + _writer.Write("foo"); + AssertToString("foo"); - _writer.WriteLine("bar"); - AssertToString("foobar\n"); + _writer.WriteLine("bar"); + AssertToString("foobar\n"); - await _writer.WriteAsync("baz"); - await _writer.WriteLineAsync("foo"); - AssertToString("foobar\nbazfoo\n"); + await _writer.WriteAsync("baz"); + await _writer.WriteLineAsync("foo"); + AssertToString("foobar\nbazfoo\n"); - _writer.Write(new string('a', 1050)); - AssertToString("foobar\nbazfoo\n" + new string('a', 1050)); - } + _writer.Write(new string('a', 1050)); + AssertToString("foobar\nbazfoo\n" + new string('a', 1050)); + } - [Test] - public async Task Write_Span() - { - _writer.Write("foo".AsSpan()); - AssertToString("foo"); + [Test] + public async Task Write_Span() + { + _writer.Write("foo".AsSpan()); + AssertToString("foo"); - _writer.WriteLine("bar".AsSpan()); - AssertToString("foobar\n"); + _writer.WriteLine("bar".AsSpan()); + AssertToString("foobar\n"); - await _writer.WriteAsync("baz".AsMemory()); - await _writer.WriteLineAsync("foo".AsMemory()); - AssertToString("foobar\nbazfoo\n"); + await _writer.WriteAsync("baz".AsMemory()); + await _writer.WriteLineAsync("foo".AsMemory()); + AssertToString("foobar\nbazfoo\n"); - _writer.Write(new string('a', 1050).AsSpan()); - AssertToString("foobar\nbazfoo\n" + new string('a', 1050)); - } + _writer.Write(new string('a', 1050).AsSpan()); + AssertToString("foobar\nbazfoo\n" + new string('a', 1050)); + } - [Test] - public async Task Write_CharArray() - { - _writer.Write("foo".ToCharArray()); - AssertToString("foo"); + [Test] + public async Task Write_CharArray() + { + _writer.Write("foo".ToCharArray()); + AssertToString("foo"); - _writer.WriteLine("bar".ToCharArray()); - AssertToString("foobar\n"); + _writer.WriteLine("bar".ToCharArray()); + AssertToString("foobar\n"); - await _writer.WriteAsync("baz".ToCharArray()); - await _writer.WriteLineAsync("foo".ToCharArray()); - AssertToString("foobar\nbazfoo\n"); + await _writer.WriteAsync("baz".ToCharArray()); + await _writer.WriteLineAsync("foo".ToCharArray()); + AssertToString("foobar\nbazfoo\n"); - _writer.Write(new string('a', 1050).ToCharArray()); - AssertToString("foobar\nbazfoo\n" + new string('a', 1050)); - } + _writer.Write(new string('a', 1050).ToCharArray()); + AssertToString("foobar\nbazfoo\n" + new string('a', 1050)); + } - [Test] - public async Task Write_CharArrayWithIndexes() - { - _writer.Write("foo".ToCharArray(), 1, 1); - AssertToString("o"); + [Test] + public async Task Write_CharArrayWithIndexes() + { + _writer.Write("foo".ToCharArray(), 1, 1); + AssertToString("o"); - _writer.WriteLine("bar".ToCharArray(), 0, 2); - AssertToString("oba\n"); + _writer.WriteLine("bar".ToCharArray(), 0, 2); + AssertToString("oba\n"); - await _writer.WriteAsync("baz".ToCharArray(), 0, 1); - await _writer.WriteLineAsync("foo".ToCharArray(), 0, 3); - AssertToString("oba\nbfoo\n"); + await _writer.WriteAsync("baz".ToCharArray(), 0, 1); + await _writer.WriteLineAsync("foo".ToCharArray(), 0, 3); + AssertToString("oba\nbfoo\n"); - _writer.Write(new string('a', 1050).ToCharArray(), 10, 1035); - AssertToString("oba\nbfoo\n" + new string('a', 1035)); - } + _writer.Write(new string('a', 1050).ToCharArray(), 10, 1035); + AssertToString("oba\nbfoo\n" + new string('a', 1035)); + } - [Test] - public async Task Write_StringBuilder() - { - _writer.Write(new StringBuilder("foo")); - AssertToString("foo"); + [Test] + public async Task Write_StringBuilder() + { + _writer.Write(new StringBuilder("foo")); + AssertToString("foo"); - _writer.WriteLine(new StringBuilder("bar")); - AssertToString("foobar\n"); + _writer.WriteLine(new StringBuilder("bar")); + AssertToString("foobar\n"); - await _writer.WriteAsync(new StringBuilder("baz")); - await _writer.WriteLineAsync(new StringBuilder("foo")); - AssertToString("foobar\nbazfoo\n"); + await _writer.WriteAsync(new StringBuilder("baz")); + await _writer.WriteLineAsync(new StringBuilder("foo")); + AssertToString("foobar\nbazfoo\n"); - var sb = new StringBuilder("foo"); - sb.Append('a', 1050); - _writer.Write(sb); - AssertToString("foobar\nbazfoo\nfoo" + new string('a', 1050)); - } + var sb = new StringBuilder("foo"); + sb.Append('a', 1050); + _writer.Write(sb); + AssertToString("foobar\nbazfoo\nfoo" + new string('a', 1050)); } } diff --git a/src/Markdig.Tests/TestFencedCodeBlocks.cs b/src/Markdig.Tests/TestFencedCodeBlocks.cs index 0f7b81661..d6bd7236c 100644 --- a/src/Markdig.Tests/TestFencedCodeBlocks.cs +++ b/src/Markdig.Tests/TestFencedCodeBlocks.cs @@ -1,46 +1,43 @@ -using System.Linq; using Markdig.Syntax; -using NUnit.Framework; -namespace Markdig.Tests +namespace Markdig.Tests; + +public class TestFencedCodeBlocks { - public class TestFencedCodeBlocks + [Test] + [TestCase("c#", "c#", "")] + [TestCase("C#", "C#", "")] + [TestCase(" c#", "c#", "")] + [TestCase(" c# ", "c#", "")] + [TestCase(" \tc# ", "c#", "")] + [TestCase("\t c# \t", "c#", "")] + [TestCase(" c# ", "c#", "")] + [TestCase(" c# foo", "c#", "foo")] + [TestCase(" c# \t fOo \t", "c#", "fOo")] + [TestCase("in\\%fo arg\\%ument", "in%fo", "arg%ument")] + [TestCase("info arg´ument", "info\t", "arg\u00B4ument")] + public void TestInfoAndArguments(string infoString, string expectedInfo, string expectedArguments) { - [Test] - [TestCase("c#", "c#", "")] - [TestCase("C#", "C#", "")] - [TestCase(" c#", "c#", "")] - [TestCase(" c# ", "c#", "")] - [TestCase(" \tc# ", "c#", "")] - [TestCase("\t c# \t", "c#", "")] - [TestCase(" c# ", "c#", "")] - [TestCase(" c# foo", "c#", "foo")] - [TestCase(" c# \t fOo \t", "c#", "fOo")] - [TestCase("in\\%fo arg\\%ument", "in%fo", "arg%ument")] - [TestCase("info arg´ument", "info\t", "arg\u00B4ument")] - public void TestInfoAndArguments(string infoString, string expectedInfo, string expectedArguments) - { - Test('`'); - Test('~'); + Test('`'); + Test('~'); - void Test(char fencedChar) - { - const string Contents = "Foo\nBar\n"; + void Test(char fencedChar) + { + const string Contents = "Foo\nBar\n"; - string fence = new string(fencedChar, 3); - string markdownText = $"{fence}{infoString}\n{Contents}\n{fence}\n"; + var fence = new string(fencedChar, 3); + string markdownText = $"{fence}{infoString}\n{Contents}\n{fence}\n"; - MarkdownDocument document = Markdown.Parse(markdownText); + MarkdownDocument document = Markdown.Parse(markdownText); - FencedCodeBlock codeBlock = document.DescendantsThis is a text with some emphasis
\n", html); - - html = Markdown.ToHtml("This is a text with a https://link.tld/"); - Assert.AreNotEqual("This is a text with a https://link.tld/
\n", html); - } + string html = Markdown.ToHtml("This is a text with some *emphasis*"); + Assert.AreEqual("This is a text with some emphasis
\n", html); + + html = Markdown.ToHtml("This is a text with a https://link.tld/"); + Assert.AreNotEqual("This is a text with a https://link.tld/
\n", html); } + } - [Test] - public void TestToHtmlWithPipeline() + [Test] + public void TestToHtmlWithPipeline() + { + var pipeline = new MarkdownPipelineBuilder() + .Build(); + + for (int i = 0; i < 5; i++) { - var pipeline = new MarkdownPipelineBuilder() - .Build(); - - for (int i = 0; i < 5; i++) - { - string html = Markdown.ToHtml("This is a text with some *emphasis*", pipeline); - Assert.AreEqual("This is a text with some emphasis
\n", html); - - html = Markdown.ToHtml("This is a text with a https://link.tld/", pipeline); - Assert.AreNotEqual("This is a text with a https://link.tld/
\n", html); - } - - pipeline = new MarkdownPipelineBuilder() - .UseAdvancedExtensions() - .Build(); - - for (int i = 0; i < 5; i++) - { - string html = Markdown.ToHtml("This is a text with a https://link.tld/", pipeline); - Assert.AreEqual("This is a text with a https://link.tld/
\n", html); - } + string html = Markdown.ToHtml("This is a text with some *emphasis*", pipeline); + Assert.AreEqual("This is a text with some emphasis
\n", html); + + html = Markdown.ToHtml("This is a text with a https://link.tld/", pipeline); + Assert.AreNotEqual("This is a text with a https://link.tld/
\n", html); } - [Test] - public void TestToHtmlWithWriter() + pipeline = new MarkdownPipelineBuilder() + .UseAdvancedExtensions() + .Build(); + + for (int i = 0; i < 5; i++) { - var writer = new StringWriter(); + string html = Markdown.ToHtml("This is a text with a https://link.tld/", pipeline); + Assert.AreEqual("This is a text with a https://link.tld/
\n", html); + } + } + + [Test] + public void TestToHtmlWithWriter() + { + var writer = new StringWriter(); - for (int i = 0; i < 5; i++) - { - _ = Markdown.ToHtml("This is a text with some *emphasis*", writer); - string html = writer.ToString(); - Assert.AreEqual("This is a text with some emphasis
\n", html); - writer.GetStringBuilder().Length = 0; - } - - writer = new StringWriter(); - var pipeline = new MarkdownPipelineBuilder() - .UseAdvancedExtensions() - .Build(); - - for (int i = 0; i < 5; i++) - { - _ = Markdown.ToHtml("This is a text with a https://link.tld/", writer, pipeline); - string html = writer.ToString(); - Assert.AreEqual("This is a text with a https://link.tld/
\n", html); - writer.GetStringBuilder().Length = 0; - } + for (int i = 0; i < 5; i++) + { + _ = Markdown.ToHtml("This is a text with some *emphasis*", writer); + string html = writer.ToString(); + Assert.AreEqual("This is a text with some emphasis
\n", html); + writer.GetStringBuilder().Length = 0; } - [Test] - public void TestDocumentToHtmlWithWriter() + writer = new StringWriter(); + var pipeline = new MarkdownPipelineBuilder() + .UseAdvancedExtensions() + .Build(); + + for (int i = 0; i < 5; i++) { - var writer = new StringWriter(); + _ = Markdown.ToHtml("This is a text with a https://link.tld/", writer, pipeline); + string html = writer.ToString(); + Assert.AreEqual("This is a text with a https://link.tld/
\n", html); + writer.GetStringBuilder().Length = 0; + } + } + + [Test] + public void TestDocumentToHtmlWithWriter() + { + var writer = new StringWriter(); - for (int i = 0; i < 5; i++) - { - MarkdownDocument document = Markdown.Parse("This is a text with some *emphasis*"); - document.ToHtml(writer); - string html = writer.ToString(); - Assert.AreEqual("This is a text with some emphasis
\n", html); - writer.GetStringBuilder().Length = 0; - } - - writer = new StringWriter(); - var pipeline = new MarkdownPipelineBuilder() - .UseAdvancedExtensions() - .Build(); - - for (int i = 0; i < 5; i++) - { - MarkdownDocument document = Markdown.Parse("This is a text with a https://link.tld/", pipeline); - document.ToHtml(writer, pipeline); - string html = writer.ToString(); - Assert.AreEqual("This is a text with a https://link.tld/
\n", html); - writer.GetStringBuilder().Length = 0; - } + for (int i = 0; i < 5; i++) + { + MarkdownDocument document = Markdown.Parse("This is a text with some *emphasis*"); + document.ToHtml(writer); + string html = writer.ToString(); + Assert.AreEqual("This is a text with some emphasis
\n", html); + writer.GetStringBuilder().Length = 0; } - [Test] - public void TestConvert() + writer = new StringWriter(); + var pipeline = new MarkdownPipelineBuilder() + .UseAdvancedExtensions() + .Build(); + + for (int i = 0; i < 5; i++) { - var writer = new StringWriter(); - var renderer = new HtmlRenderer(writer); - - for (int i = 0; i < 5; i++) - { - _ = Markdown.Convert("This is a text with some *emphasis*", renderer); - string html = writer.ToString(); - Assert.AreEqual("This is a text with some emphasis
\n", html); - writer.GetStringBuilder().Length = 0; - } - - writer = new StringWriter(); - renderer = new HtmlRenderer(writer); - var pipeline = new MarkdownPipelineBuilder() - .UseAdvancedExtensions() - .Build(); - - for (int i = 0; i < 5; i++) - { - _ = Markdown.Convert("This is a text with a https://link.tld/", renderer, pipeline); - string html = writer.ToString(); - Assert.AreEqual("This is a text with a https://link.tld/
\n", html); - writer.GetStringBuilder().Length = 0; - } + MarkdownDocument document = Markdown.Parse("This is a text with a https://link.tld/", pipeline); + document.ToHtml(writer, pipeline); + string html = writer.ToString(); + Assert.AreEqual("This is a text with a https://link.tld/
\n", html); + writer.GetStringBuilder().Length = 0; + } + } + + [Test] + public void TestConvert() + { + var writer = new StringWriter(); + var renderer = new HtmlRenderer(writer); + + for (int i = 0; i < 5; i++) + { + _ = Markdown.Convert("This is a text with some *emphasis*", renderer); + string html = writer.ToString(); + Assert.AreEqual("This is a text with some emphasis
\n", html); + writer.GetStringBuilder().Length = 0; + } + + writer = new StringWriter(); + renderer = new HtmlRenderer(writer); + var pipeline = new MarkdownPipelineBuilder() + .UseAdvancedExtensions() + .Build(); + + for (int i = 0; i < 5; i++) + { + _ = Markdown.Convert("This is a text with a https://link.tld/", renderer, pipeline); + string html = writer.ToString(); + Assert.AreEqual("This is a text with a https://link.tld/
\n", html); + writer.GetStringBuilder().Length = 0; } + } + + [Test] + public void TestParse() + { + const string markdown = "This is a text with some *emphasis*"; + + var pipeline = new MarkdownPipelineBuilder() + .UsePreciseSourceLocation() + .Build(); - [Test] - public void TestParse() + for (int i = 0; i < 5; i++) { - const string markdown = "This is a text with some *emphasis*"; - - var pipeline = new MarkdownPipelineBuilder() - .UsePreciseSourceLocation() - .Build(); - - for (int i = 0; i < 5; i++) - { - MarkdownDocument document = Markdown.Parse(markdown, pipeline); - - Assert.AreEqual(1, document.LineCount); - Assert.AreEqual(markdown.Length, document.Span.Length); - Assert.AreEqual(1, document.LineStartIndexes.Count); - Assert.AreEqual(0, document.LineStartIndexes[0]); - - Assert.AreEqual(1, document.Count); - ParagraphBlock paragraph = document[0] as ParagraphBlock; - Assert.NotNull(paragraph); - Assert.AreEqual(markdown.Length, paragraph.Span.Length); - LiteralInline literal = paragraph.Inline.FirstChild as LiteralInline; - Assert.NotNull(literal); - Assert.AreEqual("This is a text with some ", literal.ToString()); - EmphasisInline emphasis = literal.NextSibling as EmphasisInline; - Assert.NotNull(emphasis); - Assert.AreEqual("*emphasis*".Length, emphasis.Span.Length); - LiteralInline emphasisLiteral = emphasis.FirstChild as LiteralInline; - Assert.NotNull(emphasisLiteral); - Assert.AreEqual("emphasis", emphasisLiteral.ToString()); - Assert.Null(emphasisLiteral.NextSibling); - Assert.Null(emphasis.NextSibling); - } + MarkdownDocument document = Markdown.Parse(markdown, pipeline); + + Assert.AreEqual(1, document.LineCount); + Assert.AreEqual(markdown.Length, document.Span.Length); + Assert.AreEqual(1, document.LineStartIndexes.Count); + Assert.AreEqual(0, document.LineStartIndexes[0]); + + Assert.AreEqual(1, document.Count); + ParagraphBlock paragraph = document[0] as ParagraphBlock; + Assert.NotNull(paragraph); + Assert.AreEqual(markdown.Length, paragraph.Span.Length); + LiteralInline literal = paragraph.Inline.FirstChild as LiteralInline; + Assert.NotNull(literal); + Assert.AreEqual("This is a text with some ", literal.ToString()); + EmphasisInline emphasis = literal.NextSibling as EmphasisInline; + Assert.NotNull(emphasis); + Assert.AreEqual("*emphasis*".Length, emphasis.Span.Length); + LiteralInline emphasisLiteral = emphasis.FirstChild as LiteralInline; + Assert.NotNull(emphasisLiteral); + Assert.AreEqual("emphasis", emphasisLiteral.ToString()); + Assert.Null(emphasisLiteral.NextSibling); + Assert.Null(emphasis.NextSibling); } + } - [Test] - public void TestNormalize() + [Test] + public void TestNormalize() + { + for (int i = 0; i < 5; i++) { - for (int i = 0; i < 5; i++) - { - string normalized = Markdown.Normalize("Heading\n======="); - Assert.AreEqual("# Heading", normalized); - } + string normalized = Markdown.Normalize("Heading\n======="); + Assert.AreEqual("# Heading", normalized); } + } - [Test] - public void TestNormalizeWithWriter() + [Test] + public void TestNormalizeWithWriter() + { + for (int i = 0; i < 5; i++) { - for (int i = 0; i < 5; i++) - { - var writer = new StringWriter(); - - _ = Markdown.Normalize("Heading\n=======", writer); - string normalized = writer.ToString(); - Assert.AreEqual("# Heading", normalized); - } + var writer = new StringWriter(); + + _ = Markdown.Normalize("Heading\n=======", writer); + string normalized = writer.ToString(); + Assert.AreEqual("# Heading", normalized); } + } - [Test] - public void TestToPlainText() + [Test] + public void TestToPlainText() + { + for (int i = 0; i < 5; i++) { - for (int i = 0; i < 5; i++) - { - string plainText = Markdown.ToPlainText("*Hello*, [world](http://example.com)!"); - Assert.AreEqual("Hello, world!\n", plainText); - } + string plainText = Markdown.ToPlainText("*Hello*, [world](http://example.com)!"); + Assert.AreEqual("Hello, world!\n", plainText); } + } - [Test] - public void TestToPlainTextWithWriter() + [Test] + public void TestToPlainTextWithWriter() + { + for (int i = 0; i < 5; i++) { - for (int i = 0; i < 5; i++) - { - var writer = new StringWriter(); - - _ = Markdown.ToPlainText("*Hello*, [world](http://example.com)!", writer); - string plainText = writer.ToString(); - Assert.AreEqual("Hello, world!\n", plainText); - } + var writer = new StringWriter(); + + _ = Markdown.ToPlainText("*Hello*, [world](http://example.com)!", writer); + string plainText = writer.ToString(); + Assert.AreEqual("Hello, world!\n", plainText); } } } diff --git a/src/Markdig.Tests/TestMediaLinks.cs b/src/Markdig.Tests/TestMediaLinks.cs index 27ba34262..64c5d3e98 100644 --- a/src/Markdig.Tests/TestMediaLinks.cs +++ b/src/Markdig.Tests/TestMediaLinks.cs @@ -1,104 +1,102 @@ -using Markdig.Extensions.MediaLinks; -using NUnit.Framework; -using System; using System.Text.RegularExpressions; -namespace Markdig.Tests -{ - [TestFixture] - public class TestMediaLinks - { - private MarkdownPipeline GetPipeline(MediaOptions options = null) - { - return new MarkdownPipelineBuilder() - .UseMediaLinks(options) - .Build(); - } +using Markdig.Extensions.MediaLinks; - private MarkdownPipeline GetPipelineWithBootstrap(MediaOptions options = null) - { - return new MarkdownPipelineBuilder() - .UseBootstrap() - .UseMediaLinks(options) - .Build(); - } +namespace Markdig.Tests; - [Test] - [TestCase("![static mp4](https://sample.com/video.mp4)", "\n")] - [TestCase("![static mp4](//sample.com/video.mp4)", "\n")] - [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n")] - [TestCase("![yandex.ru](https://music.yandex.ru/album/411845/track/4402274)", "\n")] - [TestCase("![vimeo](https://vimeo.com/8607834)", "\n")] - [TestCase("![ok.ru](https://ok.ru/video/26870090463)", "\n")] - [TestCase("![ok.ru](//ok.ru/video/26870090463)", "\n")] - public void TestBuiltInHosts(string markdown, string expected) - { - string html = Markdown.ToHtml(markdown, GetPipeline()); - Assert.AreEqual(html, expected); - } +[TestFixture] +public class TestMediaLinks +{ + private MarkdownPipeline GetPipeline(MediaOptions options = null) + { + return new MarkdownPipelineBuilder() + .UseMediaLinks(options) + .Build(); + } - private class TestHostProvider : IHostProvider - { - public string Class { get; } = "regex"; - public bool AllowFullScreen { get; } + private MarkdownPipeline GetPipelineWithBootstrap(MediaOptions options = null) + { + return new MarkdownPipelineBuilder() + .UseBootstrap() + .UseMediaLinks(options) + .Build(); + } - public bool TryHandle(Uri mediaUri, bool isSchemaRelative, out string iframeUrl) - { - iframeUrl = null; - var uri = isSchemaRelative ? "//" + mediaUri.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Scheme, UriFormat.UriEscaped) : mediaUri.ToString(); - if (!matcher.IsMatch(uri)) - return false; - iframeUrl = matcher.Replace(uri, replacement); - return true; - } + [Test] + [TestCase("![static mp4](https://sample.com/video.mp4)", "\n")] + [TestCase("![static mp4](//sample.com/video.mp4)", "\n")] + [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n")] + [TestCase("![yandex.ru](https://music.yandex.ru/album/411845/track/4402274)", "\n")] + [TestCase("![vimeo](https://vimeo.com/8607834)", "\n")] + [TestCase("![ok.ru](https://ok.ru/video/26870090463)", "\n")] + [TestCase("![ok.ru](//ok.ru/video/26870090463)", "\n")] + public void TestBuiltInHosts(string markdown, string expected) + { + string html = Markdown.ToHtml(markdown, GetPipeline()); + Assert.AreEqual(html, expected); + } - private Regex matcher; - private string replacement; + private class TestHostProvider : IHostProvider + { + public string Class { get; } = "regex"; + public bool AllowFullScreen { get; } - public TestHostProvider(string provider, string replace) - { - matcher = new Regex(provider); - replacement = replace; - } + public bool TryHandle(Uri mediaUri, bool isSchemaRelative, out string iframeUrl) + { + iframeUrl = null; + var uri = isSchemaRelative ? "//" + mediaUri.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Scheme, UriFormat.UriEscaped) : mediaUri.ToString(); + if (!matcher.IsMatch(uri)) + return false; + iframeUrl = matcher.Replace(uri, replacement); + return true; } - [Test] - [TestCase("![p1](https://sample.com/video.mp4)", "\n", @"^https?://sample.com/(.+)$", @"https://example.com/$1")] - [TestCase("![p1](//sample.com/video.mp4)", "\n", @"^//sample.com/(.+)$", @"https://example.com/$1")] - [TestCase("![p1](https://sample.com/video.mp4)", "\n", @"^https?://sample.com/(.+)$", @"https://example.com/$1?token=aaabbb")] - public void TestCustomHostProvider(string markdown, string expected, string provider, string replace) + private Regex matcher; + private string replacement; + + public TestHostProvider(string provider, string replace) { - string html = Markdown.ToHtml(markdown, GetPipeline(new MediaOptions - { - Hosts = - { - new TestHostProvider(provider, replace), - } - })); - Assert.AreEqual(html, expected); + matcher = new Regex(provider); + replacement = replace; } + } - [Test] - [TestCase("![static mp4](//sample.com/video.mp4)", "\n", "")] - [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n", "")] - [TestCase("![static mp4](//sample.com/video.mp4)", "\n", "k")] - [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n", "k")] - public void TestCustomClass(string markdown, string expected, string klass) + [Test] + [TestCase("![p1](https://sample.com/video.mp4)", "\n", @"^https?://sample.com/(.+)$", @"https://example.com/$1")] + [TestCase("![p1](//sample.com/video.mp4)", "\n", @"^//sample.com/(.+)$", @"https://example.com/$1")] + [TestCase("![p1](https://sample.com/video.mp4)", "\n", @"^https?://sample.com/(.+)$", @"https://example.com/$1?token=aaabbb")] + public void TestCustomHostProvider(string markdown, string expected, string provider, string replace) + { + string html = Markdown.ToHtml(markdown, GetPipeline(new MediaOptions { - string html = Markdown.ToHtml(markdown, GetPipeline(new MediaOptions + Hosts = { - Class = klass, - })); - Assert.AreEqual(html, expected); - } + new TestHostProvider(provider, replace), + } + })); + Assert.AreEqual(html, expected); + } - [Test] - [TestCase("![static mp4](//sample.com/video.mp4)", "\n")] - [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n")] - public void TestWithBootstrap(string markdown, string expected) + [Test] + [TestCase("![static mp4](//sample.com/video.mp4)", "\n", "")] + [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n", "")] + [TestCase("![static mp4](//sample.com/video.mp4)", "\n", "k")] + [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n", "k")] + public void TestCustomClass(string markdown, string expected, string klass) + { + string html = Markdown.ToHtml(markdown, GetPipeline(new MediaOptions { - string html = Markdown.ToHtml(markdown, GetPipelineWithBootstrap()); - Assert.AreEqual(html, expected); - } + Class = klass, + })); + Assert.AreEqual(html, expected); + } + + [Test] + [TestCase("![static mp4](//sample.com/video.mp4)", "\n")] + [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "\n")] + public void TestWithBootstrap(string markdown, string expected) + { + string html = Markdown.ToHtml(markdown, GetPipelineWithBootstrap()); + Assert.AreEqual(html, expected); } } diff --git a/src/Markdig.Tests/TestNewLine.cs b/src/Markdig.Tests/TestNewLine.cs index d30d29a99..49bb335cf 100644 --- a/src/Markdig.Tests/TestNewLine.cs +++ b/src/Markdig.Tests/TestNewLine.cs @@ -1,31 +1,28 @@ -using System.Linq; using Markdig.Syntax; using Markdig.Syntax.Inlines; -using NUnit.Framework; -namespace Markdig.Tests +namespace Markdig.Tests; + +[TestFixture] +public class TestNewLine { - [TestFixture] - public class TestNewLine + [TestCase("a \nb", "a
\nb
a
\nb
a b c
Text B
\na
\nb
a
\nb
a b c
Text B
\n