< Summary

Information
Class: NexusLabs.Needlr.Generators.Models.ConstructorGuardModel
Assembly: NexusLabs.Needlr.Generators
File(s): /_work/nick-alienware-needlr-1-1784953859-3e8997/needlr/needlr/src/NexusLabs.Needlr.Generators/Models/Constructors/ConstructorGuardModel.cs
Line coverage
59%
Covered lines: 16
Uncovered lines: 11
Coverable lines: 27
Total lines: 95
Line coverage: 59.2%
Branch coverage
35%
Covered branches: 7
Total branches: 20
Branch coverage: 35%
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_Kind()100%11100%
get_CustomGuardTypeName()100%11100%
get_CustomGuardMethodName()100%11100%
get_ForwardedArgumentLiterals()100%11100%
Equals(...)50%66100%
Equals(...)0%620%
GetHashCode()0%4260%
op_Equality(...)100%210%
op_Inequality(...)100%210%
ForwardedArgumentLiteralsEqual(...)50%5460%

File(s)

/_work/nick-alienware-needlr-1-1784953859-3e8997/needlr/needlr/src/NexusLabs.Needlr.Generators/Models/Constructors/ConstructorGuardModel.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3
 4namespace NexusLabs.Needlr.Generators.Models;
 5
 6/// <summary>
 7/// A normalized constructor guard to emit for a generated-constructor parameter,
 8/// either a built-in kind or a resolved custom guard type and method call.
 9/// </summary>
 10/// <remarks>
 11/// Implements value equality so generated-constructor and record-overload models that
 12/// contain arrays of guards remain cacheable across incremental generator runs.
 13/// Positional literals forwarded from a parameterized alias are compared
 14/// element-by-element rather than by array reference.
 15/// </remarks>
 16internal readonly struct ConstructorGuardModel : IEquatable<ConstructorGuardModel>
 17{
 18    public ConstructorGuardModel(
 19        GeneratedConstructorGuardKind kind,
 20        string? customGuardTypeName,
 21        string? customGuardMethodName,
 22        string[]? forwardedArgumentLiterals)
 23    {
 6924        Kind = kind;
 6925        CustomGuardTypeName = customGuardTypeName;
 6926        CustomGuardMethodName = customGuardMethodName;
 6927        ForwardedArgumentLiterals = forwardedArgumentLiterals ?? Array.Empty<string>();
 6928    }
 29
 30    /// <summary>
 31    /// The guard kind. <see cref="GeneratedConstructorGuardKind.Custom"/> indicates a
 32    /// resolved custom guard type and method call.
 33    /// </summary>
 27934    public GeneratedConstructorGuardKind Kind { get; }
 35
 36    /// <summary>
 37    /// The fully qualified custom guard type name, or <see langword="null"/> for a
 38    /// built-in guard.
 39    /// </summary>
 9140    public string? CustomGuardTypeName { get; }
 41
 42    /// <summary>
 43    /// The resolved custom guard method name, or <see langword="null"/> for a built-in
 44    /// guard.
 45    /// </summary>
 9146    public string? CustomGuardMethodName { get; }
 47
 48    /// <summary>
 49    /// Rendered C# literals forwarded from a parameterized alias attribute usage, in
 50    /// declared order.
 51    /// </summary>
 11252    public string[] ForwardedArgumentLiterals { get; }
 53
 54    public bool Equals(ConstructorGuardModel other)
 55    {
 356        return Kind == other.Kind &&
 357            string.Equals(CustomGuardTypeName, other.CustomGuardTypeName, StringComparison.Ordinal) &&
 358            string.Equals(CustomGuardMethodName, other.CustomGuardMethodName, StringComparison.Ordinal) &&
 359            ForwardedArgumentLiteralsEqual(ForwardedArgumentLiterals, other.ForwardedArgumentLiterals);
 60    }
 61
 062    public override bool Equals(object? obj) => obj is ConstructorGuardModel other && Equals(other);
 63
 64    public override int GetHashCode()
 65    {
 66        unchecked
 67        {
 068            var hash = (int)Kind;
 069            hash = (hash * 397) ^ (CustomGuardTypeName?.GetHashCode() ?? 0);
 070            hash = (hash * 397) ^ (CustomGuardMethodName?.GetHashCode() ?? 0);
 71
 072            foreach (var literal in ForwardedArgumentLiterals)
 73            {
 074                hash = (hash * 397) ^ literal.GetHashCode();
 75            }
 76
 077            return hash;
 78        }
 79    }
 80
 081    public static bool operator ==(ConstructorGuardModel left, ConstructorGuardModel right) => left.Equals(right);
 82
 083    public static bool operator !=(ConstructorGuardModel left, ConstructorGuardModel right) => !left.Equals(right);
 84
 85    private static bool ForwardedArgumentLiteralsEqual(string[] left, string[] right)
 86    {
 387        if (ReferenceEquals(left, right))
 088            return true;
 89
 390        if (left.Length != right.Length)
 091            return false;
 92
 393        return left.SequenceEqual(right, StringComparer.Ordinal);
 94    }
 95}