/
StringAssertions.cs
1489 lines (1332 loc) · 74.1 KB
/
StringAssertions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using FluentAssertions.Common;
using FluentAssertions.Execution;
using JetBrains.Annotations;
namespace FluentAssertions.Primitives
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="string"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class StringAssertions : StringAssertions<StringAssertions>
{
/// <summary>
/// Initializes a new instance of the <see cref="StringAssertions"/> class.
/// </summary>
public StringAssertions(string value)
: base(value)
{
}
}
/// <summary>
/// Contains a number of methods to assert that a <see cref="string"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class StringAssertions<TAssertions> : ReferenceTypeAssertions<string, TAssertions>
where TAssertions : StringAssertions<TAssertions>
{
/// <summary>
/// Initializes a new instance of the <see cref="StringAssertions{TAssertions}"/> class.
/// </summary>
public StringAssertions(string value)
: base(value)
{
}
/// <summary>
/// Asserts that a string is exactly the same as another string, including the casing and any leading or trailing whitespace.
/// </summary>
/// <param name="expected">The expected string.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> Be(string expected, string because = "", params object[] becauseArgs)
{
var stringEqualityValidator = new StringEqualityValidator(Subject, expected, StringComparison.Ordinal, because, becauseArgs);
stringEqualityValidator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the <see cref="string"/> is one of the specified <paramref name="validValues"/>.
/// </summary>
/// <param name="validValues">
/// The values that are valid.
/// </param>
public AndConstraint<TAssertions> BeOneOf(params string[] validValues)
{
return BeOneOf(validValues, string.Empty);
}
/// <summary>
/// Asserts that the <see cref="string"/> is one of the specified <paramref name="validValues"/>.
/// </summary>
/// <param name="validValues">
/// The values that are valid.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> BeOneOf(IEnumerable<string> validValues, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(validValues.Contains(Subject))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} to be one of {0}{reason}, but found {1}.", validValues, Subject);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string is exactly the same as another string, including any leading or trailing whitespace, with
/// the exception of the casing.
/// </summary>
/// <param name="expected">
/// The string that the subject is expected to be equivalent to.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> BeEquivalentTo(string expected, string because = "",
params object[] becauseArgs)
{
var expectation = new StringEqualityValidator(
Subject, expected, StringComparison.OrdinalIgnoreCase, because, becauseArgs);
expectation.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string is not exactly the same as another string, including any leading or trailing whitespace, with
/// the exception of the casing.
/// </summary>
/// <param name="unexpected">
/// The string that the subject is not expected to be equivalent to.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotBeEquivalentTo(string unexpected, string because = "",
params object[] becauseArgs)
{
bool notEquivalent;
using (var scope = new AssertionScope())
{
Subject.Should().BeEquivalentTo(unexpected);
notEquivalent = scope.Discard().Any();
}
Execute.Assertion
.ForCondition(notEquivalent)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} not to be equivalent to {0}{reason}, but they are.", unexpected);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string is not exactly the same as the specified <paramref name="unexpected"/>,
/// including the casing and any leading or trailing whitespace.
/// </summary>
/// <param name="unexpected">The string that the subject is not expected to be equivalent to.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotBe(string unexpected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} not to be {0}{reason}.", unexpected);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string matches the <paramref name="wildcardPattern"/>.
/// </summary>
/// <param name="wildcardPattern">
/// The pattern to match against the subject. This parameter can contain a combination of literal text and wildcard
/// (* and ?) characters, but it doesn't support regular expressions.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <remarks>
/// <paramref name="wildcardPattern"/> can be a combination of literal and wildcard characters,
/// but it doesn't support regular expressions. The following wildcard specifiers are permitted in
/// <paramref name="wildcardPattern"/>.
/// <list type="table">
/// <listheader>
/// <term>Wildcard character</term>
/// <description>Description</description>
/// </listheader>
/// <item>
/// <term>* (asterisk)</term>
/// <description>Zero or more characters in that position.</description>
/// </item>
/// <item>
/// <term>? (question mark)</term>
/// <description>Exactly one character in that position.</description>
/// </item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="wildcardPattern"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="wildcardPattern"/> is empty.</exception>
public AndConstraint<TAssertions> Match(string wildcardPattern, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the BeNull method.");
if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.", nameof(wildcardPattern));
}
var stringWildcardMatchingValidator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs);
stringWildcardMatchingValidator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string does not match the <paramref name="wildcardPattern"/>.
/// </summary>
/// <param name="wildcardPattern">
/// The pattern to match against the subject. This parameter can contain a combination literal text and wildcard of
/// (* and ?) characters, but it doesn't support regular expressions.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <remarks>
/// <paramref name="wildcardPattern"/> can be a combination of literal and wildcard characters,
/// but it doesn't support regular expressions. The following wildcard specifiers are permitted in
/// <paramref name="wildcardPattern"/>.
/// <list type="table">
/// <listheader>
/// <term>Wildcard character</term>
/// <description>Description</description>
/// </listheader>
/// <item>
/// <term>* (asterisk)</term>
/// <description>Zero or more characters in that position.</description>
/// </item>
/// <item>
/// <term>? (question mark)</term>
/// <description>Exactly one character in that position.</description>
/// </item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="wildcardPattern"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="wildcardPattern"/> is empty.</exception>
public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the NotBeNull method.");
if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.", nameof(wildcardPattern));
}
new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs)
{
Negate = true
}.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string matches the <paramref name="wildcardPattern"/>.
/// </summary>
/// <param name="wildcardPattern">
/// The pattern to match against the subject. This parameter can contain a combination of literal text and wildcard
/// (* and ?) characters, but it doesn't support regular expressions.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <remarks>
/// <paramref name="wildcardPattern"/> can be a combination of literal and wildcard characters,
/// but it doesn't support regular expressions. The following wildcard specifiers are permitted in
/// <paramref name="wildcardPattern"/>.
/// <list type="table">
/// <listheader>
/// <term>Wildcard character</term>
/// <description>Description</description>
/// </listheader>
/// <item>
/// <term>* (asterisk)</term>
/// <description>Zero or more characters in that position.</description>
/// </item>
/// <item>
/// <term>? (question mark)</term>
/// <description>Exactly one character in that position.</description>
/// </item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="wildcardPattern"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="wildcardPattern"/> is empty.</exception>
public AndConstraint<TAssertions> MatchEquivalentOf(string wildcardPattern, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the BeNull method.");
if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.", nameof(wildcardPattern));
}
var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs)
{
IgnoreCase = true,
IgnoreNewLineDifferences = true
};
validator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string does not match the <paramref name="wildcardPattern"/>.
/// </summary>
/// <param name="wildcardPattern">
/// The pattern to match against the subject. This parameter can contain a combination of literal text and wildcard
/// (* and ?) characters, but it doesn't support regular expressions.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <remarks>
/// <paramref name="wildcardPattern"/> can be a combination of literal and wildcard characters,
/// but it doesn't support regular expressions. The following wildcard specifiers are permitted in
/// <paramref name="wildcardPattern"/>.
/// <list type="table">
/// <listheader>
/// <term>Wildcard character</term>
/// <description>Description</description>
/// </listheader>
/// <item>
/// <term>* (asterisk)</term>
/// <description>Zero or more characters in that position.</description>
/// </item>
/// <item>
/// <term>? (question mark)</term>
/// <description>Exactly one character in that position.</description>
/// </item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="wildcardPattern"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="wildcardPattern"/> is empty.</exception>
public AndConstraint<TAssertions> NotMatchEquivalentOf(string wildcardPattern, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the NotBeNull method.");
if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.", nameof(wildcardPattern));
}
var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs)
{
IgnoreCase = true,
IgnoreNewLineDifferences = true,
Negate = true
};
validator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string matches a regular expression with expected occurrence
/// </summary>
/// <param name="regularExpression">
/// The regular expression with which the subject is matched.
/// </param>
/// <param name="occurrenceConstraint">
/// A constraint specifying the expected amount of times a regex should match a string.
/// It can be created by invoking static methods Once, Twice, Thrice, or Times(int)
/// on the classes <see cref="Exactly"/>, <see cref="AtLeast"/>, <see cref="MoreThan"/>, <see cref="AtMost"/>, and <see cref="LessThan"/>.
/// For example, <see cref="Exactly.Times(int)"/> or <see cref="LessThan.Twice()"/>.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> MatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression,
OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression),
"Cannot match string against <null>. Provide a regex pattern or use the BeNull method.");
Regex regex;
try
{
regex = new Regex(regularExpression);
}
catch (ArgumentException)
{
Execute.Assertion.FailWith("Cannot match {context:string} against {0} because it is not a valid regular expression.",
regularExpression);
return new AndConstraint<TAssertions>((TAssertions)this);
}
return MatchRegex(regex, occurrenceConstraint, because, becauseArgs);
}
/// <summary>
/// Asserts that a string matches a regular expression.
/// </summary>
/// <param name="regularExpression">
/// The regular expression with which the subject is matched.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> MatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression,
string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression), "Cannot match string against <null>. Provide a regex pattern or use the BeNull method.");
Regex regex;
try
{
regex = new Regex(regularExpression);
}
catch (ArgumentException)
{
Execute.Assertion.FailWith("Cannot match {context:string} against {0} because it is not a valid regular expression.",
regularExpression);
return new AndConstraint<TAssertions>((TAssertions)this);
}
return MatchRegex(regex, because, becauseArgs);
}
/// <summary>
/// Asserts that a string matches a regular expression with expected occurrence
/// </summary>
/// <param name="regularExpression">
/// The regular expression with which the subject is matched.
/// </param>
/// <param name="occurrenceConstraint">
/// A constraint specifying the expected amount of times a regex should match a string.
/// It can be created by invoking static methods Once, Twice, Thrice, or Times(int)
/// on the classes <see cref="Exactly"/>, <see cref="AtLeast"/>, <see cref="MoreThan"/>, <see cref="AtMost"/>, and <see cref="LessThan"/>.
/// For example, <see cref="Exactly.Times(int)"/> or <see cref="LessThan.Twice()"/>.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> MatchRegex(Regex regularExpression,
OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression),
"Cannot match string against <null>. Provide a regex pattern or use the BeNull method.");
var regexStr = regularExpression.ToString();
if (regexStr.Length == 0)
{
throw new ArgumentException(
"Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.",
nameof(regularExpression));
}
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.UsingLineBreaks
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} to match regex {0}{reason}, but it was <null>.", regexStr);
if (success)
{
int actual = regularExpression.Matches(Subject).Count;
Execute.Assertion
.ForConstraint(occurrenceConstraint, actual)
.UsingLineBreaks
.BecauseOf(because, becauseArgs)
.FailWith($"Expected {{context:string}} {{0}} to match regex {{1}} {{expectedOccurrence}}{{reason}}, " +
$"but found it {actual.Times()}.",
Subject, regexStr);
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string matches a regular expression.
/// </summary>
/// <param name="regularExpression">
/// The regular expression with which the subject is matched.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> MatchRegex(Regex regularExpression,
string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression),
"Cannot match string against <null>. Provide a regex pattern or use the BeNull method.");
var regexStr = regularExpression.ToString();
if (regexStr.Length == 0)
{
throw new ArgumentException(
"Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.",
nameof(regularExpression));
}
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.UsingLineBreaks
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} to match regex {0}{reason}, but it was <null>.", regexStr);
if (success)
{
Execute.Assertion
.ForCondition(regularExpression.IsMatch(Subject))
.BecauseOf(because, becauseArgs)
.UsingLineBreaks
.FailWith("Expected {context:string} to match regex {0}{reason}, but {1} does not match.", regexStr, Subject);
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string does not match a regular expression.
/// </summary>
/// <param name="regularExpression">
/// The regular expression with which the subject is matched.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotMatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression,
string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression), "Cannot match string against <null>. Provide a regex pattern or use the NotBeNull method.");
Regex regex;
try
{
regex = new Regex(regularExpression);
}
catch (ArgumentException)
{
Execute.Assertion.FailWith("Cannot match {context:string} against {0} because it is not a valid regular expression.",
regularExpression);
return new AndConstraint<TAssertions>((TAssertions)this);
}
return NotMatchRegex(regex, because, becauseArgs);
}
/// <summary>
/// Asserts that a string does not match a regular expression.
/// </summary>
/// <param name="regularExpression">
/// The regular expression with which the subject is matched.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotMatchRegex(Regex regularExpression, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression),
"Cannot match string against <null>. Provide a regex pattern or use the NotBeNull method.");
var regexStr = regularExpression.ToString();
if (regexStr.Length == 0)
{
throw new ArgumentException(
"Cannot match string against an empty regex pattern. Provide a regex pattern or use the NotBeEmpty method.",
nameof(regularExpression));
}
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.UsingLineBreaks
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} to not match regex {0}{reason}, but it was <null>.", regexStr);
if (success)
{
Execute.Assertion
.ForCondition(!regularExpression.IsMatch(Subject))
.BecauseOf(because, becauseArgs)
.UsingLineBreaks
.FailWith("Did not expect {context:string} to match regex {0}{reason}, but {1} matches.", regexStr, Subject);
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string starts exactly with the specified <paramref name="expected"/> value,
/// including the casing and any leading or trailing whitespace.
/// </summary>
/// <param name="expected">The string that the subject is expected to start with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> StartWith(string expected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare start of string with <null>.");
var stringStartValidator = new StringStartValidator(Subject, expected, StringComparison.Ordinal, because, becauseArgs);
stringStartValidator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string does not start with the specified <paramref name="unexpected"/> value,
/// including the casing and any leading or trailing whitespace.
/// </summary>
/// <param name="unexpected">The string that the subject is not expected to start with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotStartWith(string unexpected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare start of string with <null>.");
var negatedStringStartValidator = new NegatedStringStartValidator(Subject, unexpected, StringComparison.Ordinal, because, becauseArgs);
negatedStringStartValidator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string starts with the specified <paramref name="expected"/>,
/// including any leading or trailing whitespace, with the exception of the casing.
/// </summary>
/// <param name="expected">The string that the subject is expected to start with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> StartWithEquivalentOf(string expected, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare string start equivalence with <null>.");
var stringStartValidator = new StringStartValidator(Subject, expected, StringComparison.OrdinalIgnoreCase, because, becauseArgs);
stringStartValidator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string does not start with the specified <paramref name="unexpected"/> value,
/// including any leading or trailing whitespace, with the exception of the casing.
/// </summary>
/// <param name="unexpected">The string that the subject is not expected to start with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotStartWithEquivalentOf(string unexpected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare start of string with <null>.");
var negatedStringStartValidator = new NegatedStringStartValidator(Subject, unexpected, StringComparison.OrdinalIgnoreCase, because, becauseArgs);
negatedStringStartValidator.Validate();
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string ends exactly with the specified <paramref name="expected"/>,
/// including the casing and any leading or trailing whitespace.
/// </summary>
/// <param name="expected">The string that the subject is expected to end with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> EndWith(string expected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare string end with <null>.");
bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith("Expected {context:string} {0} to end with {1}{reason}.", Subject, expected);
if (success)
{
success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.Length >= expected.Length)
.FailWith("Expected {context:string} to end with {0}{reason}, but {1} is too short.", expected, Subject);
if (success)
{
Execute.Assertion
.ForCondition(Subject.EndsWith(expected, StringComparison.Ordinal))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} {0} to end with {1}{reason}.", Subject, expected);
}
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string does not end exactly with the specified <paramref name="unexpected"/>,
/// including the casing and any leading or trailing whitespace.
/// </summary>
/// <param name="unexpected">The string that the subject is not expected to end with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotEndWith(string unexpected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare end of string with <null>.");
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} that does not end with {1}{reason}, but found {0}.", Subject, unexpected);
if (success)
{
Execute.Assertion
.ForCondition(!Subject.EndsWith(unexpected, StringComparison.Ordinal))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} {0} not to end with {1}{reason}.", Subject, unexpected);
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string ends with the specified <paramref name="expected"/>,
/// including any leading or trailing whitespace, with the exception of the casing.
/// </summary>
/// <param name="expected">The string that the subject is expected to end with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> EndWithEquivalentOf(string expected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare string end equivalence with <null>.");
bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith(
"Expected {context:string} that ends with equivalent of {0}{reason}, but found {1}.", expected, Subject);
if (success)
{
success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.Length >= expected.Length)
.FailWith(
"Expected {context:string} to end with equivalent of {0}{reason}, but {1} is too short.",
expected, Subject);
if (success)
{
Execute.Assertion
.ForCondition(Subject.EndsWith(expected, StringComparison.OrdinalIgnoreCase))
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:string} that ends with equivalent of {0}{reason}, but found {1}.",
expected, Subject);
}
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string does not end with the specified <paramref name="unexpected"/>,
/// including any leading or trailing whitespace, with the exception of the casing.
/// </summary>
/// <param name="unexpected">The string that the subject is not expected to end with.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotEndWithEquivalentOf(string unexpected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare end of string with <null>.");
var success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith(
"Expected {context:string} that does not end with equivalent of {0}{reason}, but found {1}.",
unexpected, Subject);
if (success)
{
Execute.Assertion
.ForCondition(!Subject.EndsWith(unexpected, StringComparison.OrdinalIgnoreCase))
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:string} that does not end with equivalent of {0}{reason}, but found {1}.",
unexpected, Subject);
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string contains another (fragment of a) string.
/// </summary>
/// <param name="expected">
/// The (fragment of a) string that the current string should contain.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> Contain(string expected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against <null>.");
if (expected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected));
}
Execute.Assertion
.ForCondition(Contains(Subject, expected, StringComparison.Ordinal))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} {0} to contain {1}{reason}.", Subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string contains another (fragment of a) string a set amount of times.
/// </summary>
/// <param name="expected">
/// The (fragment of a) string that the current string should contain.
/// </param>
/// <param name="occurrenceConstraint">
/// A constraint specifying the amount of times a substring should be present within the test subject.
/// It can be created by invoking static methods Once, Twice, Thrice, or Times(int)
/// on the classes <see cref="Exactly"/>, <see cref="AtLeast"/>, <see cref="MoreThan"/>, <see cref="AtMost"/>, and <see cref="LessThan"/>.
/// For example, <see cref="Exactly.Times(int)"/> or <see cref="LessThan.Twice()"/>.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> Contain(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against <null>.");
if (expected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected));
}
int actual = Subject.CountSubstring(expected, StringComparison.Ordinal);
Execute.Assertion
.ForConstraint(occurrenceConstraint, actual)
.BecauseOf(because, becauseArgs)
.FailWith(
$"Expected {{context:string}} {{0}} to contain {{1}} {{expectedOccurrence}}{{reason}}, but found it {actual.Times()}.",
Subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string contains the specified <paramref name="expected"/>,
/// including any leading or trailing whitespace, with the exception of the casing.
/// </summary>
/// <param name="expected">The string that the subject is expected to contain.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> ContainEquivalentOf(string expected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against <null>.");
if (expected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected));
}
Execute.Assertion
.ForCondition(Contains(Subject, expected, StringComparison.OrdinalIgnoreCase))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} {0} to contain the equivalent of {1}{reason}.", Subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that a string contains the specified <paramref name="expected"/> a set amount of times,
/// including any leading or trailing whitespace, with the exception of the casing.
/// </summary>
/// <param name="expected">
/// The (fragment of a) string that the current string should contain.
/// </param>
/// <param name="occurrenceConstraint">
/// A constraint specifying the amount of times a substring should be present within the test subject.
/// It can be created by invoking static methods Once, Twice, Thrice, or Times(int)
/// on the classes <see cref="Exactly"/>, <see cref="AtLeast"/>, <see cref="MoreThan"/>, <see cref="AtMost"/>, and <see cref="LessThan"/>.
/// For example, <see cref="Exactly.Times(int)"/> or <see cref="LessThan.Twice()"/>.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>