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

Бессараб Дмитрий #220

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions Markdown.Tests/Bold_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Markdown.MDParser;
using NUnit.Framework;

namespace Markdown.Tests
{
[TestFixture]
public class Bold_Should
{
private readonly Bold _bold = new();

[TestCaseSource(nameof(TestCases))]
public void ReturnsRightToken((string text, Token expectedToken) td)
{
var returnedToken = _bold.TryFindToken(td.text, 0);

returnedToken.Should().BeEquivalentTo(td.expectedToken);
}

public static IEnumerable<(string, Token)> TestCases()
{
yield return ("__", new Token("__", TokenProperty.Normal));
yield return ("__a", new Token("__a", TokenProperty.Normal));
yield return ("__aa", new Token("__aa", TokenProperty.Normal));
yield return ("____", new Token("____", TokenProperty.Normal));
yield return (@"__a\__b\__c", new Token("__a__b__c", TokenProperty.Normal));
yield return ("__ a__", new Token("__", TokenProperty.Normal));
yield return ("__a a__", new Token("a a", TokenProperty.Bold));
yield return ("__a __a", new Token("__a __a", TokenProperty.Normal));
yield return (@"__a\__a\__ __", new Token("__a__a__ __", TokenProperty.Normal));
}
}
}
31 changes: 31 additions & 0 deletions Markdown.Tests/Hidden_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using FluentAssertions;
using Markdown.MDParser;

