Skip to content

Commit

Permalink
Add test for parsing ms learn repository
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed Aug 17, 2023
1 parent 3c711c9 commit e8e4db8
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 25 deletions.
4 changes: 4 additions & 0 deletions Sources/Kysect.Configuin.Core/ConfiguinException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ public class ConfiguinException : Exception
public ConfiguinException(string message) : base(message)
{
}

public ConfiguinException(string message, Exception innerException) : base(message, innerException)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static IReadOnlyCollection<MarkdownHeadedBlock> SplitByHeaders(this Markd

foreach (Block block in markdownDocument)
{
if (block is HeadingBlock currentHeaderBlock)
// In some case MarkDig return HeadingBlock that is not actually heading. Like CA2153.
if (block is HeadingBlock currentHeaderBlock && currentHeaderBlock.HeaderChar == '#')
{
if (headingBlock is not null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Kysect.Configuin.Core.MarkdownParsing.Documents;
using Kysect.Configuin.Core.MarkdownParsing.Documents;
using Kysect.Configuin.Core.MarkdownParsing.Tables;
using Kysect.Configuin.Core.MarkdownParsing.Tables.Models;
using Kysect.Configuin.Core.MarkdownParsing.TextExtractor;
Expand Down Expand Up @@ -57,8 +57,9 @@ public RoslynStyleRule ParseStyleRule(string info)
MsLearnPropertyValueDescriptionTableRow category = table.GetSingleValue("Category");
MsLearnPropertyValueDescriptionTableRow subcategory = table.GetSingleValue("Subcategory");
MsLearnPropertyValueDescriptionTableRow applicableLanguages = table.GetSingleValue("Applicable languages");
MsLearnPropertyValueDescriptionTableRow introducedVersion = table.GetSingleValue("Introduced version");
IReadOnlyList<MsLearnPropertyValueDescriptionTableRow> options = table.GetValues("Options");
// TODO: return as optional parameter. Not all rules contains it
//MsLearnPropertyValueDescriptionTableRow introducedVersion = table.GetSingleValue("Introduced version");
IReadOnlyList<MsLearnPropertyValueDescriptionTableRow> options = table.FindValues("Options");

string overviewText = GetStyleOverviewText(markdownHeadedBlocks);
IReadOnlyCollection<RoslynStyleRuleOption> roslynStyleRuleOptions = ParseOptions(markdownHeadedBlocks);
Expand All @@ -81,13 +82,11 @@ public RoslynQualityRule ParseQualityRule(string info)
throw new ConfiguinException("Style rule markdown file does not contains any heading blocks. Cannot parse description");

MarkdownHeadedBlock markdownHeadedBlock = markdownHeadedBlocks.First();
if (markdownHeadedBlock.Content.Count != 1)
throw new ConfiguinException($"Style rule description block contains unexpected child count. Expected 1, but was {markdownHeadedBlock.Content.Count}");

Block block = markdownHeadedBlock.Content.Single();
if (block is not Table tableBlock)
throw new ConfiguinException($"Style rule description block must contains Table block but was {block.GetType()}");
IReadOnlyCollection<Table> contentBlocks = markdownHeadedBlock.Content.OfType<Table>().ToList();
if (contentBlocks.Count != 1)
throw new ConfiguinException($"Style rule description block contains unexpected child count. Expected 1, but was {contentBlocks.Count}");

Table tableBlock = contentBlocks.Single();
MarkdownTableContent markdownTableContent = _markdownTableParser.ParseToSimpleContent(tableBlock);
MsLearnPropertyValueDescriptionTable table = _msLearnTableParser.Parse(markdownTableContent);

Expand All @@ -96,7 +95,9 @@ public RoslynQualityRule ParseQualityRule(string info)
// TODO: add this fields to model
MsLearnPropertyValueDescriptionTableRow breakingChanges = table.GetSingleValue("Fix is breaking or non-breaking");
// TODO: remove hardcoded dotnet version
MsLearnPropertyValueDescriptionTableRow isDefault = table.GetSingleValue("Enabled by default in .NET 7");
// TODO: docs contains both .NET7 and .NET8 =_=
//MsLearnPropertyValueDescriptionTableRow isDefault = table.GetSingleValue("Enabled by default in .NET 8");


return new RoslynQualityRule(
RoslynRuleId.Parse(ruleId.Value),
Expand Down Expand Up @@ -144,7 +145,7 @@ private RoslynStyleRuleOption ParseOption(MarkdownHeadedBlock optionBlock)
// TODO: implement code block filtering (C# / VB) and parsing

MsLearnPropertyValueDescriptionTableRow optionName = table.GetSingleValue("Option name");
IReadOnlyList<MsLearnPropertyValueDescriptionTableRow> optionValues = table.GetValues("Option values");
IReadOnlyList<MsLearnPropertyValueDescriptionTableRow> optionValues = table.FindValues("Option values");
MsLearnPropertyValueDescriptionTableRow defaultValue = table.GetSingleValue("Default option value");

return new RoslynStyleRuleOption(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public MsLearnPropertyValueDescriptionTableRow GetSingleValue(string key)
return values[0];
}

public IReadOnlyList<MsLearnPropertyValueDescriptionTableRow> FindValues(string key)
{
if (!Properties.TryGetValue(key, out IReadOnlyList<MsLearnPropertyValueDescriptionTableRow>? value))
return Array.Empty<MsLearnPropertyValueDescriptionTableRow>();

return value;
}

public IReadOnlyList<MsLearnPropertyValueDescriptionTableRow> GetValues(string key)
{
if (!Properties.TryGetValue(key, out IReadOnlyList<MsLearnPropertyValueDescriptionTableRow>? value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ private static void ValidateTableHeader(MarkdownTableContent simpleTable)
if (simpleTable.Headers.Count != 2 && simpleTable.Headers.Count != 3)
throw new ArgumentException($"Unexpected column count in property-value table. Expected 2 or 3, but was {simpleTable.Headers.Count}");

string[] expectedHeaders = { "Property", "Value", "Description" };
for (int i = 0; i < simpleTable.Headers.Count; i++)
{
if (!string.IsNullOrEmpty(simpleTable.Headers[i]) && simpleTable.Headers[i] != expectedHeaders[i])
throw new ArgumentException($"Table header on index {i} must be equal to {expectedHeaders[i]} but was {simpleTable.Headers[i]}");
}
// TODO: This validation sometimes throw error. In some cases table has "Item" column instead of "Property".
// Maybe we need to drop it.
//string[] expectedHeaders = { "Property", "Value", "Description" };
//for (int i = 0; i < simpleTable.Headers.Count; i++)
//{
// if (!string.IsNullOrEmpty(simpleTable.Headers[i]) && simpleTable.Headers[i] != expectedHeaders[i])
// throw new ArgumentException($"Table header on index {i} must be equal to {expectedHeaders[i]} but was {simpleTable.Headers[i]}");
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using Kysect.Configuin.Core.MarkdownParsing;
using Kysect.Configuin.Core.MarkdownParsing.TextExtractor;
using Kysect.Configuin.Core.MsLearnDocumentation;
using Kysect.Configuin.Core.MsLearnDocumentation.Models;
using Kysect.Configuin.Core.RoslynRuleModels;
using Kysect.Configuin.Tests.Tools;
using NUnit.Framework;

namespace Kysect.Configuin.Tests.MsLearnDocumentation;
Expand Down Expand Up @@ -70,4 +72,18 @@ public void ParseQualityRule_CS1064_ReturnExpectedResult()

// TODO: parse description
}

// TODO: remove ignore
[Test]
[Ignore("Need to fix all related problems")]
public void Parse_MsDocsRepository_FinishWithoutError()
{
var repositoryPathProvider = new MsLearnDocumentationInfoLocalProvider(Constants.GetPathToMsDocsRoot());

MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = repositoryPathProvider.Provide();
RoslynRules roslynRules = _parser.Parse(msLearnDocumentationRawInfo);

// TODO: add asserts
Assert.Pass();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentAssertions;
using Kysect.Configuin.Core.MsLearnDocumentation;
using Kysect.Configuin.Tests.Tools;
using NUnit.Framework;

namespace Kysect.Configuin.Tests;
Expand All @@ -9,13 +10,7 @@ public class MsLearnRepositoryPathProviderTests
[Test]
public void GetPath_ReturnExistsFileItems()

Check warning on line 11 in Sources/Kysect.Configuin.Tests/MsLearnRepositoryPathProviderTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name Kysect.Configuin.Tests.MsLearnRepositoryPathProviderTests.GetPath_ReturnExistsFileItems() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
string pathToRoot = Path.Combine(
"..", // net7.0
"..", // Debug
"..", // bin
"..", // Kysect.Configuin.Tests
"..", // root
"ms-learn");
string pathToRoot = Constants.GetPathToMsDocsRoot();

var pathProvider = new MsLearnRepositoryPathProvider(pathToRoot);

Expand Down
15 changes: 15 additions & 0 deletions Sources/Kysect.Configuin.Tests/Tools/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Kysect.Configuin.Tests.Tools;

public class Constants
{
public static string GetPathToMsDocsRoot()
{
return Path.Combine(
"..", // netX.0
"..", // Debug
"..", // bin
"..", // Kysect.Configuin.Tests
"..", // root
"ms-learn");
}
}

0 comments on commit e8e4db8

Please sign in to comment.