-
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] Implement 'IsSmartEnum' for the transformer. (#22090)
Co-authored-by: GitHub Actions Autoformatter <[email protected]>
- Loading branch information
1 parent
5c5febc
commit abfc28e
Showing
3 changed files
with
115 additions
and
3 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
98 changes: 98 additions & 0 deletions
98
tests/rgen/Microsoft.Macios.Transformer.Tests/Extensions/TypeSymbolExtensionsTests.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,98 @@ | ||
// 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 Xamarin.Tests; | ||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Transformer.Tests.Extensions; | ||
|
||
public class TypeSymbolExtensionsTests : BaseTransformerTestClass { | ||
|
||
class TestDataIsSmartEnum : IEnumerable<object []> { | ||
public IEnumerator<object []> GetEnumerator () | ||
{ | ||
var path = "/some/random/path.cs"; | ||
|
||
const string normalEnum = @" | ||
using System; | ||
namespace Test; | ||
public enum MyEnum { | ||
First, | ||
Second, | ||
Last, | ||
} | ||
"; | ||
yield return [(Source: normalEnum, Path: path), false]; | ||
|
||
const string traditionalSmartEnum = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
namespace Test; | ||
public enum MyEnum { | ||
[Field (""FirstEnum""] | ||
First, | ||
[Field (""Second""] | ||
Second, | ||
[Field (""Last""] | ||
Last, | ||
} | ||
"; | ||
|
||
yield return [(Source: traditionalSmartEnum, Path: path), true]; | ||
|
||
const string partialSmartEnum = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
namespace Test; | ||
public enum MyEnum { | ||
First, | ||
Second, | ||
[Field (""Last""] | ||
Last, | ||
} | ||
"; | ||
|
||
// should not be very common, nevertheless test for it. | ||
yield return [(Source: partialSmartEnum, Path: path), true]; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
} | ||
|
||
[Theory] | ||
[AllSupportedPlatformsClassData<TestDataIsSmartEnum>] | ||
void IsSmartEnumTests (ApplePlatform platform, (string Source, string Path) source, bool expectedResult) | ||
{ | ||
var compilation = CreateCompilation (platform, sources: source); | ||
var syntaxTree = compilation.SyntaxTrees.ForSource (source); | ||
var trees = compilation.SyntaxTrees.Where (s => s.FilePath == source.Path).ToArray (); | ||
Assert.Single (trees); | ||
Assert.NotNull (syntaxTree); | ||
|
||
var semanticModel = compilation.GetSemanticModel (syntaxTree); | ||
Assert.NotNull (semanticModel); | ||
|
||
var declaration = syntaxTree.GetRoot () | ||
.DescendantNodes ().OfType<EnumDeclarationSyntax> () | ||
.LastOrDefault (); | ||
|
||
Assert.NotNull (declaration); | ||
|
||
var symbol = semanticModel.GetDeclaredSymbol (declaration); | ||
Assert.NotNull (symbol); | ||
var isSmartEnum = symbol.IsSmartEnum (); | ||
Assert.Equal (expectedResult, symbol.IsSmartEnum ()); | ||
} | ||
} |