OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.strings; |
| 16 |
| 17 import 'package:quiver/strings.dart'; |
| 18 import 'package:test/test.dart' hide isEmpty; |
| 19 |
| 20 main() { |
| 21 group('isBlank', () { |
| 22 test('should consider null a blank', () { |
| 23 expect(isBlank(null), isTrue); |
| 24 }); |
| 25 test('should consider empty string a blank', () { |
| 26 expect(isBlank(''), isTrue); |
| 27 }); |
| 28 test('should consider white-space-only string a blank', () { |
| 29 expect(isBlank(' \n\t\r\f'), isTrue); |
| 30 }); |
| 31 test('should consider non-whitespace string not a blank', () { |
| 32 expect(isBlank('hello'), isFalse); |
| 33 }); |
| 34 }); |
| 35 |
| 36 group('isEmpty', () { |
| 37 test('should consider null to be empty', () { |
| 38 expect(isEmpty(null), isTrue); |
| 39 }); |
| 40 test('should consider the empty string to be empty', () { |
| 41 expect(isEmpty(''), isTrue); |
| 42 }); |
| 43 test('should consider whitespace string to be not empty', () { |
| 44 expect(isEmpty(' '), isFalse); |
| 45 }); |
| 46 test('should consider non-whitespace string to be not empty', () { |
| 47 expect(isEmpty('hello'), isFalse); |
| 48 }); |
| 49 }); |
| 50 |
| 51 group('flip', () { |
| 52 test('should flip characters in a string', () { |
| 53 expect(flip('ab'), 'ba'); |
| 54 }); |
| 55 test('should return null as null', () { |
| 56 expect(flip(null), null); |
| 57 }); |
| 58 test('should return empty string as empty string', () { |
| 59 expect(flip(''), ''); |
| 60 }); |
| 61 }); |
| 62 |
| 63 group('nullToEmpty', () { |
| 64 test('should turn null to empty string', () { |
| 65 expect(nullToEmpty(null), ''); |
| 66 }); |
| 67 test('should leave non-null string unchanged', () { |
| 68 expect(nullToEmpty('hi!'), 'hi!'); |
| 69 }); |
| 70 test('should leave empty string unchanged', () { |
| 71 expect(nullToEmpty(''), ''); |
| 72 }); |
| 73 }); |
| 74 |
| 75 group('emptyToNull', () { |
| 76 test('should turn empty string to null', () { |
| 77 expect(emptyToNull(''), null); |
| 78 }); |
| 79 test('should leave non-null string unchanged', () { |
| 80 expect(emptyToNull('hi!'), 'hi!'); |
| 81 }); |
| 82 test('should leave null as null', () { |
| 83 expect(emptyToNull(null), null); |
| 84 }); |
| 85 }); |
| 86 |
| 87 group('repeat', () { |
| 88 test('should repeat a non-empty string', () { |
| 89 expect(repeat('ab', 3), 'ababab'); |
| 90 }); |
| 91 test('should repeat flipped non-empty string ' |
| 92 'on negative number of times', () { |
| 93 expect(repeat('ab', -3), 'bababa'); |
| 94 }); |
| 95 test('should return null on null', () { |
| 96 expect(repeat(null, 6), null); |
| 97 expect(repeat(null, -6), null); |
| 98 }); |
| 99 test('should return empty string on empty string', () { |
| 100 expect(repeat('', 6), ''); |
| 101 expect(repeat('', -6), ''); |
| 102 }); |
| 103 }); |
| 104 |
| 105 group('loop', () { |
| 106 // Forward direction test cases |
| 107 test('should work like normal substring', () { |
| 108 expect(loop('hello', 1, 3), 'el'); |
| 109 }); |
| 110 test('should work like normal substring full-string', () { |
| 111 expect(loop('hello', 0, 5), 'hello'); |
| 112 }); |
| 113 test('should be circular', () { |
| 114 expect(loop('ldwor', -3, 2), 'world'); |
| 115 }); |
| 116 test('should be circular over many loops', () { |
| 117 expect(loop('ab', 0, 8), 'abababab'); |
| 118 }); |
| 119 test('should be circular over many loops starting loops away', () { |
| 120 expect(loop('ab', 4, 12), 'abababab'); |
| 121 }); |
| 122 test('should be circular over many loops starting mid-way', () { |
| 123 expect(loop('ab', 1, 9), 'babababa'); |
| 124 }); |
| 125 test('should be circular over many loops starting mid-way loops away', () { |
| 126 expect(loop('ab', 5, 13), 'babababa'); |
| 127 }); |
| 128 test('should default to end of string', () { |
| 129 expect(loop('hello', 3), 'lo'); |
| 130 }); |
| 131 test('should default to end of string from negative index', () { |
| 132 expect(loop('/home/user/test.txt', -3), 'txt'); |
| 133 }); |
| 134 test('should default to end of string from far negative index', () { |
| 135 expect(loop('ab', -5), 'b'); |
| 136 }); |
| 137 test('should handle in-fragment substring loops away negative', () { |
| 138 expect(loop('hello', -4, -2), 'el'); |
| 139 }); |
| 140 test('should handle in-fragment substring loops away positive', () { |
| 141 expect(loop('hello', 6, 8), 'el'); |
| 142 }); |
| 143 |
| 144 // Backward direction test cases |
| 145 test('should traverse backwards', () { |
| 146 expect(loop('hello', 3, 0), 'leh'); |
| 147 }); |
| 148 test('should traverse backwards across boundary', () { |
| 149 expect(loop('eholl', 2, -3), 'hello'); |
| 150 }); |
| 151 test('should traverse backwards many loops', () { |
| 152 expect(loop('ab', 0, -6), 'bababa'); |
| 153 }); |
| 154 |
| 155 // Corner cases |
| 156 test('should throw on null', () { |
| 157 expect(() => loop(null, 6, 8), throws); |
| 158 }); |
| 159 test('should throw on empty', () { |
| 160 expect(() => loop('', 6, 8), throws); |
| 161 }); |
| 162 }); |
| 163 |
| 164 group('padLeft', () { |
| 165 test('should return the input if length greater than width', () { |
| 166 expect(padLeft('abc', 2, '0'), 'abc'); |
| 167 expect(padLeft('abc', 3, '0'), 'abc'); |
| 168 }); |
| 169 |
| 170 test('should pad on the left if length less than width', () { |
| 171 expect(padLeft('abc', 4, '0'), '0abc'); |
| 172 expect(padLeft('abc', 6, '0'), '000abc'); |
| 173 }); |
| 174 |
| 175 test('should use multi-character fills', () { |
| 176 expect(padLeft('abc', 4, '012345'), '0abc'); |
| 177 expect(padLeft('abc', 6, '012345'), '012abc'); |
| 178 expect(padLeft('abc', 8, '012'), '01201abc'); |
| 179 }); |
| 180 |
| 181 test('should handle null and empty inputs', () { |
| 182 expect(padLeft(null, 4, '012345'), '0123'); |
| 183 expect(padLeft('', 4, '012345'), '0123'); |
| 184 expect(padLeft(null, 4, '012'), '0120'); |
| 185 expect(padLeft('', 4, '012'), '0120'); |
| 186 }); |
| 187 }); |
| 188 |
| 189 group('padRight', () { |
| 190 test('should return the input if length greater than width', () { |
| 191 expect(padRight('abc', 2, '0'), 'abc'); |
| 192 expect(padRight('abc', 3, '0'), 'abc'); |
| 193 }); |
| 194 |
| 195 test('should pad on the right if length less than width', () { |
| 196 expect(padRight('abc', 4, '0'), 'abc0'); |
| 197 expect(padRight('abc', 6, '0'), 'abc000'); |
| 198 }); |
| 199 |
| 200 test('should use multi-character fills', () { |
| 201 expect(padRight('abc', 4, '012345'), 'abc5'); |
| 202 expect(padRight('abc', 6, '012345'), 'abc345'); |
| 203 expect(padRight('abc', 8, '012'), 'abc12012'); |
| 204 }); |
| 205 |
| 206 test('should handle null and empty inputs', () { |
| 207 expect(padRight(null, 4, '012345'), '2345'); |
| 208 expect(padRight('', 4, '012345'), '2345'); |
| 209 expect(padRight(null, 4, '012'), '2012'); |
| 210 expect(padRight('', 4, '012'), '2012'); |
| 211 }); |
| 212 }); |
| 213 |
| 214 group('trimLeft', () { |
| 215 test('should trim whitespace from the left', () { |
| 216 expect(trimLeft(''), ''); |
| 217 expect(trimLeft(' '), ''); |
| 218 expect(trimLeft(' abc '), 'abc '); |
| 219 expect(trimLeft(' abc def '), 'abc def '); |
| 220 expect(trimLeft('\t\vabc '), 'abc '); |
| 221 // these are some of the whitespace chars not defined for RexExps |
| 222 expect(trimLeft('\u000Aabc '), 'abc '); |
| 223 expect(trimLeft('\u000Dabc '), 'abc '); |
| 224 expect(trimLeft('\u0085abc '), 'abc '); |
| 225 expect(trimLeft('\u1680abc '), 'abc '); |
| 226 }); |
| 227 |
| 228 test('should throw on null', () { |
| 229 expect(() => trimLeft(null), throws); |
| 230 }); |
| 231 }); |
| 232 |
| 233 group('trimRight', () { |
| 234 test('should trim whitespace from the right', () { |
| 235 expect(trimRight(''), ''); |
| 236 expect(trimRight(' '), ''); |
| 237 expect(trimRight(' abc '), ' abc'); |
| 238 expect(trimRight(' abc def '), ' abc def'); |
| 239 expect(trimRight(' abc\t\v'), ' abc'); |
| 240 // these are some of the whitespace chars not defined for RexExps |
| 241 expect(trimRight(' abc\u000A'), ' abc'); |
| 242 expect(trimRight(' abc\u000D'), ' abc'); |
| 243 expect(trimRight(' abc\u0085'), ' abc'); |
| 244 expect(trimRight(' abc\u1680'), ' abc'); |
| 245 }); |
| 246 |
| 247 test('should throw on null', () { |
| 248 expect(() => trimRight(null), throws); |
| 249 }); |
| 250 }); |
| 251 |
| 252 group('center', () { |
| 253 test('should return the input if length greater than width', () { |
| 254 expect(center('abc', 2, '0'), 'abc'); |
| 255 expect(center('abc', 3, '0'), 'abc'); |
| 256 }); |
| 257 |
| 258 test('should pad equal chars on left and right for even padding count', () { |
| 259 expect(center('abc', 5, '0'), '0abc0'); |
| 260 expect(center('abc', 9, '0'), '000abc000'); |
| 261 }); |
| 262 |
| 263 test('should pad extra char on right for odd padding amount', () { |
| 264 expect(center('abc', 4, '0'), 'abc0'); |
| 265 expect(center('abc', 8, '0'), '00abc000'); |
| 266 }); |
| 267 |
| 268 test('should use multi-character fills', () { |
| 269 expect(center('abc', 7, '012345'), '01abc45'); |
| 270 expect(center('abc', 6, '012345'), '0abc45'); |
| 271 expect(center('abc', 9, '01'), '010abc101'); |
| 272 }); |
| 273 |
| 274 test('should handle null and empty inputs', () { |
| 275 expect(center(null, 4, '012345'), '0145'); |
| 276 expect(center('', 4, '012345'), '0145'); |
| 277 expect(center(null, 5, '012345'), '01345'); |
| 278 expect(center('', 5, '012345'), '01345'); |
| 279 }); |
| 280 }); |
| 281 |
| 282 group('equalsIgnoreCase', () { |
| 283 test('should return true for equal Strings', () { |
| 284 expect(equalsIgnoreCase('abc', 'abc'), isTrue); |
| 285 }); |
| 286 |
| 287 test('should return true for case-insensitivly equal Strings', () { |
| 288 expect(equalsIgnoreCase('abc', 'AbC'), isTrue); |
| 289 }); |
| 290 |
| 291 test('should return true for nulls', () { |
| 292 expect(equalsIgnoreCase(null, null), isTrue); |
| 293 }); |
| 294 |
| 295 test('should return false for unequal Strings', () { |
| 296 expect(equalsIgnoreCase('abc', 'bcd'), isFalse); |
| 297 }); |
| 298 |
| 299 test('should return false if one is null', () { |
| 300 expect(equalsIgnoreCase('abc', null), isFalse); |
| 301 expect(equalsIgnoreCase(null, 'abc'), isFalse); |
| 302 }); |
| 303 }); |
| 304 |
| 305 group('compareIgnoreCase', () { |
| 306 test('should return 0 for case-insensitivly equal Strings', () { |
| 307 expect(compareIgnoreCase('abc', 'abc'), 0); |
| 308 expect(compareIgnoreCase('abc', 'AbC'), 0); |
| 309 }); |
| 310 |
| 311 test('should return compare unequal Strings correctly', () { |
| 312 expect(compareIgnoreCase('abc', 'abd'), lessThan(0)); |
| 313 expect(compareIgnoreCase('abc', 'abD'), lessThan(0)); |
| 314 expect(compareIgnoreCase('abd', 'abc'), greaterThan(0)); |
| 315 expect(compareIgnoreCase('abD', 'abc'), greaterThan(0)); |
| 316 }); |
| 317 }); |
| 318 } |
OLD | NEW |