Skip to content

Commit

Permalink
Add global options
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamsker committed Oct 20, 2024
1 parent fd4522f commit 5ce44bd
Show file tree
Hide file tree
Showing 15 changed files with 475 additions and 22 deletions.
121 changes: 121 additions & 0 deletions Generator.Equals.DynamicGenerationTests/DefaultsTest.cs
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());

}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using Microsoft.CodeAnalysis;
using Generator.Equals.Tests.DynamicGeneration.Utils;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using SourceGeneratorTestHelpers;

namespace Generator.Equals.DynamicGenerationTests.Issues;

public class Issue_60_StringEquality_Enumerables
{
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 readonly List<PortableExecutableReference> References = GeneratorTestHelper.References;

[Fact]
public void Comparison_is_correctly_generated()
Expand Down
130 changes: 130 additions & 0 deletions Generator.Equals.DynamicGenerationTests/Utils/GeneratorTestHelper.cs
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);
}
}
3 changes: 2 additions & 1 deletion Generator.Equals.Tests/Classes/UnorderedEquality.Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public partial class UnorderedEquality
[Equatable]
public partial class Sample
{
[UnorderedEquality] public List<int>? Properties { get; set; }
[UnorderedEquality]
public List<int>? Properties { get; set; }
}
}
}
6 changes: 5 additions & 1 deletion Generator.Equals.Tests/Generator.Equals.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<ImplicitUsings>disable</ImplicitUsings>
<NoWarn>NU1701</NoWarn>
<Nullable>enable</Nullable>

<DemoSourceGenerator_Counter>1</DemoSourceGenerator_Counter>
</PropertyGroup>

<!-- For debugging -->
Expand Down Expand Up @@ -40,5 +42,7 @@
<ProjectReference Include="..\Generator.Equals.Runtime\Generator.Equals.Runtime.csproj" />
<ProjectReference Include="..\Generator.Equals\Generator.Equals.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<CompilerVisibleProperty Include="DemoSourceGenerator_Counter" />
</ItemGroup>
</Project>
Loading

0 comments on commit 5ce44bd

Please sign in to comment.