< Summary - Core.Tests

Information
Class: Common.Core.Classes.ModelBase
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Classes\ModelBase.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 52
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
OnPropertyChanged(...)100%22100%
CalculateAge(...)100%22100%
CalculateAge(...)100%22100%
SetNullString(...)100%44100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Classes\ModelBase.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Runtime.CompilerServices;
 3
 4namespace Common.Core.Classes;
 5
 6/// <summary>Base class for models that require the INotifyPropertyChanged interface.</summary>
 7public abstract class ModelBase : INotifyPropertyChanged
 8{
 9  #region INotifyPropertyChanged Implementation
 10
 11  /// <summary>Method that will handle the PropertyChanged event raised when a property is
 12  /// changed on a component.</summary>
 13  public event PropertyChangedEventHandler? PropertyChanged;
 14
 15  /// <summary>Invoked whenever the effective value has been updated.</summary>
 16  /// <param name="property">Name of the property that has changed.</param>
 17  public void OnPropertyChanged( [CallerMemberName] string property = "" )
 127318  {
 127319    PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( property ) );
 127320  }
 21
 22  #endregion
 23
 24  #region Public Methods
 25
 26  /// <summary>Calculate the current age based on a DateTime value.</summary>
 27  /// <param name="date">Date to use.</param>
 28  /// <returns><see langword="null"/> is returned if no date is provided.</returns>
 29  public static int? CalculateAge( DateTime? date )
 2430  {
 31    // https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-based-on-a-datetime-type-birthday
 2432    return date is not null ? (int)( ( DateTime.Now - date.Value ).TotalDays / 365.242199 ) : null;
 2433  }
 34
 35  /// <summary>Calculate the current age based on a DateOnly value.</summary>
 36  /// <param name="date">Date to use.</param>
 37  /// <returns><see langword="null"/> is returned if no date is provided.</returns>
 38  public static int? CalculateAge( DateOnly? date )
 2439  {
 2440    return date.HasValue ? CalculateAge( date.Value.ToDateTime( TimeOnly.MinValue ) ) : null;
 2441  }
 42
 43  /// <summary>Sets a string as null if the length is zero.</summary>
 44  /// <param name="value">Value to check.</param>
 45  /// <returns><see langword="null"/> is returned if the string length is zero.</returns>
 46  public static string? SetNullString( string? value )
 64447  {
 64448    return value is not null && value.Length > 0 ? value : null;
 64449  }
 50
 51  #endregion
 52}