Skip to content

Commit 4a2c060

Browse files
committed
Added conditional compilation check for backing fields assertions in RelayCommand tests
NOTE: This conditional compilation is based on roslyn version, not C# version or target platform. This could hypothetically lead to reduced test coverage in the future.
1 parent 6a23140 commit 4a2c060

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ public void Test_ObservableProperty_WithExplicitAttributeForProperty()
10031003
Assert.AreEqual((Animal)67, testAttribute2.Animal);
10041004
}
10051005

1006+
#if !ROSLYN_4_12_0_OR_GREATER
10061007
// See https://github.com/CommunityToolkit/dotnet/issues/446
10071008
[TestMethod]
10081009
public void Test_ObservableProperty_CommandNamesThatCantBeLowered()
@@ -1021,6 +1022,7 @@ public void Test_ObservableProperty_CommandNamesThatCantBeLowered()
10211022

10221023
Assert.AreSame(model.c中文Command, fieldInfo?.GetValue(model));
10231024
}
1025+
#endif
10241026

10251027
// See https://github.com/CommunityToolkit/dotnet/issues/375
10261028
[TestMethod]

tests/CommunityToolkit.Mvvm.UnitTests/Test_RelayCommandAttribute.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ public void Test_RelayCommandAttribute_CanExecuteWithNullabilityAnnotations()
571571
[TestMethod]
572572
public void Test_RelayCommandAttribute_WithExplicitAttributesForFieldAndProperty()
573573
{
574+
#if !ROSLYN_4_12_0_OR_GREATER
574575
FieldInfo fooField = typeof(MyViewModelWithExplicitFieldAndPropertyAttributes).GetField("fooCommand", BindingFlags.Instance | BindingFlags.NonPublic)!;
575576

576577
Assert.IsNotNull(fooField.GetCustomAttribute<RequiredAttribute>());
@@ -579,6 +580,7 @@ public void Test_RelayCommandAttribute_WithExplicitAttributesForFieldAndProperty
579580
Assert.IsNotNull(fooField.GetCustomAttribute<MaxLengthAttribute>());
580581
Assert.AreEqual(100, fooField.GetCustomAttribute<MaxLengthAttribute>()!.Length);
581582

583+
#endif
582584
PropertyInfo fooProperty = typeof(MyViewModelWithExplicitFieldAndPropertyAttributes).GetProperty("FooCommand")!;
583585

584586
Assert.IsNotNull(fooProperty.GetCustomAttribute<RequiredAttribute>());
@@ -618,17 +620,21 @@ static void ValidateTestAttribute(TestValidationAttribute testAttribute)
618620
Assert.AreEqual(Test_ObservablePropertyAttribute.Animal.Llama, testAttribute.Animal);
619621
}
620622

623+
#if !ROSLYN_4_12_0_OR_GREATER
621624
FieldInfo fooBarField = typeof(MyViewModelWithExplicitFieldAndPropertyAttributes).GetField("fooBarCommand", BindingFlags.Instance | BindingFlags.NonPublic)!;
622625

623626
ValidateTestAttribute(fooBarField.GetCustomAttribute<TestValidationAttribute>()!);
627+
#endif
624628

625629
PropertyInfo fooBarProperty = typeof(MyViewModelWithExplicitFieldAndPropertyAttributes).GetProperty("FooBarCommand")!;
626630

627631
ValidateTestAttribute(fooBarProperty.GetCustomAttribute<TestValidationAttribute>()!);
628632

633+
#if !ROSLYN_4_12_0_OR_GREATER
629634
FieldInfo barBazField = typeof(MyViewModelWithExplicitFieldAndPropertyAttributes).GetField("barBazCommand", BindingFlags.Instance | BindingFlags.NonPublic)!;
630635

631636
Assert.IsNotNull(barBazField.GetCustomAttribute<Test_ObservablePropertyAttribute.TestAttribute>());
637+
#endif
632638

633639
PropertyInfo barBazCommand = typeof(MyViewModelWithExplicitFieldAndPropertyAttributes).GetProperty("BarBazCommand")!;
634640

@@ -670,23 +676,27 @@ public void Test_RelayCommandAttribute_WithPartialCommandMethodDefinitions()
670676
_ = Assert.IsInstanceOfType<RelayCommand>(model.BazCommand);
671677
_ = Assert.IsInstanceOfType<AsyncRelayCommand>(model.FooBarCommand);
672678

679+
#if !ROSLYN_4_12_0_OR_GREATER
673680
FieldInfo bazField = typeof(ModelWithPartialCommandMethods).GetField("bazCommand", BindingFlags.Instance | BindingFlags.NonPublic)!;
674681

675682
Assert.IsNotNull(bazField.GetCustomAttribute<RequiredAttribute>());
676683
Assert.IsNotNull(bazField.GetCustomAttribute<MinLengthAttribute>());
677684
Assert.AreEqual(1, bazField.GetCustomAttribute<MinLengthAttribute>()!.Length);
685+
#endif
678686

679687
PropertyInfo bazProperty = typeof(ModelWithPartialCommandMethods).GetProperty("BazCommand")!;
680688

681689
Assert.IsNotNull(bazProperty.GetCustomAttribute<MinLengthAttribute>());
682690
Assert.AreEqual(2, bazProperty.GetCustomAttribute<MinLengthAttribute>()!.Length);
683691
Assert.IsNotNull(bazProperty.GetCustomAttribute<XmlIgnoreAttribute>());
684692

693+
#if !ROSLYN_4_12_0_OR_GREATER
685694
FieldInfo fooBarField = typeof(ModelWithPartialCommandMethods).GetField("fooBarCommand", BindingFlags.Instance | BindingFlags.NonPublic)!;
686695

687696
Assert.IsNotNull(fooBarField.GetCustomAttribute<RequiredAttribute>());
688697
Assert.IsNotNull(fooBarField.GetCustomAttribute<MinLengthAttribute>());
689-
Assert.AreEqual(1, fooBarField.GetCustomAttribute<MinLengthAttribute>()!.Length);
698+
Assert.AreEqual(1, fooBarField.GetCustomAttribute<MinLengthAttribute>()!.Length);
699+
#endif
690700

691701
PropertyInfo fooBarProperty = typeof(ModelWithPartialCommandMethods).GetProperty("FooBarCommand")!;
692702

0 commit comments

Comments
 (0)