< Summary

Class:ReactiveUI.Validation.Helpers.ValidationHelper
Assembly:ReactiveUI.Validation
File(s):D:\a\1\s\src\ReactiveUI.Validation\Helpers\ValidationHelper.cs
Covered lines:15
Uncovered lines:6
Coverable lines:21
Total lines:89
Line coverage:71.4% (15 of 21)
Covered branches:1
Total branches:6
Branch coverage:16.6% (1 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%50%
get_IsValid()-100%100%
get_Message()-100%100%
get_ValidationChanged()-100%100%
Dispose()-0%100%
Dispose(...)-0%0%

File(s)

D:\a\1\s\src\ReactiveUI.Validation\Helpers\ValidationHelper.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.Diagnostics.CodeAnalysis;
 8using System.Reactive.Disposables;
 9using System.Reactive.Linq;
 10using ReactiveUI.Validation.Collections;
 11using ReactiveUI.Validation.Components.Abstractions;
 12using ReactiveUI.Validation.States;
 13
 14namespace ReactiveUI.Validation.Helpers
 15{
 16    /// <inheritdoc cref="ReactiveObject" />
 17    /// <inheritdoc cref="IDisposable" />
 18    /// <summary>
 19    /// Encapsulation of a validation with bindable properties.
 20    /// </summary>
 21    public class ValidationHelper : ReactiveObject, IDisposable
 22    {
 23        private readonly IValidationComponent _validation;
 24
 25        [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed with the _dis
 26        private readonly ObservableAsPropertyHelper<bool> _isValid;
 27
 28        [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed with the _dis
 29        private readonly ObservableAsPropertyHelper<ValidationText> _message;
 30
 3831        private readonly CompositeDisposable _disposables = new CompositeDisposable();
 32
 33        /// <summary>
 34        /// Initializes a new instance of the <see cref="ValidationHelper"/> class.
 35        /// </summary>
 36        /// <param name="validation">Validation property.</param>
 3837        public ValidationHelper(IValidationComponent validation)
 38        {
 3839            _validation = validation ?? throw new ArgumentNullException(nameof(validation));
 40
 3841            _isValid = _validation.ValidationStatusChange
 9442                .Select(v => v.IsValid)
 3843                .ToProperty(this, nameof(IsValid))
 3844                .DisposeWith(_disposables);
 45
 3846            _message = _validation.ValidationStatusChange
 9447                .Select(v => v.Text)
 3848                .ToProperty<ValidationHelper, ValidationText>(this, nameof(Message))
 3849                .DisposeWith(_disposables);
 3850        }
 51
 52        /// <summary>
 53        /// Gets a value indicating whether the validation is currently valid or not.
 54        /// </summary>
 855        public bool IsValid => _isValid.Value;
 56
 57        /// <summary>
 58        /// Gets the current (optional) validation message.
 59        /// </summary>
 460        public ValidationText? Message => _message.Value;
 61
 62        /// <summary>
 63        /// Gets the observable for validation state changes.
 64        /// </summary>
 665        public IObservable<ValidationState> ValidationChanged => _validation.ValidationStatusChange;
 66
 67        /// <inheritdoc/>
 68        public void Dispose()
 69        {
 70            // Dispose of unmanaged resources.
 071            Dispose(true);
 72
 73            // Suppress finalization.
 074            GC.SuppressFinalize(this);
 075        }
 76
 77        /// <summary>
 78        /// Disposes of the managed resources.
 79        /// </summary>
 80        /// <param name="disposing">If its getting called by the <see cref="Dispose()"/> method.</param>
 81        protected virtual void Dispose(bool disposing)
 82        {
 083            if (disposing)
 84            {
 085                _disposables?.Dispose();
 86            }
 087        }
 88    }
 89}