From a43854206b8ef1b0d42dbbd89f8d8f5020fbee28 Mon Sep 17 00:00:00 2001 From: Fredi Kats Date: Sat, 2 Sep 2023 00:41:27 +0400 Subject: [PATCH] Fix test for parsing ms docs and editorcofig --- .../CodeStyleGeneration/CodeStyleGenerator.cs | 15 ++++++++ .../MsLearnDocumentationParser.cs | 36 +++++++++---------- .../RoslynRuleModels/RoslynRules.cs | 2 ++ .../CodeStyleGeneratorTests.cs | 4 +-- .../EditorConfigRuleParserTests.cs | 2 +- .../EditorConfigFileParsing/IniParserTests.cs | 2 +- .../MsLearnDocumentationParserTests.cs | 30 ++++++++++++++++ .../Resources/Editor-config-sample.ini | 15 ++++---- 8 files changed, 75 insertions(+), 31 deletions(-) diff --git a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs index c0d6a29..8f35591 100644 --- a/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs +++ b/Sources/Kysect.Configuin.Core/CodeStyleGeneration/CodeStyleGenerator.cs @@ -13,6 +13,8 @@ public CodeStyleInfo Generate(EditorConfigRuleSet editorConfigRuleSet, RoslynRul IReadOnlyCollection notProcessedRules = editorConfigRuleSet.Rules; IReadOnlyCollection optionsFromDocs = roslynRules.GetOptions(); + notProcessedRules = notProcessedRules.Where(IsSupported).ToList(); + // TODO: support in some way notProcessedRules = notProcessedRules.Where(r => r is not GeneralEditorConfigRule).ToList(); @@ -30,6 +32,7 @@ public CodeStyleInfo Generate(EditorConfigRuleSet editorConfigRuleSet, RoslynRul .OfType() .Select(r => ParseRule(r, roslynOptionEditorConfigRules, roslynRules)) .ToList(); + notProcessedRules = notProcessedRules.Where(r => r is not RoslynSeverityEditorConfigRule).ToList(); if (notProcessedRules.Any()) @@ -45,6 +48,18 @@ public CodeStyleInfo Generate(EditorConfigRuleSet editorConfigRuleSet, RoslynRul return new CodeStyleInfo(elements); } + private bool IsSupported(IEditorConfigRule rule) + { + // TODO: support parsing for this rule + if (rule is RoslynSeverityEditorConfigRule severityEditorConfigRule + && severityEditorConfigRule.RuleId.Equals(new RoslynRuleId(RoslynRuleType.StyleRule, 1006))) + { + return false; + } + + return true; + } + // TODO: Rework naming private RoslynOptionConfiguration ParseOption(RoslynOptionEditorConfigRule optionEditorConfigRule, IReadOnlyCollection optionsFromDocs) { diff --git a/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs b/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs index c376296..7f3bcb3 100644 --- a/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs +++ b/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs @@ -146,23 +146,9 @@ public IReadOnlyCollection ParseQualityRules(string info) public IReadOnlyCollection ParseAdditionalFormattingOptions(string dotnetFormattingFileContent) { - bool HeaderForOption(MarkdownHeadedBlock markdownHeadedBlock) - { - // TODO: do it in better way? - // TODO: remove StringComparison - string headerText = markdownHeadedBlock.GetHeaderText(); - - return headerText.StartsWith("dotnet_") - || headerText.StartsWith("csharp_"); - } - MarkdownDocument markdownDocument = MarkdownDocumentExtensions.CreateFromString(dotnetFormattingFileContent); IReadOnlyCollection markdownHeadedBlocks = markdownDocument.SplitByHeaders(); - - return markdownHeadedBlocks - .Where(HeaderForOption) - .Select(ParseOption) - .ToList(); + return ParseOptions(markdownHeadedBlocks); } private IReadOnlyCollection ParseQualityRuleTableIdRow(MsLearnPropertyValueDescriptionTableRow ruleId) @@ -178,7 +164,9 @@ private string GetStyleOverviewText(IReadOnlyCollection mar { MarkdownHeadedBlock? overviewBlock = markdownHeadedBlocks.FirstOrDefault(h => h.GetHeaderText() == "Overview"); if (overviewBlock is null) - throw new ConfiguinException("Style rule page does not contains Overview block."); + // TODO: IDE0055 + //throw new ConfiguinException("Style rule page does not contains Overview block."); + return string.Empty; string overviewText = overviewBlock .Content @@ -190,14 +178,24 @@ private string GetStyleOverviewText(IReadOnlyCollection mar private IReadOnlyCollection ParseOptions(IReadOnlyCollection markdownHeadedBlocks) { - // TODO: fix header with option detecting - return markdownHeadedBlocks - .Where(h => h.GetHeaderText().StartsWith("dotnet_")) + .Where(HeaderForOption) .Select(ParseOption) .ToList(); } + private bool HeaderForOption(MarkdownHeadedBlock markdownHeadedBlock) + { + // TODO: do it in better way? + // TODO: remove StringComparison + string headerText = markdownHeadedBlock.GetHeaderText(); + + return headerText.StartsWith("dotnet_") + || headerText.StartsWith("csharp_") + // IDE0073 + || headerText == "file_header_template"; + } + private RoslynStyleRuleOption ParseOption(MarkdownHeadedBlock optionBlock) { var tables = optionBlock.Content.OfType().ToList(); diff --git a/Sources/Kysect.Configuin.Core/RoslynRuleModels/RoslynRules.cs b/Sources/Kysect.Configuin.Core/RoslynRuleModels/RoslynRules.cs index 60c527b..8450026 100644 --- a/Sources/Kysect.Configuin.Core/RoslynRuleModels/RoslynRules.cs +++ b/Sources/Kysect.Configuin.Core/RoslynRuleModels/RoslynRules.cs @@ -23,6 +23,8 @@ public IReadOnlyCollection GetOptions() { return StyleRules .SelectMany(r => r.Options) + .Concat(DotnetFormattingOptions) + .Concat(SharpFormattingOptions) .ToList(); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs index 6b11433..64e59ba 100644 --- a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs +++ b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs @@ -19,16 +19,14 @@ public class CodeStyleGeneratorTests public CodeStyleGeneratorTests() { _editorConfigRuleParser = new EditorConfigRuleParser(); - _msLearnDocumentationParser = new MsLearnDocumentationParser(RoundtripRendererTextExtractor.Create()); + _msLearnDocumentationParser = new MsLearnDocumentationParser(PlainTextExtractor.Create()); string pathToRoot = Constants.GetPathToMsDocsRoot(); _repositoryPathProvider = new MsLearnDocumentationInfoLocalProvider(pathToRoot); } - // TODO: remove ignore [Test] - [Ignore("Return to this test after fixes in _msLearnDocumentationParser.Parse")] public void Generate_ForAllMsLearnDocumentation_FinishWithoutErrors() { string pathToIniFile = Path.Combine("Resources", "Editor-config-sample.ini"); diff --git a/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/EditorConfigRuleParserTests.cs b/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/EditorConfigRuleParserTests.cs index 748f53b..9d3fb81 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/EditorConfigRuleParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/EditorConfigRuleParserTests.cs @@ -92,6 +92,6 @@ public void Parse_EditorConfigFile_ReturnWithoutErrors() // TODO: add more asserts editorConfigRuleSet.Rules - .Should().HaveCount(400); + .Should().HaveCount(393); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/IniParserTests.cs b/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/IniParserTests.cs index 1d7b0d2..04c3597 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/IniParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/EditorConfigFileParsing/IniParserTests.cs @@ -38,6 +38,6 @@ public void Parse_EditorConfigFile_ParsedWithoutErrors() IReadOnlyCollection result = _parser.Parse(fileText); // TODO: add more asserts - result.Should().HaveCount(400); + result.Should().HaveCount(393); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs index 040c258..0e9c96e 100644 --- a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs @@ -295,6 +295,36 @@ void MyMethod() { roslynStyleRuleOptions.ElementAt(0).Should().BeEquivalentTo(csharp_new_line_before_open_brace); } + [Test] + [Ignore("Issue #33")] + public void Parse_CodeStyleRefactoringOptions_ReturnExpectedResult() + { + string pathToFile = string.Empty; + string fileContent = File.ReadAllText(pathToFile); + var dotnet_style_operator_placement_when_wrapping = new RoslynStyleRuleOption( + "dotnet_style_operator_placement_when_wrapping", + new[] + { + new RoslynStyleRuleOptionValue("end_of_line", "Place operator at the end of a line."), + new RoslynStyleRuleOptionValue("beginning_of_line", "Place operator on a new line."), + }, + "beginning_of_line", + """ + // dotnet_style_operator_placement_when_wrapping = end_of_line + if (true && + true) + + // dotnet_style_operator_placement_when_wrapping = beginning_of_line + if (true + && true) + """); + + IReadOnlyCollection codeStyleRefactoringOptions = _parser.ParseAdditionalFormattingOptions(fileContent); + + codeStyleRefactoringOptions.Should().HaveCount(1); + codeStyleRefactoringOptions.ElementAt(0).Should().BeEquivalentTo(dotnet_style_operator_placement_when_wrapping); + } + // TODO: remove ignore [Test] [Ignore("Need to fix all related problems")] diff --git a/Sources/Kysect.Configuin.Tests/Resources/Editor-config-sample.ini b/Sources/Kysect.Configuin.Tests/Resources/Editor-config-sample.ini index b1e565d..e920b7b 100644 --- a/Sources/Kysect.Configuin.Tests/Resources/Editor-config-sample.ini +++ b/Sources/Kysect.Configuin.Tests/Resources/Editor-config-sample.ini @@ -364,7 +364,6 @@ dotnet_diagnostic.CA1065.severity = warning dotnet_diagnostic.CA1067.severity = warning dotnet_diagnostic.CA1068.severity = warning dotnet_diagnostic.CA1069.severity = warning -dotnet_diagnostic.CA1079.severity = warning dotnet_diagnostic.CA1200.severity = none dotnet_diagnostic.CA1303.severity = none dotnet_diagnostic.CA1304.severity = none @@ -545,17 +544,19 @@ dotnet_naming_rule.other_public_protected_fields_disallowed_rule.severity # Other -dotnet_style_operator_placement_when_wrapping = beginning_of_line +# dotnet_style_operator_placement_when_wrapping = beginning_of_line # Not verified csharp_style_prefer_method_group_conversion = true:silent csharp_style_prefer_top_level_statements = true:silent -csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent -csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent -csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent -dotnet_style_allow_multiple_blank_lines_experimental = true:silent -dotnet_style_allow_statement_immediately_after_block_experimental = true:silent + +# csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent +# csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent +# csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent +# dotnet_style_allow_multiple_blank_lines_experimental = true:silent +# dotnet_style_allow_statement_immediately_after_block_experimental = true:silent + # IDE0230 csharp_style_prefer_utf8_string_literals = true:suggestion \ No newline at end of file