Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new render pipeline #80

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ModShark.Tests/Reports/Document/DocumentBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void ToString_ShouldRenderMarkdownDocument()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("# Title1\n# Title2\n\nSection1\n\n## Header1\n## Header2\n**Bold1** **Bold2** *Italics1* *Italics2* `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n* Item1\n* Item2\n * Item3\n||Spoiler1||||Spoiler2||\n");
document.Should().Be("# Title1\n# Title2\n\nSection1\n\n## Header1\n## Header2\n**Bold1** **Bold2** *Italics1* *Italics2* `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n* Item1\n* Item2\n * Item3\n||Spoiler1||||Spoiler2||new line\n");
}

[Test]
Expand All @@ -24,7 +24,7 @@ public void ToString_ShouldRenderMFMDocument()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("<b>Title1</b>\n<b>Title2</b>\n\nSection1\n\n<b>Header1</b>\n<b>Header2</b>\n<b>Bold1</b> <b>Bold2</b> <i>Italics1</i> <i>Italics2</i> `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n- Item1\n- Item2\n - Item3\n$[blur Spoiler1]$[blur Spoiler2]\n");
document.Should().Be("<b>Title1</b>\n<b>Title2</b>\n\nSection1\n\n<b>Header1</b>\n<b>Header2</b>\n<b>Bold1</b> <b>Bold2</b> <i>Italics1</i> <i>Italics2</i> `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n- Item1\n- Item2\n - Item3\n$[blur Spoiler1]$[blur Spoiler2]new line\n");
}

[Test]
Expand All @@ -35,7 +35,7 @@ public void ToString_ShouldRenderHTMLDocument()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("<h1>Title1</h1><h1>Title2</h1><div>Section1</div><div><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul>Spoiler1Spoiler2</div>");
document.Should().Be("<h1>Title1</h1><h1>Title2</h1><div>Section1</div><div><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul>Spoiler1Spoiler2new\nline</div>");
}

[Test]
Expand All @@ -46,7 +46,7 @@ public void ToString_ShouldRenderHTML5Document()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("<h1>Title1</h1><h1>Title2</h1><section>Section1</section><section><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul><details style=\"display: inline\"><summary>spoiler</summary>Spoiler1</details><details style=\"display: inline\"><summary>hidden</summary>Spoiler2</details></section>");
document.Should().Be("<h1>Title1</h1><h1>Title2</h1><section>Section1</section><section><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul><details style=\"display: inline\"><summary>spoiler</summary>Spoiler1</details><details style=\"display: inline\"><summary>hidden</summary>Spoiler2</details>new\nline</section>");
}

[TestCase(0)]
Expand Down Expand Up @@ -140,6 +140,7 @@ private void RenderDocument(DocumentBuilder builder)
.BeginSpoiler("hidden")
.AppendText("Spoiler2")
.End()
.AppendInline("new\nline")
.End();
}
}
18 changes: 18 additions & 0 deletions ModShark.Tests/Reports/Document/Format/HTMLFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ public void Text_ShouldEscapeHTML(string input, string expected)

actual.Should().Be(expected);
}

[TestCase("", "")]
[TestCase("hello, world", "hello, world")]
[TestCase("<", "&lt;")]
[TestCase(">", "&gt;")]
[TestCase("&", "&amp;")]
[TestCase("\"", "&quot;")]
[TestCase("'", "&#39;")]
[TestCase("<span class=\"foo\">'&amp;'</span>", "&lt;span class=&quot;foo&quot;&gt;&#39;&amp;amp;&#39;&lt;/span&gt;")]
[TestCase(">", "&gt;")]
public void TextInline_ShouldEscapeHTML(string input, string expected)
{
var format = new HTMLFormat();

var actual = format.TextInline(input);

actual.Should().Be(expected);
}
}
63 changes: 63 additions & 0 deletions ModShark.Tests/Reports/Document/Format/MarkdownFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,67 @@ public void Text_ShouldEscapeMarkdown(string input, string expected)

actual.Should().Be(expected);
}

