Skip to content

Commit 01f07fe

Browse files
committed
Add unit tests for new attribute expression diagnostics
1 parent 51a0ebf commit 01f07fe

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,30 @@ public partial class MyViewModel : ObservableObject
16721672
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0035");
16731673
}
16741674

1675+
// See https://github.com/CommunityToolkit/dotnet/issues/683
1676+
[TestMethod]
1677+
public void InvalidPropertyTargetedAttributeOnObservablePropertyField_InvalidExpression()
1678+
{
1679+
string source = """
1680+
using System;
1681+
using CommunityToolkit.Mvvm.ComponentModel;
1682+
1683+
public partial class MyViewModel : ObservableObject
1684+
{
1685+
[ObservableProperty]
1686+
[property: Test(TestAttribute.M)]
1687+
private int number;
1688+
}
1689+
1690+
public class TestAttribute : Attribute
1691+
{
1692+
public static string M => "";
1693+
}
1694+
""";
1695+
1696+
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0037");
1697+
}
1698+
16751699
[TestMethod]
16761700
public void InvalidPropertyTargetedAttributeOnRelayCommandMethod_MissingUsingDirective()
16771701
{
@@ -1725,6 +1749,58 @@ private void Test()
17251749
VerifyGeneratedDiagnostics<RelayCommandGenerator>(source, "MVVMTK0036");
17261750
}
17271751

1752+
// See https://github.com/CommunityToolkit/dotnet/issues/683
1753+
[TestMethod]
1754+
public void InvalidPropertyTargetedAttributeOnRelayCommandMethod_InvalidExpressionOnFieldAttribute()
1755+
{
1756+
string source = """
1757+
using System;
1758+
using CommunityToolkit.Mvvm.Input;
1759+
1760+
public partial class MyViewModel
1761+
{
1762+
[RelayCommand]
1763+
[field: Test(TestAttribute.M)]
1764+
private void Test()
1765+
{
1766+
}
1767+
}
1768+
1769+
public class TestAttribute : Attribute
1770+
{
1771+
public static string M => "";
1772+
}
1773+
""";
1774+
1775+
VerifyGeneratedDiagnostics<RelayCommandGenerator>(source, "MVVMTK0038");
1776+
}
1777+
1778+
// See https://github.com/CommunityToolkit/dotnet/issues/683
1779+
[TestMethod]
1780+
public void InvalidPropertyTargetedAttributeOnRelayCommandMethod_InvalidExpressionOnPropertyAttribute()
1781+
{
1782+
string source = """
1783+
using System;
1784+
using CommunityToolkit.Mvvm.Input;
1785+
1786+
public partial class MyViewModel
1787+
{
1788+
[RelayCommand]
1789+
[property: Test(TestAttribute.M)]
1790+
private void Test()
1791+
{
1792+
}
1793+
}
1794+
1795+
public class TestAttribute : Attribute
1796+
{
1797+
public static string M => "";
1798+
}
1799+
""";
1800+
1801+
VerifyGeneratedDiagnostics<RelayCommandGenerator>(source, "MVVMTK0038");
1802+
}
1803+
17281804
/// <summary>
17291805
/// Verifies the diagnostic errors for a given analyzer, and that all available source generators can run successfully with the input source (including subsequent compilation).
17301806
/// </summary>

0 commit comments

Comments
 (0)