Skip to content

Commit a072770

Browse files
mandel-macaqueGitHub Actions Autoformatter
andauthored
[Rgen] Add a central location for the aux variable naming. (#22063)
Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
1 parent 3b647ca commit a072770

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Macios.Generator.DataModel;
5+
6+
readonly partial struct Parameter {
7+
8+
public enum VariableType {
9+
Handle,
10+
NSArray,
11+
BlockLiteral,
12+
PrimitivePointer,
13+
}
14+
15+
/// <summary>
16+
/// Returns the name of the aux variable that would have needed for the given parameter. Use the
17+
/// variable type to name it.
18+
/// </summary>
19+
/// <param name="variableType">The type of aux variable.</param>
20+
/// <returns>The name of the aux variable to use.</returns>
21+
public string? GetNameForVariableType (VariableType variableType)
22+
=> variableType switch {
23+
VariableType.Handle => $"{Name}__handle__",
24+
VariableType.NSArray => $"nsa_{Name}",
25+
VariableType.BlockLiteral => $"block_ptr_{Name}",
26+
VariableType.PrimitivePointer => $"converted_{Name}",
27+
_ => null
28+
};
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using Microsoft.Macios.Generator.DataModel;
7+
using Xunit;
8+
using static Microsoft.Macios.Generator.Tests.TestDataFactory;
9+
10+
namespace Microsoft.Macios.Generator.Tests.DataModel;
11+
12+
public class ParameterTests {
13+
14+
class TestDataGetVariableName : IEnumerable<object []> {
15+
public IEnumerator<object []> GetEnumerator ()
16+
{
17+
var exampleParameter = new Parameter (0, ReturnTypeForBool (), "firstParameter");
18+
yield return [exampleParameter, Parameter.VariableType.Handle, $"{exampleParameter.Name}__handle__"];
19+
yield return [exampleParameter, Parameter.VariableType.BlockLiteral, $"block_ptr_{exampleParameter.Name}"];
20+
yield return [exampleParameter, Parameter.VariableType.PrimitivePointer, $"converted_{exampleParameter.Name}"];
21+
yield return [exampleParameter, Parameter.VariableType.NSArray, $"nsa_{exampleParameter.Name}"];
22+
}
23+
24+
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
25+
}
26+
27+
[Theory]
28+
[ClassData (typeof (TestDataGetVariableName))]
29+
void GetNameForVariableTypeTests (Parameter parameter, Parameter.VariableType variableType, string expectedName)
30+
=> Assert.Equal (expectedName, parameter.GetNameForVariableType (variableType));
31+
}

0 commit comments

Comments
 (0)