Skip to content

Commit d6c7286

Browse files
authored
feat: Added additional Methods, for better support (#523)
* feat: Added additional Methods, for better support * refactor(test): ArgumentExceptionPolyfills and expand tests Simplified collection count and duplicate checks in ArgumentExceptionPolyfills. Improved default value comparison. Added comprehensive unit tests for null and empty collections across all overloads. Updated test for Guid default value. Suppressed IDE1006 warnings in test project. * fix: Improve argument validation in ArgumentException polyfills and update README for .NET Framework support
1 parent dfea67a commit d6c7286

18 files changed

+2846
-208
lines changed

README.md

Lines changed: 161 additions & 204 deletions
Large diffs are not rendered by default.

src/NetEvolve.Arguments/ArgumentExceptionPolyfill.cs

Lines changed: 636 additions & 0 deletions
Large diffs are not rendered by default.

src/NetEvolve.Arguments/ArgumentOutOfRangeExceptionPolyfills.cs

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,166 @@ public static void ThrowIfLessThanOrEqual<T>(
331331
}
332332
}
333333
#endif
334+
335+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is outside the specified range [<paramref name="min"/>, <paramref name="max"/>].</summary>
336+
/// <typeparam name="T">The type of the objects to compare.</typeparam>
337+
/// <param name="value">The argument to validate as within the range.</param>
338+
/// <param name="min">The minimum allowed value (inclusive).</param>
339+
/// <param name="max">The maximum allowed value (inclusive).</param>
340+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
341+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is outside the range [<paramref name="min"/>, <paramref name="max"/>].</exception>
342+
public static void ThrowIfOutOfRange<T>(
343+
T value,
344+
T min,
345+
T max,
346+
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
347+
)
348+
where T : IComparable<T>
349+
{
350+
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
351+
{
352+
throw new ArgumentOutOfRangeException(
353+
paramName,
354+
value,
355+
$"Value must be between {min} and {max} (inclusive)."
356+
);
357+
}
358+
}
359+
360+
#if NET8_0_OR_GREATER
361+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current time.</summary>
362+
/// <param name="value">The <see cref="DateTimeOffset"/> argument to validate as not in the past.</param>
363+
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
364+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
365+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the past.</exception>
366+
public static void ThrowIfInPast(
367+
DateTimeOffset value,
368+
TimeProvider? timeProvider = null,
369+
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
370+
)
371+
{
372+
var now = (timeProvider ?? TimeProvider.System).GetUtcNow();
373+
if (value < now)
374+
{
375+
throw new ArgumentOutOfRangeException(
376+
paramName,
377+
value,
378+
$"Value must not be in the past. Current time: {now}."
379+
);
380+
}
381+
}
382+
383+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the future relative to the current time.</summary>
384+
/// <param name="value">The <see cref="DateTimeOffset"/> argument to validate as not in the future.</param>
385+
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
386+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
387+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the future.</exception>
388+
public static void ThrowIfInFuture(
389+
DateTimeOffset value,
390+
TimeProvider? timeProvider = null,
391+
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
392+
)
393+
{
394+
var now = (timeProvider ?? TimeProvider.System).GetUtcNow();
395+
if (value > now)
396+
{
397+
throw new ArgumentOutOfRangeException(
398+
paramName,
399+
value,
400+
$"Value must not be in the future. Current time: {now}."
401+
);
402+
}
403+
}
404+
405+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current time.</summary>
406+
/// <param name="value">The <see cref="DateTime"/> argument to validate as not in the past.</param>
407+
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
408+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
409+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the past.</exception>
410+
public static void ThrowIfInPast(
411+
DateTime value,
412+
TimeProvider? timeProvider = null,
413+
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
414+
)
415+
{
416+
var now = (timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime;
417+
if (value < now)
418+
{
419+
throw new ArgumentOutOfRangeException(
420+
paramName,
421+
value,
422+
$"Value must not be in the past. Current time: {now}."
423+
);
424+
}
425+
}
426+
427+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the future relative to the current time.</summary>
428+
/// <param name="value">The <see cref="DateTime"/> argument to validate as not in the future.</param>
429+
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
430+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
431+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the future.</exception>
432+
public static void ThrowIfInFuture(
433+
DateTime value,
434+
TimeProvider? timeProvider = null,
435+
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
436+
)
437+
{
438+
var now = (timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime;
439+
if (value > now)
440+
{
441+
throw new ArgumentOutOfRangeException(
442+
paramName,
443+
value,
444+
$"Value must not be in the future. Current time: {now}."
445+
);
446+
}
447+
}
448+
#endif
449+
450+
#if NET8_0_OR_GREATER
451+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current date.</summary>
452+
/// <param name="value">The <see cref="DateOnly"/> argument to validate as not in the past.</param>
453+
/// <param name="timeProvider">The time provider to use for getting the current date. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
454+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
455+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the past.</exception>
456+
public static void ThrowIfInPast(
457+
DateOnly value,
458+
TimeProvider? timeProvider = null,
459+
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
460+
)
461+
{
462+
var today = DateOnly.FromDateTime((timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime);
463+
if (value < today)
464+
{
465+
throw new ArgumentOutOfRangeException(
466+
paramName,
467+
value,
468+
$"Value must not be in the past. Current date: {today}."
469+
);
470+
}
471+
}
472+
473+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the future relative to the current date.</summary>
474+
/// <param name="value">The <see cref="DateOnly"/> argument to validate as not in the future.</param>
475+
/// <param name="timeProvider">The time provider to use for getting the current date. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
476+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
477+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the future.</exception>
478+
public static void ThrowIfInFuture(
479+
DateOnly value,
480+
TimeProvider? timeProvider = null,
481+
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
482+
)
483+
{
484+
var today = DateOnly.FromDateTime((timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime);
485+
if (value > today)
486+
{
487+
throw new ArgumentOutOfRangeException(
488+
paramName,
489+
value,
490+
$"Value must not be in the future. Current date: {today}."
491+
);
492+
}
493+
}
494+
#endif
334495
}
335496
}

0 commit comments

Comments
 (0)