-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
492 additions
and
494 deletions.
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Playground.Console.NoCodeGen; | ||
[SuppressMessage( | ||
"Reliability", "CA1812:'InternalClass' is an internal class that is apparently never instantiated.If so, remove the code from the assembly.If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).", | ||
Justification = "Markertype needed for resolution of resx-file") | ||
] | ||
internal sealed class InternalClass | ||
{ | ||
} | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Playground.Console.NoCodeGen; | ||
[SuppressMessage( | ||
"Reliability", "CA1812:'InternalClass' is an internal class that is apparently never instantiated.If so, remove the code from the assembly.If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).", | ||
Justification = "Markertype needed for resolution of resx-file") | ||
] | ||
internal sealed class InternalClass | ||
{ | ||
} |
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
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
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
using TypealizR.CLI.Abstractions; | ||
|
||
namespace TypealizR.Tests.CLI.Tests; | ||
internal sealed class InMemoryStorage : IStorage | ||
{ | ||
private Dictionary<string, string> files = new(); | ||
public Task AddAsync(string fileName, string content) | ||
{ | ||
files.Add(fileName, content); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public IReadOnlyDictionary<string, string> Files => files; | ||
using TypealizR.CLI.Abstractions; | ||
|
||
namespace TypealizR.Tests.CLI.Tests; | ||
internal sealed class InMemoryStorage : IStorage | ||
{ | ||
private readonly Dictionary<string, string> files = []; | ||
public Task AddAsync(string fileName, string content) | ||
{ | ||
files.Add(fileName, content); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public IReadOnlyDictionary<string, string> Files => files; | ||
} |
280 changes: 140 additions & 140 deletions
280
src/TypealizR.Tests/Snapshots/GeneratorTesterBuilder.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 |
---|---|---|
@@ -1,140 +1,140 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using TypealizR.Diagnostics; | ||
|
||
namespace TypealizR.Tests.Snapshots; | ||
|
||
internal sealed class GeneratorTesterBuilder<TGenerator> where TGenerator : IIncrementalGenerator, new() | ||
{ | ||
internal static GeneratorTesterBuilder<TGenerator> Create(string baseDirectory, string? rootNamespace = null, string? useParamNamesInMethodNames = null) => new(baseDirectory, rootNamespace, useParamNamesInMethodNames); | ||
|
||
private readonly DirectoryInfo baseDirectory; | ||
private readonly List<FileInfo> sourceFiles = new(); | ||
private readonly List<FileInfo> resxFiles = new(); | ||
private readonly Dictionary<string, string> customToolNamespaces = new(); | ||
private readonly Dictionary<string, string> useParamNamesInMethodNames = new(); | ||
|
||
private readonly string? rootNamespace; | ||
private readonly string? useParamNamesInMethodNamesBuildProperty; | ||
|
||
public GeneratorTesterBuilder(string baseDirectory, string? rootNamespace = null, string? useParamNamesInMethodNames = null) | ||
{ | ||
this.baseDirectory = new DirectoryInfo(baseDirectory); | ||
|
||
if (!this.baseDirectory.Exists) | ||
{ | ||
throw new ArgumentException($"the specified directory {this.baseDirectory.FullName} does not exist", nameof(baseDirectory)); | ||
} | ||
|
||
this.rootNamespace = rootNamespace; | ||
useParamNamesInMethodNamesBuildProperty = useParamNamesInMethodNames; | ||
} | ||
|
||
private bool withoutMsBuildProjectDirectory; | ||
private DirectoryInfo? projectDir; | ||
public GeneratorTesterBuilder<TGenerator> WithoutMsBuildProjectDirectory(string? butWithProjectDir = null) | ||
{ | ||
withoutMsBuildProjectDirectory = true; | ||
if (butWithProjectDir is not null) | ||
{ | ||
this.projectDir = new DirectoryInfo(butWithProjectDir); | ||
} | ||
return this; | ||
} | ||
|
||
public GeneratorTesterBuilder<TGenerator> WithSourceFile(string fileName) | ||
{ | ||
var path = Path.Combine(baseDirectory.FullName, fileName); | ||
|
||
var fileInfo = new FileInfo(path); | ||
|
||
if (!fileInfo.Exists) | ||
{ | ||
throw new FileNotFoundException("source-file not found", fileInfo.FullName); | ||
} | ||
sourceFiles.Add(fileInfo); | ||
return this; | ||
} | ||
|
||
public GeneratorTesterBuilder<TGenerator> WithResxFile( | ||
string fileName, | ||
bool andDesignerFile = false, | ||
string? andCustomToolNamespace = null, | ||
string useParamNamesInMethodNames = "" | ||
) | ||
{ | ||
var path = Path.Combine(baseDirectory.FullName, fileName); | ||
var fileInfo = new FileInfo(path); | ||
|
||
if (!fileInfo.Exists) | ||
{ | ||
throw new FileNotFoundException($"{fileInfo.FullName} not found", fileInfo.FullName); | ||
} | ||
|
||
resxFiles.Add(fileInfo); | ||
|
||
if (!string.IsNullOrEmpty(andCustomToolNamespace)) | ||
{ | ||
customToolNamespaces.Add(fileInfo.FullName, andCustomToolNamespace); | ||
} | ||
|
||
this.useParamNamesInMethodNames.Add(fileInfo.FullName, useParamNamesInMethodNames); | ||
|
||
if (andDesignerFile) | ||
{ | ||
WithSourceFile(fileName.Replace(".resx", ".Designer.cs", StringComparison.Ordinal)); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public IVerifiable Build() | ||
{ | ||
var syntaxTrees = sourceFiles | ||
.Select(x => new { Path = x.FullName, Content = File.ReadAllText(x.FullName) }) | ||
.Select(x => CSharpSyntaxTree.ParseText(x.Content, path: x.Path)) | ||
.ToArray(); | ||
|
||
var compilation = CSharpCompilation.Create( | ||
assemblyName: "TypealizR.SnapshotTests", | ||
syntaxTrees: syntaxTrees | ||
); | ||
|
||
var additionalTexts = resxFiles | ||
.Select(x => new ResxFile(x.FullName) as AdditionalText) | ||
.ToArray() | ||
; | ||
|
||
var generator = new TGenerator(); | ||
var driver = CSharpGeneratorDriver.Create(generator) | ||
.AddAdditionalTexts(ImmutableArray.CreateRange(additionalTexts)) | ||
.WithUpdatedAnalyzerConfigOptions( | ||
new GeneratorTesterOptionsProvider( | ||
withoutMsBuildProjectDirectory ? null : baseDirectory, | ||
projectDir, | ||
rootNamespace, | ||
severityConfig, | ||
customToolNamespaces, | ||
useParamNamesInMethodNames, | ||
useParamNamesInMethodNamesBuildProperty | ||
) | ||
); | ||
|
||
var generatorDriver = driver.RunGenerators(compilation); | ||
|
||
return new GeneratorTester(generatorDriver, Path.Combine(baseDirectory.FullName, ".snapshots")); | ||
} | ||
|
||
private readonly Dictionary<DiagnosticsId, string> severityConfig = new(); | ||
|
||
internal GeneratorTesterBuilder<TGenerator> WithSeverityConfig(DiagnosticsId id, DiagnosticSeverity severity) | ||
=> WithSeverityConfig(id, severity.ToString()); | ||
|
||
internal GeneratorTesterBuilder<TGenerator> WithSeverityConfig(DiagnosticsId id, string severity) | ||
{ | ||
severityConfig[id] = severity; | ||
return this; | ||
} | ||
} | ||
|
||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using TypealizR.Diagnostics; | ||
|
||
namespace TypealizR.Tests.Snapshots; | ||
|
||
internal sealed class GeneratorTesterBuilder<TGenerator> where TGenerator : IIncrementalGenerator, new() | ||
{ | ||
internal static GeneratorTesterBuilder<TGenerator> Create(string baseDirectory, string? rootNamespace = null, string? useParamNamesInMethodNames = null) => new(baseDirectory, rootNamespace, useParamNamesInMethodNames); | ||
|
||
private readonly DirectoryInfo baseDirectory; | ||
private readonly List<FileInfo> sourceFiles = []; | ||
private readonly List<FileInfo> resxFiles = []; | ||
private readonly Dictionary<string, string> customToolNamespaces = []; | ||
private readonly Dictionary<string, string> useParamNamesInMethodNames = []; | ||
|
||
private readonly string? rootNamespace; | ||
private readonly string? useParamNamesInMethodNamesBuildProperty; | ||
|
||
public GeneratorTesterBuilder(string baseDirectory, string? rootNamespace = null, string? useParamNamesInMethodNames = null) | ||
{ | ||
this.baseDirectory = new DirectoryInfo(baseDirectory); | ||
|
||
if (!this.baseDirectory.Exists) | ||
{ | ||
throw new ArgumentException($"the specified directory {this.baseDirectory.FullName} does not exist", nameof(baseDirectory)); | ||
} | ||
|
||
this.rootNamespace = rootNamespace; | ||
useParamNamesInMethodNamesBuildProperty = useParamNamesInMethodNames; | ||
} | ||
|
||
private bool withoutMsBuildProjectDirectory; | ||
private DirectoryInfo? projectDir; | ||
public GeneratorTesterBuilder<TGenerator> WithoutMsBuildProjectDirectory(string? butWithProjectDir = null) | ||
{ | ||
withoutMsBuildProjectDirectory = true; | ||
if (butWithProjectDir is not null) | ||
{ | ||
this.projectDir = new DirectoryInfo(butWithProjectDir); | ||
} | ||
return this; | ||
} | ||
|
||
public GeneratorTesterBuilder<TGenerator> WithSourceFile(string fileName) | ||
{ | ||
var path = Path.Combine(baseDirectory.FullName, fileName); | ||
|
||
var fileInfo = new FileInfo(path); | ||
|
||
if (!fileInfo.Exists) | ||
{ | ||
throw new FileNotFoundException("source-file not found", fileInfo.FullName); | ||
} | ||
sourceFiles.Add(fileInfo); | ||
return this; | ||
} | ||
|
||
public GeneratorTesterBuilder<TGenerator> WithResxFile( | ||
string fileName, | ||
bool andDesignerFile = false, | ||
string? andCustomToolNamespace = null, | ||
string useParamNamesInMethodNames = "" | ||
) | ||
{ | ||
var path = Path.Combine(baseDirectory.FullName, fileName); | ||
var fileInfo = new FileInfo(path); | ||
|
||
if (!fileInfo.Exists) | ||
{ | ||
throw new FileNotFoundException($"{fileInfo.FullName} not found", fileInfo.FullName); | ||
} | ||
|
||
resxFiles.Add(fileInfo); | ||
|
||
if (!string.IsNullOrEmpty(andCustomToolNamespace)) | ||
{ | ||
customToolNamespaces.Add(fileInfo.FullName, andCustomToolNamespace); | ||
} | ||
|
||
this.useParamNamesInMethodNames.Add(fileInfo.FullName, useParamNamesInMethodNames); | ||
|
||
if (andDesignerFile) | ||
{ | ||
WithSourceFile(fileName.Replace(".resx", ".Designer.cs", StringComparison.Ordinal)); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public IVerifiable Build() | ||
{ | ||
var syntaxTrees = sourceFiles | ||
.Select(x => new { Path = x.FullName, Content = File.ReadAllText(x.FullName) }) | ||
.Select(x => CSharpSyntaxTree.ParseText(x.Content, path: x.Path)) | ||
.ToArray(); | ||
|
||
var compilation = CSharpCompilation.Create( | ||
assemblyName: "TypealizR.SnapshotTests", | ||
syntaxTrees: syntaxTrees | ||
); | ||
|
||
var additionalTexts = resxFiles | ||
.Select(x => new ResxFile(x.FullName) as AdditionalText) | ||
.ToArray() | ||
; | ||
|
||
var generator = new TGenerator(); | ||
var driver = CSharpGeneratorDriver.Create(generator) | ||
.AddAdditionalTexts(ImmutableArray.CreateRange(additionalTexts)) | ||
.WithUpdatedAnalyzerConfigOptions( | ||
new GeneratorTesterOptionsProvider( | ||
withoutMsBuildProjectDirectory ? null : baseDirectory, | ||
projectDir, | ||
rootNamespace, | ||
severityConfig, | ||
customToolNamespaces, | ||
useParamNamesInMethodNames, | ||
useParamNamesInMethodNamesBuildProperty | ||
) | ||
); | ||
|
||
var generatorDriver = driver.RunGenerators(compilation); | ||
|
||
return new GeneratorTester(generatorDriver, Path.Combine(baseDirectory.FullName, ".snapshots")); | ||
} | ||
|
||
private readonly Dictionary<DiagnosticsId, string> severityConfig = []; | ||
|
||
internal GeneratorTesterBuilder<TGenerator> WithSeverityConfig(DiagnosticsId id, DiagnosticSeverity severity) | ||
=> WithSeverityConfig(id, severity.ToString()); | ||
|
||
internal GeneratorTesterBuilder<TGenerator> WithSeverityConfig(DiagnosticsId id, string severity) | ||
{ | ||
severityConfig[id] = severity; | ||
return this; | ||
} | ||
} | ||
Oops, something went wrong.