< Summary

Information
Class: Allyaria.Theming.StyleTypes.StyleNumber
Assembly: Allyaria.Theming
File(s): /home/runner/work/allyaria/allyaria/src/Allyaria.Theming/StyleTypes/StyleNumber.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 101
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
get_Number()100%11100%
Parse(...)100%22100%
TryNormalizeInteger(...)50%22100%
TryParse(...)100%11100%
op_Implicit(...)100%11100%
op_Implicit(...)100%22100%

File(s)

/home/runner/work/allyaria/allyaria/src/Allyaria.Theming/StyleTypes/StyleNumber.cs

#LineLine coverage
 1namespace Allyaria.Theming.StyleTypes;
 2
 3/// <summary>
 4/// Represents a numeric CSS value (integer-based) used within the Allyaria theming system. Provides a strongly typed
 5/// wrapper for integer-only CSS properties and validates numeric input upon construction.
 6/// </summary>
 7public sealed record StyleNumber : StyleValueBase
 8{
 9    /// <summary>
 10    /// Initializes a new instance of the <see cref="StyleNumber" /> record using the specified string representation of
 11    /// numeric value.
 12    /// </summary>
 13    /// <param name="value">The string representation of a numeric value.</param>
 14    /// <exception cref="AryArgumentException">
 15    /// Thrown when the provided <paramref name="value" /> cannot be parsed as a valid
 16    /// integer.
 17    /// </exception>
 18    public StyleNumber(string value)
 1519        : base(value: value)
 20    {
 1521        if (string.IsNullOrWhiteSpace(value: Value))
 22        {
 423            return;
 24        }
 25
 1126        AryGuard.Check(
 1127            condition: TryNormalizeInteger(input: Value, number: out var number),
 1128            argName: nameof(value),
 1129            message: $"Invalid number: {Value}"
 1130        );
 31
 732        Number = number;
 733    }
 34
 35    /// <summary>Gets the parsed integer value represented by this style number.</summary>
 1036    public int Number { get; }
 37
 38    /// <summary>Parses the specified string into a <see cref="StyleNumber" /> instance.</summary>
 39    /// <param name="value">The string representation of the numeric value to parse.</param>
 40    /// <returns>A new <see cref="StyleNumber" /> instance representing the parsed value.</returns>
 41    /// <exception cref="AryArgumentException">
 42    /// Thrown when the provided <paramref name="value" /> cannot be parsed as a valid
 43    /// integer.
 44    /// </exception>
 845    public static StyleNumber Parse(string? value) => new(value: value ?? string.Empty);
 46
 47    /// <summary>Attempts to normalize and parse an integer value from the specified input string.</summary>
 48    /// <param name="input">The string to parse.</param>
 49    /// <param name="number">When this method returns, contains the parsed integer if successful; otherwise, <c>0</c>.</
 50    /// <returns>
 51    /// <see langword="true" /> if parsing succeeded or the input is empty/whitespace; otherwise, <see langword="false" 
 52    /// </returns>
 53    private static bool TryNormalizeInteger(string input, out int number)
 54    {
 1155        number = 0;
 56
 1157        return string.IsNullOrWhiteSpace(value: input) || int.TryParse(
 1158            s: input,
 1159            style: NumberStyles.Integer,
 1160            provider: CultureInfo.InvariantCulture,
 1161            result: out number
 1162        );
 63    }
 64
 65    /// <summary>Attempts to parse a string into a <see cref="StyleNumber" /> instance.</summary>
 66    /// <param name="value">The string representation of the numeric value to parse.</param>
 67    /// <param name="result">
 68    /// When this method returns, contains the parsed <see cref="StyleNumber" /> instance or <see langword="null" /> if 
 69    /// failed.
 70    /// </param>
 71    /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns>
 72    public static bool TryParse(string? value, out StyleNumber? result)
 73    {
 74        try
 75        {
 376            result = Parse(value: value);
 77
 278            return true;
 79        }
 180        catch
 81        {
 182            result = null;
 83
 184            return false;
 85        }
 386    }
 87
 88    /// <summary>Implicitly converts a string into a <see cref="StyleNumber" /> instance.</summary>
 89    /// <param name="value">The string representation of the numeric value to convert.</param>
 90    /// <returns>A <see cref="StyleNumber" /> instance representing the provided value.</returns>
 91    /// <exception cref="ArgumentException">Thrown when the provided string cannot be parsed as a valid integer.</except
 292    public static implicit operator StyleNumber(string? value) => Parse(value: value);
 93
 94    /// <summary>Implicitly converts a <see cref="StyleNumber" /> instance to its string representation.</summary>
 95    /// <param name="value">The <see cref="StyleNumber" /> instance to convert.</param>
 96    /// <returns>
 97    /// The original string value represented by the <see cref="StyleNumber" />, or an empty string if
 98    /// <paramref name="value" /> is <see langword="null" />.
 99    /// </returns>
 2100    public static implicit operator string(StyleNumber? value) => (value?.Value).OrDefaultIfEmpty();
 101}