Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RGen] Provide a method that returns the selector for an accessor. #21995

Merged
merged 5 commits into from
Jan 17, 2025
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
25 changes: 25 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Accessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ public Accessor (AccessorKind accessorKind,
Modifiers = modifiers;
}

/// <summary>
/// Retrieve the selector to be used with the associated property.
/// </summary>
/// <param name="associatedProperty">The property associated with the accessor.</param>
/// <returns>The selector to use for the accessor.</returns>
public string? GetSelector (in Property associatedProperty)
{
// this is not a property but a field, we cannot retrieve a selector.
if (!associatedProperty.IsProperty)
return null;

// There are two possible cases, the current accessor has an export attribute, if that
// is the case, we will use the selector in that attribute. Otherwise, we have:
//
// * getter: return the property selector.
// * setter: use the registrar code (it has the right logic) to get the setter.
if (ExportPropertyData is null) {
return Kind == AccessorKind.Getter
? associatedProperty.ExportPropertyData.Value.Selector
: Registrar.Registrar.CreateSetterSelector (associatedProperty.ExportPropertyData.Value.Selector);
}

return ExportPropertyData.Value.Selector;
}

/// <inheritdoc />
public bool Equals (Accessor other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<Compile Include="..\..\bgen\Extensions\PlatformNameExtensions.cs" >
<Link>external\PlatformNameExtensions.cs</Link>
</Compile>
<Compile Include="..\..\ObjCRuntime\Registrar.core.cs" >
<Link>external\Registrar.core.cs</Link>
</Compile>
<Compile Update="Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand Down
161 changes: 161 additions & 0 deletions tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/AccessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Generator.DataModel;
using Xunit;
using static Microsoft.Macios.Generator.Tests.TestDataFactory;

namespace Microsoft.Macios.Generator.Tests.DataModel;

Expand Down Expand Up @@ -308,4 +309,164 @@ public void CompareSameKindSameAttrSameAvailability ()
Assert.True (x == y);
Assert.False (x != y);
}

[Fact]
public void GetSelectorForFieldProperty ()
{
var property = new Property (
name: "MyProperty",
returnType: ReturnTypeForString (),
symbolAvailability: new (),
attributes: [],
modifiers: [],
accessors: []
) {
ExportFieldData = new (new ("Constant"), "lib"),
};

var accessor = new Accessor (
accessorKind: AccessorKind.Getter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [
new ("First"),
new ("Second"),
],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PrivateKeyword)
]);

Assert.Null (accessor.GetSelector (property));
}

[Fact]
public void GetGetterSelectorNoExportData ()
{
var property = new Property (
name: "MyProperty",
returnType: ReturnTypeForString (),
symbolAvailability: new (),
attributes: [],
modifiers: [],
accessors: []
) {
ExportPropertyData = new ("label")
};

var accessor = new Accessor (
accessorKind: AccessorKind.Getter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [
new ("First"),
new ("Second"),
],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PrivateKeyword)
]);

var selector = accessor.GetSelector (property);
Assert.NotNull (selector);
Assert.Equal (property.ExportPropertyData.Value.Selector, selector);
}

[Fact]
public void GetGetterSelectorExportData ()
{
var property = new Property (
name: "MyProperty",
returnType: ReturnTypeForString (),
symbolAvailability: new (),
attributes: [],
modifiers: [],
accessors: []
) {
ExportPropertyData = new ("label")
};

var customSelector = "custom";
var accessor = new Accessor (
accessorKind: AccessorKind.Getter,
symbolAvailability: new (),
exportPropertyData: new (customSelector),
attributes: [
new ("First"),
new ("Second"),
],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PrivateKeyword)
]);

var selector = accessor.GetSelector (property);
Assert.NotNull (selector);
Assert.Equal (customSelector, selector);
}

[Fact]
public void GetSetterSelectorNoExportData ()
{
var property = new Property (
name: "MyProperty",
returnType: ReturnTypeForString (),
symbolAvailability: new (),
attributes: [],
modifiers: [],
accessors: []
) {
ExportPropertyData = new ("label")
};

var accessor = new Accessor (
accessorKind: AccessorKind.Setter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [
new ("First"),
new ("Second"),
],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PrivateKeyword)
]);

var selector = accessor.GetSelector (property);
Assert.NotNull (selector);
Assert.Equal ("setLabel:", selector);
}

[Fact]
public void GetSetterSelectorExportData ()
{
var property = new Property (
name: "MyProperty",
returnType: ReturnTypeForString (),
symbolAvailability: new (),
attributes: [],
modifiers: [],
accessors: []
) {
ExportPropertyData = new ("label")
};

var customSelector = "setCustom:";
var accessor = new Accessor (
accessorKind: AccessorKind.Setter,
symbolAvailability: new (),
exportPropertyData: new (customSelector),
attributes: [
new ("First"),
new ("Second"),
],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PrivateKeyword)
]);

var selector = accessor.GetSelector (property);
Assert.NotNull (selector);
Assert.Equal (customSelector, selector);
}
}
Loading