From d3ae3d3c5cbcbc01d56e1424d29e9bacdcfb0afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E6=B0=B4?= <1123993881@qq.com> Date: Fri, 12 Dec 2025 18:28:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8Add=20null-check=20assertions=20an?= =?UTF-8?q?d=20`ExceptionExtensions`=20for=20parameter=20validation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AssertionExtensions.cs | 59 ++++++++++++++++++- .../ExceptionExtensions.cs | 41 +++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/libraries/TedToolkit.Assertions/ExceptionExtensions.cs 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 From 4ddfb0ad24efbe86fa00659c25098043098dcd9f Mon Sep 17 00:00:00 2001 From: nuke-bot Date: Fri, 12 Dec 2025 10:31:24 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=96=202025.12.12.2=20Released!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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