< Summary

Information
Class: ArturRios.Extensions.EnumerableExtensions
Assembly: ArturRios.Extensions
File(s): D:\Repositories\dotnet-extensions\src\EnumerableExtensions.cs
Line coverage
90%
Covered lines: 37
Uncovered lines: 4
Coverable lines: 41
Total lines: 91
Line coverage: 90.2%
Branch coverage
90%
Covered branches: 18
Total branches: 20
Branch coverage: 90%
Method coverage
100%
Covered methods: 3
Fully covered methods: 1
Total methods: 3
Method coverage: 100%
Full method coverage: 33.3%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsEmpty(...)83.33%6692.85%
IsNotEmpty(...)100%11100%
PrintContents(...)92.85%141488.46%

File(s)

D:\Repositories\dotnet-extensions\src\EnumerableExtensions.cs

#LineLine coverage
 1using System.Collections;
 2
 3namespace ArturRios.Extensions;
 4
 5/// <summary>
 6///     Provides extension methods for IEnumerable instances, including emptiness checks and printing contents.
 7/// </summary>
 8public static class EnumerableExtensions
 9{
 10    /// <summary>
 11    ///     Provides enumerable helpers for the given sequence.
 12    /// </summary>
 13    /// <param name="enumerable">The sequence to print.</param>
 14    extension(IEnumerable? enumerable)
 15    {
 16        /// <summary>
 17        ///     Determines whether the enumerable is null or has no elements.
 18        /// </summary>
 19        /// <returns>True if empty or null; otherwise false.</returns>
 20        public bool IsEmpty()
 621        {
 622            switch (enumerable)
 23            {
 24                case null:
 225                    return true;
 26                case ICollection collection:
 027                    return collection.Count == 0;
 28                default:
 429                {
 430                    var enumerator = enumerable.GetEnumerator();
 31                    try
 432                    {
 433                        return !enumerator.MoveNext();
 34                    }
 35                    finally
 436                    {
 437                        if (enumerator is IDisposable disposable)
 438                        {
 439                            disposable.Dispose();
 440                        }
 441                    }
 42                }
 43            }
 44        }
 45
 46        /// <summary>
 47        ///     Determines whether the enumerable contains at least one element.
 48        /// </summary>
 49        /// <returns>True if not empty; otherwise false.</returns>
 350        public bool IsNotEmpty() => !IsEmpty(enumerable);
 51
 52        /// <summary>
 53        ///     Prints each item in the enumerable. For complex objects, prints property name and value pairs.
 54        /// </summary>
 55        public void PrintContents()
 356        {
 357            if (enumerable is null)
 158            {
 159                Console.WriteLine("Enumerable is null");
 60
 161                return;
 62            }
 63
 1664            foreach (var item in enumerable)
 565            {
 566                if (item == null)
 067                {
 068                    Console.WriteLine("null");
 69
 070                    continue;
 71                }
 72
 573                var type = item.GetType();
 74
 575                if (type.IsPrimitive || item is string || item is decimal)
 376                {
 377                    Console.WriteLine(item);
 378                }
 79                else
 280                {
 281                    var properties = type.GetProperties();
 1882                    foreach (var prop in properties)
 683                    {
 684                        var value = prop.GetValue(item, null);
 685                        Console.WriteLine($"{prop.Name}: {value}");
 686                    }
 287                }
 588            }
 289        }
 90    }
 91}