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

feat: Added ConfigFile to simplify AnalyzerConfigFile creation. #981

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.CodeAnalysis.Testing
{
public class ConfigFile
{
public Dictionary<string, string> Preamble { get; }

public Dictionary<string, Dictionary<string, string>> Sections { get; }

public ConfigFile(
Dictionary<string, string>? preamble = null,
Dictionary<string, Dictionary<string, string>>? sections = null)
{
Preamble = preamble ?? new Dictionary<string, string>();
Sections = sections ?? new Dictionary<string, Dictionary<string, string>>();
}

private static string ToString(Dictionary<string, string> dictionary)
{
return string.Join(
Environment.NewLine,
dictionary.Select(pair => $@"{pair.Key} = {pair.Value}"));
}

private static string ToString(Dictionary<string, Dictionary<string, string>> dictionary)
{
return string.Join(
Environment.NewLine,
dictionary.Select(pair => $@"[{pair.Key}]
{ToString(pair.Value)}"));
}

public override string ToString()
{
return @$"
{ToString(Preamble)}

{ToString(Sections)}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Microsoft.CodeAnalysis.Testing.CompilerDiagnostics.Errors = 1 -> Microsoft.CodeA
Microsoft.CodeAnalysis.Testing.CompilerDiagnostics.None = 0 -> Microsoft.CodeAnalysis.Testing.CompilerDiagnostics
Microsoft.CodeAnalysis.Testing.CompilerDiagnostics.Suggestions = 3 -> Microsoft.CodeAnalysis.Testing.CompilerDiagnostics
Microsoft.CodeAnalysis.Testing.CompilerDiagnostics.Warnings = 2 -> Microsoft.CodeAnalysis.Testing.CompilerDiagnostics
Microsoft.CodeAnalysis.Testing.ConfigFile
Microsoft.CodeAnalysis.Testing.ConfigFile.ConfigFile(System.Collections.Generic.Dictionary<string, string> preamble = null, System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>> sections = null) -> void
Microsoft.CodeAnalysis.Testing.ConfigFile.Preamble.get -> System.Collections.Generic.Dictionary<string, string>
Microsoft.CodeAnalysis.Testing.ConfigFile.Sections.get -> System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>
Microsoft.CodeAnalysis.Testing.DefaultVerifier
Microsoft.CodeAnalysis.Testing.DefaultVerifier.Context.get -> System.Collections.Immutable.ImmutableStack<string>
Microsoft.CodeAnalysis.Testing.DefaultVerifier.DefaultVerifier() -> void
Expand Down Expand Up @@ -218,6 +222,7 @@ Microsoft.CodeAnalysis.Testing.SolutionState.WithProcessedMarkup(Microsoft.CodeA
Microsoft.CodeAnalysis.Testing.SourceFileCollection
Microsoft.CodeAnalysis.Testing.SourceFileCollection.Add((System.Type sourceGeneratorType, string filename, Microsoft.CodeAnalysis.Text.SourceText content) file) -> void
Microsoft.CodeAnalysis.Testing.SourceFileCollection.Add((System.Type sourceGeneratorType, string filename, string content) file) -> void
Microsoft.CodeAnalysis.Testing.SourceFileCollection.Add((string filename, Microsoft.CodeAnalysis.Testing.ConfigFile configFile) file) -> void
Microsoft.CodeAnalysis.Testing.SourceFileCollection.Add((string filename, string content) file) -> void
Microsoft.CodeAnalysis.Testing.SourceFileCollection.SourceFileCollection() -> void
Microsoft.CodeAnalysis.Testing.SourceFileList
Expand All @@ -240,6 +245,7 @@ abstract Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.DefaultFileExt.g
abstract Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.GetDiagnosticAnalyzers() -> System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer>
abstract Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.Language.get -> string
abstract Microsoft.CodeAnalysis.Testing.CodeActionTest<TVerifier>.SyntaxKindType.get -> System.Type
override Microsoft.CodeAnalysis.Testing.ConfigFile.ToString() -> string
override Microsoft.CodeAnalysis.Testing.DiagnosticResult.ToString() -> string
override Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer.Initialize(Microsoft.CodeAnalysis.Diagnostics.AnalysisContext context) -> void
override Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer.SupportedDiagnostics.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.DiagnosticDescriptor>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ public void Add((Type sourceGeneratorType, string filename, SourceText content)
var generatedPath = Path.Combine(file.sourceGeneratorType.GetTypeInfo().Assembly.GetName().Name ?? string.Empty, file.sourceGeneratorType.FullName!, file.filename);
Add((generatedPath, file.content));
}

public void Add((string filename, ConfigFile configFile) file)
{
Add((file.filename, SourceText.From(file.configFile.ToString(), Encoding.UTF8)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#if !NETCOREAPP1_1 && !NET46

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -119,6 +120,32 @@ public async Task TestDiagnosticInAnalyzerConfigFileBraceNotTreatedAsMarkup()
}.RunAsync();
}

[Fact]
public void AnalyzerConfigFileEquivalentToManualCreation()
{
var expected = @"
root = true

[*]
key = {|Literal:value|}
";

var actual = new ConfigFile(
preamble: new Dictionary<string, string>
{
["root"] = "true",
},
sections: new Dictionary<string, Dictionary<string, string>>
{
["*"] = new Dictionary<string, string>
{
["key"] = "{|Literal:value|}",
},
}).ToString();

Assert.Equal(expected, actual);
}

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class HighlightBracesIfAnalyzerConfigMissingAnalyzer : DiagnosticAnalyzer
{
Expand Down