-
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 to parse the field attribute in the transformer. (#…
- Loading branch information
1 parent
1480b5d
commit 727a00f
Showing
4 changed files
with
180 additions
and
10 deletions.
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
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
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
108 changes: 108 additions & 0 deletions
108
tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/FieldDataTests.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,108 @@ | ||
// 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 FieldDataTests : BaseTransformerTestClass { | ||
|
||
class TestDataTryCreate : IEnumerable<object []> { | ||
public IEnumerator<object []> GetEnumerator () | ||
{ | ||
const string symbolOnly = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
using UIKit; | ||
namespace Test; | ||
[NoTV] | ||
[MacCatalyst (13, 1)] | ||
[DisableDefaultCtor] | ||
[Abstract] | ||
[BaseType (typeof (NSObject))] | ||
interface UIFeedbackGenerator : UIInteraction { | ||
[Field (""prepare"")] | ||
void Prepare { get; } | ||
} | ||
"; | ||
yield return [(Source: symbolOnly, Path: "/some/random/path.cs"), new FieldData ("prepare")]; | ||
|
||
const string symbolAndLibrary = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
using UIKit; | ||
namespace Test; | ||
[NoTV] | ||
[MacCatalyst (13, 1)] | ||
[DisableDefaultCtor] | ||
[Abstract] | ||
[BaseType (typeof (NSObject))] | ||
interface UIFeedbackGenerator : UIInteraction { | ||
[Field (""prepare"", ""LibraryName"")] | ||
void Prepare { get; } | ||
} | ||
"; | ||
yield return [(Source: symbolAndLibrary, Path: "/some/random/path.cs"), new FieldData ("prepare", "LibraryName")]; | ||
|
||
const string symbolAndLibraryNamed = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
using UIKit; | ||
namespace Test; | ||
[NoTV] | ||
[MacCatalyst (13, 1)] | ||
[DisableDefaultCtor] | ||
[Abstract] | ||
[BaseType (typeof (NSObject))] | ||
interface UIFeedbackGenerator : UIInteraction { | ||
[Field (""prepare"", LibraryName = ""LibraryName"")] | ||
void Prepare { get; } | ||
} | ||
"; | ||
yield return [(Source: symbolAndLibraryNamed, Path: "/some/random/path.cs"), new FieldData ("prepare", "LibraryName")]; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
} | ||
|
||
[Theory] | ||
[AllSupportedPlatformsClassData<TestDataTryCreate>] | ||
void TryCreateTests (ApplePlatform platform, (string Source, string Path) source, FieldData expectedData) | ||
{ | ||
// create a compilation used to create the transformer | ||
var compilation = CreateCompilation (platform, sources: source); | ||
var syntaxTree = compilation.SyntaxTrees.FirstOrDefault (); | ||
Assert.NotNull (syntaxTree); | ||
|
||
var semanticModel = compilation.GetSemanticModel (syntaxTree); | ||
Assert.NotNull (semanticModel); | ||
|
||
var declaration = syntaxTree.GetRoot () | ||
.DescendantNodes ().OfType<PropertyDeclarationSyntax> () | ||
.FirstOrDefault (); | ||
Assert.NotNull (declaration); | ||
|
||
var symbol = semanticModel.GetDeclaredSymbol (declaration); | ||
Assert.NotNull (symbol); | ||
var fieldData = symbol.GetAttribute<FieldData> (AttributesNames.FieldAttribute, FieldData.TryParse); | ||
Assert.Equal (expectedData, fieldData); | ||
} | ||
} |