[TestCase("", "")]
[TestCase("Hello, world", "Hello, world")]
[TestCase("-text", "-text")]
[TestCase("*text", "*text")]
[TestCase(" - text", @" \- text")]
[TestCase(" * text", @" \* text")]
[TestCase("- text", @"\- text")]
[TestCase("* text", @"\* text")]
[TestCase("|", @"\|")]
[TestCase("#", @"\#")]
[TestCase(@"\", @"\\")]
[TestCase(" |", @" \|")]
[TestCase(" #", @" \#")]
[TestCase(@" \", @" \\")]
[TestCase("text |", "text |")]
[TestCase("text #", "text #")]
[TestCase(@"text \", @"text \\")]
[TestCase("<tag>", @"\<tag>")]
[TestCase("<tag/>", @"\<tag/>")]
[TestCase("</tag>", @"\</tag>")]
[TestCase("<tag >", "<tag >")]
[TestCase("< tag>", "< tag>")]
[TestCase("< tag >", "< tag >")]
[TestCase("[not a link](https://example.com)", @"[not a link\](https://example.com)")]
[TestCase("[not a link]", "[not a link]")]
public void TextInline_ShouldEscapeMarkdown(string input, string expected)
{
var format = new MarkdownFormat();

var actual = format.TextInline(input);

actual.Should().Be(expected);
}

[TestCase("", "")]
[TestCase(" ", " ")]
[TestCase("\t", " ")]
[TestCase("\r", " ")]
[TestCase("\n", " ")]
[TestCase("\v", " ")]
public void Text_ShouldEscapeWhitespace(string input, string expected)
{
var format = new MarkdownFormat();

var actual = format.TextInline(input);

actual.Should().Be(expected);
}

[TestCase(" ", " ")]
[TestCase("\t \t", " ")]
[TestCase("\r \r", " ")]
[TestCase("\n \n", " ")]
[TestCase("\v \v", " ")]
public void Text_ShouldEscapeConsecutiveWhitespace(string input, string expected)
{
var format = new MarkdownFormat();

var actual = format.TextInline(input);

actual.Should().Be(expected);
}
}
113 changes: 113 additions & 0 deletions ModShark.Tests/Reports/Render/RenderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using FluentAssertions;
using ModShark.Reports.Document;
using ModShark.Reports.Render;

namespace ModShark.Tests.Reports.Render;

public class RenderExtensionsTests
{
[Test]
public void AppendFullFlaggedText_ShouldRenderMixed()
{
var ranges = new List<Range> { new(0, 5), new(11, 13) };
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendFullFlaggedText("hello, world!", ranges);

var result = builder.ToString();
result.Should().Be("||**`hello`**`, worl`**`d!`**||");
}

[Test]
public void AppendFullFlaggedText_ShouldRenderAllText()
{
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendFullFlaggedText("hello, world!", new List<Range>());

var result = builder.ToString();
result.Should().Be("||`hello, world!`||");
}

[Test]
public void AppendFullFlaggedText_ShouldRenderAllFlagged()
{
var ranges = new List<Range> { Range.All };
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendFullFlaggedText("hello, world!", ranges);

var result = builder.ToString();
result.Should().Be("||**`hello, world!`**||");
}

[Test]
public void AppendFullFlaggedText_ShouldIgnoreOutOfRange()
{
var ranges = new List<Range> { new(20, 25), Range.StartAt(23) };
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendFullFlaggedText("hello, world!", ranges);

var result = builder.ToString();
result.Should().Be("||`hello, world!`||");
}

[Test]
public void AppendFullFlaggedText_ShouldDoNothing_ForEmptyInput()
{
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendFullFlaggedText("", new List<Range>());

var result = builder.ToString();
result.Should().Be("");
}

[Test]
public void AppendMinimalFlaggedText_ShouldIncludeAllSegments()
{
var ranges = new List<Range> { new(0, 5), new(11, 13) };
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendMinimalFlaggedText("hello, world!", ranges);

var result = builder.ToString();
result.Should().Be("||`hello`||, ||`d!`||");
}

[Test]
public void AppendMinimalFlaggedText_ShouldCapBounds()
{
var ranges = new List<Range> { new(0, 5), new(11, 25) };
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendMinimalFlaggedText("hello, world!", ranges);

var result = builder.ToString();
result.Should().Be("||`hello`||, ||`d!`||");
}

[Test]
public void AppendMinimalFlaggedText_ShouldIgnoreOutOfRange()
{
var ranges = new List<Range> { new(0, 5), new(11, 13), new (25, 30) };
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendMinimalFlaggedText("hello, world!", ranges);

var result = builder.ToString();
result.Should().Be("||`hello`||, ||`d!`||");
}

[Test]
public void AppendMinimalFlaggedText_ShouldDoNothing_ForEmptyInput()
{
var builder = new DocumentBuilder(DocumentFormat.Markdown);

builder.AppendMinimalFlaggedText("", new List<Range>());

var result = builder.ToString();
result.Should().Be("");
}
}
85 changes: 73 additions & 12 deletions ModShark.Tests/Reports/Render/RenderServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using ModShark.Reports.Document;
using ModShark.Reports.Render;
using ModShark.Services;
using ModShark.Utils;
using Moq;
using SharkeyDB.Entities;
using Range = System.Range;

namespace ModShark.Tests.Reports.Render;

Expand Down Expand Up @@ -88,8 +90,14 @@ public void Setup()
{
Text =
{
["software", "soapbox 1.2.3"] = true,
["description", "free speech community"] = true
["software"] = new MultiMap<string, Range>
{
{ "soapbox 1.2.3", Range.EndAt(7) }
},
["description"] = new MultiMap<string, Range>
{
{ "free speech community", Range.EndAt(12) }
}
}
}
}
Expand All @@ -104,7 +112,10 @@ public void Setup()
{
Text =
{
["text", "slur"] = true
["text"] = new MultiMap<string, Range>
{
{ "slur", Range.All }
}
}
}
},
Expand All @@ -115,7 +126,10 @@ public void Setup()
{
Text =
{
["bio", "Age: 12"] = true
["bio"] = new MultiMap<string, Range>
{
{ "Age: 12", Range.All }
}
}
}
}
Expand All @@ -131,7 +145,10 @@ public void Setup()
{
Text =
{
["text", "kys"] = true
["text"] = new MultiMap<string, Range>
{
{ "kys", Range.All }
}
}
}
},
Expand All @@ -143,8 +160,14 @@ public void Setup()
{
Text =
{
["text", "https://forbidden-domain.example.com"] = true,
["emoji", "nsfw_emoji"] = true
["text"] = new MultiMap<string, Range>
{
{ "https://forbidden-domain.example.com", Range.StartAt(8) }
},
["emoji"] = new MultiMap<string, Range>
{
{ "nsfw_emoji", Range.All }
}
}
}
}
Expand Down Expand Up @@ -202,18 +225,56 @@ public void RenderReport_ShouldRenderNoteReports()
}

