Skip to content

Commit 225544c

Browse files
authored
Merge pull request #77 from Ted-Jin-Lab/development
✨Add null-check assertions and `ExceptionExtensions` for parameter va…
2 parents 6b76000 + 4ddfb0a commit 225544c

3 files changed

Lines changed: 100 additions & 2 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>2025.12.12.1</Version>
4+
<Version>2025.12.12.2</Version>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<LangVersion>preview</LangVersion>

src/libraries/TedToolkit.Assertions/AssertionExtensions.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Runtime.CompilerServices;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Runtime.CompilerServices;
23
using TedToolkit.Assertions.Assertions;
34
using TedToolkit.Assertions.Constraints;
45

@@ -86,4 +87,60 @@ public static ObjectAssertion<T> Could<T>(this T value,
8687
return new ObjectAssertion<T>(value, valueName, AssertionType.Could,
8788
new CallerInfo(memberName, filePath, lineNumber));
8889
}
90+
91+
/// <summary>
92+
/// The must assertion with null check
93+
/// </summary>
94+
/// <param name="value"></param>
95+
/// <param name="valueName"></param>
96+
/// <param name="memberName"></param>
97+
/// <param name="filePath"></param>
98+
/// <param name="lineNumber"></param>
99+
/// <typeparam name="T"></typeparam>
100+
/// <returns></returns>
101+
public static AndConstraint<T> MustNotNull<T>(
102+
[NotNull] this T value,
103+
[CallerArgumentExpression(nameof(value))]
104+
string valueName = "",
105+
[CallerMemberName] string memberName = "",
106+
[CallerFilePath] string filePath = "",
107+
[CallerLineNumber] int lineNumber = 0)
108+
where T : class
109+
110+
{
111+
ThrowIfInvalid(value);
112+
return new(new ObjectAssertion<T>(
113+
value.ThrowIfNull(valueName),
114+
valueName,
115+
AssertionType.Must,
116+
new CallerInfo(memberName, filePath, lineNumber)));
117+
}
118+
119+
120+
/// <summary>
121+
/// The must assertion with null check
122+
/// </summary>
123+
/// <param name="value"></param>
124+
/// <param name="valueName"></param>
125+
/// <param name="memberName"></param>
126+
/// <param name="filePath"></param>
127+
/// <param name="lineNumber"></param>
128+
/// <typeparam name="T"></typeparam>
129+
/// <returns></returns>
130+
public static AndConstraint<T> MustNotNull<T>(
131+
[NotNull] this T? value,
132+
[CallerArgumentExpression(nameof(value))]
133+
string valueName = "",
134+
[CallerMemberName] string memberName = "",
135+
[CallerFilePath] string filePath = "",
136+
[CallerLineNumber] int lineNumber = 0)
137+
where T : struct
138+
{
139+
ThrowIfInvalid(value);
140+
return new(new ObjectAssertion<T>(
141+
value.ThrowIfNull(valueName),
142+
valueName,
143+
AssertionType.Must,
144+
new CallerInfo(memberName, filePath, lineNumber)));
145+
}
89146
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace TedToolkit.Assertions;
5+
6+
/// <summary>
7+
/// Some exceptions extensions.
8+
/// </summary>
9+
public static class ExceptionExtensions
10+
{
11+
/// <summary>
12+
/// Throw ArgumentNullException if obj is null.
13+
/// </summary>
14+
/// <param name="obj">The object</param>
15+
/// <param name="objName">the name of obj</param>
16+
/// <exception cref="ArgumentNullException">exceptions</exception>
17+
[return: NotNull]
18+
public static T ThrowIfNull<T>(
19+
[NotNull] this T obj,
20+
[CallerArgumentExpression(nameof(obj))]
21+
string objName = "")
22+
where T : class
23+
{
24+
return obj ?? throw new ArgumentNullException(objName);
25+
}
26+
27+
/// <summary>
28+
/// Throw ArgumentNullException if obj is null.
29+
/// </summary>
30+
/// <param name="obj">The object</param>
31+
/// <param name="objName">the name of obj</param>
32+
/// <exception cref="ArgumentNullException">exceptions</exception>
33+
public static T ThrowIfNull<T>(
34+
[NotNull] this T? obj,
35+
[CallerArgumentExpression(nameof(obj))]
36+
string objName = "")
37+
where T : struct
38+
{
39+
return obj ?? throw new ArgumentNullException(objName);
40+
}
41+
}

0 commit comments

Comments
 (0)