< Summary - Combined Code Coverage

Information
Class: NLightning.Bolt11.Models.TaggedFields.DescriptionTaggedField
Assembly: NLightning.Bolt11
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Bolt11/Models/TaggedFields/DescriptionTaggedField.cs
Tag: 30_15166811759
Line coverage
85%
Covered lines: 23
Uncovered lines: 4
Coverable lines: 27
Total lines: 98
Line coverage: 85.1%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Type()100%11100%
get_Value()100%11100%
get_Length()100%11100%
.ctor(...)75%4.05485.71%
WriteToBitWriter(...)50%2.06275%
IsValid()100%11100%
FromBitReader(...)83.33%6.29680%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Bolt11/Models/TaggedFields/DescriptionTaggedField.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace NLightning.Bolt11.Models.TaggedFields;
 4
 5using Common.Utils;
 6using Enums;
 7using Interfaces;
 8
 9/// <summary>
 10/// Tagged field for the description
 11/// </summary>
 12/// <remarks>
 13/// The description is a UTF-8 encoded string that describes, in short, the purpose of payment
 14/// </remarks>
 15/// <seealso cref="ITaggedField"/>
 16internal sealed class DescriptionTaggedField : ITaggedField
 17{
 18    private readonly byte[] _data;
 19
 80820    public TaggedFieldTypes Type => TaggedFieldTypes.DESCRIPTION;
 24821    internal string Value { get; }
 20822    public short Length { get; }
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="DescriptionTaggedField"/> class.
 26    /// </summary>
 27    /// <param name="value">The Description</param>
 17628    internal DescriptionTaggedField(string value)
 29    {
 17630        Value = value;
 31
 17632        if (string.IsNullOrEmpty(value))
 33        {
 034            _data = [];
 035            Length = 0;
 36        }
 37        else
 38        {
 39            // Add Padding if needed
 17640            var data = Encoding.UTF8.GetBytes(Value);
 17641            var bitLength = data.Length * 8;
 17642            var totalBits = bitLength + (5 - bitLength % 5) % 5;
 17643            if (totalBits != bitLength)
 44            {
 15645                _data = new byte[data.Length + 1];
 15646                Array.Copy(data, _data, data.Length);
 47            }
 48            else
 49            {
 2050                _data = data;
 51            }
 52
 17653            Length = (short)(totalBits / 5);
 54        }
 17655    }
 56
 57    /// <inheritdoc/>
 58    public void WriteToBitWriter(BitWriter bitWriter)
 59    {
 5260        if (Length == 0)
 61        {
 062            return;
 63        }
 64
 65        // Write data
 5266        bitWriter.WriteBits(_data, Length * 5);
 5267    }
 68
 69    /// <inheritdoc/>
 70    public bool IsValid()
 71    {
 4072        return true;
 73    }
 74
 75    /// <summary>
 76    /// Reads a DescriptionTaggedField from a BitReader
 77    /// </summary>
 78    /// <param name="bitReader">The BitReader to read from</param>
 79    /// <param name="length">The length of the field</param>
 80    /// <returns>The DescriptionTaggedField</returns>
 81    /// <exception cref="ArgumentException">Thrown when the length is invalid</exception>
 82    internal static DescriptionTaggedField FromBitReader(BitReader bitReader, short length)
 83    {
 84        switch (length)
 85        {
 86            case < 0:
 487                throw new ArgumentException("Invalid length for DescriptionTaggedField. Length must be greater or equal 
 88            case 0:
 089                return new DescriptionTaggedField(string.Empty);
 90        }
 91
 92        // Read the data from the BitReader
 5693        var data = new byte[(length * 5 + 7) / 8];
 5694        bitReader.ReadBits(data, length * 5);
 95
 5696        return new DescriptionTaggedField(Encoding.UTF8.GetString(length * 5 % 8 > 0 ? data[..^1] : data));
 97    }
 98}