< Summary

Information
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 101
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Of(...)100%11100%
Of(...)100%11100%
Of(...)100%11100%
To(...)100%11100%
Add(...)100%11100%
Subtract(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/Extensions/QuantityExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="QuantityExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using MyNet.Primitives.Conversion;
 9
 10#pragma warning disable IDE0130 // Namespace does not match folder structure
 11namespace MyNet.Primitives;
 12#pragma warning restore IDE0130 // Namespace does not match folder structure
 13
 14public static class QuantityExtensions
 15{
 16    extension(int value)
 17    {
 18        /// <summary>
 19        /// Creates a new quantity with the specified value and unit. This method allows you to easily create a quantity
 20        /// </summary>
 21        /// <param name="unit">The unit of measurement for the quantity.</param>
 22        /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam>
 23        /// <returns>A new instance of <see cref="Quantity"/> with the specified value and unit.</returns>
 24        public Quantity<TUnit> Of<TUnit>(TUnit unit)
 25            where TUnit : struct, Enum
 1826            => Quantity.Of(value, unit);
 27    }
 28
 29    extension(double value)
 30    {
 31        /// <summary>
 32        /// Creates a new quantity with the specified value and unit. This method allows you to easily create a quantity
 33        /// </summary>
 34        /// <param name="unit">The unit of measurement for the quantity.</param>
 35        /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam>
 36        /// <returns>A new instance of <see cref="Quantity"/> with the specified value and unit.</returns>
 37        public Quantity<TUnit> Of<TUnit>(TUnit unit)
 38            where TUnit : struct, Enum
 1839            => Quantity.Of(value, unit);
 40    }
 41
 42    extension(decimal value)
 43    {
 44        /// <summary>
 45        /// Creates a new quantity with the specified value and unit. This method allows you to easily create a quantity
 46        /// </summary>
 47        /// <param name="unit">The unit of measurement for the quantity.</param>
 48        /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam>
 49        /// <returns>A new instance of <see cref="Quantity"/> with the specified value and unit.</returns>
 50        public Quantity<TUnit> Of<TUnit>(TUnit unit)
 51            where TUnit : struct, Enum
 352            => Quantity.Of((double)value, unit);
 53    }
 54
 55    extension<TUnit>(Quantity<TUnit> quantity)
 56        where TUnit : struct, Enum
 57    {
 58        /// <summary>
 59        /// Converts the quantity to the specified target unit using the appropriate converter from the registry.
 60        /// </summary>
 61        /// <param name="target">The target unit to convert the quantity to.</param>
 62        /// <returns>A new <see cref="Quantity{TUnit}"/> instance representing the converted quantity.</returns>
 63        public Quantity<TUnit> To(TUnit target)
 64        {
 365            var converter = UnitConverterRegistry.Get<TUnit>();
 66
 367            var converted = converter.Convert(quantity.Value, quantity.Unit, target);
 68
 369            return new(converted, target);
 70        }
 71
 72        /// <summary>
 73        /// Adds another quantity to this quantity, converting the other quantity to this quantity's unit if necessary.
 74        /// </summary>
 75        /// <param name="right">The quantity to add.</param>
 76        /// <returns>A new <see cref="Quantity{TUnit}"/> instance representing the sum of the two quantities.</returns>
 77        public Quantity<TUnit> Add(Quantity<TUnit> right)
 78        {
 679            var converter = UnitConverterRegistry.Get<TUnit>();
 80
 681            var rightValue = converter.Convert(right.Value, right.Unit, quantity.Unit);
 82
 683            return new(quantity.Value + rightValue, quantity.Unit);
 84        }
 85
 86        /// <summary>
 87        /// Subtracts another quantity from this quantity, converting the other quantity to this quantity's unit if nece
 88        /// </summary>
 89        /// <param name="right">The quantity to subtract.</param>
 90        /// <returns>A new <see cref="Quantity{TUnit}"/> instance representing the difference of the two quantities.</re
 91        public Quantity<TUnit> Subtract(Quantity<TUnit> right)
 92        {
 393            var converter = UnitConverterRegistry.Get<TUnit>();
 94
 395            var rightValue = converter.Convert(right.Value, right.Unit, quantity.Unit);
 96
 397            return new(quantity.Value - rightValue, quantity.Unit);
 98        }
 99    }
 100}
 101