-
Notifications
You must be signed in to change notification settings - Fork 133
/
strings_test.dart
353 lines (301 loc) · 11.1 KB
/
strings_test.dart
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
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
library quiver.strings;
import 'package:quiver/strings.dart';
import 'package:unittest/unittest.dart';
main() {
group('isBlank', () {
test('should consider null a blank', () {
expect(isBlank(null), isTrue);
});
test('should consider empty string a blank', () {
expect(isBlank(''), isTrue);
});
test('should consider white-space-only string a blank', () {
expect(isBlank(' \n\t\r\f'), isTrue);
});
test('should consider non-whitespace string not a blank', () {
expect(isBlank('hello'), isFalse);
});
});
group('flip', () {
test('should flip characters in a string', () {
expect(flip('ab'), 'ba');
});
test('should return null as null', () {
expect(flip(null), null);
});
test('should return empty string as empty string', () {
expect(flip(''), '');
});
});
group('nullToEmpty', () {
test('should turn null to empty string', () {
expect(nullToEmpty(null), '');
});
test('should leave non-null string unchanged', () {
expect(nullToEmpty('hi!'), 'hi!');
});
test('should leave empty string unchanged', () {
expect(nullToEmpty(''), '');
});
});
group('emptyToNull', () {
test('should turn empty string to null', () {
expect(emptyToNull(''), null);
});
test('should leave non-null string unchanged', () {
expect(emptyToNull('hi!'), 'hi!');
});
test('should leave null as null', () {
expect(emptyToNull(null), null);
});
});
group('repeat', () {
test('should repeat a non-empty string', () {
expect(repeat('ab', 3), 'ababab');
});
test('should repeat flipped non-empty string '
'on negative number of times', () {
expect(repeat('ab', -3), 'bababa');
});
test('should return null on null', () {
expect(repeat(null, 6), null);
expect(repeat(null, -6), null);
});
test('should return empty string on empty string', () {
expect(repeat('', 6), '');
expect(repeat('', -6), '');
});
});
group('loop', () {
// Forward direction test cases
test('should work like normal substring', () {
expect(loop('hello', 1, 3), 'el');
});
test('should work like normal substring full-string', () {
expect(loop('hello', 0, 5), 'hello');
});
test('should be circular', () {
expect(loop('ldwor', -3, 2), 'world');
});
test('should be circular over many loops', () {
expect(loop('ab', 0, 8), 'abababab');
});
test('should be circular over many loops starting loops away', () {
expect(loop('ab', 4, 12), 'abababab');
});
test('should be circular over many loops starting mid-way', () {
expect(loop('ab', 1, 9), 'babababa');
});
test('should be circular over many loops starting mid-way loops away', () {
expect(loop('ab', 5, 13), 'babababa');
});
test('should default to end of string', () {
expect(loop('hello', 3), 'lo');
});
test('should default to end of string from negative index', () {
expect(loop('/home/user/test.txt', -3), 'txt');
});
test('should default to end of string from far negative index', () {
expect(loop('ab', -5), 'b');
});
test('should handle in-fragment substring loops away negative', () {
expect(loop('hello', -4, -2), 'el');
});
test('should handle in-fragment substring loops away positive', () {
expect(loop('hello', 6, 8), 'el');
});
// Backward direction test cases
test('should traverse backwards', () {
expect(loop('hello', 3, 0), 'leh');
});
test('should traverse backwards across boundary', () {
expect(loop('eholl', 2, -3), 'hello');
});
test('should traverse backwards many loops', () {
expect(loop('ab', 0, -6), 'bababa');
});
// Corner cases
test('should throw on null', () {
expect(() => loop(null, 6, 8), throws);
});
test('should throw on empty', () {
expect(() => loop('', 6, 8), throws);
});
});
group('padLeft', () {
test('should return the input if length greater than width', () {
expect(padLeft('abc', 2, '0'), 'abc');
expect(padLeft('abc', 3, '0'), 'abc');
});
test('should pad on the left if length less than width', () {
expect(padLeft('abc', 4, '0'), '0abc');
expect(padLeft('abc', 6, '0'), '000abc');
});
test('should use multi-character fills', () {
expect(padLeft('abc', 4, '012345'), '0abc');
expect(padLeft('abc', 6, '012345'), '012abc');
expect(padLeft('abc', 8, '012'), '01201abc');
});
test('should handle null and empty inputs', () {
expect(padLeft(null, 4, '012345'), '0123');
expect(padLeft('', 4, '012345'), '0123');
expect(padLeft(null, 4, '012'), '0120');
expect(padLeft('', 4, '012'), '0120');
});
});
group('padRight', () {
test('should return the input if length greater than width', () {
expect(padRight('abc', 2, '0'), 'abc');
expect(padRight('abc', 3, '0'), 'abc');
});
test('should pad on the right if length less than width', () {
expect(padRight('abc', 4, '0'), 'abc0');
expect(padRight('abc', 6, '0'), 'abc000');
});
test('should use multi-character fills', () {
expect(padRight('abc', 4, '012345'), 'abc5');
expect(padRight('abc', 6, '012345'), 'abc345');
expect(padRight('abc', 8, '012'), 'abc12012');
});
test('should handle null and empty inputs', () {
expect(padRight(null, 4, '012345'), '2345');
expect(padRight('', 4, '012345'), '2345');
expect(padRight(null, 4, '012'), '2012');
expect(padRight('', 4, '012'), '2012');
});
});
group('trimLeft', () {
test('should trim whitespace from the left', () {
expect(trimLeft(''), '');
expect(trimLeft(' '), '');
expect(trimLeft(' abc '), 'abc ');
expect(trimLeft(' abc def '), 'abc def ');
expect(trimLeft('\t\vabc '), 'abc ');
// these are some of the whitespace chars not defined for RexExps
expect(trimLeft('\u000Aabc '), 'abc ');
expect(trimLeft('\u000Dabc '), 'abc ');
expect(trimLeft('\u0085abc '), 'abc ');
expect(trimLeft('\u1680abc '), 'abc ');
});
test('should throw on null', () {
expect(() => trimLeft(null), throws);
});
});
group('trimRight', () {
test('should trim whitespace from the right', () {
expect(trimRight(''), '');
expect(trimRight(' '), '');
expect(trimRight(' abc '), ' abc');
expect(trimRight(' abc def '), ' abc def');
expect(trimRight(' abc\t\v'), ' abc');
// these are some of the whitespace chars not defined for RexExps
expect(trimRight(' abc\u000A'), ' abc');
expect(trimRight(' abc\u000D'), ' abc');
expect(trimRight(' abc\u0085'), ' abc');
expect(trimRight(' abc\u1680'), ' abc');
});
test('should throw on null', () {
expect(() => trimRight(null), throws);
});
});
group('center', () {
test('should return the input if length greater than width', () {
expect(center('abc', 2, '0'), 'abc');
expect(center('abc', 3, '0'), 'abc');
});
test('should pad equal chars on left and right for even padding count', () {
expect(center('abc', 5, '0'), '0abc0');
expect(center('abc', 9, '0'), '000abc000');
});
test('should pad extra char on right for odd padding amount', () {
expect(center('abc', 4, '0'), 'abc0');
expect(center('abc', 8, '0'), '00abc000');
});
test('should use multi-character fills', () {
expect(center('abc', 7, '012345'), '01abc45');
expect(center('abc', 6, '012345'), '0abc45');
expect(center('abc', 9, '01'), '010abc101');
});
test('should handle null and empty inputs', () {
expect(center(null, 4, '012345'), '0145');
expect(center('', 4, '012345'), '0145');
expect(center(null, 5, '012345'), '01345');
expect(center('', 5, '012345'), '01345');
});
});
group('equalsIgnoreCase', () {
test('should return true for equal Strings', () {
expect(equalsIgnoreCase('abc', 'abc'), isTrue);
});
test('should return true for case-insensitivly equal Strings', () {
expect(equalsIgnoreCase('abc', 'AbC'), isTrue);
});
test('should return true for nulls', () {
expect(equalsIgnoreCase(null, null), isTrue);
});
test('should return false for unequal Strings', () {
expect(equalsIgnoreCase('abc', 'bcd'), isFalse);
});
test('should return false if one is null', () {
expect(equalsIgnoreCase('abc', null), isFalse);
expect(equalsIgnoreCase(null, 'abc'), isFalse);
});
});
group('compareIgnoreCase', () {
test('should return 0 for case-insensitivly equal Strings', () {
expect(compareIgnoreCase('abc', 'abc'), 0);
expect(compareIgnoreCase('abc', 'AbC'), 0);
});
test('should return compare unequal Strings correctly', () {
expect(compareIgnoreCase('abc', 'abd'), lessThan(0));
expect(compareIgnoreCase('abc', 'abD'), lessThan(0));
expect(compareIgnoreCase('abd', 'abc'), greaterThan(0));
expect(compareIgnoreCase('abD', 'abc'), greaterThan(0));
});
});
group('capitalize', () {
test('should return "This Was A Triumph!" after capitalizing non-capitalized the input string', () {
expect(capitalize('this was a triumph!'), 'This Was A Triumph!');
});
});
group('insert', () {
test('should return "abc" after inserting "b" into the string "ac" at index [1]', () {
expect(insert('ac', 1, 'b'), 'abc');
});
});
group('replace', () {
test('should return "abc" after replacing the index [1] character in the string "adc" with "b" '
'with length defaulted to the length of the replacement', () {
expect(replace('adc', 1 , 'b'), 'abc');
});
test('should return "abcde" after replacing the index [1] character in the string "adc" with "bcde" '
'with overwrite defaulted to the length of the replacement', () {
expect(replace('adc', 1 , 'bcde'), 'abcde');
});
test('should return "aabbcc" after replacing the index [1] character in the string "abc"'
'with "abbc" with overwrite = 1 (1 character overwritten)', () {
expect(replace('abc', 1, 'abbc', overwrite: 1), 'aabbcc');
});
});
group('remove', () {
test('should return "abc" after remvoing the character at index [1] from "abbc"', () {
expect(remove('abbc', 1, 1), 'abc');
});
test('should return "abc" after removing 5 characters starting at index [1] from "abbbbbbc"', () {
expect(remove('abbbbbbc', 1, 5), 'abc');
});
});
}