mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-02-23 13:46:00 +01:00
Improve diagnostics, add unit tests
This commit is contained in:
parent
171b89b0f0
commit
31918450ef
CommunityToolkit.Mvvm.SourceGenerators
tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests
@ -32,3 +32,4 @@ ### New Rules
|
||||
MVVMTK0023 | CommunityToolkit.Mvvm.SourceGenerators.ICommandGenerator | Error | See https://aka.ms/mvvmtoolkit/error
|
||||
MVVMTK0024 | CommunityToolkit.Mvvm.SourceGenerators.ICommandGenerator | Error | See https://aka.ms/mvvmtoolkit/error
|
||||
MVVMTK0025 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit/error
|
||||
MVVMTK0026 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit/error
|
||||
|
@ -152,7 +152,7 @@ internal static class Execute
|
||||
}
|
||||
}
|
||||
|
||||
// Log the diagnostics if needed
|
||||
// Log the diagnostic for missing ObservableValidator, if needed
|
||||
if (hasAnyValidationAttributes &&
|
||||
!fieldSymbol.ContainingType.InheritsFromFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableValidator"))
|
||||
{
|
||||
@ -164,6 +164,16 @@ internal static class Execute
|
||||
forwardedAttributes.Count);
|
||||
}
|
||||
|
||||
// Log the diagnostic for missing validation attributes, if any
|
||||
if (alsoValidateProperty && !hasAnyValidationAttributes)
|
||||
{
|
||||
builder.Add(
|
||||
MissingValidationAttributesForAlsoValidatePropertyError,
|
||||
fieldSymbol,
|
||||
fieldSymbol.ContainingType,
|
||||
fieldSymbol.Name);
|
||||
}
|
||||
|
||||
diagnostics = builder.ToImmutable();
|
||||
|
||||
return new(
|
||||
|
@ -409,6 +409,22 @@ internal static class DiagnosticDescriptors
|
||||
category: typeof(ObservablePropertyGenerator).FullName,
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "Cannot apply [AlsoValidateProperty] to field that are declared in a type that doesn't inherit from ObservableValidator.",
|
||||
description: "Cannot apply [AlsoValidateProperty] to fields that are declared in a type that doesn't inherit from ObservableValidator.",
|
||||
helpLinkUri: "https://aka.ms/mvvmtoolkit");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when the target field uses [AlsoValidateProperty] but has no validation attributes.
|
||||
/// <para>
|
||||
/// Format: <c>"The field {0}.{1} cannot be annotated with [AlsoValidateProperty], as it doesn't have any validation attributes to use during validation"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor MissingValidationAttributesForAlsoValidatePropertyError = new DiagnosticDescriptor(
|
||||
id: "MVVMTK0026",
|
||||
title: "Missing validation attributes",
|
||||
messageFormat: "The field {0}.{1} cannot be annotated with [AlsoValidateProperty], as it doesn't have any validation attributes to use during validation",
|
||||
category: typeof(ObservablePropertyGenerator).FullName,
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "Cannot apply [AlsoValidateProperty] to fields that don't have any validation attributes to use during validation.",
|
||||
helpLinkUri: "https://aka.ms/mvvmtoolkit");
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ public partial class SampleViewModel
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MissingObservableValidatorInheritanceError()
|
||||
public void MissingObservableValidatorInheritanceForValidationAttributeError()
|
||||
{
|
||||
string source = @"
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
@ -1188,6 +1188,48 @@ public partial class MyViewModel : ObservableObject
|
||||
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0024");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MissingObservableValidatorInheritanceForAlsoValidatePropertyError()
|
||||
{
|
||||
string source = @"
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace MyApp
|
||||
{
|
||||
[INotifyPropertyChanged]
|
||||
public partial class SampleViewModel
|
||||
{
|
||||
[ObservableProperty]
|
||||
[Required]
|
||||
[AlsoValidateProperty]
|
||||
private string name;
|
||||
}
|
||||
}";
|
||||
|
||||
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0006", "MVVMTK0025");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MissingValidationAttributesForAlsoValidatePropertyError()
|
||||
{
|
||||
string source = @"
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace MyApp
|
||||
{
|
||||
public partial class SampleViewModel : ObservableValidator
|
||||
{
|
||||
[ObservableProperty]
|
||||
[AlsoValidateProperty]
|
||||
private string name;
|
||||
}
|
||||
}";
|
||||
|
||||
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0026");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the output of a source generator.
|
||||
/// </summary>
|
||||
@ -1232,7 +1274,7 @@ from assembly in AppDomain.CurrentDomain.GetAssemblies()
|
||||
|
||||
HashSet<string> resultingIds = diagnostics.Select(diagnostic => diagnostic.Id).ToHashSet();
|
||||
|
||||
Assert.IsTrue(resultingIds.SetEquals(diagnosticsIds));
|
||||
CollectionAssert.AreEquivalent(diagnosticsIds, resultingIds.ToArray());
|
||||
|
||||
GC.KeepAlive(observableObjectType);
|
||||
GC.KeepAlive(validationAttributeType);
|
||||
|
Loading…
Reference in New Issue
Block a user