diff --git a/src/CommunityToolkit.Maui.Markup.SourceGenerators/SourceGenerators/TextAlignmentExtensionsGenerator.cs b/src/CommunityToolkit.Maui.Markup.SourceGenerators/SourceGenerators/TextAlignmentExtensionsGenerator.cs index c78dfd6f..f3d07ba2 100644 --- a/src/CommunityToolkit.Maui.Markup.SourceGenerators/SourceGenerators/TextAlignmentExtensionsGenerator.cs +++ b/src/CommunityToolkit.Maui.Markup.SourceGenerators/SourceGenerators/TextAlignmentExtensionsGenerator.cs @@ -54,7 +54,7 @@ static void Execute(SourceProductionContext context, Compilation compilation, Im foreach (var namedTypeSymbol in mauiTextAlignmentImplementors) { - textAlignmentClassList.Add((namedTypeSymbol.Name, "public", namedTypeSymbol.ContainingNamespace.ToDisplayString(), namedTypeSymbol.TypeArguments.GetGenericTypeArgumentsString(), namedTypeSymbol.GetGenericTypeConstraintsAsString())); + textAlignmentClassList.Add((namedTypeSymbol.Name, "internal", namedTypeSymbol.ContainingNamespace.ToDisplayString(), namedTypeSymbol.TypeArguments.GetGenericTypeArgumentsString(), namedTypeSymbol.GetGenericTypeConstraintsAsString())); } // Collect All Classes in User Library that Implement ITextAlignment @@ -114,7 +114,7 @@ namespace CommunityToolkit.Maui.Markup /// /// Extension Methods for /// - static partial class TextAlignmentExtensions_" + textAlignmentClass.ClassName + @" + " + textAlignmentClass.ClassAcessModifier + @" static partial class TextAlignmentExtensions_" + textAlignmentClass.ClassName + @" { /// /// = @@ -242,7 +242,7 @@ namespace LeftToRight /// /// Extension Methods for /// - " + textAlignmentClass.ClassAcessModifier + " static partial class TextAlignmentExtensions_" + textAlignmentClass.ClassName + @" + " + textAlignmentClass.ClassAcessModifier + @" static partial class TextAlignmentExtensions_" + textAlignmentClass.ClassName + @" { /// /// = @@ -289,7 +289,7 @@ namespace RightToLeft /// /// Extension methods for /// - " + textAlignmentClass.ClassAcessModifier + " static partial class TextAlignmentExtensions_" + textAlignmentClass.ClassName + @" + " + textAlignmentClass.ClassAcessModifier + @" static partial class TextAlignmentExtensions_" + textAlignmentClass.ClassName + @" { /// /// = diff --git a/src/CommunityToolkit.Maui.Markup.UnitTests/TextAlignmentExtensionsTests.cs b/src/CommunityToolkit.Maui.Markup.UnitTests/TextAlignmentExtensionsTests.cs index 9de538c2..b28be8bc 100644 --- a/src/CommunityToolkit.Maui.Markup.UnitTests/TextAlignmentExtensionsTests.cs +++ b/src/CommunityToolkit.Maui.Markup.UnitTests/TextAlignmentExtensionsTests.cs @@ -1,4 +1,8 @@ -using CommunityToolkit.Maui.Markup.UnitTests.Base; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using CommunityToolkit.Maui.Markup.UnitTests.Base; using CommunityToolkit.Maui.UnitTests.Extensions.TextAlignmentExtensions; using Microsoft.Maui; using Microsoft.Maui.Controls; @@ -212,6 +216,41 @@ public void SupportCustomTextAlignment() .TextCenter()); } + [Test] + public void AccessModifierForMauiControlsShouldNotBePublic() + { + foreach (var (generatedType, control) in GetGeneratedTextAlignmentExtensionTypes()) + { + if (control.Assembly == typeof(Button).Assembly) + { + Assert.False(generatedType.IsPublic); + } + } + } + + [Test] + public void AccessModifierForCustomControlsShouldMatchTheControl() + { + var executingAssembly = Assembly.GetExecutingAssembly(); + + foreach (var (generatedType, control) in GetGeneratedTextAlignmentExtensionTypes()) + { + if (control.Assembly == executingAssembly) + { + Assert.AreEqual(control.IsPublic, generatedType.IsPublic); + } + } + } + + static IEnumerable<(Type generatedType, Type control)> GetGeneratedTextAlignmentExtensionTypes() + { + return from type in Assembly.GetExecutingAssembly().GetTypes() + where type.Name.StartsWith("TextAlignmentExtensions_") + let method = type.GetMethods().Single(m => m.Name.StartsWith("TextLeft") || m.Name.StartsWith("TextStart")) + let control = method.GetParameters()[0].ParameterType.BaseType + select (type, control); + } + class DerivedFromSearchBar : SearchBar { } } @@ -374,9 +413,9 @@ class InternalTextAlignmentView : View, ICustomTextAlignment public TextAlignment HorizontalTextAlignment { get; set; } public TextAlignment VerticalTextAlignment { get; set; } - } - - // Ensures custom ITextAlignment interfaces are supported + } + + // Ensures custom ITextAlignment interfaces are supported interface ICustomTextAlignment : ITextAlignment { @@ -422,8 +461,8 @@ public class GenericPicker : where TA : notnull, ISomeInterface where TB : class where TC : struct - where TD : class, ISomeInterface, new() - //TE has no constraints + where TD : class, ISomeInterface, new() + //TE has no constraints where TF : notnull where TG : unmanaged where TH : ISomeInterface?