-
Notifications
You must be signed in to change notification settings - Fork 19
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
15 changed files
with
475 additions
and
22 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
Generator.Equals.DynamicGenerationTests/DefaultsTest.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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using Generator.Equals.Tests.DynamicGeneration.Utils; | ||
|
||
namespace Generator.Equals.Tests.DynamicGeneration; | ||
|
||
public class DefaultsTest | ||
{ | ||
[Theory] | ||
[InlineData("Unordered", | ||
"global::Generator.Equals.UnorderedEqualityComparer<int>.Default.Equals(this.Properties!, other.Properties!)")] | ||
[InlineData("Ordered", | ||
"global::Generator.Equals.OrderedEqualityComparer<int>.Default.Equals(this.Properties!, other.Properties!)")] | ||
public void Global_Enumerable_Order_Is_Respected(string enumerableEquality, string expectedComparisonLine) | ||
{ | ||
var source = | ||
""" | ||
public partial class UnorderedEquality | ||
{ | ||
[Equatable] | ||
public partial class Sample | ||
{ | ||
public List<int>? Properties { get; set; } | ||
} | ||
} | ||
"""; | ||
|
||
|
||
// generator_equals_comparison_string = OrdinalIgnoreCase | ||
// generator_equals_comparison_enumerable = Unordered | ||
var genResult = GeneratorTestHelper.RunGenerator(source, new() | ||
{ | ||
{ "generator_equals_comparison_enumerable", enumerableEquality } | ||
}); | ||
|
||
var gensource = genResult.Results | ||
.SelectMany(x => x.GeneratedSources) | ||
.Select(x => x.SourceText) | ||
.ToList() | ||
; | ||
|
||
Assert.NotNull(gensource); | ||
|
||
Assert.Contains( | ||
expectedComparisonLine, | ||
gensource.FirstOrDefault()?.ToString()); | ||
} | ||
|
||
// generator_equals_comparison_string is respected | ||
[Theory] | ||
[InlineData("OrdinalIgnoreCase", | ||
"global::System.StringComparer.OrdinalIgnoreCase.Equals(this.Tag!, other.Tag!)")] | ||
[InlineData("Ordinal", | ||
"global::System.StringComparer.Ordinal.Equals(this.Tag!, other.Tag!)")] | ||
[InlineData("InvariantCulture", | ||
"global::System.StringComparer.InvariantCulture.Equals(this.Tag!, other.Tag!)")] | ||
public void Global_String_Comparison_Is_Respected(string stringComparison, string expectedComparisonLine) | ||
{ | ||
var source = | ||
""" | ||
[Equatable] | ||
public partial class Resource | ||
{ | ||
public string Tag {get;set;} | ||
} | ||
"""; | ||
|
||
var genResult = GeneratorTestHelper.RunGenerator(source, new() | ||
{ | ||
{ "generator_equals_comparison_string", stringComparison } | ||
}); | ||
|
||
var gensource = genResult.Results | ||
.SelectMany(x => x.GeneratedSources) | ||
.Select(x => x.SourceText) | ||
.ToList() | ||
; | ||
|
||
Assert.NotNull(gensource); | ||
|
||
Assert.Contains( | ||
expectedComparisonLine, | ||
gensource.FirstOrDefault()?.ToString()); | ||
|
||
} | ||
|
||
[Theory] | ||
[InlineData("OrdinalIgnoreCase", | ||
"new global::Generator.Equals.OrderedEqualityComparer<string>(global::System.StringComparer.OrdinalIgnoreCase).Equals(this.Tags!, other.Tags!)")] | ||
[InlineData("Ordinal", | ||
"new global::Generator.Equals.OrderedEqualityComparer<string>(global::System.StringComparer.Ordinal).Equals(this.Tags!, other.Tags!)")] | ||
[InlineData("InvariantCulture", | ||
"new global::Generator.Equals.OrderedEqualityComparer<string>(global::System.StringComparer.InvariantCulture).Equals(this.Tags!, other.Tags!)")] | ||
public void Global_String_Comparison_Is_Respected_In_Lists(string stringComparison, string expectedComparisonLine) | ||
{ | ||
var source = | ||
""" | ||
[Equatable] | ||
public partial class Resource | ||
{ | ||
public List<string>? Tags {get;set;} | ||
} | ||
"""; | ||
|
||
var genResult = GeneratorTestHelper.RunGenerator(source, new() | ||
{ | ||
{ "generator_equals_comparison_string", stringComparison } | ||
}); | ||
|
||
var gensource = genResult.Results | ||
.SelectMany(x => x.GeneratedSources) | ||
.Select(x => x.SourceText) | ||
.ToList() | ||
; | ||
|
||
Assert.NotNull(gensource); | ||
|
||
Assert.Contains( | ||
expectedComparisonLine, | ||
gensource.FirstOrDefault()?.ToString()); | ||
|
||
} | ||
} |
10 changes: 3 additions & 7 deletions
10
Generator.Equals.DynamicGenerationTests/Issues/Issue-60-StringEquality-Enumerables.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
130 changes: 130 additions & 0 deletions
130
Generator.Equals.DynamicGenerationTests/Utils/GeneratorTestHelper.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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Generator.Equals.Tests.DynamicGeneration.Utils; | ||
|
||
public class GeneratorTestHelper | ||
{ | ||
public static readonly List<PortableExecutableReference> References = | ||
AppDomain.CurrentDomain.GetAssemblies() | ||
.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location)) | ||
.Select(x => MetadataReference.CreateFromFile(x.Location)) | ||
.Append(MetadataReference.CreateFromFile(typeof(EquatableAttribute).Assembly.Location)) | ||
.ToList(); | ||
|
||
public static GeneratorDriverRunResult RunGenerator | ||
( | ||
[StringSyntax("c#-test")] string source, | ||
Dictionary<string, string>? options | ||
) | ||
{ | ||
// If source has no namespace, add one | ||
if (!source.Contains("namespace")) | ||
{ | ||
source = "namespace TestNamespace;\n" + source; | ||
} | ||
|
||
// using CompositeDto.Generator.Runtime; | ||
// using System; | ||
|
||
if (!source.Contains("using Generator.Equals;")) | ||
{ | ||
source = "using Generator.Equals;\n" + source; | ||
} | ||
|
||
if (!source.Contains("using System;")) | ||
{ | ||
source = "using System;\n" + source; | ||
} | ||
|
||
if (!source.Contains("using System.Collections.Generic;")) | ||
{ | ||
source = "using System.Collections.Generic;\n" + source; | ||
} | ||
|
||
|
||
|
||
|
||
var syntaxTree = CSharpSyntaxTree.ParseText(source); | ||
|
||
var compilation = CSharpCompilation.Create( | ||
assemblyName: "Tests", | ||
syntaxTrees: [syntaxTree], | ||
references: References, | ||
options: new( | ||
outputKind: OutputKind.DynamicallyLinkedLibrary | ||
) | ||
); | ||
|
||
IEnumerable<ISourceGenerator> generator = new[] | ||
{ | ||
new EqualsGenerator().AsSourceGenerator() | ||
}; | ||
|
||
AnalyzerConfigOptionsProvider? prov = | ||
options is null | ||
? null | ||
: new MockAnalyzerConfigOptionsProvider(options); | ||
|
||
var driver = CSharpGeneratorDriver | ||
.Create | ||
( | ||
generator | ||
, optionsProvider: prov | ||
) | ||
.RunGeneratorsAndUpdateCompilation( | ||
compilation, | ||
out var outputCompilation, | ||
out var diagnostics | ||
); | ||
|
||
var diag = outputCompilation.GetDiagnostics(); | ||
var warnings = diag.Where(d => d.Severity == DiagnosticSeverity.Warning).ToList(); | ||
|
||
// Assert.Empty( | ||
// outputCompilation | ||
// .GetDiagnostics() | ||
// .Where(d => d.Severity is DiagnosticSeverity.Error or DiagnosticSeverity.Warning) | ||
// ); | ||
|
||
// Assert.Empty(diagnostics); | ||
return driver.GetRunResult(); | ||
} | ||
} | ||
|
||
public class MockAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider | ||
{ | ||
public override AnalyzerConfigOptions GlobalOptions { get; } | ||
|
||
public MockAnalyzerConfigOptionsProvider(Dictionary<string, string> options) | ||
{ | ||
GlobalOptions = new MockAnalyzerConfigOptions(options); | ||
} | ||
|
||
public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) | ||
{ | ||
return GlobalOptions; | ||
} | ||
|
||
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) | ||
{ | ||
return GlobalOptions; | ||
} | ||
} | ||
|
||
public class MockAnalyzerConfigOptions : AnalyzerConfigOptions | ||
{ | ||
private readonly Dictionary<string, string> _options; | ||
|
||
public MockAnalyzerConfigOptions(Dictionary<string, string> options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) | ||
{ | ||
return _options.TryGetValue(key, out value); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.