-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStringExtensionsTests.cs
683 lines (593 loc) · 32.1 KB
/
StringExtensionsTests.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
// ReSharper disable StringLiteralTypo
namespace Atc.Tests.Extensions;
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments", Justification = "OK.")]
public class StringExtensionsTests
{
[Theory]
[InlineData(new[] { 4, 7 }, "Hallo world", "o")]
public void IndexersOf(int[] expected, string input, string pattern)
=> Assert.Equal(expected, input.IndexersOf(pattern));
[Theory]
[InlineData(new[] { 4, 7, 24 }, "Hallo world. Over the top.", "o", false)]
[InlineData(new[] { 4, 7, 13, 24 }, "Hallo world. Over the top.", "o", true)]
public void IndexersOf_IgnoreCaseSensitive(int[] expected, string input, string pattern, bool ignoreCaseSensitive)
=> Assert.Equal(expected, input.IndexersOf(pattern, ignoreCaseSensitive));
[Theory]
[InlineData(new[] { 4, 7, 24 }, "Hallo world. Over the top.", "o", false, false)]
[InlineData(new[] { 4, 7, 13, 24 }, "Hallo world. Over the top.", "o", true, false)]
[InlineData(new[] { 5, 8, 25 }, "Hallo world. Over the top.", "o", false, true)]
[InlineData(new[] { 5, 8, 14, 25 }, "Hallo world. Over the top.", "o", true, true)]
public void IndexersOf_IgnoreCaseSensitive_UseEndOfPatternToMatch(int[] expected, string input, string pattern, bool ignoreCaseSensitive, bool useEndOfPatternToMatch)
=> Assert.Equal(expected, input.IndexersOf(pattern, ignoreCaseSensitive, useEndOfPatternToMatch));
[Theory]
[InlineData(1, "Hallo-world")]
[InlineData(2, "Hallo world")]
[InlineData(2, "Hallo world .")]
public void WordCount(int expected, string input)
=> Assert.Equal(expected, input.WordCount());
[Theory]
[InlineData(0, "Hallo world")]
[InlineData(2, "Hest {0}, {1}")]
[InlineData(-1, "Hest {0, {1}")]
[InlineData(-1, "Hest {a}, {1}")]
[InlineData(1, "Hest {0}, {0}")]
[InlineData(-1, "Hest {{0}, {0}")]
[InlineData(-1, "Hest {0{0}}, {0}")]
[InlineData(-1, "Hest {{0}0}, {0}")]
public void GetStringFormatParameterNumericCount(int expected, string input)
=> Assert.Equal(expected, input.GetStringFormatParameterNumericCount());
[Theory]
[InlineData(0, "Hallo world")]
[InlineData(1, "Hest {a}")]
[InlineData(1, "Hest {a} {a}")]
[InlineData(2, "Hest {a} {A}")]
[InlineData(2, "Hest {a} {b}")]
[InlineData(-1, "Hest {a, {1}")]
[InlineData(-1, "Hest {a}, {1}")]
[InlineData(-1, "Hest {0}, {0}")]
[InlineData(-1, "Hest {{a}, {0}")]
[InlineData(-1, "Hest {a{a}}, {a}")]
[InlineData(-1, "Hest {{a}a}, {a}")]
public void GetStringFormatParameterLiteralCount(int expected, string input)
=> Assert.Equal(expected, input.GetStringFormatParameterLiteralCount());
[Theory]
[InlineData(new string[] { }, "Hallo World")]
[InlineData(new string[] { }, "Hallo World {0}-{1}")]
[InlineData(new[] { "{{0}}", "{{1}}", "{{A1}}" }, "Hallo World {{0}}-{{1}}-{{A1}}")]
public void GetStringFormatParameterTemplatePlaceholders(string[] expected, string input)
=> Assert.Equal(expected, input.GetStringFormatParameterTemplatePlaceholders());
[Theory]
[InlineData("Hallo World John-Doe-42", "Hallo World {{0}}-{{1}}-{{A1}}", new[] { "0", "John", "1", "Doe", "A1", "42" })]
[InlineData("Hallo World John-Doe-42", "Hallo World {{0}}-{{1}}-{{A1}}", new[] { "{{0}}", "John", "{{1}}", "Doe", "{{A1}}", "42" })]
public void SetStringFormatParameterTemplatePlaceholders(string expected, string input, string[] data)
{
// Arrange
var replacements = new Dictionary<string, string>(StringComparer.Ordinal);
for (var i = 0; i < data.Length; i += 2)
{
replacements.Add(data[i], data[i + 1]);
}
// Act
var actual = input.SetStringFormatParameterTemplatePlaceholders(replacements);
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(false, "Hello, World!", TemplatePatternType.None)]
[InlineData(true, "Hello [World]!", TemplatePatternType.SingleHardBrackets)]
[InlineData(false, "Hello [World]!", TemplatePatternType.SingleCurlyBraces)]
[InlineData(false, "Hello [World]!", TemplatePatternType.DoubleHardBrackets)]
[InlineData(false, "Hello [World]!", TemplatePatternType.DoubleCurlyBraces)]
[InlineData(true, "Hello [[World]]!", TemplatePatternType.DoubleHardBrackets)]
[InlineData(false, "Hello {World}!", TemplatePatternType.DoubleCurlyBraces)]
[InlineData(false, "Hello {World}!", TemplatePatternType.SingleHardBrackets)]
[InlineData(true, "Hello {World}!", TemplatePatternType.SingleCurlyBraces)]
[InlineData(false, "Hello {World}!", TemplatePatternType.DoubleHardBrackets)]
[InlineData(false, "Hello {{World}}!", TemplatePatternType.DoubleHardBrackets)]
[InlineData(true, "Hello {{World}}!", TemplatePatternType.DoubleCurlyBraces)]
[InlineData(false, "Hello World!", TemplatePatternType.All)]
[InlineData(true, "Hello [World]!", TemplatePatternType.All)]
[InlineData(true, "Hello {World}!", TemplatePatternType.All)]
[InlineData(true, "Hello [[World]]!", TemplatePatternType.All)]
[InlineData(true, "Hello {{World}}!", TemplatePatternType.All)]
public void ContainsTemplatePattern(bool expected, string input, TemplatePatternType patternType)
=> Assert.Equal(expected, input.ContainsTemplatePattern(patternType));
[Theory]
[InlineData(new[] { "[World]" }, "Hello [World]!", TemplatePatternType.SingleHardBrackets, true)]
[InlineData(new[] { "World" }, "Hello [World]!", TemplatePatternType.SingleHardBrackets, false)]
[InlineData(new[] { "[[World]]" }, "Hello [[World]]!", TemplatePatternType.DoubleHardBrackets, true)]
[InlineData(new[] { "World" }, "Hello [[World]]!", TemplatePatternType.DoubleHardBrackets, false)]
[InlineData(new[] { "[World]" }, "Hello [World]!", TemplatePatternType.HardBrackets, true)]
[InlineData(new[] { "World" }, "Hello [World]!", TemplatePatternType.HardBrackets, false)]
[InlineData(new[] { "[[World]]" }, "Hello [[World]]!", TemplatePatternType.HardBrackets, true)]
[InlineData(new[] { "World" }, "Hello [[World]]!", TemplatePatternType.HardBrackets, false)]
[InlineData(new[] { "[Hello]", "[World]" }, "[Hello] [World]!", TemplatePatternType.HardBrackets, true)]
[InlineData(new[] { "Hello", "World" }, "[Hello] [World]!", TemplatePatternType.HardBrackets, false)]
[InlineData(new[] { "{World}" }, "Hello {World}!", TemplatePatternType.SingleCurlyBraces, true)]
[InlineData(new[] { "World" }, "Hello {World}!", TemplatePatternType.SingleCurlyBraces, false)]
[InlineData(new[] { "{{World}}" }, "Hello {{World}}!", TemplatePatternType.DoubleCurlyBraces, true)]
[InlineData(new[] { "World" }, "Hello {{World}}!", TemplatePatternType.DoubleCurlyBraces, false)]
[InlineData(new[] { "{World}" }, "Hello {World}!", TemplatePatternType.CurlyBraces, true)]
[InlineData(new[] { "World" }, "Hello {World}!", TemplatePatternType.CurlyBraces, false)]
[InlineData(new[] { "{{World}}" }, "Hello {{World}}!", TemplatePatternType.CurlyBraces, true)]
[InlineData(new[] { "World" }, "Hello {{World}}!", TemplatePatternType.CurlyBraces, false)]
[InlineData(new[] { "{Hello}", "{World}" }, "{Hello} {World}!", TemplatePatternType.CurlyBraces, true)]
[InlineData(new[] { "Hello", "World" }, "{Hello} {World}!", TemplatePatternType.CurlyBraces, false)]
[InlineData(new[] { "[Hello]", "{World}" }, "[Hello] {World}!", TemplatePatternType.All, true)]
[InlineData(new[] { "Hello", "World" }, "[Hello] {World}!", TemplatePatternType.All, false)]
[InlineData(new[] { "[Hello]", "[[Hello2]]", "{World}", "{{World2}}" }, "[Hello] {World}! [[Hello2]] {{World2}}", TemplatePatternType.All, true)]
[InlineData(new[] { "Hello", "Hello2", "World", "World2" }, "[Hello] {World}! [[Hello2]] {{World2}}", TemplatePatternType.All, false)]
public void GetTemplateKeys(string[] expected, string input, TemplatePatternType patternType, bool includePattern)
=> Assert.Equal(expected, input.GetTemplateKeys(patternType, includePattern));
[Theory]
[InlineData("World", 2, "Hello [World] and [World] again.", TemplatePatternType.SingleHardBrackets, false)]
[InlineData("[World]", 2, "Hello [World] and [World] again.", TemplatePatternType.SingleHardBrackets, true)]
[InlineData("World", 2, "Hello {World} and {World} again.", TemplatePatternType.SingleCurlyBraces, false)]
[InlineData("{World}", 2, "Hello {World} and {World} again.", TemplatePatternType.SingleCurlyBraces, true)]
public void GetUniqueTemplateKeysWithOccurrence(
string expectedString, int expectedCount, string input, TemplatePatternType patternType, bool includePattern)
{
// Act
var result = input.GetUniqueTemplateKeysWithOccurrence(patternType, includePattern);
// Assert
Assert.NotNull(result);
Assert.Single(result);
Assert.Equal(expectedString, result.Keys.First());
Assert.Equal(expectedCount, result.Values.First());
}
[Theory]
[InlineData("Hello John!", "Hello [Name]!", "Name", "John", TemplatePatternType.SingleHardBrackets)]
[InlineData("Hello [John]!", "Hello [[Name]]!", "Name", "John", TemplatePatternType.SingleHardBrackets)]
[InlineData("Hello John!", "Hello [[Name]]!", "Name", "John", TemplatePatternType.DoubleHardBrackets)]
[InlineData("Hello John!", "Hello [[Name]]!", "Name", "John", TemplatePatternType.HardBrackets)]
[InlineData("Hello John!", "Hello {Name}!", "Name", "John", TemplatePatternType.SingleCurlyBraces)]
[InlineData("Hello {John}!", "Hello {{Name}}!", "Name", "John", TemplatePatternType.SingleCurlyBraces)]
[InlineData("Hello John!", "Hello {{Name}}!", "Name", "John", TemplatePatternType.DoubleCurlyBraces)]
[InlineData("Hello John!", "Hello {{Name}}!", "Name", "John", TemplatePatternType.CurlyBraces)]
[InlineData("Hello John John!", "Hello [[Name]] {{Name}}!", "Name", "John", TemplatePatternType.All)]
public void ReplaceTemplateKeyWithValue(
string expected, string input, string templateKey, string templateValue, TemplatePatternType patternType)
=> Assert.Equal(expected, input.ReplaceTemplateKeyWithValue(templateKey, templateValue, patternType));
[Theory]
[InlineData("Hello John", "Hello [FirstName]", TemplatePatternType.SingleHardBrackets)]
[InlineData("Hello John Doe", "Hello [FirstName] [LastName]", TemplatePatternType.SingleHardBrackets)]
[InlineData("Hello John Doe and 30!", "Hello [FirstName] [LastName] and [Age]!", TemplatePatternType.SingleHardBrackets)]
[InlineData("Hello John", "Hello [[FirstName]]", TemplatePatternType.DoubleHardBrackets)]
[InlineData("Hello John Doe", "Hello [[FirstName]] [[LastName]]", TemplatePatternType.DoubleHardBrackets)]
[InlineData("Hello John Doe and 30!", "Hello [[FirstName]] [[LastName]] and [[Age]]!", TemplatePatternType.DoubleHardBrackets)]
[InlineData("Hello [Name]", "Hello [Name]", TemplatePatternType.SingleHardBrackets)]
[InlineData("Hello [[Name]]", "Hello [[Name]]", TemplatePatternType.DoubleHardBrackets)]
[InlineData("Hello John", "Hello {FirstName}", TemplatePatternType.SingleCurlyBraces)]
[InlineData("Hello John Doe", "Hello {FirstName} {LastName}", TemplatePatternType.SingleCurlyBraces)]
[InlineData("Hello John Doe and 30!", "Hello {FirstName} {LastName} and {Age}!", TemplatePatternType.SingleCurlyBraces)]
[InlineData("Hello John", "Hello {{FirstName}}", TemplatePatternType.DoubleCurlyBraces)]
[InlineData("Hello John Doe", "Hello {{FirstName}} {{LastName}}", TemplatePatternType.DoubleCurlyBraces)]
[InlineData("Hello John Doe and 30!", "Hello {{FirstName}} {{LastName}} and {{Age}}!", TemplatePatternType.DoubleCurlyBraces)]
[InlineData("Hello {Name}", "Hello {Name}", TemplatePatternType.SingleCurlyBraces)]
[InlineData("Hello {{Name}}", "Hello {{Name}}", TemplatePatternType.DoubleCurlyBraces)]
public void ReplaceTemplateKeysWithValues(string expected, string input, TemplatePatternType patternType)
{
// Arrange
var templateKeyValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "FirstName", "John" },
{ "LastName", "Doe" },
{ "Age", "30" },
};
// Act
var result = input.ReplaceTemplateKeysWithValues(templateKeyValues, patternType);
// Assert
Assert.Equal(expected, result);
}
[Theory]
[InlineData("2000-12-01T23:47:37", "2000-12-01T23:47:37")]
public void ParseDateFromIso8601(string expected, string input)
{
// Act
var actual = input.ParseDateFromIso8601();
// Assert
Assert.Equal(expected, actual.ToIso8601Date());
}
[Theory]
[InlineData(true, "2000-12-01T23:47:37")]
[InlineData(false, "2000-12-01")]
public void TryParseDateFromIso8601(bool expected, string input)
=> Assert.Equal(expected, input.TryParseDateFromIso8601(out var _));
[Theory]
[InlineData(true, "03-24-2000")]
[InlineData(false, "24-03-2000")]
public void TryParseDate(bool expected, string input)
=> Assert.Equal(expected, input.TryParseDate(out var _));
[Theory]
[InlineData(false, "03-24-2000")]
[InlineData(true, "24-03-2000")]
public void TryParseDate_DanishCultureCulture(bool expected, string input)
=> Assert.Equal(expected, input.TryParseDate(out var _, GlobalizationConstants.DanishCultureInfo));
[Theory]
[InlineData(false, "03-24-2000", DateTimeStyles.None)]
[InlineData(true, "24-03-2000", DateTimeStyles.None)]
public void TryParseDate_DanishCultureCulture_DateTimeStyles(bool expected, string input, DateTimeStyles dateTimeStyles)
=> Assert.Equal(expected, input.TryParseDate(out var _, GlobalizationConstants.DanishCultureInfo, dateTimeStyles));
[Theory]
[InlineData("", "")]
[InlineData("Zg==", "f")]
[InlineData("Zm8=", "fo")]
[InlineData("Zm9v", "foo")]
[InlineData("Zm9vYg==", "foob")]
[InlineData("Zm9vYmE=", "fooba")]
[InlineData("Zm9vYmFy", "foobar")]
public void Base64Encode(string expected, string input)
{
// Act
var actual = input.Base64Encode();
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData("", "")]
[InlineData("Zg==", "f")]
[InlineData("Zm8=", "fo")]
[InlineData("Zm9v", "foo")]
[InlineData("Zm9vYg==", "foob")]
[InlineData("Zm9vYmE=", "fooba")]
[InlineData("Zm9vYmFy", "foobar")]
public void Base64Encode_EncodingOverload(string expected, string input)
{
// Act
var actual = input.Base64Encode(Encoding.ASCII);
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData("", "")]
[InlineData("f", "Zg==")]
[InlineData("fo", "Zm8=")]
[InlineData("foo", "Zm9v")]
[InlineData("foob", "Zm9vYg==")]
[InlineData("fooba", "Zm9vYmE=")]
[InlineData("foobar", "Zm9vYmFy")]
public void Base64Decode(string expected, string input)
{
// Act
var actual = input.Base64Decode();
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData("", "")]
[InlineData("f", "Zg==")]
[InlineData("fo", "Zm8=")]
[InlineData("foo", "Zm9v")]
[InlineData("foob", "Zm9vYg==")]
[InlineData("fooba", "Zm9vYmE=")]
[InlineData("foobar", "Zm9vYmFy")]
public void Base64Decode_EncodingOverload(string expected, string input)
{
// Act
var actual = input.Base64Decode(Encoding.ASCII);
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData("<script>window.alert('Hallo')</script>", "<script>window.alert('Hallo')</script>", false)]
[InlineData("<script>window.alert('Hallo')</script>", "<script>window.alert('Hallo')</script>", true)]
public void JavaScriptEncode(string expected, string input, bool htmlEncode)
=> Assert.Equal(expected, input.JavaScriptEncode(htmlEncode));
[Theory]
[InlineData("<script>window.alert('Hallo')</script>", "<script>window.alert('Hallo')</script>", false)]
[InlineData("<script>window.alert('Hallo')</script>", "<script>window.alert('Hallo')</script>", true)]
public void JavaScriptDecode(string expected, string input, bool htmlDecode)
=> Assert.Equal(expected, input.JavaScriptDecode(htmlDecode));
[Theory]
[InlineData("<root><node name='TheName'>Hallo</node></root>", "<root><node name='TheName'>Hallo</node></root>")]
public void XmlEncode(string expected, string input)
=> Assert.Equal(expected, input.XmlEncode());
[Theory]
[InlineData("<root><node name='TheName'>Hallo</node></root>", "<root><node name='TheName'>Hallo</node></root>")]
public void XmlDecode(string expected, string input)
=> Assert.Equal(expected, input.XmlDecode());
[Theory]
[InlineData("abc", "abc")]
[InlineData("abc", "bac")]
[InlineData("Bac", "aBc")]
[InlineData("Bac", "Bac")]
public void Alphabetize(string expected, string input)
=> Assert.Equal(expected, input.Alphabetize());
[Theory]
[InlineData("abc", "àbc")]
[InlineData("abc", "ábc")]
[InlineData("abc", "âbc")]
[InlineData("abc", "ãbc")]
[InlineData("abc", "äbc")]
public void NormalizeAccents(string expected, string input)
=> Assert.Equal(expected, input.NormalizeAccents());
[Theory]
[InlineData("abc", "àbc", LetterAccentType.Grave, true, true, true)]
[InlineData("abc", "ábc", LetterAccentType.Acute, true, true, true)]
[InlineData("abc", "âbc", LetterAccentType.Circumflex, true, true, true)]
[InlineData("abc", "ãbc", LetterAccentType.Tilde, true, true, true)]
[InlineData("abc", "äbc", LetterAccentType.Umlaut, true, true, true)]
public void NormalizeAccents_LetterAccentType_LetterAccentType_Decode_ForLower_ForUpper(string expected, string input, LetterAccentType letterAccentType, bool decode, bool forLower, bool forUpper)
=> Assert.Equal(expected, input.NormalizeAccents(letterAccentType, decode, forLower, forUpper));
[Theory]
[InlineData("Hallo world", "HalloWorld")]
[InlineData("Hallo_ world", "Hallo_World")]
public void NormalizePascalCase(string expected, string input)
=> Assert.Equal(expected, input.NormalizePascalCase());
[Theory]
[InlineData("Hallo World", "HalloWorld")]
[InlineData("Hallo World", "Hallo_World")]
public void Humanize(string expected, string input)
=> Assert.Equal(expected, input.Humanize());
[Theory]
[InlineData("halloWorld", "HalloWorld")]
[InlineData("hallo world", "Hallo world")]
[InlineData("hallo World", "Hallo World")]
public void CamelCase(string expected, string input)
=> Assert.Equal(expected, input.CamelCase());
[Theory]
[InlineData("HalloWorld", "halloWorld")]
[InlineData("Hallo World", "hallo World")]
[InlineData("Hallo World", "hallo world")]
[InlineData("Hallo World-Yea", "HALLO WORLD-YeA")]
public void PascalCase(string expected, string input)
=> Assert.Equal(expected, input.PascalCase());
[Theory]
[InlineData("HalloWorld", "halloWorld", false)]
[InlineData("Hallo World", "hallo World", false)]
[InlineData("Hallo World", "hallo world", false)]
[InlineData("Hallo World-Yea", "HALLO WORLD-YeA", false)]
[InlineData("HalloWorld", "halloWorld", true)]
[InlineData("HalloWorld", "hallo World", true)]
[InlineData("HalloWorld", "hallo world", true)]
[InlineData("HalloWorldYea", "HALLO WORLD-YeA", true)]
public void PascalCase_RemoveSeparators(string expected, string input, bool removeSeparators)
=> Assert.Equal(expected, input.PascalCase(removeSeparators));
[Theory]
[InlineData("HalloWorld", "halloWorld", new[] { ' ', '-' })]
[InlineData("Hallo World", "hallo World", new[] { ' ', '-' })]
[InlineData("Hallo World", "hallo world", new[] { ' ', '-' })]
[InlineData("Hallo-World", "hallo-World", new[] { ' ', '-' })]
[InlineData("Hallo-World", "hallo-world", new[] { ' ', '-' })]
public void PascalCase_Separators(string expected, string input, char[] separators)
=> Assert.Equal(expected, input.PascalCase(separators));
[Theory]
[InlineData("HalloWorld", "halloWorld", new[] { ' ', '-' }, false)]
[InlineData("Hallo World", "hallo World", new[] { ' ', '-' }, false)]
[InlineData("Hallo World", "hallo world", new[] { ' ', '-' }, false)]
[InlineData("Hallo-World", "hallo-World", new[] { ' ', '-' }, false)]
[InlineData("Hallo-World", "hallo-world", new[] { ' ', '-' }, false)]
[InlineData("HalloWorld", "halloWorld", new[] { ' ', '-' }, true)]
[InlineData("HalloWorld", "hallo World", new[] { ' ', '-' }, true)]
[InlineData("HalloWorld", "hallo world", new[] { ' ', '-' }, true)]
[InlineData("HalloWorld", "hallo-World", new[] { ' ', '-' }, true)]
[InlineData("HalloWorld", "hallo-world", new[] { ' ', '-' }, true)]
public void PascalCase_Separators_RemoveSeparators(string expected, string input, char[] separators, bool removeSeparators)
=> Assert.Equal(expected, input.PascalCase(separators, removeSeparators));
[Theory]
[InlineData("Hallo{{NEWLINE}}World", "Hallo\r\nWorld")]
[InlineData("Hallo{{NEWLINE}}World", "Hallo\nWorld")]
[InlineData("Hallo{{NEWLINE}}World", "Hallo\rWorld")]
[InlineData("Hallo{{NEWLINE}}World{{NEWLINE}}John{{NEWLINE}}Doe", "Hallo\r\nWorld\nJohn\rDoe")]
public void EnsureEnvironmentNewLines(string expected, string input)
=> Assert.Equal(expected.Replace("{{NEWLINE}}", Environment.NewLine, StringComparison.Ordinal), input.EnsureEnvironmentNewLines());
[Theory]
[InlineData("Hallo", "hallo")]
public void EnsureFirstCharacterToUpper(string expected, string input)
=> Assert.Equal(expected, input.EnsureFirstCharacterToUpper());
[Theory]
[InlineData("hallo", "Hallo")]
public void EnsureFirstCharacterToLower(string expected, string input)
=> Assert.Equal(expected, input.EnsureFirstCharacterToLower());
[Theory]
[InlineData("hallo.", "hallo")]
[InlineData("hallo:.", "hallo:")]
[InlineData("hallo.", "hallo.")]
public void EnsureEndsWithDot(string expected, string input)
=> Assert.Equal(expected, input.EnsureEndsWithDot());
[Theory]
[InlineData("hallo:", "hallo")]
[InlineData("hallo.:", "hallo.")]
[InlineData("hallo:", "hallo:")]
public void EnsureEndsWithColon(string expected, string input)
=> Assert.Equal(expected, input.EnsureEndsWithColon());
[Theory]
[InlineData("Hallo", "Hallo")]
[InlineData("Hallo", "Hallos")]
public void EnsureSingular(string expected, string input)
=> Assert.Equal(expected, input.EnsureSingular());
[Theory]
[InlineData("Hallos", "Hallo")]
[InlineData("Hallos", "Hallos")]
public void EnsurePlural(string expected, string input)
=> Assert.Equal(expected, input.EnsurePlural());
[Theory]
[InlineData("Hallo", "hallo")]
[InlineData("Hallo", "hallos")]
public void EnsureFirstCharacterToUpperAndSingular(string expected, string input)
=> Assert.Equal(expected, input.EnsureFirstCharacterToUpperAndSingular());
[Theory]
[InlineData("Hallos", "hallo")]
[InlineData("Hallos", "hallos")]
public void EnsureFirstCharacterToUpperAndPlural(string expected, string input)
=> Assert.Equal(expected, input.EnsureFirstCharacterToUpperAndPlural());
[Theory]
[InlineData(false, "Hallo World", "world")]
[InlineData(true, "Hallo World", "World")]
public void Contains(bool expected, string inputA, string inputB)
=> Assert.Equal(expected, inputA.Contains(inputB, StringComparison.Ordinal));
[Theory]
[InlineData(false, "Hallo World", "world", false)]
[InlineData(true, "Hallo World", "world", true)]
public void Contains_IgnoreCaseSensitive(bool expected, string inputA, string inputB, bool ignoreCaseSensitive)
=> Assert.Equal(expected, inputA.Contains(inputB, ignoreCaseSensitive));
[Theory]
[InlineData(false, "Hallo World", new[] { 'w' }, false)]
[InlineData(true, "Hallo World", new[] { 'W' }, true)]
[InlineData(false, "Hallo World", new[] { 'h', 'w' }, false)]
[InlineData(true, "Hallo World", new[] { 'h', 'w' }, true)]
[InlineData(false, "Hallo World", new[] { 'h', 'w', 'b' }, false)]
[InlineData(false, "Hallo World", new[] { 'h', 'W', 'b' }, true)]
public void Contains_IgnoreCaseSensitive_MultipleChars(bool expected, string inputA, char[] inputB, bool ignoreCaseSensitive)
=> Assert.Equal(expected, inputA.Contains(inputB, ignoreCaseSensitive));
[Theory]
[InlineData(false, "Hallo World", new[] { "world" }, false)]
[InlineData(true, "Hallo World", new[] { "World" }, true)]
[InlineData(false, "Hallo World", new[] { "hallo", "world" }, false)]
[InlineData(true, "Hallo World", new[] { "hallo", "World" }, true)]
[InlineData(false, "Hallo World", new[] { "hallo", "world", "bob" }, false)]
[InlineData(false, "Hallo World", new[] { "hallo", "World", "bob" }, true)]
public void Contains_IgnoreCaseSensitive_MultipleStrings(bool expected, string inputA, string[] inputB, bool ignoreCaseSensitive)
=> Assert.Equal(expected, inputA.Contains(inputB, ignoreCaseSensitive));
[Theory]
[InlineData("Hallo Wo...", "Hallo World", 8)]
[InlineData("Hallo World", "Hallo World", 20)]
public void Cut(string expected, string input, int maxLength)
=> Assert.Equal(expected, input.Cut(maxLength));
[Theory]
[InlineData("Hallo Wo#", "Hallo World", 8, "#")]
[InlineData("Hallo World", "Hallo World", 20, "#")]
public void Cut_AppendValue(string expected, string input, int maxLength, string appendValue)
=> Assert.Equal(expected, input.Cut(maxLength, appendValue));
[Theory]
[InlineData("Hallo Wo#ld", "Hallo World", 8, '#')]
public void ReplaceAt(string expected, string input, int index, char newChar)
=> Assert.Equal(expected, input.ReplaceAt(index, newChar));
[Theory]
[InlineData("Hallo World John-Doe-42", "Hallo World 0-1-2", new[] { "0", "John", "1", "Doe", "2", "42" })]
[InlineData("Hallo World John-Doe-42", "Hallo World {{0}}-{{1}}-{{A1}}", new[] { "{{0}}", "John", "{{1}}", "Doe", "{{A1}}", "42" })]
public void ReplaceMany_ReplacementsKeyValue(string expected, string input, string[] data)
{
// Arrange
var replacements = new Dictionary<string, string>(StringComparer.Ordinal);
for (var i = 0; i < data.Length; i += 2)
{
replacements.Add(data[i], data[i + 1]);
}
// Act
var actual = input.ReplaceMany(replacements);
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData("H#ll# W#rld", "Hallo World", new[] { 'a', 'o' }, '#')]
public void ReplaceMany_Chars(string expected, string input, char[] chars, char replacement)
=> Assert.Equal(expected, input.ReplaceMany(chars, replacement));
[Theory]
[InlineData("Hallo World", "Hallo\r\nWorld", " ")]
[InlineData("Hallo World", "Hallo\nWorld", " ")]
[InlineData("Hallo World", "Hallo\rWorld", " ")]
[InlineData("Hallo World John Doe", "Hallo\r\nWorld\nJohn\rDoe", " ")]
[InlineData("Hallo-World-John-Doe", "Hallo\r\nWorld\nJohn\rDoe", "-")]
public void ReplaceNewLines(string expected, string input, string newValue)
=> Assert.Equal(expected, input.ReplaceNewLines(newValue));
[Theory]
[InlineData("HalloWorld", "Hallo\r\nWorld")]
[InlineData("HalloWorld", "Hallo\nWorld")]
[InlineData("HalloWorld", "Hallo\rWorld")]
[InlineData("HalloWorldJohnDoe", "Hallo\r\nWorld\nJohn\rDoe")]
public void RemoveNewLines(string expected, string input)
=> Assert.Equal(expected, input.RemoveNewLines());
[Theory]
[InlineData("llo World", "Hallo World", "Ha")]
public void RemoveStart(string expected, string input, string startValue)
=> Assert.Equal(expected, input.RemoveStart(startValue));
[Theory]
[InlineData("Hallo World", "Hallo World", "HA", false)]
[InlineData("llo World", "Hallo World", "HA", true)]
public void RemoveStart_IgnoreCaseSensitive(string expected, string input, string startValue, bool ignoreCaseSensitive)
=> Assert.Equal(expected, input.RemoveStart(startValue, ignoreCaseSensitive));
[Theory]
[InlineData("Hallo Wor", "Hallo World", "ld")]
public void RemoveEnd(string expected, string input, string endValue)
=> Assert.Equal(expected, input.RemoveEnd(endValue));
[Theory]
[InlineData("Hallo World", "Hallo World", "LD", false)]
[InlineData("Hallo Wor", "Hallo World", "LD", true)]
public void RemoveEnd_IgnoreCaseSensitive(string expected, string input, string endValue, bool ignoreCaseSensitive)
=> Assert.Equal(expected, input.RemoveEnd(endValue, ignoreCaseSensitive));
[Theory]
[InlineData("Hallo World", "Hallo World/")]
public void RemoveEndingSlashIfExist(string expected, string input)
=> Assert.Equal(expected, input.RemoveEndingSlashIfExist());
[Theory]
[InlineData("Hallo World", "Hallo\u0006World")]
public void RemoveDataCrap(string expected, string input)
=> Assert.Equal(expected, input.RemoveDataCrap());
[Theory]
[InlineData("HalloWorld", "Hallo\u0006World")]
public void RemoveNonPrintableCharacter(string expected, string input)
=> Assert.Equal(expected, input.RemoveNonPrintableCharacter());
[Theory]
[InlineData("Hallo Wo...", "Hallo World", 8)]
[InlineData("Hallo World", "Hallo World", 20)]
public void Truncate(string expected, string input, int maxLength)
=> Assert.Equal(expected, input.Truncate(maxLength));
[Theory]
[InlineData("Hallo Wo#", "Hallo World", 8, "#")]
[InlineData("Hallo World", "Hallo World", 20, "#")]
public void Truncate_AppendValue(string expected, string input, int maxLength, string appendValue)
=> Assert.Equal(expected, input.Truncate(maxLength, appendValue));
[Theory]
[InlineData("Hallo World.", " Hallo\tWorld.. ")]
public void TrimSpecial(string expected, string input)
=> Assert.Equal(expected, input.TrimSpecial());
[Theory]
[InlineData("Hallo World", "Hallo World")]
[InlineData("Hallo World", " Hallo World ")]
public void TrimExtended(string expected, string input)
=> Assert.Equal(expected, input.TrimExtended());
[Theory]
[InlineData(new string[0], null)]
[InlineData(new[] { "Hallo", "World" }, "Hallo\r\nWorld")]
public void ToLines(string[] expected, string input)
=> Assert.Equal(expected, input.ToLines());
[Theory]
[InlineData("MyData", "MyData")]
[InlineData("MyData", "List<MyData>")]
[InlineData("MyData", "Hallo world List<MyData> HalloFoo")]
[InlineData("MyData Foo", "MyData Foo")]
[InlineData("MyDataListFoo", "MyDataListFoo")]
public void GetValueBetweenLessAndGreaterThanCharsIfExist(string expected, string input)
=> Assert.Equal(expected, input.GetValueBetweenLessAndGreaterThanCharsIfExist());
[Fact]
public void ToStream()
{
// Arrange
const string input = "Hallo word";
// Atc
var actual = input.ToStream();
// Assert
Assert.NotNull(actual);
}
[Fact]
public void ToStreamFromBase64()
{
// Arrange
var input = "Hallo word".Base64Encode();
// Atc
var actual = input!.ToStreamFromBase64();
// Assert
Assert.NotNull(actual);
}
[Theory]
[InlineData("OK", true, (int)HttpStatusCode.OK)]
[InlineData("NotFound", true, (int)HttpStatusCode.NotFound)]
[InlineData("BadRequest", true, (int)HttpStatusCode.BadRequest)]
[InlineData("InternalServerError", true, (int)HttpStatusCode.InternalServerError)]
[InlineData("InvalidStatusCode", false, 0)]
[InlineData("", false, 0)]
public void TryParseToHttpStatusCode(
string input,
bool expectedResult,
int expectedStatusCodeAsInt)
{
// Arrange
var expectedStatusCode = (HttpStatusCode)expectedStatusCodeAsInt;
// Act
var result = input.TryParseToHttpStatusCode(out var httpStatusCode);
// Assert
Assert.Equal(expectedResult, result);
Assert.Equal(expectedStatusCode, httpStatusCode);
}
}