mfgames-cil/tests/MfGames.Markdown.Tests/RewriteLinkTransformer.cs
D. Moonfire 23a65c8674
All checks were successful
deploy / deploy (push) Successful in 41m4s
feat: added a Markdown transformer to convert links to index paths
2024-03-18 22:27:42 -05:00

80 lines
2.1 KiB
C#

using MfGames.TestSetup;
using Xunit;
using Xunit.Abstractions;
namespace MfGames.Markdown.Tests;
/// <summary>
/// Tests the functionality of RewriteLinkTransformer.
/// </summary>
public class RewriteLinkTransformerTests : TestBase<TestContext>
{
public RewriteLinkTransformerTests(ITestOutputHelper output)
: base(output) { }
[Fact]
public void ComplexExample()
{
string input = string.Join(
"\n",
"# A [Heading](/heading/index.md)",
"",
"That includes many [not-changing](https://example.org) examples",
"and ones that [will change](/gary/index.md).",
"",
"No one knows how this [won't change](/favicon.ico).",
""
);
string expected = string.Join(
"\n",
"# A [Heading](/heading/)",
"",
"That includes many [not-changing](https://example.org) examples",
"and ones that [will change](/gary/).",
"",
"",
"No one knows how this [won't change](/favicon.ico).",
""
);
RewriteLinkTransformer transformer =
new((link) => link.Url = link.Url?.Replace("/index.md", "/"));
string? output = transformer.Transform(input);
Assert.Equal(expected, output);
}
[Fact]
public void HandleLink()
{
const string Input = "[Link Title](/link/index.md)";
RewriteLinkTransformer transformer =
new((link) => link.Url = link.Url?.Replace("/index.md", "/"));
string? output = transformer.Transform(Input);
Assert.Equal("[Link Title](/link/)", output);
}
[Fact]
public void HandleNulls()
{
RewriteLinkTransformer transformer = new();
Assert.Null(transformer.Transform(null));
}
[Fact]
public void HandleText()
{
const string Input = "Content";
RewriteLinkTransformer transformer = new();
string? output = transformer.Transform(Input);
Assert.Equal("Content", output);
}
}