Skip to content

Commit

Permalink
Add unit tests for new analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 11, 2022
1 parent 98a5233 commit 5fd2c8a
Showing 1 changed file with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,42 @@ private void GreetUser(User user)
VerifyGeneratedDiagnostics<RelayCommandGenerator>(source, "MVVMTK0031");
}

[TestMethod]
public async Task UsingINotifyPropertyChangedAttributeOnClassWithNoBaseType()
{
string source = """
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp
{
[INotifyPropertyChanged]
public partial class {|MVVMTK0032:SampleViewModel|}
{
}
}
""";

await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<ClassUsingAttributeInsteadOfInheritanceAnalyzer>(source, LanguageVersion.CSharp8);
}

[TestMethod]
public async Task UsingObservableObjectAttributeOnClassWithNoBaseType()
{
string source = """
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp
{
[ObservableObject]
public partial class {|MVVMTK0033:SampleViewModel|}
{
}
}
""";

await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<ClassUsingAttributeInsteadOfInheritanceAnalyzer>(source, LanguageVersion.CSharp8);
}

/// <summary>
/// Verifies the diagnostic errors for a given analyzer, and that all available source generators can run successfully with the input source (including subsequent compilation).
/// </summary>
Expand Down Expand Up @@ -1538,20 +1574,6 @@ private static void VerifyGeneratedDiagnostics<TGenerator>(string source, params
VerifyGeneratedDiagnostics(CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)), new[] { generator }, diagnosticsIds);
}

/// <summary>
/// Verifies the output of a source generator.
/// </summary>
/// <typeparam name="TGenerator">The generator type to use.</typeparam>
/// <param name="syntaxTree">The input source to process.</param>
/// <param name="diagnosticsIds">The diagnostic ids to expect for the input source code.</param>
private static void VerifyGeneratedDiagnostics<TGenerator>(SyntaxTree syntaxTree, params string[] diagnosticsIds)
where TGenerator : class, IIncrementalGenerator, new()
{
IIncrementalGenerator generator = new TGenerator();

VerifyGeneratedDiagnostics(syntaxTree, new[] { generator }, diagnosticsIds);
}

/// <summary>
/// Verifies the output of one or more source generators.
/// </summary>
Expand Down

0 comments on commit 5fd2c8a

Please sign in to comment.