-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rgen] Add the code needed to parse the core image property attr in t…
…he transformer. We need to be able to parse it to convert it to the new API.
- Loading branch information
1 parent
86b5d59
commit 8203293
Showing
4 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/rgen/Microsoft.Macios.Transformer/Attributes/CoreImageFilterPropertyData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Macios.Transformer.Attributes; | ||
|
||
readonly struct CoreImageFilterPropertyData : IEquatable<CoreImageFilterPropertyData> { | ||
|
||
public string Name { get; } | ||
|
||
public CoreImageFilterPropertyData (string name) | ||
{ | ||
Name = name; | ||
} | ||
|
||
public static bool TryParse (AttributeData attributeData, | ||
[NotNullWhen (true)] out CoreImageFilterPropertyData? data) | ||
{ | ||
data = null; | ||
var count = attributeData.ConstructorArguments.Length; | ||
string name; | ||
|
||
switch (count) { | ||
case 1: | ||
name = (string) attributeData.ConstructorArguments [0].Value!; | ||
break; | ||
default: | ||
// 0 should not be an option.. | ||
return false; | ||
} | ||
|
||
if (attributeData.NamedArguments.Length == 0) { | ||
data = new (name); | ||
return true; | ||
} | ||
|
||
foreach (var (argumentName, value) in attributeData.NamedArguments) { | ||
switch (argumentName) { | ||
case "Name": | ||
name = (string) value.Value!; | ||
break; | ||
default: | ||
data = null; | ||
return false; | ||
} | ||
} | ||
|
||
data = new(name); | ||
return true; | ||
} | ||
|
||
public bool Equals (CoreImageFilterPropertyData other) | ||
=> Name == other.Name; | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals (object? obj) | ||
{ | ||
return obj is CoreImageFilterPropertyData other && Equals (other); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode () | ||
=> HashCode.Combine (Name); | ||
|
||
|
||
public static bool operator == (CoreImageFilterPropertyData x, CoreImageFilterPropertyData y) | ||
{ | ||
return x.Equals (y); | ||
} | ||
|
||
public static bool operator != (CoreImageFilterPropertyData x, CoreImageFilterPropertyData y) | ||
{ | ||
return !(x == y); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string ToString () | ||
{ | ||
return $"{{ Name: '{Name}' }}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/CoreImageFilterPropertyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.Macios.Generator.Extensions; | ||
using Microsoft.Macios.Transformer.Attributes; | ||
using Xamarin.Tests; | ||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Transformer.Tests.Attributes; | ||
|
||
public class CoreImageFilterPropertyTests : BaseTransformerTestClass { | ||
class TestDataTryCreate : IEnumerable<object []> { | ||
public IEnumerator<object []> GetEnumerator () | ||
{ | ||
const string path = "/some/random/path.cs"; | ||
|
||
const string coreImageFilterWithProperties = @" | ||
using CoreImage; | ||
using Foundation; | ||
using ObjCRuntime; | ||
using Mono.Cecil; | ||
namespace NS; | ||
[CoreImageFilter (IntPtrCtorVisibility = MethodAttributes.Family)] // was already protected in classic | ||
[BaseType (typeof (CIFilter))] | ||
interface CICompositingFilter : CIAccordionFoldTransitionProtocol { | ||
[CoreImageFilterProperty (""inputBackgroundImage"")] | ||
CIImage BackgroundImage { get; set; } | ||
} | ||
"; | ||
yield return [ | ||
(Source: coreImageFilterWithProperties, Path: path), | ||
new CoreImageFilterPropertyData ("inputBackgroundImage") | ||
]; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
} | ||
|
||
[Theory] | ||
[AllSupportedPlatformsClassData<TestDataTryCreate>] | ||
void TryCreateTests (ApplePlatform platform, (string Source, string Path) source, | ||
CoreImageFilterPropertyData expectedData) | ||
{ | ||
// create a compilation used to create the transformer | ||
var compilation = CreateCompilation (platform, sources: source); | ||
var syntaxTree = compilation.SyntaxTrees.ForSource (source); | ||
Assert.NotNull (syntaxTree); | ||
|
||
var semanticModel = compilation.GetSemanticModel (syntaxTree); | ||
Assert.NotNull (semanticModel); | ||
|
||
var declaration = syntaxTree.GetRoot () | ||
.DescendantNodes ().OfType<PropertyDeclarationSyntax> () | ||
.LastOrDefault (); | ||
Assert.NotNull (declaration); | ||
|
||
var symbol = semanticModel.GetDeclaredSymbol (declaration); | ||
Assert.NotNull (symbol); | ||
var exportData = | ||
symbol.GetAttribute<CoreImageFilterPropertyData> (AttributesNames.CoreImageFilterPropertyAttribute, | ||
CoreImageFilterPropertyData.TryParse); | ||
Assert.Equal (expectedData, exportData); | ||
} | ||
} |