< Summary

Information
Class: ArturRios.Extensions.EnumerableExtensions
Assembly: ArturRios.Extensions
File(s): /home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/EnumerableExtensions.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 108
Line coverage: 100%
Branch coverage
100%
Covered branches: 38
Total branches: 38
Branch coverage: 100%
Method coverage
100%
Covered methods: 4
Fully covered methods: 4
Total methods: 4
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsEmpty(...)100%66100%
IsNotEmpty(...)100%11100%
PrintContents(...)100%1212100%
IsSingleValue(...)100%2020100%

File(s)

/home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/EnumerableExtensions.cs

#LineLine coverage
 1using System.Collections;
 2using System.Reflection;
 3
 4namespace ArturRios.Extensions;
 5
 6/// <summary>
 7///     Provides extension methods for IEnumerable instances, including emptiness checks and printing contents.
 8/// </summary>
 9public static class EnumerableExtensions
 10{
 11    /// <summary>
 12    ///     Provides enumerable helpers for the given sequence.
 13    /// </summary>
 14    /// <param name="enumerable">The sequence to print.</param>
 15    extension(IEnumerable? enumerable)
 16    {
 17        /// <summary>
 18        ///     Determines whether the enumerable is null or has no elements.
 19        /// </summary>
 20        /// <returns>True if empty or null; otherwise false.</returns>
 21        public bool IsEmpty()
 22        {
 23            switch (enumerable)
 24            {
 25                case null:
 426                    return true;
 27                case ICollection collection:
 1228                    return collection.Count == 0;
 29                default:
 30                {
 1631                    var enumerator = enumerable.GetEnumerator();
 32                    try
 33                    {
 1634                        return !enumerator.MoveNext();
 35                    }
 36                    finally
 37                    {
 1638                        if (enumerator is IDisposable disposable)
 39                        {
 1640                            disposable.Dispose();
 41                        }
 1642                    }
 43                }
 44            }
 45        }
 46
 47        /// <summary>
 48        ///     Determines whether the enumerable contains at least one element.
 49        /// </summary>
 50        /// <returns>True if not empty; otherwise false.</returns>
 1051        public bool IsNotEmpty() => !IsEmpty(enumerable);
 52
 53        /// <summary>
 54        ///     Prints each item in the enumerable. For complex objects, prints property name and value pairs.
 55        /// </summary>
 56        /// <remarks>
 57        ///     Primitives, strings, decimals, enums and the common value types that represent a single value —
 58        ///     <see cref="DateTime"/>, <see cref="DateTimeOffset"/>, <see cref="DateOnly"/>, <see cref="TimeOnly"/>,
 59        ///     <see cref="TimeSpan"/> and <see cref="Guid"/> — are printed as one line, as is any item that has
 60        ///     no readable instance property. Anything else is printed one public, readable instance property
 61        ///     per line; static properties and indexers are skipped.
 62        /// </remarks>
 63        public void PrintContents()
 64        {
 2065            if (enumerable is null)
 66            {
 267                Console.WriteLine("Enumerable is null");
 68
 269                return;
 70            }
 71
 16072            foreach (var item in enumerable)
 73            {
 6274                if (item == null)
 75                {
 476                    Console.WriteLine("null");
 77
 478                    continue;
 79                }
 80
 5881                PropertyInfo[] properties = IsSingleValue(item)
 5882                    ? []
 5883                    : ObjectExtensions.ReadableInstanceProperties(item.GetType()).ToArray();
 84
 5885                if (properties.Length == 0)
 86                {
 4887                    Console.WriteLine(item);
 88                }
 89                else
 90                {
 7291                    foreach (var prop in properties)
 92                    {
 2693                        var value = prop.GetValue(item, null);
 2694                        Console.WriteLine($"{prop.Name}: {value}");
 95                    }
 96                }
 97            }
 1898        }
 99    }
 100
 101    /// <summary>
 102    ///     Whether <paramref name="item"/> represents a single value that reads best through its own
 103    ///     <see cref="object.ToString"/> rather than as a list of properties.
 104    /// </summary>
 105    private static bool IsSingleValue(object item) =>
 58106        item.GetType().IsPrimitive || item.GetType().IsEnum ||
 58107        item is string or decimal or DateTime or DateTimeOffset or DateOnly or TimeOnly or TimeSpan or Guid;
 108}