-
Notifications
You must be signed in to change notification settings - Fork 495
Filter out non-public types in TextColorToGenerator assembly scanning #3161
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
Merged
TheCodeTraveler
merged 2 commits into
CommunityToolkit:main
from
jfversluis:fix/textcolorto-filter-internal-types
Mar 18, 2026
+125
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
122 changes: 122 additions & 0 deletions
122
...it.Maui.SourceGenerators.UnitTests/TextColorToGeneratorTests/TextColorToGeneratorTests.cs
This file contains hidden or 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,122 @@ | ||
| using System.IO; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using CommunityToolkit.Maui.SourceGenerators.Generators; | ||
| using Xunit; | ||
|
|
||
| namespace CommunityToolkit.Maui.SourceGenerators.UnitTests; | ||
|
|
||
| public class TextColorToGeneratorTests | ||
| { | ||
| [Fact] | ||
| public async Task Generator_DoesNotEmitCode_ForInternalMauiControlsTypes() | ||
| { | ||
| var cancellationToken = TestContext.Current.CancellationToken; | ||
| // Arrange: create a synthetic "Microsoft.Maui.Controls" assembly that | ||
| // contains both a public and an internal type implementing ITextStyle + IAnimatable. | ||
| const string syntheticMauiSource = | ||
| """ | ||
| namespace Microsoft.Maui | ||
| { | ||
| public interface ITextStyle | ||
| { | ||
| Microsoft.Maui.Graphics.Color TextColor { get; set; } | ||
| } | ||
| } | ||
|
|
||
| namespace Microsoft.Maui.Graphics | ||
| { | ||
| public class Color | ||
| { | ||
| public float Red { get; } | ||
| public float Green { get; } | ||
| public float Blue { get; } | ||
| public float Alpha { get; } | ||
|
|
||
| public Color WithRed(float v) => this; | ||
| public Color WithGreen(float v) => this; | ||
| public Color WithBlue(float v) => this; | ||
| public Color WithAlpha(float v) => this; | ||
| } | ||
|
|
||
| public static class Colors | ||
| { | ||
| public static Color Transparent => new(); | ||
| } | ||
| } | ||
|
|
||
| namespace Microsoft.Maui.Controls | ||
| { | ||
| public interface IAnimatable { } | ||
|
|
||
| public class BindableObject | ||
| { | ||
| public void Animate(string name, Animation animation, uint rate, uint length, Easing easing) { } | ||
| } | ||
|
|
||
| public class Animation : System.Collections.IEnumerable | ||
| { | ||
| public void Add(double beginAt, double finishAt, Animation animation) { } | ||
| public void Commit(IAnimatable owner, string name, uint rate = 16, uint length = 250, Easing easing = null, System.Action<double, bool> finished = null) { } | ||
| System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => null; | ||
| } | ||
|
|
||
| public class Easing { } | ||
|
|
||
| public class View : BindableObject, Microsoft.Maui.ITextStyle, IAnimatable | ||
| { | ||
| public Microsoft.Maui.Graphics.Color TextColor { get; set; } | ||
| } | ||
|
|
||
| public class PublicLabel : View { } | ||
| internal class InternalLabel : View { } | ||
| } | ||
| """; | ||
|
|
||
| var coreRef = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); | ||
|
|
||
| var mauiCompilation = CSharpCompilation.Create( | ||
| "Microsoft.Maui.Controls", | ||
| [CSharpSyntaxTree.ParseText(syntheticMauiSource, cancellationToken: cancellationToken)], | ||
| [coreRef], | ||
| new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
|
|
||
| using var ms = new MemoryStream(); | ||
| var emitResult = mauiCompilation.Emit(ms, cancellationToken: cancellationToken); | ||
| Assert.True(emitResult.Success, | ||
| "Synthetic Maui assembly failed to compile: " + | ||
| string.Join("\n", emitResult.Diagnostics)); | ||
|
|
||
| ms.Seek(0, SeekOrigin.Begin); | ||
| var mauiRef = MetadataReference.CreateFromStream(ms); | ||
|
|
||
| // A trivial user compilation that references the synthetic assembly. | ||
| const string userSource = | ||
| """ | ||
| namespace MyApp | ||
| { | ||
| public class Placeholder { } | ||
| } | ||
| """; | ||
|
|
||
| var userCompilation = CSharpCompilation.Create( | ||
| "UserAssembly", | ||
| [CSharpSyntaxTree.ParseText(userSource, cancellationToken: cancellationToken)], | ||
| [coreRef, mauiRef], | ||
| new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
|
|
||
| // Act: run the generator | ||
| var generator = new TextColorToGenerator(); | ||
| GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); | ||
| driver = driver.RunGenerators(userCompilation, cancellationToken); | ||
|
|
||
| var runResult = driver.GetRunResult(); | ||
| var generatedSources = runResult.GeneratedTrees | ||
| .Select(t => t.GetText(cancellationToken).ToString()) | ||
| .ToList(); | ||
|
|
||
| // Assert: code is generated for the public type, but NOT the internal one. | ||
| Assert.Contains(generatedSources, src => src.Contains("PublicLabel")); | ||
| Assert.DoesNotContain(generatedSources, src => src.Contains("InternalLabel")); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.