< Summary

Information
Class: MyNet.Generator.RandomGeneratorExtensions
Assembly: MyNet.Generator
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Generator/Extensions/RandomGeneratorExtensions.cs
Tag: 323_28699572109
Line coverage
92%
Covered lines: 12
Uncovered lines: 1
Coverable lines: 13
Total lines: 84
Line coverage: 92.3%
Branch coverage
60%
Covered branches: 6
Total branches: 10
Branch coverage: 60%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SafeSubset(...)100%11100%
TryItem(...)100%22100%
Date(...)50%44100%
Date(...)100%210%
SafeDate(...)50%44100%
GenerateDate(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Generator/Extensions/RandomGeneratorExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="RandomGeneratorExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using MyNet.Primitives;
 10using MyNet.Primitives.Intervals;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Generator;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16public static class RandomGeneratorExtensions
 17{
 18    extension(IRandomGenerator random)
 19    {
 20        /// <summary>
 21        /// Generates a random subset of the provided list with a specified count. The method first checks if the input 
 22        /// </summary>
 23        /// <param name="list">The list from which to generate a random subset.</param>
 24        /// <param name="count">The number of items to include in the subset.</param>
 25        /// <typeparam name="T">The type of items in the list.</typeparam>
 26        /// <returns>A random subset of the list with the specified count.</returns>
 27        public IReadOnlyList<T> SafeSubset<T>(IReadOnlyCollection<T> list, int count)
 28        {
 329            ArgumentNullException.ThrowIfNull(list);
 30
 331            var safeCount = count.SafeClamp(0, list.Count);
 32
 333            return random.Subset(list, safeCount);
 34        }
 35
 36        /// <summary>
 37        /// Attempts to retrieve a random item from the provided list. If the list is empty, it returns the default valu
 38        /// </summary>
 39        /// <param name="list">The list from which to retrieve a random item.</param>
 40        /// <typeparam name="T">The type of items in the list.</typeparam>
 41        /// <returns>A random item from the list, or the default value if the list is empty.</returns>
 42        /// <exception cref="ArgumentNullException">Thrown if the list is null.</exception>
 43        public T? TryItem<T>(IReadOnlyCollection<T> list)
 44        {
 645            ArgumentNullException.ThrowIfNull(list);
 46
 647            return list.Count == 0 ? default : random.Item(list);
 48        }
 49
 50        /// <summary>
 51        /// Generates a random DateTime value that falls within the specified interval. The method extracts the start an
 52        /// </summary>
 53        /// <param name="interval">The interval specifying the start and end DateTime values.</param>
 54        /// <returns>A random DateTime value within the specified interval.</returns>
 6055        public DateTime Date(IInterval<DateTime> interval) => random.SafeDate(interval.Start?.Value, interval.End?.Value
 56
 57        /// <summary>
 58        /// Generates a random DateTime value that falls within the specified minimum and maximum range. If the minimum 
 59        /// </summary>
 60        /// <param name="min">The minimum DateTime value.</param>
 61        /// <param name="max">The maximum DateTime value.</param>
 62        /// <returns>A random DateTime value within the specified range.</returns>
 063        public DateTime Date(DateTime? min = null, DateTime? max = null) => random.SafeDate(min, max);
 64
 65        /// <summary>
 66        /// Generates a random DateTime value that falls within the specified minimum and maximum range, ensuring that t
 67        /// </summary>
 68        /// <param name="min">The minimum DateTime value.</param>
 69        /// <param name="max">The maximum DateTime value.</param>
 70        /// <returns>A random DateTime value within the specified range.</returns>
 71        public DateTime SafeDate(DateTime? min = null, DateTime? max = null)
 72        {
 6373            var minMax = min.MinMax(max);
 6374            return GenerateDate(
 6375                random,
 6376                minMax.Min ?? DateTime.MinValue,
 6377                minMax.Max ?? DateTime.MaxValue);
 78        }
 79    }
 80
 81    private static DateTime GenerateDate(IRandomGenerator generator, DateTime minimum, DateTime maximum)
 6382        => generator.Date(minimum, maximum);
 83}
 84