Skip to content

Commit 9c33591

Browse files
committed
Added unit tests for validation attributes generation
1 parent 02c87eb commit 9c33591

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.ComponentModel;
8+
using System.ComponentModel.DataAnnotations;
9+
using System.Configuration;
10+
using System.Reflection;
711
using Microsoft.Toolkit.Mvvm.ComponentModel;
812
using Microsoft.VisualStudio.TestTools.UnitTesting;
913

@@ -68,6 +72,50 @@ public void Test_AlsoNotifyForAttribute_Events()
6872
CollectionAssert.AreEqual(new[] { nameof(model.Name), nameof(model.FullName), nameof(model.Surname), nameof(model.FullName) }, propertyNames);
6973
}
7074

75+
[TestCategory("Mvvm")]
76+
[TestMethod]
77+
public void Test_ValidationAttributes()
78+
{
79+
var nameProperty = typeof(MyFormViewModel).GetProperty(nameof(MyFormViewModel.Name));
80+
81+
Assert.IsNotNull(nameProperty.GetCustomAttribute<RequiredAttribute>());
82+
Assert.IsNotNull(nameProperty.GetCustomAttribute<MinLengthAttribute>());
83+
Assert.AreEqual(nameProperty.GetCustomAttribute<MinLengthAttribute>().Length, 1);
84+
Assert.IsNotNull(nameProperty.GetCustomAttribute<MaxLengthAttribute>());
85+
Assert.AreEqual(nameProperty.GetCustomAttribute<MaxLengthAttribute>().Length, 100);
86+
87+
var ageProperty = typeof(MyFormViewModel).GetProperty(nameof(MyFormViewModel.Age));
88+
89+
Assert.IsNotNull(ageProperty.GetCustomAttribute<RangeAttribute>());
90+
Assert.AreEqual(ageProperty.GetCustomAttribute<RangeAttribute>().Minimum, 0);
91+
Assert.AreEqual(ageProperty.GetCustomAttribute<RangeAttribute>().Maximum, 120);
92+
93+
var emailProperty = typeof(MyFormViewModel).GetProperty(nameof(MyFormViewModel.Email));
94+
95+
Assert.IsNotNull(emailProperty.GetCustomAttribute<EmailAddressAttribute>());
96+
97+
var comboProperty = typeof(MyFormViewModel).GetProperty(nameof(MyFormViewModel.IfThisWorksThenThatsGreat));
98+
99+
TestValidationAttribute? testAttribute = comboProperty.GetCustomAttribute<TestValidationAttribute>();
100+
101+
Assert.IsNotNull(testAttribute);
102+
Assert.IsNull(testAttribute.O);
103+
Assert.AreEqual(testAttribute.T, typeof(SampleModel));
104+
Assert.AreEqual(testAttribute.Flag, true);
105+
Assert.AreEqual(testAttribute.D, 6.28);
106+
CollectionAssert.AreEqual(testAttribute.Names, new[] { "Bob", "Ross" });
107+
108+
object[] nestedArray = (object[])testAttribute.NestedArray;
109+
110+
Assert.AreEqual(nestedArray.Length, 3);
111+
Assert.AreEqual(nestedArray[0], 1);
112+
Assert.AreEqual(nestedArray[1], "Hello");
113+
Assert.IsTrue(nestedArray[2] is int[]);
114+
CollectionAssert.AreEqual((int[])nestedArray[2], new[] { 2, 3, 4 });
115+
116+
Assert.AreEqual(testAttribute.Animal, Animal.Llama);
117+
}
118+
71119
public partial class SampleModel : ObservableObject
72120
{
73121
/// <summary>
@@ -90,5 +138,59 @@ public sealed partial class DependentPropertyModel
90138

91139
public string FullName => $"{Name} {Surname}";
92140
}
141+
142+
public partial class MyFormViewModel : ObservableRecipient
143+
{
144+
[ObservableProperty]
145+
[Required]
146+
[MinLength(1)]
147+
[MaxLength(100)]
148+
private string name;
149+
150+
[ObservableProperty]
151+
[Range(0, 120)]
152+
private int age;
153+
154+
[ObservableProperty]
155+
[EmailAddress]
156+
private string email;
157+
158+
[ObservableProperty]
159+
[TestValidation(null, typeof(SampleModel), true, 6.28, new[] { "Bob", "Ross" }, NestedArray = new object[] { 1, "Hello", new int[] { 2, 3, 4 } }, Animal = Animal.Llama)]
160+
private int ifThisWorksThenThatsGreat;
161+
}
162+
163+
private sealed class TestValidationAttribute : ValidationAttribute
164+
{
165+
public TestValidationAttribute(object o, Type t, bool flag, double d, string[] names)
166+
{
167+
O = o;
168+
T = t;
169+
Flag = flag;
170+
D = d;
171+
Names = names;
172+
}
173+
174+
public object O { get; }
175+
176+
public Type T { get; }
177+
178+
public bool Flag { get; }
179+
180+
public double D { get; }
181+
182+
public string[] Names { get; }
183+
184+
public object NestedArray { get; set; }
185+
186+
public Animal Animal { get; set; }
187+
}
188+
189+
public enum Animal
190+
{
191+
Cat,
192+
Dog,
193+
Llama
194+
}
93195
}
94196
}

0 commit comments

Comments
 (0)