Skip to content

Commit a77c100

Browse files
Update [BindableProperty] Support for Additional Scenarios, Add CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests, Implement [BindableProperty] for Behaviors (#2932)
* Update IconTintColorBehavior.shared.cs * Update ImageTouchBehavior.shared.cs * Add `new` keyword functionality to `BindablePropertyAttributeSourceGenerator` * Update BindablePropertyAttributeSourceGenerator.cs * Update StatusBarBehavior * Properly Sanitize ReturnType and DeclaringType * Return correct string for boolean operators * Update StatusBarBehavior * Update TouchBehavior * Return correct string for boolean operators * Update Expander.shared.cs * Add `GetEnumValueByNameAsString` * Fix`GetEnumValueByNameAsString` * Finish TouchBehavior * Finish Behaviors * Finish Behaviors * Fix Declaring Type * Fix ReturnType * Fix Enum Support * Add `PolySharp` * Add `BindablePropertyModel .BindablePropertyName ` * Fix Enum Support * Add `BindablePropertyModel .BindablePropertyName ` * Ensure Floating Point Types include a decimal * Fix TouchBehaviorTests * Fix TouchBehaviorTests * Add `GetNamedTypeArgumentsAttributeValueByNameAsCastedString` and `GetNamedMethodGroupArgumentsAttributeValueByNameAsString` * Fix ProgressBar * `dotnet format` * `dotnet format` * Add `CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests.csproj` * Update to Microsoft.CodeAnalysis.Testing.ReferenceAssemblies.Net.NET100 * Update CommunityToolkit.Maui.slnx * Add ReferenceAssembliesExtensions extension * Update ReferenceAssembliesExtensions.cs * `dotnet format` * Create ReferenceAssembliesExtensions.cs * Fix `Microsoft.CodeAnalysis.Testing.ReferenceAssemblies.Net.NET100` * Update NuGet Packages * Add `global::` * Add `global::` * GenerateBindableProperty_SimpleExample_GeneratesCorrectCode Passing * Add support for `SpecialType.System_String` * Finish BindablePropertyAttributeSourceGeneratorTests * Add Support for Keywords * Update Namings * Update BindablePropertyAttributeSourceGenerator.cs * Add support for global namespaces * Add support for global namespaces * `dotnet format` * Finish Edge Cases * Use constants for class and namespaces * Complete GenerateBindableProperty_ComplexInheritanceScenario_GeneratesCorrectCode * Complete GenerateBindableProperty_MultipleClassesInSameFile_GeneratesCorrectCode * Complete GenerateBindableProperty_WithCustomTypes_GeneratesCorrectCode * Update IntegrationTests.cs * Add support for nested classes * Add support for nested classes * Add support for genericclasses * Update dotnet-build.yml * Update csproj * Update csproj * Update csproj * Use `Path.Combine` for macos support * Fix support for nested classes * Update BindablePropertyAttributeSourceGenerator.cs * Revert "Update BindablePropertyAttributeSourceGenerator.cs" This reverts commit 1f97d27. * Normalize Line Endings in Unit Test for Windows vs macOS * Revert "Normalize Line Endings in Unit Test for Windows vs macOS" This reverts commit d5f68e7. * Only run Internal Source Generator Unit Tests on Windows
1 parent 1873607 commit a77c100

File tree

39 files changed

+2118
-1383
lines changed

39 files changed

+2118
-1383
lines changed

.github/workflows/dotnet-build.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ env:
4242
PathToCommunityToolkitCameraAnalyzersCodeFixCsproj: 'src/CommunityToolkit.Maui.Camera.Analyzers.CodeFixes/CommunityToolkit.Maui.Camera.Analyzers.CodeFixes.csproj'
4343
PathToCommunityToolkitMediaElementAnalyzersCodeFixCsproj: 'src/CommunityToolkit.Maui.MediaElement.Analyzers.CodeFixes/CommunityToolkit.Maui.MediaElement.Analyzers.CodeFixes.csproj'
4444
PathToCommunityToolkitAnalyzersUnitTestProjectDirectory: 'src/CommunityToolkit.Maui.Analyzers.UnitTests'
45+
PathToCommunityToolkitSourceGeneratorsInternalUnitTestDirectory: 'src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests'
46+
PathToCommunityToolkitSourceGeneratorsInternalUnitTestCsproj: 'src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests.csproj'
4547
PathToCommunityToolkitAnalyzersBenchmarkCsproj: 'src/CommunityToolkit.Maui.Analyzers.Benchmarks/CommunityToolkit.Maui.Analyzers.Benchmarks.csproj'
4648
CommunityToolkitLibrary_Xcode_Version: '26.1'
4749

@@ -209,6 +211,12 @@ jobs:
209211
run: |
210212
cd ${{ env.PathToCommunityToolkitAnalyzersUnitTestProjectDirectory }}
211213
dotnet run -c Release --results-directory "${{ runner.temp }}" --coverage --coverage-output "${{ runner.temp }}/ut-analyzers.cobertura.xml" --coverage-output-format cobertura --report-xunit
214+
215+
- name: Run CommunityToolkit Source Generators Internal UnitTests
216+
if: runner.os == 'Windows'
217+
run: |
218+
cd ${{ env.PathToCommunityToolkitSourceGeneratorsInternalUnitTestDirectory }}
219+
dotnet run -c Release --results-directory "${{ runner.temp }}" --coverage --coverage-output "${{ runner.temp }}/ut-sourcegenerators-internal.cobertura.xml" --coverage-output-format cobertura --report-xunit
212220
213221
- name: Run CommunityToolkit UnitTests
214222
run: |

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<SupportedPlatform Include="Tizen" />
236236
<InternalsVisibleTo Include="CommunityToolkit.Maui.UnitTests"/>
237237
<InternalsVisibleTo Include="CommunityToolkit.Maui.Analyzers.UnitTests"/>
238+
<InternalsVisibleTo Include="CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests"/>
238239
</ItemGroup>
239240

240241
<PropertyGroup Condition="('$(TF_BUILD)' == 'true' OR '$(GITHUB_ACTIONS)' == 'true') and $([MSBuild]::IsOSPlatform('windows')) == 'true'">

samples/CommunityToolkit.Maui.Sample.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
</Folder>
2828
<Folder Name="/Tests/">
2929
<Project Path="../src/CommunityToolkit.Maui.Analyzers.UnitTests/CommunityToolkit.Maui.Analyzers.UnitTests.csproj" />
30+
<Project Path="../src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests.csproj" />
3031
<Project Path="../src/CommunityToolkit.Maui.UnitTests/CommunityToolkit.Maui.UnitTests.csproj">
3132
<Deploy />
3233
</Project>

src/CommunityToolkit.Maui.Analyzers.UnitTests/CommunityToolkit.Maui.Analyzers.UnitTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GF</CompilerGeneratedFilesOutputPath>
77
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
88
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
9-
9+
<IsPackable>false</IsPackable>
10+
<IsTestProject>true</IsTestProject>
1011
<OutputType>Exe</OutputType>
1112
<RootNamespace>CommunityToolkit.Maui.Analyzers.UnitTests</RootNamespace>
1213
</PropertyGroup>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CommunityToolkit.Maui.Core;
2+
3+
static class ProgressBarAnimationBehaviorDefaults
4+
{
5+
public const double Progress = 0.0;
6+
public const uint Length = 500;
7+
public static Easing Easing { get; } = Easing.Linear;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CommunityToolkit.Maui.Core;
2+
3+
static class UserStoppedTypingBehaviorDefaults
4+
{
5+
public const int StoppedTypingTimeThreshold = 1000;
6+
public const int MinimumLengthThreshold = 0;
7+
public const bool ShouldDismissKeyboardAutomatically = false;
8+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp.Testing;
3+
using Microsoft.CodeAnalysis.Testing;
4+
using Xunit;
5+
6+
namespace CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests;
7+
8+
public abstract class BaseTest
9+
{
10+
protected static async Task VerifySourceGeneratorAsync(string source, string expectedAttribute, params List<(string FileName, string GeneratedFile)> expectedGenerated)
11+
{
12+
const string sourceGeneratorNamespace = "CommunityToolkit.Maui.SourceGenerators.Internal";
13+
const string bindablePropertyAttributeGeneratedFileName = "BindablePropertyAttribute.g.cs";
14+
var sourceGeneratorFullName = typeof(BindablePropertyAttributeSourceGenerator).FullName ?? throw new InvalidOperationException("Source Generator Type Path cannot be null");
15+
16+
var test = new CSharpSourceGeneratorTest<BindablePropertyAttributeSourceGenerator, DefaultVerifier>
17+
{
18+
#if NET10_0
19+
ReferenceAssemblies = Microsoft.CodeAnalysis.Testing.ReferenceAssemblies.Net.Net100,
20+
#else
21+
#error ReferenceAssemblies must be updated to current version of .NET
22+
#endif
23+
TestState =
24+
{
25+
Sources = { source },
26+
27+
AdditionalReferences =
28+
{
29+
MetadataReference.CreateFromFile(typeof(Microsoft.Maui.Controls.BindableObject).Assembly.Location),
30+
MetadataReference.CreateFromFile(typeof(Microsoft.Maui.Controls.BindableProperty).Assembly.Location),
31+
MetadataReference.CreateFromFile(typeof(Microsoft.Maui.Controls.BindingMode).Assembly.Location)
32+
}
33+
}
34+
};
35+
36+
var expectedAttributeText = Microsoft.CodeAnalysis.Text.SourceText.From(expectedAttribute, System.Text.Encoding.UTF8);
37+
var bindablePropertyAttributeFilePath = Path.Combine(sourceGeneratorNamespace, sourceGeneratorFullName, bindablePropertyAttributeGeneratedFileName);
38+
test.TestState.GeneratedSources.Add((bindablePropertyAttributeFilePath, expectedAttributeText));
39+
40+
foreach (var generatedFile in expectedGenerated.Where(static x => !string.IsNullOrEmpty(x.GeneratedFile)))
41+
{
42+
var expectedGeneratedText = Microsoft.CodeAnalysis.Text.SourceText.From(generatedFile.GeneratedFile, System.Text.Encoding.UTF8);
43+
var generatedFilePath = Path.Combine(sourceGeneratorNamespace, sourceGeneratorFullName, generatedFile.FileName);
44+
test.TestState.GeneratedSources.Add((generatedFilePath, expectedGeneratedText));
45+
}
46+
47+
await test.RunAsync(TestContext.Current.CancellationToken);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests.BindablePropertyAttributeSourceGeneratorTests;
2+
3+
public class BaseBindablePropertyAttributeSourceGeneratorTest : BaseTest
4+
{
5+
protected const string defaultTestClassName = "TestView";
6+
protected const string defaultTestNamespace = "TestNamespace";
7+
8+
protected const string expectedAttribute =
9+
/* language=C#-test */
10+
//lang=csharp
11+
"""
12+
// <auto-generated>
13+
// See: CommunityToolkit.Maui.SourceGenerators.Internal.BindablePropertyAttributeSourceGenerator
14+
15+
#pragma warning disable
16+
#nullable enable
17+
namespace CommunityToolkit.Maui;
18+
19+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
20+
[global::System.AttributeUsage(global::System.AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
21+
sealed partial class BindablePropertyAttribute : global::System.Attribute
22+
{
23+
public string? PropertyName { get; }
24+
public global::System.Type? DeclaringType { get; set; }
25+
public object? DefaultValue { get; set; }
26+
public global::Microsoft.Maui.Controls.BindingMode DefaultBindingMode { get; set; }
27+
public string ValidateValueMethodName { get; set; } = string.Empty;
28+
public string PropertyChangedMethodName { get; set; } = string.Empty;
29+
public string PropertyChangingMethodName { get; set; } = string.Empty;
30+
public string CoerceValueMethodName { get; set; } = string.Empty;
31+
public string DefaultValueCreatorMethodName { get; set; } = string.Empty;
32+
}
33+
""";
34+
35+
protected static Task VerifySourceGeneratorAsync(string source, string expectedGenerated) =>
36+
VerifySourceGeneratorAsync(source, expectedAttribute, ($"{defaultTestClassName}.g.cs", expectedGenerated));
37+
}

0 commit comments

Comments
 (0)