[Test]
public void RenderReport_ShouldIncludeFlaggedText()
public void RenderReport_ShouldNotIncludeAnyContent_WhenFlagInclusionIsNone()
{
var document = ServiceUnderTest
.RenderReport(FakeReport, DocumentFormat.HTML)
.RenderReport(FakeReport, DocumentFormat.HTML, includeFlags: FlagInclusion.None)
.ToString();

document.Should()
.NotContain("soapbox")
.And.NotContain("1.2.3")
.And.NotContain("free speech")
.And.NotContain("community")
.And.NotContain("slur")
.And.NotContain("Age: 12")
.And.NotContain("kys")
.And.NotContain("forbidden-domain.example.com");
}

[Test]
public void RenderReport_ShouldIncludeFlaggedContent_WhenFlagInclusionIsMinimal()
{
var document = ServiceUnderTest
.RenderReport(FakeReport, DocumentFormat.HTML, includeFlags: FlagInclusion.Minimal)
.ToString();

document.Should()
.Contain("soapbox")
.And.NotContain("1.2.3")
.And.Contain("free speech")
.And.NotContain("community")
.And.Contain("slur")
.And.Contain("Age: 12")
.And.Contain("kys")
.And.Contain("forbidden-domain.example.com");
}

[Test]
public void RenderReport_ShouldIncludeAllContent_WhenFlagInclusionIsFull()
{
var document = ServiceUnderTest
.RenderReport(FakeReport, DocumentFormat.HTML, includeFlags: FlagInclusion.Full)
.ToString();

document.Should()
.Contain("soapbox 1.2.3")
.And.Contain("free speech community")
.Contain("soapbox")
.And.Contain("1.2.3")
.And.Contain("free speech")
.And.Contain("community")
.And.Contain("slur")
.And.Contain("Age: 12")
.And.Contain("kys")
.And.Contain("https://forbidden-domain.example.com");
.And.Contain("forbidden-domain.example.com");
}
}
Loading