namespace Markdown.Tests
{
[TestFixture]
public class Hidden_Should
{
[TestCaseSource(nameof(TestCases))]
public void ReturnsRightToken((string text, Token expectedToken) td)
{
var returnedToken = new Hidden().TryFindToken(td.text, 0);

returnedToken.Should().BeEquivalentTo(td.expectedToken);
}

public static IEnumerable<(string, Token)> TestCases()
{
yield return (@"\", new Token( @"\", TokenProperty.Normal));
yield return (@"\\", new Token( @"\", TokenProperty.Normal));
yield return (@"\_", new Token( @"_", TokenProperty.Normal));
yield return (@"\abra", new Token("", TokenProperty.Normal));
}
}
}
36 changes: 36 additions & 0 deletions Markdown.Tests/Italic_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Markdown.MDParser;
using NUnit.Framework;

namespace Markdown.Tests
{
[TestFixture]
public class Italic_Should
{
private readonly Italic _italic = new();

[TestCaseSource(nameof(TestCases))]
public void ReturnsRightToken((string text, Token expectedToken) td)
{
var returnedToken = _italic.TryFindToken(td.text, 0);

returnedToken.Should().BeEquivalentTo(td.expectedToken);
}

public static IEnumerable<(string, Token)> TestCases()
{
yield return ("_", new Token("_", TokenProperty.Normal));
yield return ("__a__", new Token("a", TokenProperty.Bold));
yield return ("_a a_", new Token("_a", TokenProperty.Normal));
yield return ("_a1a_", new Token("_a1a", TokenProperty.Normal));
yield return ("_abc", new Token("_abc", TokenProperty.Normal));
yield return ("_a_", new Token("a", TokenProperty.Italic));
yield return ("_a\\_", new Token("_a", TokenProperty.Normal));
}
}
}
37 changes: 37 additions & 0 deletions Markdown.Tests/Link_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Markdown.MDParser;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;

namespace Markdown.Tests
{
[TestFixture]
public class Link_Should
{
private readonly Link _link = new();

[TestCaseSource(nameof(TestCases))]
public void ReturnsRightToken((string text, Token expectedToken) td)
{
var returnedToken = _link.TryFindToken(td.text, 0);

returnedToken.Should().BeEquivalentTo(td.expectedToken);
}

public static IEnumerable<(string, Token)> TestCases()
{
yield return ("[name](address)]", new Token("name"+"+++"+"address", TokenProperty.Link));
yield return (@"[name\](address)]", new Token("[", TokenProperty.Normal));
yield return (@"[name](address\)]", new Token("[", TokenProperty.Normal));
yield return (@"[name", new Token("[", TokenProperty.Normal));
yield return (@"[name]", new Token("[", TokenProperty.Normal));
yield return (@"[name]add", new Token("[", TokenProperty.Normal));
yield return (@"[name](", new Token("[", TokenProperty.Normal));
yield return (@"[name](address", new Token("[", TokenProperty.Normal));
}
}
}
93 changes: 93 additions & 0 deletions Markdown.Tests/MD.Render_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit;
using FluentAssertions;
using Markdown.MDParser;
using NUnit.Framework;
using NUnit.Framework.Internal;
using Markdown.HTMLConverter;

namespace Markdown.Tests
{
[TestFixture]
public class Render_Should
{
private readonly Parser _parser = new();
private readonly Converter _converter = new();

[Test]
public void Render_ShouldReturnItalic()
{
Md.Render(
"_italic_", _parser, _converter)
.Should().Be("<em>italic</em>\n");
}

[Test]
public void Render_ShouldReturnBold()
{
Md.Render(
"__bold__", _parser, _converter)
.Should().Be("<strong>bold</strong>\n");
}

[Test]
public void Render_ShouldSkipEscapedChars()
{
Md.Render(@"\_notItalic\_", _parser, _converter)
.Should().NotBe("<em>notItalic</em>\n");
Md.Render(@"\_notItalic\_", _parser, _converter)
.Should().Be("_notItalic_\n");
}

[Test]
public void Render_ShouldNotSkipSlashWithoutMarker()
{
Md.Render(@"a\a", _parser, _converter)
.Should().Be("a\\a\n");
}

[Test]
public void Render_SlashHaveToBeEscapedWithAnotherSlash()
{
Md.Render(@"\\", _parser, _converter)
.Should().Be("\\\n");
}

[Test]
public void Render_ItalicInBold()
{
Md.Render("Внутри __двойного выделения _одинарное_ тоже__ работает", _parser, _converter)
.Should().Be("Внутри <strong>двойного выделения <em>одинарное</em> тоже</strong> работает\n");
}

[Test]
public void Render_ConvertLink()
{
Md.Render("[name](address)", _parser, _converter)
.Should().Be("<a href=address>name</a>\n");
}

[TestCaseSource(nameof(TestCases))]
public void RenderWorksCorrectly((string text, string expectedText) td)
{
var returnedText = Md.Render(td.text, _parser, _converter);

returnedText.Should().BeEquivalentTo(td.expectedText);
}

public static IEnumerable<(string, string)> TestCases()
{
yield return ("# Заголовок __с _разными_ символами__",
"<h1> Заголовок <strong>с <em>разными</em> символами</strong></h1>\n");
yield return ("____", "____\n");
yield return ("текста c цифрами_12_3", "текста c цифрами_12_3\n");
yield return ("в _нач_але, и в сер_еди_не, и в кон_це._", "в <em>нач</em>але, и в сер<em>еди</em>не, и в кон<em>це.</em>\n");
yield return ("в ра_зных сл_овах", "в ра_зных сл_овах\n");
}

}
}
21 changes: 21 additions & 0 deletions Markdown.Tests/Markdown.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Markdown\Markdown.csproj" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions Markdown.Tests/Normal_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using FluentAssertions;
using Markdown.MDParser;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Markdown.Tests
{
[TestFixture]
public class Normal_Should
{
private static IEnumerable<string> NormalTextStrings()
{
return Markers.Chars.Select(marker => "text" + marker);
}

[TestCaseSource(nameof(TestCases))]
public void ReturnsRightToken((string text, Token expectedToken) td)
{
var returnedToken = new Normal().TryFindToken(td.text, 0);

returnedToken.Should().BeEquivalentTo(td.expectedToken);
}

public static IEnumerable<(string, Token)> TestCases()
{
return NormalTextStrings().Select(str => (str, new Token(str[..^1], TokenProperty.Normal)));
}
}
}
17 changes: 17 additions & 0 deletions Markdown/Complex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Markdown
{
public class Complex
{
public static readonly HashSet<TokenProperty> Properties = [
TokenProperty.Head,
TokenProperty.Paragraph,
TokenProperty.Bold
];
}
}
13 changes: 13 additions & 0 deletions Markdown/DOM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Markdown
{
public class DOM(List<Token> tokens)
{
public readonly List<Token> Tokens = tokens;
}
}
63 changes: 63 additions & 0 deletions Markdown/HTMLConverter/Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Markdown.HTMLConverter
{
public class Converter : IConverter
{
public static void GetContent(Token token, StringBuilder sb)
{
if (token.Children.Count == 0)
{
if (token.Property != TokenProperty.Link)
{
sb.Append(Tags.Open[token.Property]);
sb.Append(token.Value);
sb.Append(Tags.Close[token.Property]);
}
else ConvertLink(token.Value, sb);
}
else
{
sb.Append(Tags.Open[token.Property]);
foreach (var nextToken in token.Children)
GetContent(nextToken, sb);
sb.Append(Tags.Close[token.Property]);
}
return;
}

public static void ConvertLink(string text, StringBuilder sb)
{
var i = 3;
string name;
string address;
while (true)
{
if (text[i] == '+' && text[i - 1] == '+' && text[i - 2] == '+')
{
name = text[..(i - 2)];
address = text[(i + 1)..];
break;
}
i++;
}
sb.Append(Tags.Open[TokenProperty.Link]);
sb.Append(address);
sb.Append('>');
sb.Append(name);
sb.Append(Tags.Close[TokenProperty.Link]);
}

public string Convert(DOM dom)
{
var result = new StringBuilder();
foreach (var token in dom.Tokens)
GetContent(token, result);
return result.ToString();
}
}
}
Loading