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] Add factory method to cast a byte result to a bool. #22202

Merged
merged 2 commits into from
Feb 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
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ static partial class BindingSyntaxFactory {
whenFalse: castZero);
}

/// <summary>
/// Return the expression needed to cast an invocation that returns a byte to a bool.
/// </summary>
/// <param name="invocation">The byte returning invocation expression.</param>
/// <returns>The expression need to cast the invocation to a byte.</returns>
internal static BinaryExpressionSyntax ByteToBool (InvocationExpressionSyntax invocation)
{
// generates: invocation != 0
return BinaryExpression (
SyntaxKind.NotEqualsExpression,
invocation.WithTrailingTrivia (Space),
LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (0)).WithLeadingTrivia (Space));
}

/// <summary>
/// Returns the aux nsarray variable for an array object. This method will do the following:
/// 1. Check if the object is nullable or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Collections;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.DataModel;
using Xunit;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Microsoft.Macios.Generator.Emitters.BindingSyntaxFactory;
using static Microsoft.Macios.Generator.Tests.TestDataFactory;
using TypeInfo = Microsoft.Macios.Generator.DataModel.TypeInfo;
Expand Down Expand Up @@ -112,6 +114,44 @@ void CastToPrimitiveTests (Parameter parameter, string? expectedCast)
}
}

class TestDataByteToBoolTests : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
yield return [
InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("NSObject"),
IdentifierName ("TestFunction").WithTrailingTrivia (Space))),
"NSObject.TestFunction () != 0"
];

yield return [
InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("NSObject"),
IdentifierName ("TestFunction").WithTrailingTrivia (Space)))
.WithArgumentList (
ArgumentList (
SingletonSeparatedList<ArgumentSyntax> (
Argument (
IdentifierName ("arg1"))))),
"NSObject.TestFunction (arg1) != 0"
];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
}

[Theory]
[ClassData (typeof (TestDataByteToBoolTests))]
void ByteToBoolTests (InvocationExpressionSyntax invocationExpressionSyntax, string expectedDeclaration)
{
var declaration = ByteToBool (invocationExpressionSyntax);
Assert.Equal (expectedDeclaration, declaration.ToString ());
}

[Fact]
void CastToByteTests ()
{
Expand Down
Loading