|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +using System; |
5 | 6 | using System.Collections.Generic;
|
6 | 7 | using System.ComponentModel;
|
7 | 8 | using System.ComponentModel.DataAnnotations;
|
@@ -207,6 +208,60 @@ public void Test_ObservableValidator_TrySetProperty()
|
207 | 208 | Assert.AreEqual(model.Name, "This is fine");
|
208 | 209 | }
|
209 | 210 |
|
| 211 | + [TestCategory("Mvvm")] |
| 212 | + [TestMethod] |
| 213 | + public void Test_ObservableValidator_ValidateProperty() |
| 214 | + { |
| 215 | + var model = new ComparableModel(); |
| 216 | + var events = new List<DataErrorsChangedEventArgs>(); |
| 217 | + |
| 218 | + model.ErrorsChanged += (s, e) => events.Add(e); |
| 219 | + |
| 220 | + // Set a correct value for both properties, first A then B |
| 221 | + model.A = 42; |
| 222 | + model.B = 30; |
| 223 | + |
| 224 | + Assert.AreEqual(events.Count, 0); |
| 225 | + Assert.IsFalse(model.HasErrors); |
| 226 | + |
| 227 | + // Make B greater than A, hence invalidating A |
| 228 | + model.B = 50; |
| 229 | + |
| 230 | + Assert.AreEqual(events.Count, 1); |
| 231 | + Assert.AreEqual(events.Last().PropertyName, nameof(ComparableModel.A)); |
| 232 | + Assert.IsTrue(model.HasErrors); |
| 233 | + |
| 234 | + events.Clear(); |
| 235 | + |
| 236 | + // Make A greater than B, hence making it valid again |
| 237 | + model.A = 51; |
| 238 | + |
| 239 | + Assert.AreEqual(events.Count, 1); |
| 240 | + Assert.AreEqual(events.Last().PropertyName, nameof(ComparableModel.A)); |
| 241 | + Assert.AreEqual(model.GetErrors(nameof(ComparableModel.A)).Count(), 0); |
| 242 | + Assert.IsFalse(model.HasErrors); |
| 243 | + |
| 244 | + events.Clear(); |
| 245 | + |
| 246 | + // Make A smaller than B, hence invalidating it |
| 247 | + model.A = 49; |
| 248 | + |
| 249 | + Assert.AreEqual(events.Count, 1); |
| 250 | + Assert.AreEqual(events.Last().PropertyName, nameof(ComparableModel.A)); |
| 251 | + Assert.AreEqual(model.GetErrors(nameof(ComparableModel.A)).Count(), 1); |
| 252 | + Assert.IsTrue(model.HasErrors); |
| 253 | + |
| 254 | + events.Clear(); |
| 255 | + |
| 256 | + // Lower B, hence making A valid again |
| 257 | + model.B = 20; |
| 258 | + |
| 259 | + Assert.AreEqual(events.Count, 1); |
| 260 | + Assert.AreEqual(events.Last().PropertyName, nameof(ComparableModel.A)); |
| 261 | + Assert.AreEqual(model.GetErrors(nameof(ComparableModel.A)).Count(), 0); |
| 262 | + Assert.IsFalse(model.HasErrors); |
| 263 | + } |
| 264 | + |
210 | 265 | public class Person : ObservableValidator
|
211 | 266 | {
|
212 | 267 | private string name;
|
@@ -234,5 +289,59 @@ public int Age
|
234 | 289 | set => SetProperty(ref this.age, value, true);
|
235 | 290 | }
|
236 | 291 | }
|
| 292 | + |
| 293 | + /// <summary> |
| 294 | + /// Test model for linked properties, to test <see cref="ObservableValidator.ValidateProperty(object?, string?)"/> instance. |
| 295 | + /// See https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/3665 for the original request for this feature. |
| 296 | + /// </summary> |
| 297 | + public class ComparableModel : ObservableValidator |
| 298 | + { |
| 299 | + private int a; |
| 300 | + |
| 301 | + [Range(10, 100)] |
| 302 | + [GreaterThan(nameof(B))] |
| 303 | + public int A |
| 304 | + { |
| 305 | + get => this.a; |
| 306 | + set => SetProperty(ref this.a, value, true); |
| 307 | + } |
| 308 | + |
| 309 | + private int b; |
| 310 | + |
| 311 | + [Range(20, 80)] |
| 312 | + public int B |
| 313 | + { |
| 314 | + get => this.b; |
| 315 | + set |
| 316 | + { |
| 317 | + SetProperty(ref this.b, value, true); |
| 318 | + ValidateProperty(A, nameof(A)); |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + public sealed class GreaterThanAttribute : ValidationAttribute |
| 324 | + { |
| 325 | + public GreaterThanAttribute(string propertyName) |
| 326 | + { |
| 327 | + PropertyName = propertyName; |
| 328 | + } |
| 329 | + |
| 330 | + public string PropertyName { get; } |
| 331 | + |
| 332 | + protected override ValidationResult IsValid(object value, ValidationContext validationContext) |
| 333 | + { |
| 334 | + object |
| 335 | + instance = validationContext.ObjectInstance, |
| 336 | + otherValue = instance.GetType().GetProperty(PropertyName).GetValue(instance); |
| 337 | + |
| 338 | + if (((IComparable)value).CompareTo(otherValue) > 0) |
| 339 | + { |
| 340 | + return ValidationResult.Success; |
| 341 | + } |
| 342 | + |
| 343 | + return new ValidationResult("The current value is smaller than the other one"); |
| 344 | + } |
| 345 | + } |
237 | 346 | }
|
238 | 347 | }
|
0 commit comments