< Summary

Information
Class: Hertzole.ScriptableValues.Helpers.EqualityHelper
Assembly: Hertzole.ScriptableValues
File(s): ./Packages/se.hertzole.scriptable-values/Runtime/Helpers/EqualityHelper.cs
Tag: 52_28197466029
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 82
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Equals[T](...)0%330100%
IsNull[T](...)0%330100%
IsSameType[TType](...)0%440100%

File(s)

./Packages/se.hertzole.scriptable-values/Runtime/Helpers/EqualityHelper.cs

#LineLine coverage
 1#nullable enable
 2
 3using System.Collections.Generic;
 4using System.Diagnostics.CodeAnalysis;
 5using System.Runtime.CompilerServices;
 6using UnityEngine;
 7
 8namespace Hertzole.ScriptableValues.Helpers
 9{
 10    /// <summary>
 11    ///     Helper methods for ScriptableValues.
 12    /// </summary>
 13    public static class EqualityHelper
 14    {
 15        /// <summary>
 16        ///     Helper method for checking if two types are the same.
 17        /// </summary>
 18        /// <remarks>This takes Unity objects into consideration.</remarks>
 19        /// <param name="x">The first object to compare.</param>
 20        /// <param name="y">The second object to compare.</param>
 21        /// <typeparam name="T">The type of the object.</typeparam>
 22        /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns>
 23        public static bool Equals<T>(T x, T y)
 19740324        {
 25            // Do special checking on Unity objects.
 19740326            if (typeof(T).IsSubclassOf(typeof(Object)) || typeof(T) == typeof(Object))
 104227            {
 104228                Object? xUnityObject = x as Object;
 104229                Object? yUnityObject = y as Object;
 30
 104231                return xUnityObject == yUnityObject;
 32            }
 33
 19636134            return EqualityComparer<T>.Default.Equals(x, y);
 19740335        }
 36
 37        /// <summary>
 38        ///     Helper method for checking if an object is null.
 39        /// </summary>
 40        /// <remarks>This takes Unity objects into consideration.</remarks>
 41        /// <param name="obj">The object to check.</param>
 42        /// <typeparam name="T">The type of the object.</typeparam>
 43        /// <returns><c>true</c> if the object is null; otherwise <c>false</c>.</returns>
 44        public static bool IsNull<T>([NotNullWhen(false)] T? obj)
 3136845        {
 46            // Do special checking on Unity objects.
 3136847            if (typeof(T).IsSubclassOf(typeof(Object)) || typeof(T) == typeof(Object))
 2616048            {
 2616049                Object? unityObject = obj as Object;
 2616050                return unityObject == null;
 51            }
 52
 520853            return obj == null;
 3136854        }
 55
 56        /// <summary>
 57        ///     Helper method to check if the given object is the same type as the provided generic type.
 58        /// </summary>
 59        /// <param name="value">The object value to check.</param>
 60        /// <param name="newValue">The value as the generic value.</param>
 61        /// <typeparam name="TType">The type to match.</typeparam>
 62        /// <returns><c>true</c> if the value is the same type; otherwise <c>false</c>.</returns>
 63        /// <exception cref="System.ArgumentNullException"><paramref name="value" /> is <c>null</c>.</exception>
 64        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 65        public static bool IsSameType<TType>(object? value, [NotNullWhen(true)] out TType? newValue)
 22266        {
 22267            if (value is TType newValueT)
 20468            {
 20469                newValue = newValueT;
 20470                return true;
 71            }
 72
 1873            Guard.IsNotNull(value, nameof(value));
 74
 75#if DEBUG
 1876            Debug.LogError($"{typeof(TType)} is not assignable from {value.GetType()}.");
 77#endif
 1878            newValue = default;
 1879            return false;
 22280        }
 81    }
 82}