// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; #nullable enable namespace Microsoft.Toolkit.Diagnostics { /// /// Helper methods to throw exceptions /// [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] internal static partial class ThrowHelper { /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or empty, was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or empty, was {(text is null ? "null" : "empty")}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or whitespace, was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(string text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be empty, was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be empty"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsWhitespace(string text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be whitespace, was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be whitespace, was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size equal to {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not have a size equal to {size}, was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size over {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size of at least {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size less than {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name) { ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size less than or equal to {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } } }