< Summary

Class:ReactiveUI.Validation.Extensions.ValidationContextExtensions
Assembly:ReactiveUI.Validation
File(s):D:\a\1\s\src\ReactiveUI.Validation\Extensions\ValidationContextExtensions.cs
Covered lines:6
Uncovered lines:29
Coverable lines:35
Total lines:138
Line coverage:17.1% (6 of 35)
Covered branches:2
Total branches:28
Branch coverage:7.1% (2 of 28)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ResolveFor(...)-75%50%
ResolveFor(...)-0%0%
ResolveFor(...)-0%0%

File(s)

D:\a\1\s\src\ReactiveUI.Validation\Extensions\ValidationContextExtensions.cs

#LineLine coverage
 1// Copyright (c) 2020 .NET Foundation and Contributors. All rights reserved.
 2// Licensed to the .NET Foundation under one or more agreements.
 3// The .NET Foundation licenses this file to you under the MIT license.
 4// See the LICENSE file in the project root for full license information.
 5
 6using System;
 7using System.Collections.Generic;
 8using System.Linq;
 9using System.Linq.Expressions;
 10using ReactiveUI.Validation.Components;
 11using ReactiveUI.Validation.Components.Abstractions;
 12using ReactiveUI.Validation.Contexts;
 13
 14namespace ReactiveUI.Validation.Extensions
 15{
 16    /// <summary>
 17    /// Extensions methods for <see cref="ValidationContext"/>.
 18    /// </summary>
 19    public static class ValidationContextExtensions
 20    {
 21        /// <summary>
 22        /// Resolves the property valuation for a specified property.
 23        /// </summary>
 24        /// <typeparam name="TViewModel">ViewModel type.</typeparam>
 25        /// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam>
 26        /// <param name="context">ValidationContext instance.</param>
 27        /// <param name="viewModelProperty">ViewModel property.</param>
 28        /// <param name="strict">Indicates if the ViewModel property to find is unique.</param>
 29        /// <returns>Returns a collection of <see cref="BasePropertyValidation{TViewModel}"/> objects.</returns>
 30        public static IEnumerable<IPropertyValidationComponent<TViewModel>> ResolveFor<TViewModel, TViewModelProperty>(
 31            this ValidationContext context,
 32            Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
 33            bool strict = true)
 34        {
 1635            if (context is null)
 36            {
 037                throw new ArgumentNullException(nameof(context));
 38            }
 39
 1640            if (viewModelProperty is null)
 41            {
 042                throw new ArgumentNullException(nameof(viewModelProperty));
 43            }
 44
 1645            return context
 1646                .Validations
 1647                .OfType<IPropertyValidationComponent<TViewModel>>()
 4048                .Where(v => v.ContainsProperty(viewModelProperty, strict));
 49        }
 50
 51        /// <summary>
 52        /// Resolves the property valuation for two properties.
 53        /// </summary>
 54        /// <typeparam name="TViewModel">ViewModel type.</typeparam>
 55        /// <typeparam name="TProperty1">ViewModel first property type.</typeparam>
 56        /// <typeparam name="TProperty2">ViewModel second property type.</typeparam>
 57        /// <param name="context">ValidationContext instance.</param>
 58        /// <param name="viewModelProperty1">First ViewModel property.</param>
 59        /// <param name="viewModelProperty2">Second ViewModel property.</param>
 60        /// <returns>Returns a collection of <see cref="BasePropertyValidation{TViewModel}"/> objects.</returns>
 61        public static IEnumerable<IPropertyValidationComponent<TViewModel>> ResolveFor<TViewModel, TProperty1,
 62            TProperty2>(
 63            this ValidationContext context,
 64            Expression<Func<TViewModel, TProperty1>> viewModelProperty1,
 65            Expression<Func<TViewModel, TProperty2>> viewModelProperty2)
 66        {
 067            if (context is null)
 68            {
 069                throw new ArgumentNullException(nameof(context));
 70            }
 71
 072            if (viewModelProperty1 is null)
 73            {
 074                throw new ArgumentNullException(nameof(viewModelProperty1));
 75            }
 76
 077            if (viewModelProperty2 is null)
 78            {
 079                throw new ArgumentNullException(nameof(viewModelProperty2));
 80            }
 81
 082            return context
 083                .Validations
 084                .OfType<IPropertyValidationComponent<TViewModel>>()
 085                .Where(v => v.ContainsProperty(viewModelProperty1) &&
 086                            v.ContainsProperty(viewModelProperty2) &&
 087                            v.PropertyCount == 2);
 88        }
 89
 90        /// <summary>
 91        /// Resolves the property valuation for three properties.
 92        /// </summary>
 93        /// <typeparam name="TViewModel">ViewModel type.</typeparam>
 94        /// <typeparam name="TProperty1">ViewModel first property type.</typeparam>
 95        /// <typeparam name="TProperty2">ViewModel second property type.</typeparam>
 96        /// <typeparam name="TProperty3">ViewModel third property type.</typeparam>
 97        /// <param name="context">ValidationContext instance.</param>
 98        /// <param name="viewModelProperty1">First ViewModel property.</param>
 99        /// <param name="viewModelProperty2">Second ViewModel property.</param>
 100        /// <param name="viewModelProperty3">Third ViewModel property.</param>
 101        /// <returns>Returns a collection of <see cref="BasePropertyValidation{TViewModel}"/> objects.</returns>
 102        public static IEnumerable<IPropertyValidationComponent<TViewModel>> ResolveFor<TViewModel,
 103            TProperty1, TProperty2, TProperty3>(
 104            this ValidationContext context,
 105            Expression<Func<TViewModel, TProperty1>> viewModelProperty1,
 106            Expression<Func<TViewModel, TProperty2>> viewModelProperty2,
 107            Expression<Func<TViewModel, TProperty3>> viewModelProperty3)
 108        {
 0109            if (context is null)
 110            {
 0111                throw new ArgumentNullException(nameof(context));
 112            }
 113
 0114            if (viewModelProperty1 is null)
 115            {
 0116                throw new ArgumentNullException(nameof(viewModelProperty1));
 117            }
 118
 0119            if (viewModelProperty2 is null)
 120            {
 0121                throw new ArgumentNullException(nameof(viewModelProperty2));
 122            }
 123
 0124            if (viewModelProperty3 is null)
 125            {
 0126                throw new ArgumentNullException(nameof(viewModelProperty3));
 127            }
 128
 0129            return context
 0130                .Validations
 0131                .OfType<IPropertyValidationComponent<TViewModel>>()
 0132                .Where(v => v.ContainsProperty(viewModelProperty1) &&
 0133                            v.ContainsProperty(viewModelProperty2) &&
 0134                            v.ContainsProperty(viewModelProperty3) &&
 0135                            v.PropertyCount == 3);
 136        }
 137    }
 138}