< Summary

Information
Class: NexusLabs.Needlr.Generators.Models.EligibleConstructorField
Assembly: NexusLabs.Needlr.Generators
File(s): /_work/nick-alienware-needlr-1-1784953859-3e8997/needlr/needlr/src/NexusLabs.Needlr.Generators/Models/Constructors/EligibleConstructorField.cs
Line coverage
64%
Covered lines: 20
Uncovered lines: 11
Coverable lines: 31
Total lines: 103
Line coverage: 64.5%
Branch coverage
68%
Covered branches: 11
Total branches: 16
Branch coverage: 68.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_FieldName()100%11100%
get_ParameterName()100%11100%
get_ParameterTypeName()100%11100%
get_IsNonNullableReferenceType()100%11100%
get_ExplicitGuards()100%11100%
Equals(...)100%88100%
Equals(...)0%620%
GetHashCode()0%620%
op_Equality(...)100%210%
op_Inequality(...)100%210%
GuardsEqual(...)75%4480%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Linq;
 3
 4namespace NexusLabs.Needlr.Generators.Models;
 5
 6/// <summary>
 7/// A single field eligible to become a generated constructor parameter.
 8/// </summary>
 9/// <remarks>
 10/// Implements value equality (rather than the default struct member-by-member
 11/// reflection-based comparison) — in particular <see cref="ExplicitGuards"/> is
 12/// compared element-by-element rather than by array reference — so that
 13/// <see cref="GeneratedConstructorModel"/> stays cacheable across incremental
 14/// generator runs: two separately-built field lists with the same effective content
 15/// must compare equal even though they are different array instances.
 16/// </remarks>
 17internal readonly struct EligibleConstructorField : IEquatable<EligibleConstructorField>
 18{
 19    public EligibleConstructorField(
 20        string fieldName,
 21        string parameterName,
 22        string parameterTypeName,
 23        bool isNonNullableReferenceType,
 24        ConstructorGuardModel[] explicitGuards)
 25    {
 14626        FieldName = fieldName;
 14627        ParameterName = parameterName;
 14628        ParameterTypeName = parameterTypeName;
 14629        IsNonNullableReferenceType = isNonNullableReferenceType;
 14630        ExplicitGuards = explicitGuards;
 14631    }
 32
 33    /// <summary>
 34    /// The original field name, e.g. <c>_repository</c>.
 35    /// </summary>
 16836    public string FieldName { get; }
 37
 38    /// <summary>
 39    /// The normalized constructor parameter name, e.g. <c>repository</c>.
 40    /// </summary>
 38441    public string ParameterName { get; }
 42
 43    /// <summary>
 44    /// The fully qualified parameter type name, preserving nullable-reference and
 45    /// generic type syntax.
 46    /// </summary>
 13347    public string ParameterTypeName { get; }
 48
 49    /// <summary>
 50    /// True when the field's type is a reference type with no nullable annotation,
 51    /// making it eligible for the class-level automatic null-guard default.
 52    /// </summary>
 1953    public bool IsNonNullableReferenceType { get; }
 54
 55    /// <summary>
 56    /// Explicit guards requested for this field, in source declaration order.
 57    /// </summary>
 8758    public ConstructorGuardModel[] ExplicitGuards { get; }
 59
 60    public bool Equals(EligibleConstructorField other)
 61    {
 962        return string.Equals(FieldName, other.FieldName, StringComparison.Ordinal) &&
 963            string.Equals(ParameterName, other.ParameterName, StringComparison.Ordinal) &&
 964            string.Equals(ParameterTypeName, other.ParameterTypeName, StringComparison.Ordinal) &&
 965            IsNonNullableReferenceType == other.IsNonNullableReferenceType &&
 966            GuardsEqual(ExplicitGuards, other.ExplicitGuards);
 67    }
 68
 069    public override bool Equals(object? obj) => obj is EligibleConstructorField other && Equals(other);
 70
 71    public override int GetHashCode()
 72    {
 73        unchecked
 74        {
 075            var hash = FieldName.GetHashCode();
 076            hash = (hash * 397) ^ ParameterName.GetHashCode();
 077            hash = (hash * 397) ^ ParameterTypeName.GetHashCode();
 078            hash = (hash * 397) ^ IsNonNullableReferenceType.GetHashCode();
 79
 080            foreach (var guard in ExplicitGuards)
 81            {
 082                hash = (hash * 397) ^ guard.GetHashCode();
 83            }
 84
 085            return hash;
 86        }
 87    }
 88
 089    public static bool operator ==(EligibleConstructorField left, EligibleConstructorField right) => left.Equals(right);
 90
 091    public static bool operator !=(EligibleConstructorField left, EligibleConstructorField right) => !left.Equals(right)
 92
 93    private static bool GuardsEqual(ConstructorGuardModel[] left, ConstructorGuardModel[] right)
 94    {
 695        if (ReferenceEquals(left, right))
 396            return true;
 97
 398        if (left.Length != right.Length)
 099            return false;
 100
 3101        return left.SequenceEqual(right);
 102    }
 103}