diff --git a/Directory.Build.props b/Directory.Build.props index d63a977..4903cd2 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@  - 2025.12.12.1 + 2025.12.12.2 enable enable preview diff --git a/src/libraries/TedToolkit.Assertions/AssertionExtensions.cs b/src/libraries/TedToolkit.Assertions/AssertionExtensions.cs index 6dbc691..7b618a6 100644 --- a/src/libraries/TedToolkit.Assertions/AssertionExtensions.cs +++ b/src/libraries/TedToolkit.Assertions/AssertionExtensions.cs @@ -1,4 +1,5 @@ -using System.Runtime.CompilerServices; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using TedToolkit.Assertions.Assertions; using TedToolkit.Assertions.Constraints; @@ -86,4 +87,60 @@ public static ObjectAssertion Could(this T value, return new ObjectAssertion(value, valueName, AssertionType.Could, new CallerInfo(memberName, filePath, lineNumber)); } + + /// + /// The must assertion with null check + /// + /// + /// + /// + /// + /// + /// + /// + public static AndConstraint MustNotNull( + [NotNull] this T value, + [CallerArgumentExpression(nameof(value))] + string valueName = "", + [CallerMemberName] string memberName = "", + [CallerFilePath] string filePath = "", + [CallerLineNumber] int lineNumber = 0) + where T : class + + { + ThrowIfInvalid(value); + return new(new ObjectAssertion( + value.ThrowIfNull(valueName), + valueName, + AssertionType.Must, + new CallerInfo(memberName, filePath, lineNumber))); + } + + + /// + /// The must assertion with null check + /// + /// + /// + /// + /// + /// + /// + /// + public static AndConstraint MustNotNull( + [NotNull] this T? value, + [CallerArgumentExpression(nameof(value))] + string valueName = "", + [CallerMemberName] string memberName = "", + [CallerFilePath] string filePath = "", + [CallerLineNumber] int lineNumber = 0) + where T : struct + { + ThrowIfInvalid(value); + return new(new ObjectAssertion( + value.ThrowIfNull(valueName), + valueName, + AssertionType.Must, + new CallerInfo(memberName, filePath, lineNumber))); + } } \ No newline at end of file diff --git a/src/libraries/TedToolkit.Assertions/ExceptionExtensions.cs b/src/libraries/TedToolkit.Assertions/ExceptionExtensions.cs new file mode 100644 index 0000000..7df8032 --- /dev/null +++ b/src/libraries/TedToolkit.Assertions/ExceptionExtensions.cs @@ -0,0 +1,41 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace TedToolkit.Assertions; + +/// +/// Some exceptions extensions. +/// +public static class ExceptionExtensions +{ + /// + /// Throw ArgumentNullException if obj is null. + /// + /// The object + /// the name of obj + /// exceptions + [return: NotNull] + public static T ThrowIfNull( + [NotNull] this T obj, + [CallerArgumentExpression(nameof(obj))] + string objName = "") + where T : class + { + return obj ?? throw new ArgumentNullException(objName); + } + + /// + /// Throw ArgumentNullException if obj is null. + /// + /// The object + /// the name of obj + /// exceptions + public static T ThrowIfNull( + [NotNull] this T? obj, + [CallerArgumentExpression(nameof(obj))] + string objName = "") + where T : struct + { + return obj ?? throw new ArgumentNullException(objName); + } +} \ No newline at end of file