Skip to content

Commit 73e7a88

Browse files
committed
Add unit test for required properties
1 parent e82074f commit 73e7a88

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,82 @@ partial int Number
9292
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, LanguageVersion.Preview, ("MyApp.MyViewModel.g.cs", result));
9393
}
9494

95+
// See https://github.com/CommunityToolkit/dotnet/issues/969
96+
[TestMethod]
97+
public void ObservablePropertyWithValueType_OnPartialProperty_RequiredProperty_WorksCorrectly()
98+
{
99+
string source = """
100+
using System;
101+
using CommunityToolkit.Mvvm.ComponentModel;
102+
103+
namespace MyApp;
104+
105+
partial class MyViewModel : ObservableObject
106+
{
107+
[ObservableProperty]
108+
public required partial int Number { get; private set; }
109+
}
110+
""";
111+
112+
string result = """
113+
// <auto-generated/>
114+
#pragma warning disable
115+
#nullable enable
116+
namespace MyApp
117+
{
118+
/// <inheritdoc/>
119+
partial class MyViewModel
120+
{
121+
/// <inheritdoc/>
122+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
123+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
124+
public required partial int Number
125+
{
126+
get => field;
127+
private set
128+
{
129+
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(field, value))
130+
{
131+
OnNumberChanging(value);
132+
OnNumberChanging(default, value);
133+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Number);
134+
field = value;
135+
OnNumberChanged(value);
136+
OnNumberChanged(default, value);
137+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Number);
138+
}
139+
}
140+
}
141+
142+
/// <summary>Executes the logic for when <see cref="Number"/> is changing.</summary>
143+
/// <param name="value">The new property value being set.</param>
144+
/// <remarks>This method is invoked right before the value of <see cref="Number"/> is changed.</remarks>
145+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
146+
partial void OnNumberChanging(int value);
147+
/// <summary>Executes the logic for when <see cref="Number"/> is changing.</summary>
148+
/// <param name="oldValue">The previous property value that is being replaced.</param>
149+
/// <param name="newValue">The new property value being set.</param>
150+
/// <remarks>This method is invoked right before the value of <see cref="Number"/> is changed.</remarks>
151+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
152+
partial void OnNumberChanging(int oldValue, int newValue);
153+
/// <summary>Executes the logic for when <see cref="Number"/> just changed.</summary>
154+
/// <param name="value">The new property value that was set.</param>
155+
/// <remarks>This method is invoked right after the value of <see cref="Number"/> is changed.</remarks>
156+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
157+
partial void OnNumberChanged(int value);
158+
/// <summary>Executes the logic for when <see cref="Number"/> just changed.</summary>
159+
/// <param name="oldValue">The previous property value that was replaced.</param>
160+
/// <param name="newValue">The new property value that was set.</param>
161+
/// <remarks>This method is invoked right after the value of <see cref="Number"/> is changed.</remarks>
162+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
163+
partial void OnNumberChanged(int oldValue, int newValue);
164+
}
165+
}
166+
""";
167+
168+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, LanguageVersion.Preview, ("MyApp.MyViewModel.g.cs", result));
169+
}
170+
95171
[TestMethod]
96172
public void ObservablePropertyWithValueType_OnPartialProperty_WithExplicitModifiers_WorksCorrectly1()
97173
{

0 commit comments

Comments
 (0)