Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Integrations/Shared/Type/TypeDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ internal static class TypeDataHelper

private static string GetFileName(INamedTypeSymbol typeSymbol)
{
var namespacePrefix = GetNamespacePrefix(typeSymbol);
var prefix = GetFileNamePrefix(typeSymbol);
var suffix = typeSymbol.Arity == 0 ? string.Empty : $"_{typeSymbol.Arity}";

return prefix + typeSymbol.Name + suffix;
return namespacePrefix + prefix + typeSymbol.Name + suffix;
}

private static string GetNamespacePrefix(INamedTypeSymbol typeSymbol)
{
if (typeSymbol.ContainingNamespace.IsGlobalNamespace)
return string.Empty;

return typeSymbol.ContainingNamespace.ToDisplayString() + ".";
}

private static string GetFileNamePrefix(ISymbol typeSymbol)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -65,6 +66,24 @@ public partial class NotRaiseGirCore1004;
public class NotRaiseGirCore1004<T> : NotRaiseGirCore1004;
""";

private const string DuplicateSubclassNameOne = """
using GObject;

namespace DuplicateSubclassName.One;

[Subclass<GObject.Object>]
public partial class Widget;
""";

private const string DuplicateSubclassNameTwo = """
using GObject;

namespace DuplicateSubclassName.Two;

[Subclass<GObject.Object>]
public partial class Widget;
""";

[TestMethod]
[DataRow(RaiseGirCore1002, "GirCore1002", true)]
[DataRow(RaiseGirCore1004, "GirCore1004", true)]
Expand Down Expand Up @@ -100,4 +119,38 @@ public async Task ShouldRaiseExpectedDiagnosticIds(string code, string diagnosti
else
diagnostics.ContainsNoDiagnostic(diagnosticId);
}

[TestMethod]
public void ShouldGenerateNamespaceQualifiedHintNamesForDuplicateSubclassNames()
{
var compilation = CSharpCompilation.Create(
assemblyName: nameof(ShouldGenerateNamespaceQualifiedHintNamesForDuplicateSubclassNames),
syntaxTrees: [
CSharpSyntaxTree.ParseText(DuplicateSubclassNameOne),
CSharpSyntaxTree.ParseText(DuplicateSubclassNameTwo)
],
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
references: [
MetadataReference.CreateFromFile(System.Reflection.Assembly.Load("System.Runtime").Location),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(GObject.Object).Assembly.Location)
]
);

GeneratorDriver driver = CSharpGeneratorDriver.Create(new SourceGenerator.Generator());
driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out var generatorDiagnostics, TestContext.CancellationToken);

generatorDiagnostics.ContainsNoDiagnostic("CS8785");

var generatedHintNames = driver
.GetRunResult()
.Results
.SelectMany(x => x.GeneratedSources)
.Select(x => x.HintName)
.ToList();

CollectionAssert.Contains(generatedHintNames, "DuplicateSubclassName.One.Widget.Subclass.g.cs");
CollectionAssert.Contains(generatedHintNames, "DuplicateSubclassName.Two.Widget.Subclass.g.cs");
CollectionAssert.AllItemsAreUnique(generatedHintNames);
}
}
52 changes: 52 additions & 0 deletions src/Tests/Libs/Gtk-4.0.Integration.Tests/DiagnosticAnalyzerTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -51,6 +52,22 @@ public partial class RaiseGirCore2003
}
""";

private const string DuplicateTemplateNameOne = """
namespace DuplicateTemplateName.One;

[GObject.Subclass<Gtk.Widget>]
[Gtk.Template<Gtk.AssemblyResource>("CompositeBoxWidget.ui")]
public partial class Widget;
""";

private const string DuplicateTemplateNameTwo = """
namespace DuplicateTemplateName.Two;

[GObject.Subclass<Gtk.Widget>]
[Gtk.Template<Gtk.AssemblyResource>("CompositeBoxWidget.ui")]
public partial class Widget;
""";

[TestMethod]
[DataRow(RaiseGirCore2001, "GirCore2001", true)]
[DataRow(NotRaiseGirCore2001, "GirCore2001", false)]
Expand Down Expand Up @@ -84,4 +101,39 @@ public async Task ShouldRaiseExpectedDiagnosticIds(string code, string diagnosti
else
diagnostics.ContainsNoDiagnostic(diagnosticId);
}

[TestMethod]
public void ShouldGenerateNamespaceQualifiedHintNamesForDuplicateTemplateNames()
{
var compilation = CSharpCompilation.Create(
assemblyName: nameof(ShouldGenerateNamespaceQualifiedHintNamesForDuplicateTemplateNames),
syntaxTrees: [
CSharpSyntaxTree.ParseText(DuplicateTemplateNameOne),
CSharpSyntaxTree.ParseText(DuplicateTemplateNameTwo)
],
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
references: [
MetadataReference.CreateFromFile(System.Reflection.Assembly.Load("System.Runtime").Location),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(GObject.Object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Gtk.Widget).Assembly.Location)
]
);

GeneratorDriver driver = CSharpGeneratorDriver.Create(new SourceGenerator.Generator());
driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out var generatorDiagnostics, TestContext.CancellationToken);

generatorDiagnostics.ContainsNoDiagnostic("CS8785");

var generatedHintNames = driver
.GetRunResult()
.Results
.SelectMany(x => x.GeneratedSources)
.Select(x => x.HintName)
.ToList();

CollectionAssert.Contains(generatedHintNames, "DuplicateTemplateName.One.Widget.Template.g.cs");
CollectionAssert.Contains(generatedHintNames, "DuplicateTemplateName.Two.Widget.Template.g.cs");
CollectionAssert.AllItemsAreUnique(generatedHintNames);
}
}
Loading