Skip to content

Commit e31a088

Browse files
committed
Add unit tests for attributes stripping
1 parent 350c237 commit e31a088

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

dotnet Community Toolkit.sln

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityToolkit.Mvvm.Disab
7474
EndProject
7575
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Mvvm.Internals.UnitTests", "tests\CommunityToolkit.Mvvm.Internals.UnitTests\CommunityToolkit.Mvvm.Internals.UnitTests.csproj", "{743D74BA-12AE-4639-AD77-B9DDA9C03255}"
7676
EndProject
77+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Mvvm.KeepSourceGeneratorAttributes.UnitTests", "tests\CommunityToolkit.Mvvm.KeepSourceGeneratorAttributes.UnitTests\CommunityToolkit.Mvvm.KeepSourceGeneratorAttributes.UnitTests.csproj", "{AFB55517-3B50-47E3-B9CA-F1C2770479B6}"
78+
EndProject
7779
Global
7880
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7981
Debug|Any CPU = Debug|Any CPU
@@ -348,6 +350,26 @@ Global
348350
{743D74BA-12AE-4639-AD77-B9DDA9C03255}.Release|x64.Build.0 = Release|Any CPU
349351
{743D74BA-12AE-4639-AD77-B9DDA9C03255}.Release|x86.ActiveCfg = Release|Any CPU
350352
{743D74BA-12AE-4639-AD77-B9DDA9C03255}.Release|x86.Build.0 = Release|Any CPU
353+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
354+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
355+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|ARM.ActiveCfg = Debug|Any CPU
356+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|ARM.Build.0 = Debug|Any CPU
357+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|ARM64.ActiveCfg = Debug|Any CPU
358+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|ARM64.Build.0 = Debug|Any CPU
359+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|x64.ActiveCfg = Debug|Any CPU
360+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|x64.Build.0 = Debug|Any CPU
361+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|x86.ActiveCfg = Debug|Any CPU
362+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Debug|x86.Build.0 = Debug|Any CPU
363+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
364+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|Any CPU.Build.0 = Release|Any CPU
365+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|ARM.ActiveCfg = Release|Any CPU
366+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|ARM.Build.0 = Release|Any CPU
367+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|ARM64.ActiveCfg = Release|Any CPU
368+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|ARM64.Build.0 = Release|Any CPU
369+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|x64.ActiveCfg = Release|Any CPU
370+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|x64.Build.0 = Release|Any CPU
371+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|x86.ActiveCfg = Release|Any CPU
372+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6}.Release|x86.Build.0 = Release|Any CPU
351373
EndGlobalSection
352374
GlobalSection(SolutionProperties) = preSolution
353375
HideSolutionNode = FALSE
@@ -365,6 +387,7 @@ Global
365387
{6640D447-C28D-4DBB-91F4-3ADCE0CA64AD} = {B30036C4-D514-4E5B-A323-587A061772CE}
366388
{9E09DA49-4389-4ECE-8B68-EBDB1221DA90} = {6640D447-C28D-4DBB-91F4-3ADCE0CA64AD}
367389
{743D74BA-12AE-4639-AD77-B9DDA9C03255} = {B30036C4-D514-4E5B-A323-587A061772CE}
390+
{AFB55517-3B50-47E3-B9CA-F1C2770479B6} = {6640D447-C28D-4DBB-91F4-3ADCE0CA64AD}
368391
EndGlobalSection
369392
GlobalSection(ExtensibilityGlobals) = postSolution
370393
SolutionGuid = {5403B0C4-F244-4F73-A35C-FE664D0F4345}

tests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests/Test_DisableINotifyPropertyChanging.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using CommunityToolkit.Mvvm.ComponentModel;
88
using Microsoft.VisualStudio.TestTools.UnitTesting;
99

10-
namespace CommunityToolkit.Mvvm.UnitTests.Configuration;
10+
namespace CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests;
1111

1212
[TestClass]
1313
public class Test_DisableINotifyPropertyChanging
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net472;netcoreapp3.1;net6.0</TargetFrameworks>
5+
<DefineConstants>$(DefineConstants);MVVMTOOLKIT_KEEP_SOURCE_GENERATOR_ATTRIBUTES</DefineConstants>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
10+
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\CommunityToolkit.Mvvm\CommunityToolkit.Mvvm.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.ComponentModel;
6+
using System.Linq;
7+
using System.Reflection;
8+
using CommunityToolkit.Mvvm.ComponentModel;
9+
using CommunityToolkit.Mvvm.Input;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
namespace CommunityToolkit.Mvvm.KeepSourceGeneratorAttributes.UnitTests;
13+
14+
[TestClass]
15+
public class Test_SourceGeneratorAttributes
16+
{
17+
[TestMethod]
18+
public void Test_AttributeMetadata()
19+
{
20+
#if !MVVMTOOLKIT_KEEP_SOURCE_GENERATOR_ATTRIBUTES
21+
Assert.Fail();
22+
#endif
23+
Assert.AreEqual(3, typeof(TestModel).GetCustomAttributes().Count(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
24+
Assert.AreEqual(3, typeof(TestModel).GetField(nameof(TestModel.name))!.GetCustomAttributes().Count(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
25+
Assert.AreEqual(1, typeof(TestModel).GetMethod(nameof(TestModel.Test))!.GetCustomAttributes().Count(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
26+
}
27+
28+
[INotifyPropertyChanged]
29+
[ObservableObject]
30+
[ObservableRecipient]
31+
public class TestModel
32+
{
33+
[ObservableProperty]
34+
[AlsoNotifyChangeFor("")]
35+
[AlsoNotifyCanExecuteFor("")]
36+
public string? name;
37+
38+
[ICommand]
39+
public void Test()
40+
{
41+
}
42+
}
43+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.ComponentModel;
6+
using System.Linq;
7+
using System.Reflection;
8+
using CommunityToolkit.Mvvm.ComponentModel;
9+
using CommunityToolkit.Mvvm.Input;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
namespace CommunityToolkit.Mvvm.UnitTests;
13+
14+
[TestClass]
15+
public partial class Test_SourceGeneratorAttributes
16+
{
17+
[TestMethod]
18+
public void Test_AttributeMetadata()
19+
{
20+
#if MVVMTOOLKIT_KEEP_SOURCE_GENERATOR_ATTRIBUTES
21+
Assert.Fail();
22+
#endif
23+
Assert.IsFalse(typeof(INotifyPropertyChangedModel).GetCustomAttributes().Any(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
24+
Assert.IsFalse(typeof(TestModel).GetCustomAttributes().Any(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
25+
Assert.IsFalse(typeof(TestModel).GetField(nameof(TestModel.name))!.GetCustomAttributes().Any(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
26+
Assert.IsFalse(typeof(TestModel).GetMethod(nameof(TestModel.Test))!.GetCustomAttributes().Any(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
27+
Assert.IsFalse(typeof(ObservableRecipientModel).GetCustomAttributes().Any(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
28+
}
29+
30+
[INotifyPropertyChanged]
31+
public partial class INotifyPropertyChangedModel
32+
{
33+
}
34+
35+
[ObservableObject]
36+
public partial class TestModel
37+
{
38+
[ObservableProperty]
39+
[AlsoNotifyChangeFor(nameof(Name))]
40+
[AlsoNotifyCanExecuteFor(nameof(TestCommand))]
41+
public string? name;
42+
43+
[ICommand]
44+
public void Test()
45+
{
46+
}
47+
}
48+
49+
[ObservableRecipient]
50+
public partial class ObservableRecipientModel : ObservableObject
51+
{
52+
}
53+
}

0 commit comments

Comments
 (0)