Skip to content

Commit

Permalink
Add basic code fixer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 12, 2024
1 parent df50c2f commit 2ddde78
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
using static CommunityToolkit.GeneratedDependencyProperty.Diagnostics.DiagnosticDescriptors;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace CommunityToolkit.Mvvm.CodeFixers;
namespace CommunityToolkit.GeneratedDependencyProperty;

/// <summary>
/// A code fixer that converts manual properties into partial properties using <c>[GeneratedDependencytProperty]</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CommunityToolkit.DependencyPropertyGenerator.CodeFixers\CommunityToolkit.DependencyPropertyGenerator.CodeFixers.csproj" />
<ProjectReference Include="..\CommunityToolkit.DependencyPropertyGenerator.SourceGenerators\CommunityToolkit.DependencyPropertyGenerator.SourceGenerators.csproj" />
<ProjectReference Include="..\src\CommunityToolkit.WinUI.DependencyPropertyGenerator.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using CommunityToolkit.WinUI;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using CSharpCodeFixTest = CommunityToolkit.GeneratedDependencyProperty.Tests.Helpers.CSharpCodeFixTest<
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyAnalyzer,
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyCodeFixer>;
using VerifyCS = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier<
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyAnalyzer,
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyCodeFixer,
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;

namespace CommunityToolkit.GeneratedDependencyProperty.Tests;

[TestClass]
public class Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer
{
[TestMethod]
public async Task SimpleProperty()
{
const string original = """
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace MyApp;
public class MyControl : Control
{
public static readonly DependencyProperty NameProperty = DependencyProperty.Register(
name: nameof(Name),
propertyType: typeof(string),
ownerType: typeof(MyControl),
typeMetadata: null);
public string Name
{
get => (string)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}
}
""";

const string @fixed = """
using CommunityToolkit.WinUI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace MyApp;
public partial class MyControl : Control
{
[GeneratedDependencyProperty]
public partial string Name { get; set; }
}
""";

CSharpCodeFixTest test = new(LanguageVersion.Preview)
{
TestCode = original,
FixedCode = @fixed,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
TestState = { AdditionalReferences =
{
MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DependencyProperty).Assembly.Location),
MetadataReference.CreateFromFile(typeof(GeneratedDependencyPropertyAttribute).Assembly.Location)
}}
};

test.ExpectedDiagnostics.AddRange(
[
// /0/Test0.cs(14,19): info WCTDP0017: The manual property 'MyApp.MyControl.Name' can be converted to a partial property using [GeneratedDependencyProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)
VerifyCS.Diagnostic().WithSpan(14, 19, 14, 23).WithSpan(8, 5, 12, 29).WithArguments("MyApp.MyControl.Name"),
]);

test.FixedState.ExpectedDiagnostics.AddRange(
[
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.Name' must have an implementation part.
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 31).WithArguments("MyApp.SampleViewModel.Name"),
]);

await test.RunAsync();
}
}

0 comments on commit 2ddde78

Please sign in to comment.