< Summary

Information
Class: NGql.Core.Variable
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Variable.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 85
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Name()100%11100%
get_Type()100%11100%
.ctor(...)100%66100%
ToString()100%11100%
CompareTo(...)100%44100%
CompareTo(...)100%22100%
Print(...)100%44100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
op_LessThan(...)100%11100%
op_GreaterThan(...)100%11100%
op_LessThanOrEqual(...)100%11100%
op_GreaterThanOrEqual(...)100%11100%
Equals(...)100%11100%
Equals(...)100%11100%
GetHashCode()100%11100%

File(s)

/home/runner/work/NGql/NGql/src/Core/Variable.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace NGql.Core;
 4
 5/// <summary>
 6///     Represents a variable in GraphQL query.
 7/// </summary>
 8public readonly struct Variable : IComparable, IComparable<Variable>, IEquatable<Variable>
 9{
 10    /// <summary>
 11    /// Name of the variable.
 12    /// </summary>
 6811513    public string Name { get; }
 14
 15    /// <summary>
 16    /// Type of the variable.
 17    /// </summary>
 2045418    public string Type { get; }
 19
 20    public Variable(string name, string type)
 21    {
 811822        if (string.IsNullOrWhiteSpace(name))
 23        {
 1524            throw new ArgumentException("Variable name cannot be null or whitespace.", nameof(name));
 25        }
 26
 810327        if (!name.StartsWith('$'))
 28        {
 629            throw new ArgumentException("Variable name must start with '$'.", nameof(name));
 30        }
 31
 809732        if (string.IsNullOrWhiteSpace(type))
 33        {
 1534            throw new ArgumentException("Variable type cannot be null or whitespace.", nameof(type));
 35        }
 36
 808237        Name = name;
 808238        Type = type;
 808239    }
 40
 941    public override string ToString() => $"{Name}:{Type}";
 42
 43    public int CompareTo(object? obj)
 44    {
 17745        return obj switch
 17746        {
 347            null => 1,
 16848            Variable variable => CompareTo(variable),
 649            _ => throw new ArgumentException($"Object must be of type {nameof(Variable)}")
 17750        };
 51    }
 52
 53    public int CompareTo(Variable other)
 54    {
 1765855        var nameComparison = string.Compare(Name, other.Name, StringComparison.OrdinalIgnoreCase);
 1765856        return nameComparison != 0 ? nameComparison : string.Compare(Type, other.Type, StringComparison.OrdinalIgnoreCas
 57    }
 58
 59    internal void Print(StringBuilder builder, string key, bool isRootElement)
 60    {
 1383361        builder.Append(isRootElement ? Name : key);
 1383362        builder.Append(':');
 1383363        builder.Append(isRootElement ? Type : Name);
 1383364    }
 65
 2166    public static bool operator ==(Variable left, Variable right) => left.Equals(right);
 2767    public static bool operator !=(Variable left, Variable right) => !left.Equals(right);
 68
 1569    public static bool operator <(Variable left, Variable right) => left.CompareTo(right) < 0;
 1270    public static bool operator >(Variable left, Variable right) => left.CompareTo(right) > 0;
 71
 1272    public static bool operator <=(Variable left, Variable right) => left.CompareTo(right) <= 0;
 1273    public static bool operator >=(Variable left, Variable right) => left.CompareTo(right) >= 0;
 74
 16875    public override bool Equals(object? obj) => CompareTo(obj) == 0;
 76
 6977    public bool Equals(Variable other) => CompareTo(other) == 0;
 78
 79    // Constructor rejects null/whitespace name/type, so neither field is null here.
 80    // Comparer-based hashing matches the OrdinalIgnoreCase equality in CompareTo without
 81    // allocating uppercase copies of both strings on every call.
 1282    public override int GetHashCode() => HashCode.Combine(
 1283        StringComparer.OrdinalIgnoreCase.GetHashCode(Name),
 1284        StringComparer.OrdinalIgnoreCase.GetHashCode(Type));
 85}