1- using System . Runtime . CompilerServices ;
1+ using System . Diagnostics . CodeAnalysis ;
2+ using System . Runtime . CompilerServices ;
23using TedToolkit . Assertions . Assertions ;
34using 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}
0 commit comments