66using FluentAssertions . Formatting ;
77using FluentAssertions . Primitives ;
88using Newtonsoft . Json . Linq ;
9+ using System . Text . RegularExpressions ;
10+ using System ;
911
1012namespace FluentAssertions . Json
1113{
@@ -180,6 +182,67 @@ public AndConstraint<JTokenAssertions> NotHaveValue(string unexpected, string be
180182 return new AndConstraint < JTokenAssertions > ( this ) ;
181183 }
182184
185+ /// <summary>
186+ /// Asserts that the current <see cref="JToken" /> matches the specified <paramref name="regularExpression" /> regular expression pattern.
187+ /// </summary>
188+ /// <param name="regularExpression">The expected regular expression pattern</param>
189+ public AndConstraint < JTokenAssertions > MatchRegex ( string regularExpression )
190+ {
191+ return MatchRegex ( regularExpression , string . Empty ) ;
192+ }
193+
194+ /// <summary>
195+ /// Asserts that the current <see cref="JToken" /> matches the specified <paramref name="regularExpression" /> regular expression pattern.
196+ /// </summary>
197+ /// <param name="regularExpression">The expected regular expression pattern</param>
198+ /// <param name="because">
199+ /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
200+ /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
201+ /// </param>
202+ /// <param name="becauseArgs">
203+ /// Zero or more objects to format using the placeholders in <see paramref="because" />.
204+ /// </param>
205+ public AndConstraint < JTokenAssertions > MatchRegex ( string regularExpression , string because , params object [ ] becauseArgs )
206+ {
207+ if ( regularExpression == null ) {
208+ throw new ArgumentNullException ( nameof ( regularExpression ) , "MatchRegex does not support <null> pattern" ) ;
209+ }
210+
211+ Execute . Assertion
212+ . ForCondition ( Regex . IsMatch ( Subject . Value < string > ( ) , regularExpression ?? "" ) )
213+ . BecauseOf ( because , becauseArgs )
214+ . FailWith ( "Expected {context:JSON property} {0} to match regex pattern {1}{reason}, but found {2}." ,
215+ Subject . Path , regularExpression , Subject . Value < string > ( ) ) ;
216+
217+ return new AndConstraint < JTokenAssertions > ( this ) ;
218+ }
219+
220+ /// <summary>
221+ /// Asserts that the current <see cref="JToken" /> does not match the specified <paramref name="regularExpression" /> regular expression pattern.
222+ /// </summary>
223+ /// <param name="regularExpression">The expected regular expression pattern</param>
224+ /// <param name="because">
225+ /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
226+ /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
227+ /// </param>
228+ /// <param name="becauseArgs">
229+ /// Zero or more objects to format using the placeholders in <see paramref="because" />.
230+ /// </param>
231+ public AndConstraint < JTokenAssertions > NotMatchRegex ( string regularExpression , string because = "" , params object [ ] becauseArgs )
232+ {
233+ if ( regularExpression == null ) {
234+ throw new ArgumentNullException ( nameof ( regularExpression ) , "MatchRegex does not support <null> pattern" ) ;
235+ }
236+
237+ Execute . Assertion
238+ . ForCondition ( ! Regex . IsMatch ( Subject . Value < string > ( ) , regularExpression ) )
239+ . BecauseOf ( because , becauseArgs )
240+ . FailWith ( "Did not expect {context:JSON property} {0} to match regex pattern {1}{reason}." ,
241+ Subject . Path , regularExpression , Subject . Value < string > ( ) ) ;
242+
243+ return new AndConstraint < JTokenAssertions > ( this ) ;
244+ }
245+
183246 /// <summary>
184247 /// Asserts that the current <see cref="JToken" /> has a direct child element with the specified
185248 /// <paramref name="expected" /> name.
@@ -397,6 +460,5 @@ public string Format(JToken value, bool useLineBreaks = false)
397460 UseLineBreaks = useLineBreaks
398461 } , null ) ;
399462 }
400-
401463 }
402464}
0 commit comments