Skip to content

Commit

Permalink
chore: code-rub
Browse files Browse the repository at this point in the history
  • Loading branch information
earloc committed Oct 26, 2024
1 parent 8b0f023 commit fa93b59
Show file tree
Hide file tree
Showing 18 changed files with 492 additions and 494 deletions.
21 changes: 10 additions & 11 deletions src/Playground.Console/NoCodeGen/InternalClass.cs
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
{
}
4 changes: 2 additions & 2 deletions src/Playground.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using My.Super.Special.Namespace;
using Playground.Console.NoCodeGen;
using Playground.Console.WithCodeGen;
using Playground.Common;
using Playground.Common.Groups;
using Playground.Common.Groups.TypealizR;
using Playground.Common.NoCodeGen;
using Playground.Console;
using Playground.Console.NoCodeGen;
using Playground.Console.WithCodeGen;

const string arthur = "Arthur";
const string chewbacca = "Chewbacca";
Expand Down
2 changes: 1 addition & 1 deletion src/TypealizR.CLI/Resources/ResxBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal class ResxBuilder
{
private const string resHeader = "resheader";
private const string value = "value";
private readonly Dictionary<string, string> entries = new();
private readonly Dictionary<string, string> entries = [];
public ResxBuilder Add(string key, string value)
{
entries.Add(key, value);
Expand Down
26 changes: 13 additions & 13 deletions src/TypealizR.Tests/CLI.Tests/InMemoryStorage.cs
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 src/TypealizR.Tests/Snapshots/GeneratorTesterBuilder.cs
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;
}
}
Loading

0 comments on commit fa93b59

Please sign in to comment.