Skip to content

Commit 84476ce

Browse files
committed
Add unit tests for new field escaping logic
1 parent 92185e0 commit 84476ce

File tree

1 file changed

+307
-0
lines changed

1 file changed

+307
-0
lines changed

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

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,313 @@ partial class MyViewModel
20602060
VerifyGenerateSources(source, new[] { new RelayCommandGenerator() }, ("MyViewModel.Test.g.cs", result));
20612061
}
20622062

2063+
[TestMethod]
2064+
public void ObservableProperty_AnnotatedFieldHasValueIdentifier()
2065+
{
2066+
string source = """
2067+
using CommunityToolkit.Mvvm.ComponentModel;
2068+
2069+
namespace MyApp;
2070+
2071+
partial class MyViewModel : ObservableObject
2072+
{
2073+
[ObservableProperty]
2074+
double value;
2075+
}
2076+
""";
2077+
2078+
string result = """
2079+
// <auto-generated/>
2080+
#pragma warning disable
2081+
#nullable enable
2082+
namespace MyApp
2083+
{
2084+
/// <inheritdoc/>
2085+
partial class MyViewModel
2086+
{
2087+
/// <inheritdoc cref="value"/>
2088+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2089+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2090+
public double Value
2091+
{
2092+
get => value;
2093+
set
2094+
{
2095+
if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(this.value, value))
2096+
{
2097+
OnValueChanging(value);
2098+
OnValueChanging(default, value);
2099+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Value);
2100+
this.value = value;
2101+
OnValueChanged(value);
2102+
OnValueChanged(default, value);
2103+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Value);
2104+
}
2105+
}
2106+
}
2107+
2108+
/// <summary>Executes the logic for when <see cref="Value"/> is changing.</summary>
2109+
/// <param name="value">The new property value being set.</param>
2110+
/// <remarks>This method is invoked right before the value of <see cref="Value"/> is changed.</remarks>
2111+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2112+
partial void OnValueChanging(double value);
2113+
/// <summary>Executes the logic for when <see cref="Value"/> is changing.</summary>
2114+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2115+
/// <param name="newValue">The new property value being set.</param>
2116+
/// <remarks>This method is invoked right before the value of <see cref="Value"/> is changed.</remarks>
2117+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2118+
partial void OnValueChanging(double oldValue, double newValue);
2119+
/// <summary>Executes the logic for when <see cref="Value"/> just changed.</summary>
2120+
/// <param name="value">The new property value that was set.</param>
2121+
/// <remarks>This method is invoked right after the value of <see cref="Value"/> is changed.</remarks>
2122+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2123+
partial void OnValueChanged(double value);
2124+
/// <summary>Executes the logic for when <see cref="Value"/> just changed.</summary>
2125+
/// <param name="oldValue">The previous property value that was replaced.</param>
2126+
/// <param name="newValue">The new property value that was set.</param>
2127+
/// <remarks>This method is invoked right after the value of <see cref="Value"/> is changed.</remarks>
2128+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2129+
partial void OnValueChanged(double oldValue, double newValue);
2130+
}
2131+
}
2132+
""";
2133+
2134+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
2135+
}
2136+
2137+
[TestMethod]
2138+
public void ObservableProperty_AnnotatedFieldHasValueIdentifier_WithChangedMethods()
2139+
{
2140+
string source = """
2141+
using CommunityToolkit.Mvvm.ComponentModel;
2142+
2143+
namespace MyApp;
2144+
2145+
partial class MyViewModel : ObservableObject
2146+
{
2147+
[ObservableProperty]
2148+
double value;
2149+
2150+
partial void OnValueChanged(double oldValue, double NewValue)
2151+
{
2152+
}
2153+
}
2154+
""";
2155+
2156+
string result = """
2157+
// <auto-generated/>
2158+
#pragma warning disable
2159+
#nullable enable
2160+
namespace MyApp
2161+
{
2162+
/// <inheritdoc/>
2163+
partial class MyViewModel
2164+
{
2165+
/// <inheritdoc cref="value"/>
2166+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2167+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2168+
public double Value
2169+
{
2170+
get => value;
2171+
set
2172+
{
2173+
if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(this.value, value))
2174+
{
2175+
double __oldValue = this.value;
2176+
OnValueChanging(value);
2177+
OnValueChanging(__oldValue, value);
2178+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Value);
2179+
this.value = value;
2180+
OnValueChanged(value);
2181+
OnValueChanged(__oldValue, value);
2182+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Value);
2183+
}
2184+
}
2185+
}
2186+
2187+
/// <summary>Executes the logic for when <see cref="Value"/> is changing.</summary>
2188+
/// <param name="value">The new property value being set.</param>
2189+
/// <remarks>This method is invoked right before the value of <see cref="Value"/> is changed.</remarks>
2190+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2191+
partial void OnValueChanging(double value);
2192+
/// <summary>Executes the logic for when <see cref="Value"/> is changing.</summary>
2193+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2194+
/// <param name="newValue">The new property value being set.</param>
2195+
/// <remarks>This method is invoked right before the value of <see cref="Value"/> is changed.</remarks>
2196+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2197+
partial void OnValueChanging(double oldValue, double newValue);
2198+
/// <summary>Executes the logic for when <see cref="Value"/> just changed.</summary>
2199+
/// <param name="value">The new property value that was set.</param>
2200+
/// <remarks>This method is invoked right after the value of <see cref="Value"/> is changed.</remarks>
2201+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2202+
partial void OnValueChanged(double value);
2203+
/// <summary>Executes the logic for when <see cref="Value"/> just changed.</summary>
2204+
/// <param name="oldValue">The previous property value that was replaced.</param>
2205+
/// <param name="newValue">The new property value that was set.</param>
2206+
/// <remarks>This method is invoked right after the value of <see cref="Value"/> is changed.</remarks>
2207+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2208+
partial void OnValueChanged(double oldValue, double newValue);
2209+
}
2210+
}
2211+
""";
2212+
2213+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
2214+
}
2215+
2216+
// See https://github.com/CommunityToolkit/dotnet/issues/710
2217+
[TestMethod]
2218+
public void ObservableProperty_AnnotatedFieldWithEscapedIdentifier()
2219+
{
2220+
string source = """
2221+
using CommunityToolkit.Mvvm.ComponentModel;
2222+
2223+
namespace MyApp;
2224+
2225+
partial class MyViewModel : ObservableObject
2226+
{
2227+
[ObservableProperty]
2228+
double @event;
2229+
}
2230+
""";
2231+
2232+
string result = """
2233+
// <auto-generated/>
2234+
#pragma warning disable
2235+
#nullable enable
2236+
namespace MyApp
2237+
{
2238+
/// <inheritdoc/>
2239+
partial class MyViewModel
2240+
{
2241+
/// <inheritdoc cref="@event"/>
2242+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2243+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2244+
public double Event
2245+
{
2246+
get => @event;
2247+
set
2248+
{
2249+
if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(@event, value))
2250+
{
2251+
OnEventChanging(value);
2252+
OnEventChanging(default, value);
2253+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Event);
2254+
@event = value;
2255+
OnEventChanged(value);
2256+
OnEventChanged(default, value);
2257+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Event);
2258+
}
2259+
}
2260+
}
2261+
2262+
/// <summary>Executes the logic for when <see cref="Event"/> is changing.</summary>
2263+
/// <param name="value">The new property value being set.</param>
2264+
/// <remarks>This method is invoked right before the value of <see cref="Event"/> is changed.</remarks>
2265+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2266+
partial void OnEventChanging(double value);
2267+
/// <summary>Executes the logic for when <see cref="Event"/> is changing.</summary>
2268+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2269+
/// <param name="newValue">The new property value being set.</param>
2270+
/// <remarks>This method is invoked right before the value of <see cref="Event"/> is changed.</remarks>
2271+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2272+
partial void OnEventChanging(double oldValue, double newValue);
2273+
/// <summary>Executes the logic for when <see cref="Event"/> just changed.</summary>
2274+
/// <param name="value">The new property value that was set.</param>
2275+
/// <remarks>This method is invoked right after the value of <see cref="Event"/> is changed.</remarks>
2276+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2277+
partial void OnEventChanged(double value);
2278+
/// <summary>Executes the logic for when <see cref="Event"/> just changed.</summary>
2279+
/// <param name="oldValue">The previous property value that was replaced.</param>
2280+
/// <param name="newValue">The new property value that was set.</param>
2281+
/// <remarks>This method is invoked right after the value of <see cref="Event"/> is changed.</remarks>
2282+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2283+
partial void OnEventChanged(double oldValue, double newValue);
2284+
}
2285+
}
2286+
""";
2287+
2288+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
2289+
}
2290+
2291+
[TestMethod]
2292+
public void ObservableProperty_AnnotatedFieldWithEscapedIdentifier_WithChangedMethods()
2293+
{
2294+
string source = """
2295+
using CommunityToolkit.Mvvm.ComponentModel;
2296+
2297+
namespace MyApp;
2298+
2299+
partial class MyViewModel : ObservableObject
2300+
{
2301+
[ObservableProperty]
2302+
double @object;
2303+
2304+
partial void OnObjectChanged(object oldValue, object NewValue)
2305+
{
2306+
}
2307+
}
2308+
""";
2309+
2310+
string result = """
2311+
// <auto-generated/>
2312+
#pragma warning disable
2313+
#nullable enable
2314+
namespace MyApp
2315+
{
2316+
/// <inheritdoc/>
2317+
partial class MyViewModel
2318+
{
2319+
/// <inheritdoc cref="@object"/>
2320+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2321+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2322+
public double Object
2323+
{
2324+
get => @object;
2325+
set
2326+
{
2327+
if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(@object, value))
2328+
{
2329+
double __oldValue = @object;
2330+
OnObjectChanging(value);
2331+
OnObjectChanging(__oldValue, value);
2332+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Object);
2333+
@object = value;
2334+
OnObjectChanged(value);
2335+
OnObjectChanged(__oldValue, value);
2336+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Object);
2337+
}
2338+
}
2339+
}
2340+
2341+
/// <summary>Executes the logic for when <see cref="Object"/> is changing.</summary>
2342+
/// <param name="value">The new property value being set.</param>
2343+
/// <remarks>This method is invoked right before the value of <see cref="Object"/> is changed.</remarks>
2344+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2345+
partial void OnObjectChanging(double value);
2346+
/// <summary>Executes the logic for when <see cref="Object"/> is changing.</summary>
2347+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2348+
/// <param name="newValue">The new property value being set.</param>
2349+
/// <remarks>This method is invoked right before the value of <see cref="Object"/> is changed.</remarks>
2350+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2351+
partial void OnObjectChanging(double oldValue, double newValue);
2352+
/// <summary>Executes the logic for when <see cref="Object"/> just changed.</summary>
2353+
/// <param name="value">The new property value that was set.</param>
2354+
/// <remarks>This method is invoked right after the value of <see cref="Object"/> is changed.</remarks>
2355+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2356+
partial void OnObjectChanged(double value);
2357+
/// <summary>Executes the logic for when <see cref="Object"/> just changed.</summary>
2358+
/// <param name="oldValue">The previous property value that was replaced.</param>
2359+
/// <param name="newValue">The new property value that was set.</param>
2360+
/// <remarks>This method is invoked right after the value of <see cref="Object"/> is changed.</remarks>
2361+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2362+
partial void OnObjectChanged(double oldValue, double newValue);
2363+
}
2364+
}
2365+
""";
2366+
2367+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
2368+
}
2369+
20632370
/// <summary>
20642371
/// Generates the requested sources
20652372
/// </summary>

0 commit comments

Comments
 (0)