-
Notifications
You must be signed in to change notification settings - Fork 300
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
Шевырин Никита #229
Open
alexadralt
wants to merge
27
commits into
kontur-courses:master
Choose a base branch
from
alexadralt:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Шевырин Никита #229
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
883f607
created project Markdown
alexadralt c1f6b4d
initial design
alexadralt 09601ae
initial design
alexadralt f27204e
added ArgumentExceptionHelpers
alexadralt bc62038
implemented abstract syntax tree
alexadralt f0d106e
did some syntax tree testing
alexadralt 106f113
tokenizer WIP
alexadralt 8de5081
addded tests for Md
alexadralt 6e53af2
deleted tree tests
alexadralt b67b916
added usage example
alexadralt ae81615
implementation WIP 1
alexadralt 9005981
renamed IMdTest to IMdTests
alexadralt 1ef1aa0
renamed files for refactoring
alexadralt 887266f
refactored the whole thing
alexadralt 5be7d46
added nesting syntax rule
alexadralt 90e83aa
refactored nesting rule
alexadralt f106f80
added new syntax rules and fixed potential bugs
alexadralt 8b6a4d8
added descriptions to tests
alexadralt 8efaaf3
deleted files with old version of existing code
alexadralt 8549226
removed old files
alexadralt b5df139
merged version 1.0
alexadralt f298b01
deleted old files
alexadralt 9f42573
added factory method for Md
alexadralt 75251ad
refactored MdTokenizer
alexadralt 262d9a2
refactored syntax rules
alexadralt f7be964
moved private fields declarations
alexadralt 96b2796
moved performance tests to a different project and added more test cases
alexadralt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
|
||
<IsPackable>false</IsPackable> | ||
</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Text; | ||
using FluentAssertions; | ||
using Markdown.Parser; | ||
using Markdown.ParseTree; | ||
using Markdown.SyntaxRules; | ||
using Markdown.Token; | ||
using Markdown.Tokenizer; | ||
using NUnit.Framework; | ||
|
||
namespace Markdown.Tests; | ||
|
||
[TestFixture] | ||
[TestOf(typeof(Md))] | ||
public class MdTests | ||
{ | ||
private IRenderer _md; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
var tokenAliases = new Dictionary<string, MdTokenType>(); | ||
tokenAliases.Add("_", MdTokenType.Italic); | ||
tokenAliases.Add("__", MdTokenType.Bold); | ||
tokenAliases.Add("# ", MdTokenType.Heading); | ||
tokenAliases.Add("\n", MdTokenType.Line); | ||
|
||
var tokenTags = new Dictionary<MdTokenType, string>(); | ||
tokenTags.Add(MdTokenType.Italic, "em"); | ||
tokenTags.Add(MdTokenType.Bold, "strong"); | ||
tokenTags.Add(MdTokenType.Heading, "h1"); | ||
|
||
var syntaxRules = new List<ISyntaxRule<MdTokenType>>(); | ||
syntaxRules.Add(new NestingRule()); | ||
syntaxRules.Add(new NumberRule()); | ||
syntaxRules.Add(new TokensInDifferentWordsRule()); | ||
|
||
_md = new Md( | ||
tokenTags, new MdTokenizer(tokenAliases, '\\'), new MdParser(new MdParseTree()), syntaxRules.ToArray()); | ||
} | ||
|
||
[Test] | ||
[Description("Базовые тесты")] | ||
[TestCase("", "")] | ||
[TestCase("Hello world", "Hello world")] | ||
[TestCase("Hello _world_!", "Hello <em>world</em>!")] | ||
[TestCase("# _Hello_ __world__!", "<h1><em>Hello</em> <strong>world</strong>!</h1>")] | ||
public void Render_ReturnsCorrectMarkdown_ForSimpleCases( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты на вложенность двойного и одинарного выделения")] | ||
[TestCase("This __text _contains_ nested__ markdown", "This <strong>text <em>contains</em> nested</strong> markdown")] | ||
[TestCase("This is _an example __of inversed__ nested_ markdown", "This is <em>an example __of inversed__ nested</em> markdown")] | ||
public void Render_ReturnsCorrectMarkdown_ForCasesWithNesting( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты для разметки внутри текста с цифрами")] | ||
[TestCase("Text_12_3", "Text_12_3")] | ||
[TestCase("5__12_3__4", "5__12_3__4")] | ||
[TestCase("Text __that_12_3__ is in bold", "Text <strong>that_12_3</strong> is in bold")] | ||
public void Render_ReturnsCorrectMarkdown_ForTextWithNumbers( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты для разметки внутри слов")] | ||
[TestCase("_begin_ning", "<em>begin</em>ning")] | ||
[TestCase("mi_ddl_e", "mi<em>ddl</em>e")] | ||
[TestCase("end_ing_", "end<em>ing</em>")] | ||
[TestCase("__begin__ning", "<strong>begin</strong>ning")] | ||
[TestCase("mi__ddl__e", "mi<strong>ddl</strong>e")] | ||
[TestCase("end__ing__", "end<strong>ing</strong>")] | ||
public void Render_ReturnsCorrectMarkdown_ForPartsOfWords( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты для подчерков, находящихся внутри разных слов")] | ||
[TestCase("This sh_ould not cha_nge", "This sh_ould not cha_nge")] | ||
[TestCase("As w__ell a__s this", "As w__ell a__s this")] | ||
[TestCase("This sh__o_uld_ wo__rk like this", "This sh__o<em>uld</em> wo__rk like this")] | ||
public void Render_ReturnsCorrectMarkdown_ForMarkdownInDifferentWords( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты для непарных символов разметки")] | ||
[TestCase("__Unpaired_ markdown", "__Unpaired_ markdown")] | ||
[TestCase("Another _unpaired markdown__", "Another _unpaired markdown__")] | ||
public void Render_ReturnsCorrectMarkdown_ForUnpairedMarkdownSymbols( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Проверяем, что подчерки должны следовать за (стоять перед) непробельным символом")] | ||
[TestCase("This_ should not_ change", "This_ should not_ change")] | ||
[TestCase("This _should _be in_ italics", "This <em>should _be in</em> italics")] | ||
public void Render_ReturnsCorrectMarkdown_ForIncorrectlyPlacedUnderscores( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты на пересечение двойных и одинарных подчерков")] | ||
[TestCase("Intersecting _markdown __should_ work__ like this", "Intersecting _markdown __should_ work__ like this")] | ||
[TestCase("Another __example of _intersecting__ markdown_", "Another __example of _intersecting__ markdown_")] | ||
public void Render_ReturnsCorrectMarkdown_ForIntersectingMarkdown( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты на пустую разметку")] | ||
[TestCase("This should ____ remain the same", "This should ____ remain the same")] | ||
[TestCase("This also should __ not change", "This also should __ not change")] | ||
public void Render_ReturnsCorrectMarkdown_ForEmptyMarkdown( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тесты на экранирование")] | ||
[TestCase(@"This should \_not turn\_ into tags", "This should _not turn_ into tags")] | ||
[TestCase(@"This should \remain the\ same", @"This should \remain the\ same")] | ||
public void Render_ReturnsCorrectMarkdown_ForEscapeCharacters( | ||
string input, | ||
string expectedOutput) | ||
{ | ||
_md.Render(input) | ||
.Should() | ||
.Be(expectedOutput); | ||
} | ||
|
||
[Test] | ||
[Description("Тест на производительность")] | ||
public void Render_PerformanceTest() | ||
{ | ||
var fullStr = ArrangePerformanceTest("_Hello_ world_12. Hel_lo world_", 20000); | ||
Console.WriteLine($"Total length: {fullStr.Length}"); | ||
|
||
var totalTime = MeasureTime(fullStr); | ||
Console.WriteLine($"Time elapsed in ms: {totalTime}"); | ||
|
||
totalTime | ||
.Should() | ||
.BeLessThan(1000); | ||
} | ||
|
||
private long MeasureTime(string fullStr) | ||
{ | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
_md.Render(fullStr); | ||
sw.Stop(); | ||
return sw.ElapsedMilliseconds; | ||
} | ||
|
||
private string ArrangePerformanceTest(string input, int copyCount) | ||
{ | ||
var sb = new StringBuilder(); | ||
for (var i = 0; i < copyCount; i++) | ||
sb.Append(input); | ||
return sb.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
| ||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Markdown", "Markdown\Markdown.csproj", "{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Markdown.Tests", "Markdown.Tests\Markdown.Tests.csproj", "{0B1D2315-E457-4F38-92C9-5BC11A8752B6}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{0B1D2315-E457-4F38-92C9-5BC11A8752B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{0B1D2315-E457-4F38-92C9-5BC11A8752B6}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{0B1D2315-E457-4F38-92C9-5BC11A8752B6}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{0B1D2315-E457-4F38-92C9-5BC11A8752B6}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
8 changes: 8 additions & 0 deletions
8
Markdown/Markdown/AbstractSyntaxTree/AbstractSyntaxTreeNodeView.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Markdown.NodeView; | ||
|
||
namespace Markdown.AbstractSyntaxTree; | ||
|
||
public record AbstractSyntaxTreeNodeView<TTokenType>( | ||
ReadOnlyMemory<char> Text, | ||
TTokenType TokenType) | ||
: BaseNodeView<TTokenType>; |
11 changes: 11 additions & 0 deletions
11
Markdown/Markdown/AbstractSyntaxTree/IAbstractSyntaxTree.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Markdown.NodeView; | ||
using Markdown.SyntaxRules; | ||
using Markdown.Traversable; | ||
|
||
namespace Markdown.AbstractSyntaxTree; | ||
|
||
public interface IAbstractSyntaxTree<TTokenType> : ITraversable<BaseNodeView<TTokenType>> | ||
{ | ||
public IAbstractSyntaxTree<TTokenType> AddRule(ISyntaxRule<TTokenType> rule); | ||
public IAbstractSyntaxTree<TTokenType> ApplyRules(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.