Skip to content

Commit e3980a7

Browse files
committed
Add unit tests for new analyzer
1 parent 88a56f4 commit e3980a7

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,42 @@ private void GreetUser(User user)
14951495
VerifyGeneratedDiagnostics<RelayCommandGenerator>(source, "MVVMTK0031");
14961496
}
14971497

1498+
[TestMethod]
1499+
public async Task UsingINotifyPropertyChangedAttributeOnClassWithNoBaseType()
1500+
{
1501+
string source = """
1502+
using CommunityToolkit.Mvvm.ComponentModel;
1503+
1504+
namespace MyApp
1505+
{
1506+
[INotifyPropertyChanged]
1507+
public partial class {|MVVMTK0032:SampleViewModel|}
1508+
{
1509+
}
1510+
}
1511+
""";
1512+
1513+
await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<ClassUsingAttributeInsteadOfInheritanceAnalyzer>(source, LanguageVersion.CSharp8);
1514+
}
1515+
1516+
[TestMethod]
1517+
public async Task UsingObservableObjectAttributeOnClassWithNoBaseType()
1518+
{
1519+
string source = """
1520+
using CommunityToolkit.Mvvm.ComponentModel;
1521+
1522+
namespace MyApp
1523+
{
1524+
[ObservableObject]
1525+
public partial class {|MVVMTK0033:SampleViewModel|}
1526+
{
1527+
}
1528+
}
1529+
""";
1530+
1531+
await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration<ClassUsingAttributeInsteadOfInheritanceAnalyzer>(source, LanguageVersion.CSharp8);
1532+
}
1533+
14981534
/// <summary>
14991535
/// Verifies the diagnostic errors for a given analyzer, and that all available source generators can run successfully with the input source (including subsequent compilation).
15001536
/// </summary>
@@ -1538,20 +1574,6 @@ private static void VerifyGeneratedDiagnostics<TGenerator>(string source, params
15381574
VerifyGeneratedDiagnostics(CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)), new[] { generator }, diagnosticsIds);
15391575
}
15401576

1541-
/// <summary>
1542-
/// Verifies the output of a source generator.
1543-
/// </summary>
1544-
/// <typeparam name="TGenerator">The generator type to use.</typeparam>
1545-
/// <param name="syntaxTree">The input source to process.</param>
1546-
/// <param name="diagnosticsIds">The diagnostic ids to expect for the input source code.</param>
1547-
private static void VerifyGeneratedDiagnostics<TGenerator>(SyntaxTree syntaxTree, params string[] diagnosticsIds)
1548-
where TGenerator : class, IIncrementalGenerator, new()
1549-
{
1550-
IIncrementalGenerator generator = new TGenerator();
1551-
1552-
VerifyGeneratedDiagnostics(syntaxTree, new[] { generator }, diagnosticsIds);
1553-
}
1554-
15551577
/// <summary>
15561578
/// Verifies the output of one or more source generators.
15571579
/// </summary>

0 commit comments

Comments
 (0)