-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathStringMethodTests.cs
415 lines (367 loc) · 11.7 KB
/
StringMethodTests.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
using System.Text;
using Text_Grab;
using System.Linq;
using Text_Grab.Utilities;
namespace Tests;
public class StringMethodTests
{
[Fact]
public void MakeMultiLineStringSingleLine()
{
string bodyOfText = @"
This has
multiple
lines
";
string lineOfText = "This has multiple lines";
Assert.Equal(lineOfText, bodyOfText.MakeStringSingleLine());
}
[Theory]
[InlineData("", "")]
[InlineData("is", "This is test string data")]
[InlineData("and", "Hello and How do you do?")]
[InlineData("a", "What a wonderful world!")]
[InlineData("me", "Take me out to the ballgame")]
public void ReturnWordAtCursorPositionSix(string expectedWord, string fullLine)
{
(int start, int length) = fullLine.CursorWordBoundaries(6);
string singleWordAtSix = fullLine.Substring(start, length);
Assert.Equal(expectedWord, singleWordAtSix);
}
private static string multiLineInput = @"Hello this is lots
of text which has several lines
and some spaces at the ends of line
to throw off any easy check";
[Theory]
[InlineData("Hello", "", " this ...")]
[InlineData("lots", "Hello this is ", " ...")]
[InlineData("of", "...", " text ...")]
[InlineData("several", "...h has ", " lines...")]
public void ReturnPreviewsFromWord(string firstWord, string expectedLeftPreview, string expectedRightPreview)
{
int length = firstWord.Length;
int previewLength = 6;
int cursorPosition = multiLineInput.IndexOf(firstWord);
string PreviewLeft = StringMethods.GetCharactersToLeftOfNewLine(ref multiLineInput, cursorPosition, previewLength);
string PreviewRight = StringMethods.GetCharactersToRightOfNewLine(ref multiLineInput, cursorPosition + length, previewLength);
Assert.Equal(expectedLeftPreview, PreviewLeft);
Assert.Equal(expectedRightPreview, PreviewRight);
}
[Theory]
[InlineData(15, "lots")]
[InlineData(20, "of")]
[InlineData(51, "lines")]
[InlineData(114, "check")]
[InlineData(0, "Hello")]
[InlineData(1000, "check")]
[InlineData(-10, "Hello")]
[InlineData(-1, "Hello")]
[InlineData(10, "this")]
public void ReturnWordAtCursorWithNewLines(int cursorPosition, string expectedWord)
{
// Given
string actualWord = multiLineInput.GetWordAtCursorPosition(cursorPosition);
// Then
Assert.Equal(expectedWord, actualWord);
}
[Theory]
[InlineData("", "")]
[InlineData("Hello, world! 0123456789", "Hello, world! olz3hSb7Bg")]
[InlineData("Foo 4r b4r", "Foo hr bhr")]
[InlineData("B4zz5 9zzl3", "BhzzS gzzl3")]
[InlineData("abcdefghijklmnop", "abcdefghijklmnop")]
public void TryFixToLetters_ReplacesDigitsWithLetters_AsExpected(string input, string expected)
{
// Act
string result = input.TryFixToLetters();
// Assert
Assert.Equal(expected, result);
}
[Theory]
[InlineData("","")]
[InlineData("he11o there", "hello there")]
[InlineData("my number is l23456789o", "my number is 1234567890")]
public void TryFixNumOrLetters(string input, string expected)
{
string result = input.TryFixEveryWordLetterNumberErrors();
Assert.Equal(expected, result);
}
[Theory]
[InlineData("", "")]
[InlineData("Hello, world! 0123456789", "4e110, w0r1d! 0123456789")]
[InlineData("Foo 4r b4r", "F00 4r 64r")]
[InlineData("B4zzS 9zzl3", "84225 92213")]
[InlineData("abcdefghijklmnopqrs", "a60def941jk1mn0pqr5")]
public void TryFixToLetters_ReplacesLettersWithDigits_AsExpected(string input, string expected)
{
// Act
string result = input.TryFixToNumbers();
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void RemoveDuplicateLines_AsExpected()
{
// Given
string inputString = @"This is a line
This is a line
This is a line
This is a line
Another Line
Another Line
This is a line";
string expectedString = @"This is a line
Another Line";
// When
string actualString = inputString.RemoveDuplicateLines();
// Then
Assert.Equal(expectedString, actualString);
}
// { ' ', '"', '*', '/', ':', '<', '>', '?', '\\', '|', '+', ',', '.', ';', '=', '[', ']', '!', '@' };
[Theory]
[InlineData("", "")]
[InlineData("A<>B<>C", "A-B-C")]
[InlineData("abc+123/def:*", "abc-123-def-")]
[InlineData("@TheJoeFin", "-TheJoeFin")]
[InlineData("Hello World!", "Hello-World-")]
[InlineData("Nothing", "Nothing")]
[InlineData(" ", "-")]
[InlineData("-----", "-")]
public void ReplaceReservedCharacters(string inputString, string expectedString)
{
// When
string actualString = inputString.ReplaceReservedCharacters();
// Then
Assert.Equal(expectedString, actualString);
}
[Theory]
[InlineData("", @"")]
[InlineData("Hello World!", @"[A-z]{5}\s[A-z]{5}!")]
[InlineData("123-555-6789", @"\d{3}-\d{3}-\d{4}")]
[InlineData("(123)-555-6789", @"(\()\d{3}(\))-\d{3}-\d{4}")]
[InlineData("Abc123456-99", @"[A-z]{3}\d{6}-\d{2}")]
[InlineData("ab12ab12ab12ab12ab12", @"([A-z]{2}\d{2}){5}")]
public void ExtractSimplePatternFromEachString(string inputString, string expectedString)
{
string actualString = inputString.ExtractSimplePattern();
Assert.Equal(expectedString, actualString);
}
[Theory]
[InlineData("", false)]
[InlineData("test@example.", false)]
[InlineData("joe@Text Grab.net", false)]
public void TestIsValidEmailAddress(string inputString, bool expectedIsValid)
{
Assert.Equal(expectedIsValid, inputString.IsValidEmailAddress());
}
[Fact]
public void TestGetLineStartAndLength()
{
string inputString = @"Don't Forget to do
the method just the way
The quick brown fox
jumped over the lazy
brown dog";
(int start, int length) = inputString.GetStartAndLengthOfLineAtPosition(20);
string actualString = inputString.Substring(start, length);
string expectedString = "the method just the way\r\n";
Assert.Equal(expectedString, actualString);
}
[Fact]
public void TestUnstackGroups()
{
string inputString = @"1
2
3
4
5
a
b
c
d
e
jan
feb
mar
apr
may";
string acualString = inputString.UnstackGroups(5);
string expectedString = @"1 a jan
2 b feb
3 c mar
4 d apr
5 e may";
Assert.Equal(expectedString, acualString);
}
[Fact]
public void TestUnstackString()
{
string inputString = @"1
a
jan
2
b
feb
3
c
mar
4
d
apr
5
e
may";
string acualString = inputString.UnstackStrings(3);
string expectedString = @"1 a jan
2 b feb
3 c mar
4 d apr
5 e may";
Assert.Equal(expectedString, acualString);
}
[Theory]
[InlineData("The quick brown fox", "fox", "The quick brown ")]
[InlineData("jumped over over the lazy", "over", "jumped the lazy")]
[InlineData("brown dogs and what not", "o", "brwn dgs and what nt")]
public void TestRemoveThisString(string inputString, string remove, string expected)
{
Assert.Equal(expected, inputString.RemoveAllInstancesOf(remove));
}
[Theory]
[InlineData("The quick brown fox", "fox brown quick The\r\n")]
[InlineData("jumped over the lazy", "lazy the over jumped\r\n")]
[InlineData("brown dogs and what not", "not what and dogs brown\r\n")]
[InlineData(@"brown dogs
and what not", @"dogs brown
not what and
")]
public void TestReverseString(string inputString, string expected)
{
StringBuilder sb = new(inputString);
sb.ReverseWordsForRightToLeft();
Assert.Equal(expected, sb.ToString());
}
[Theory]
[InlineData(@"hello there
general kenobi", @"lo there
eral kenobi
", 3, SpotInLine.Beginning)]
[InlineData(@"hello there
general kenobi", @"hello th
general ken
", 3, SpotInLine.End)]
[InlineData(@"hello there
general kenobi
you are a bold one!", @"hello th
general ken
you are a bold o
", 3, SpotInLine.End)]
[InlineData(@"hello there
general kenobi
22
you are a bold one!", @"hello th
general ken
you are a bold o
", 3, SpotInLine.End)]
public void TestRemoveFromEachLines(string inputString, string expected, int numberOfChars, SpotInLine spotInLine)
{
Assert.Equal(expected, inputString.RemoveFromEachLine(numberOfChars, spotInLine));
}
[Theory]
[InlineData(@"hello there
general kenobi", @"Yep hello there
Yep general kenobi", "Yep ", SpotInLine.Beginning)]
[InlineData(@"hello there
general kenobi", @"hello there Great
general kenobi Great", " Great", SpotInLine.End)]
[InlineData(@"hello there
general kenobi
you are a bold one!", @"hello there Awesome
general kenobi Awesome
you are a bold one! Awesome", " Awesome", SpotInLine.End)]
public void TestAddToEachLines(string inputString, string expected, string stringToAdd, SpotInLine spotInLine)
{
Assert.Equal(expected, inputString.AddCharsToEachLine(stringToAdd, spotInLine));
}
[Theory]
[InlineData("AWESOME", CurrentCase.Upper)]
[InlineData("awesome", CurrentCase.Lower)]
[InlineData("Awesome", CurrentCase.Camel)]
[InlineData("", CurrentCase.Unknown)]
[InlineData(" ", CurrentCase.Unknown)]
[InlineData("the case", CurrentCase.Lower)]
[InlineData("THE CASE", CurrentCase.Upper)]
[InlineData("The Case", CurrentCase.Camel)]
public void TestDetermineToggleCase(string inputString, CurrentCase expectedCase)
{
Assert.Equal(expectedCase, StringMethods.DetermineToggleCase(inputString));
}
[Theory]
[InlineData('A', true)]
[InlineData('a', true)]
[InlineData('b', true)]
[InlineData('c', true)]
[InlineData('C', true)]
[InlineData('d', true)]
[InlineData('z', true)]
[InlineData('Z', true)]
[InlineData('1', true)]
[InlineData('4', true)]
[InlineData('-', true)]
[InlineData('*', true)]
[InlineData('+', true)]
[InlineData('%', true)]
[InlineData('3', true)]
[InlineData('|', true)]
[InlineData('\r', true)]
[InlineData('\n', true)]
[InlineData('\t', true)]
[InlineData('À', false)]
[InlineData('Ü', false)]
[InlineData('Ö', false)]
[InlineData('Ç', false)]
public void TestIsBasicLatin(char inputChar, bool isLatin)
{
Assert.Equal(isLatin, inputChar.IsBasicLatin());
}
[Theory]
[InlineData("string to test", "string to test")]
[InlineData("ABCDEФGHIJKLMNOПQЯSTUVWXYZ", "ABCDEOGHIJKLMNOnQRSTUVWXYZ")]
[InlineData("HЭllΘ There! @$2890", "H3llO There! @$2890")]
[InlineData("", "")]
public void TestReplaceGreekAndCyrillic(string inputString, string expectedString)
{
Assert.Equal(expectedString, inputString.ReplaceGreekOrCyrillicWithLatin());
}
[Theory]
[InlineData(@"hello there
general kenobi", @"hello ther
general ke", 10, SpotInLine.Beginning)]
[InlineData(@"hello there
general kenobi", @"hello there
neral kenobi", 12, SpotInLine.End)]
[InlineData(@"hello there
general kenobi", @"hello there
general kenobi", 100, SpotInLine.Beginning)]
[InlineData(@"hello there
general kenobi", @"hello there
general kenobi", 100, SpotInLine.End)]
[InlineData(@"hello there
general kenobi
you are a bold one!", @"hello
gener
you a", 5, SpotInLine.Beginning)]
[InlineData(@"hello there
general kenobi
you are a bold one!", @"", 0, SpotInLine.Beginning)]
[InlineData(@"hello there
general kenobi
you are a bold one!", @"", 0, SpotInLine.End)]
public void TestLimitEachLine(string inputString, string expected, int charLimit, SpotInLine spotInLine)
{
Assert.Equal(expected, inputString.LimitCharactersPerLine(charLimit, spotInLine));
}
}