Skip to content

Commit 0ea77c3

Browse files
committed
Add codegen test for filename hints on Roslyn 4.0.1
1 parent 030c3b3 commit 0ea77c3

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Extensions/SourceProductionContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void AddSource(this SourceProductionContext context, string name,
2424
#if !ROSLYN_4_3_1_OR_GREATER
2525
// We're fine with the extra allocation in the few cases where adjusting the filename is necessary.
2626
// This will only ever be done when code generation is executed again anyway, which is a slow path.
27-
name = name.Replace('+', '_').Replace('`', '_');
27+
name = name.Replace('+', '.').Replace('`', '_');
2828
#endif
2929

3030
// Add the UTF8 text for the input compilation unit

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn401.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn401.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest" Version="1.1.1" />
99
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.1" />
1010
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest" Version="1.1.1" />
11-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
11+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.1" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
1313
<PackageReference Include="MSTest.TestAdapter" Version="3.0.1" />
1414
<PackageReference Include="MSTest.TestFramework" Version="3.0.1" />

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn431.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn431.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net472;net6.0;net7.0</TargetFrameworks>
5+
<DefineConstants>$(DefineConstants);ROSLYN_4_3_1_OR_GREATER</DefineConstants>
56
</PropertyGroup>
67

78
<ItemGroup>

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

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,92 @@ public object? A
193193
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
194194
}
195195

196+
[TestMethod]
197+
public void ObservablePropertyWithinGenericAndNestedTypes()
198+
{
199+
string source = """
200+
using System.ComponentModel;
201+
using CommunityToolkit.Mvvm.ComponentModel;
202+
203+
#nullable enable
204+
205+
namespace MyApp;
206+
207+
partial class Foo
208+
{
209+
partial class MyViewModel<T> : ObservableObject
210+
{
211+
[ObservableProperty]
212+
private string? a;
213+
}
214+
}
215+
""";
216+
217+
string result = """
218+
// <auto-generated/>
219+
#pragma warning disable
220+
#nullable enable
221+
namespace MyApp
222+
{
223+
partial class Foo
224+
{
225+
partial class MyViewModel<T>
226+
{
227+
/// <inheritdoc cref="a"/>
228+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
229+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
230+
public string? A
231+
{
232+
get => a;
233+
set
234+
{
235+
if (!global::System.Collections.Generic.EqualityComparer<string?>.Default.Equals(a, value))
236+
{
237+
OnAChanging(value);
238+
OnAChanging(default, value);
239+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.A);
240+
a = value;
241+
OnAChanged(value);
242+
OnAChanged(default, value);
243+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.A);
244+
}
245+
}
246+
}
247+
248+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
249+
/// <param name="value">The new property value being set.</param>
250+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
251+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
252+
partial void OnAChanging(string? value);
253+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
254+
/// <param name="oldValue">The previous property value that is being replaced.</param>
255+
/// <param name="newValue">The new property value being set.</param>
256+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
257+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
258+
partial void OnAChanging(string? oldValue, string? newValue);
259+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
260+
/// <param name="value">The new property value that was set.</param>
261+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
262+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
263+
partial void OnAChanged(string? value);
264+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
265+
/// <param name="oldValue">The previous property value that was replaced.</param>
266+
/// <param name="newValue">The new property value that was set.</param>
267+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
268+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
269+
partial void OnAChanged(string? oldValue, string? newValue);
270+
}
271+
}
272+
}
273+
""";
274+
275+
#if ROSLYN_4_3_1_OR_GREATER
276+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.Foo+MyViewModel`1.g.cs", result));
277+
#else
278+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.Foo.MyViewModel_1.g.cs", result));
279+
#endif
280+
}
281+
196282
/// <summary>
197283
/// Generates the requested sources
198284
/// </summary>
@@ -212,7 +298,7 @@ from assembly in AppDomain.CurrentDomain.GetAssemblies()
212298
let reference = MetadataReference.CreateFromFile(assembly.Location)
213299
select reference;
214300

215-
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp11));
301+
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp10));
216302

217303
// Create a syntax tree with the input source
218304
CSharpCompilation compilation = CSharpCompilation.Create(

0 commit comments

Comments
 (0)