Skip to content

Commit 873c1c3

Browse files
committed
Add unit tests for XML docs on generated property change args
1 parent 103ef3f commit 873c1c3

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,131 @@ public string? A
15381538
#endif
15391539
}
15401540

1541+
[TestMethod]
1542+
public void ObservableProperty_ValidateGeneratedCachedArguments()
1543+
{
1544+
string source = """
1545+
using System.ComponentModel;
1546+
using CommunityToolkit.Mvvm.ComponentModel;
1547+
1548+
namespace MyApp;
1549+
1550+
partial class MyViewModel : ObservableObject
1551+
{
1552+
[ObservableProperty]
1553+
private string? name;
1554+
}
1555+
""";
1556+
1557+
string result = """
1558+
// <auto-generated/>
1559+
#pragma warning disable
1560+
#nullable enable
1561+
namespace MyApp
1562+
{
1563+
/// <inheritdoc/>
1564+
partial class MyViewModel
1565+
{
1566+
/// <inheritdoc cref="name"/>
1567+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1568+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
1569+
public string? Name
1570+
{
1571+
get => name;
1572+
set
1573+
{
1574+
if (!global::System.Collections.Generic.EqualityComparer<string?>.Default.Equals(name, value))
1575+
{
1576+
OnNameChanging(value);
1577+
OnNameChanging(default, value);
1578+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
1579+
name = value;
1580+
OnNameChanged(value);
1581+
OnNameChanged(default, value);
1582+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
1583+
}
1584+
}
1585+
}
1586+
1587+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
1588+
/// <param name="value">The new property value being set.</param>
1589+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
1590+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1591+
partial void OnNameChanging(string? value);
1592+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
1593+
/// <param name="oldValue">The previous property value that is being replaced.</param>
1594+
/// <param name="newValue">The new property value being set.</param>
1595+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
1596+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1597+
partial void OnNameChanging(string? oldValue, string? newValue);
1598+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
1599+
/// <param name="value">The new property value that was set.</param>
1600+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
1601+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1602+
partial void OnNameChanged(string? value);
1603+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
1604+
/// <param name="oldValue">The previous property value that was replaced.</param>
1605+
/// <param name="newValue">The new property value that was set.</param>
1606+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
1607+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1608+
partial void OnNameChanged(string? oldValue, string? newValue);
1609+
}
1610+
}
1611+
""";
1612+
1613+
string changingArgs = """
1614+
// <auto-generated/>
1615+
#pragma warning disable
1616+
#nullable enable
1617+
namespace CommunityToolkit.Mvvm.ComponentModel.__Internals
1618+
{
1619+
/// <summary>
1620+
/// A helper type providing cached, reusable <see cref="global::System.ComponentModel.PropertyChangingEventArgs"/> instances
1621+
/// for all properties generated with <see cref="global::CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute"/>.
1622+
/// </summary>
1623+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1624+
[global::System.Diagnostics.DebuggerNonUserCode]
1625+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
1626+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
1627+
[global::System.Obsolete("This type is not intended to be used directly by user code")]
1628+
internal static class __KnownINotifyPropertyChangingArgs
1629+
{
1630+
/// <summary>The cached <see cref="global::System.ComponentModel.PropertyChangingEventArgs"/> instance for all "Name" generated properties.</summary>
1631+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
1632+
[global::System.Obsolete("This field is not intended to be referenced directly by user code")]
1633+
public static readonly global::System.ComponentModel.PropertyChangingEventArgs Name = new global::System.ComponentModel.PropertyChangingEventArgs("Name");
1634+
}
1635+
}
1636+
""";
1637+
1638+
string changedArgs = """
1639+
// <auto-generated/>
1640+
#pragma warning disable
1641+
#nullable enable
1642+
namespace CommunityToolkit.Mvvm.ComponentModel.__Internals
1643+
{
1644+
/// <summary>
1645+
/// A helper type providing cached, reusable <see cref="global::System.ComponentModel.PropertyChangedEventArgs"/> instances
1646+
/// for all properties generated with <see cref="global::CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute"/>.
1647+
/// </summary>
1648+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1649+
[global::System.Diagnostics.DebuggerNonUserCode]
1650+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
1651+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
1652+
[global::System.Obsolete("This type is not intended to be used directly by user code")]
1653+
internal static class __KnownINotifyPropertyChangedArgs
1654+
{
1655+
/// <summary>The cached <see cref="global::System.ComponentModel.PropertyChangedEventArgs"/> instance for all "Name" generated properties.</summary>
1656+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
1657+
[global::System.Obsolete("This field is not intended to be referenced directly by user code")]
1658+
public static readonly global::System.ComponentModel.PropertyChangedEventArgs Name = new global::System.ComponentModel.PropertyChangedEventArgs("Name");
1659+
}
1660+
}
1661+
""";
1662+
1663+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result), ("__KnownINotifyPropertyChangingArgs.g.cs", changingArgs), ("__KnownINotifyPropertyChangedArgs.g.cs", changedArgs));
1664+
}
1665+
15411666
[TestMethod]
15421667
public void RelayCommandMethodInTopLevelTypeWithNoNamespace_PreservesInheritdoc()
15431668
{

0 commit comments

Comments
 (0)