Skip to content

Commit

Permalink
[RGen] Provide a method that returns the selector for an accessor. (#…
Browse files Browse the repository at this point in the history
…21995)

The method takes into account the property associated with the accessor
and any possible export method that has been added to the accessor
itself.

The setter label for the default case uses the Registrar.core code.

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Jan 17, 2025
1 parent 93ac3f4 commit fb7073e
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
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);
}
}

0 comments on commit fb7073e

Please sign in to comment.