PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/specs/core/string.ds

http://github.com/wilkie/djehuty
Unknown | 507 lines | 426 code | 81 blank | 0 comment | 0 complexity | 57883f7a5243935571dd04c58d26439d MD5 | raw file
  1module specs.core.string;
  2
  3import testing.support;
  4
  5import core.string;
  6import math.currency;
  7
  8describe string {
  9	describe trim {
 10		it should_handle_empty_string() {
 11			should("".trim() == "");
 12		}
 13
 14		it should_handle_whitespace_on_left() {
 15			should(" \t\nhello".trim() == "hello");
 16			should(" hello".trim() == "hello");
 17			should("\t\t\thello".trim() == "hello");
 18			should("\n\n\nhello".trim() == "hello");
 19		}
 20
 21		it should_handle_whitespace_on_right() {
 22			should("hello \t\n".trim() == "hello");
 23			should("hello\t\t".trim() == "hello");
 24			should("hello\n\n".trim() == "hello");
 25			should("hello   ".trim() == "hello");
 26		}
 27
 28		it should_handle_whitespace_on_both_sides() {
 29			should(" \t\nhello \t\n".trim() == "hello");
 30			should("\t\t\thello\n".trim() == "hello");
 31			should("\n\n\t\thello    \n".trim() == "hello");
 32			should("     \t   hello \t\t\t\n\n\t ".trim() == "hello");
 33		}
 34	}
 35
 36	describe split {
 37		it should_work_on_empty_strings {
 38			string[] foo1 = "".split('a');
 39			string[] foo2 = "".split("a");
 40			should(foo1[0] == "");
 41			should(foo2[0] == "");
 42		}
 43
 44		it should_work_on_characters {
 45			string[] foo = "work.on.characters".split('.');
 46			should(foo.length == 3);
 47			should(foo[0] == "work");
 48			should(foo[1] == "on");
 49			should(foo[2] == "characters");
 50		}
 51
 52		it should_work_on_characters_with_delimiter_at_beginning {
 53			string[] foo = ".work.a.b".split('.');
 54			should(foo.length == 4);
 55			should(foo[0] == "");
 56			should(foo[1] == "work");
 57			should(foo[2] == "a");
 58			should(foo[3] == "b");
 59		}
 60
 61		it should_work_on_characters_with_delimiter_at_end {
 62			string[] foo = "work.a.b.".split('.');
 63			should(foo.length == 4);
 64			should(foo[0] == "work");
 65			should(foo[1] == "a");
 66			should(foo[2] == "b");
 67			should(foo[3] == "");
 68		}
 69
 70		it should_work_on_strings {
 71			string[] foo = "work(on strings.foo)".split("( .)");
 72			should(foo.length == 5);
 73			should(foo[0] == "work");
 74			should(foo[1] == "on");
 75			should(foo[2] == "strings");
 76			should(foo[3] == "foo");
 77			should(foo[4] == "");
 78		}
 79	}
 80
 81	describe nextInt {
 82		it should_work_on_empty_strings {
 83			int foo;
 84			should("".nextInt(foo) == false);
 85		}
 86
 87		it should_return_the_next_int {
 88			int foo;
 89			bool returnVal = "123foo".nextInt(foo);
 90			should(foo == 123);
 91			should(returnVal == true);
 92		}
 93
 94		it should_fail_when_there_is_not_a_next_int {
 95			int foo;
 96			bool returnVal = "foo123".nextInt(foo);
 97			should(returnVal == false);
 98			should(foo == 0);
 99		}
100	}
101
102	describe substring {
103		it should_work_on_empty_strings {
104			string foo = "";
105			foo = foo.substring(0);
106			should(foo == "");
107		}
108
109		it should_work_for_start_larger_than_length {
110			string foo = "abc";
111			foo = foo.substring(4);
112			should(foo == "");
113			foo = "abc".substring(3);
114			should(foo == "");
115		}
116
117		it should_work_for_start_at_zero_and_length_omitted {
118			string foo = "abc";
119			foo = foo.substring(0);
120			should(foo == "abc");
121		}
122
123		it should_work_for_start_at_zero_and_length_longer_than_string {
124			string foo = "abc";
125			foo = foo.substring(0, 4);
126			should(foo == "abc");
127		}
128
129		it should_work_for_start_at_zero_and_length_at_zero {
130			string foo = "abc";
131			foo = foo.substring(0,0);
132			should(foo == "");
133		}
134
135		it should_work_for_start_at_zero_and_length_within_string {
136			string foo1 = "abc".substring(0, 1);
137			string foo2 = "abc".substring(0, 2);
138			should(foo1 == "a");
139			should(foo2 == "ab");
140		}
141
142		it should_work_for_start_within_string_and_length_omitted {
143			string foo1 = "abc".substring(1);
144			string foo2 = "abc".substring(2);
145			should(foo1 == "bc");
146			should(foo2 == "c");
147		}
148
149		it should_work_for_start_within_string_and_length_longer_than_string {
150			string foo1 = "abc".substring(1, 4);
151			string foo2 = "abc".substring(2, 4);
152			string foo3 = "abc".substring(3, 4);
153			should(foo1 == "bc");
154			should(foo2 == "c");
155			should(foo3 == "");
156		}
157
158		it should_work_for_start_within_string_and_length_at_zero {
159			string foo1 = "abc".substring(1,0);
160			string foo2 = "abc".substring(2,0);
161			string foo3 = "abc".substring(3,0);
162			should(foo1 == "");
163			should(foo2 == "");
164			should(foo3 == "");
165		}
166	}
167
168	describe replace {
169		it should_work_on_empty_strings {
170			string foo = "".replace('a', 'b');
171			should(foo == "");
172		}
173
174		it should_work_as_expected {
175			string foo = "abcaefahi".replace('a', 'x');
176			should(foo == "xbcxefxhi");
177		}
178	}
179
180	describe find {
181		it should_work_on_empty_strings {
182			int foo = "".find("foo", 0);
183			should(foo == -1);
184		}
185
186		it should_fail_on_finding_empty_strings {
187			int foo1 = "".find("", 0);
188			int foo2 = "abc".find("", 0);
189
190			should(foo1 == -1);
191			should(foo2 == -1);
192		}
193
194		it should_work_when_start_is_omitted {
195			int foo1 = "abcdebc".find("bc");
196			int foo2 = "abcdebc".find("ce");
197			should(foo1 == 1);
198			should(foo2 == -1);
199		}
200
201		it should_work_when_search_string_is_at_beginning {
202			int foo = "abcd".find("ab");
203			should(foo == 0);
204		}
205
206		it should_work_when_search_string_is_at_end {
207			int foo = "abcd".find("cd");
208			should(foo == 2);
209		}
210
211		it should_work_when_search_string_is_within_string {
212			int foo = "abcd".find("bc");
213			should(foo == 1);
214		}
215
216		it should_work_when_start_is_given {
217			int foo1 = "abcdab".find("ab", 0);
218			int foo2 = "abcdab".find("ab", 1);
219
220			should(foo1 == 0);
221			should(foo2 == 4);
222		}
223	}
224
225	describe findReverse {
226		it should_work_on_empty_strings {
227			int foo = "".findReverse("foo", 0);
228			should(foo == -1);
229		}
230
231		it should_fail_on_finding_empty_strings {
232			int foo1 = "".findReverse("", 0);
233			int foo2 = "abc".findReverse("", 0);
234
235			should(foo1 == -1);
236			should(foo2 == -1);
237		}
238
239		it should_work_when_start_is_omitted {
240			int foo1 = "abcdebc".findReverse("bc");
241			int foo2 = "abcdebc".findReverse("ce");
242			should(foo1 == 5);
243			should(foo2 == -1);
244		}
245
246		it should_work_when_search_string_is_at_beginning {
247			int foo = "abcd".findReverse("ab");
248			should(foo == 0);
249		}
250
251		it should_work_when_search_string_is_at_end {
252			int foo = "abcd".findReverse("cd");
253			should(foo == 2);
254		}
255
256		it should_work_when_search_string_is_within_string {
257			int foo = "abcd".findReverse("bc");
258			should(foo == 1);
259		}
260
261		it should_work_when_start_is_given {
262			int foo1 = "abcdabcd".findReverse("ab", 0);
263			int foo2 = "abcdabcd".findReverse("ab", 2);
264			int foo3 = "abcdabcd".findReverse("ab", 6);
265
266			should(foo1 == -1);
267			should(foo2 == 0);
268			should(foo3 == 4);
269		}
270	}
271
272	describe times {
273		it should_work_on_empty_strings {
274			should("".times(4) == "");
275		}
276
277		it should_return_empty_string_with_amount_being_zero {
278			should("abc".times(0) == "");
279		}
280
281		it should_work_with_identity {
282			should("abc".times(1) == "abc");
283		}
284
285		it should_work_as_expected {
286			should("abc".times(3) == "abcabcabc");
287		}
288	}
289
290	describe format {
291		it should_work_on_empty_strings {
292			should("".format() == "");
293		}
294
295		it should_work_on_empty_specifier {
296			should("{:}".format(3) == "3");
297			should("{0:}".format(3) == "3");
298			should("{1:}".format(3,4) == "4");
299			should("{2:}".format(3,4,5) == "5");
300		}
301
302		it should_work_on_d_specifier {
303			should("a{d}b".format(4) == "a4b");
304			should("a{D}b".format(4) == "a4b");
305		}
306
307		it should_work_on_x_specifier {
308			should("a{x}b".format(10) == "aab");
309		}
310
311		it should_work_with_d_specifier_with_width {
312			should("a{d8}b".format(4) == "a00000004b");
313		}
314
315		it should_work_with_x_specifier_with_width {
316			should("a{x8}b".format(10) == "a0000000ab");
317		}
318
319		it should_work_on_X_specifier {
320			should("a{X8}b".format(10) == "a0000000Ab");
321		}
322		
323		it should_work_when_specifier_is_at_beginning {
324			should("{d}xxx".format(4) == "4xxx");
325			should("{x}xxx".format(10) == "axxx");
326			should("{X}xxx".format(10) == "Axxx");
327		}
328		
329		it should_work_when_specifier_is_at_end {
330			should("xxx{d}".format(4) == "xxx4");
331			should("xxx{x}".format(10) == "xxxa");
332			should("xxx{X}".format(10) == "xxxA");
333		}
334
335		it should_work_when_specifier_is_alone {
336			should("{d}".format(4) == "4");
337			should("{x}".format(10) == "a");
338			should("{X}".format(10) == "A");
339		}
340
341		it should_work_with_two_specifiers_in_a_row {
342			should("{d}{d}".format(4,5) == "45");
343			should("{x8}{x8}".format(10,11) == "0000000a0000000b");
344			should("{x8}{X8}".format(10,11) == "0000000a0000000B");
345			should("{x}{d}".format(10,4) == "a4");
346			should("{X}{d}".format(10,4) == "A4");
347		}
348
349		it should_work_with_empty_specifier {
350			should("{}".format("hello") == "hello");
351			should("aaa{}bbb{}ccc".format(1,"f") == "aaa1bbbfccc");
352		}
353
354		it should_work_with_position_specifier {
355			should("{1}{0}".format('a', 'b') == "ba");
356			should("aaa{0}aaa{1}bbb{0}bbb{1}ccc".format('a', 'b') == "aaaaaaabbbbabbbbccc");
357		}
358
359		it should_fail_with_position_out_of_bounds {
360			shouldThrow("Invalid Format String");
361			format("{0}{1}{2}",1,2);
362		}
363
364		it should_work_with_zero_placeholder {
365			should("{0:00.0000}".format(1500.42) == "1500.4200");
366			should("{0:00.0000}".format(3.42) == "03.4200");
367			should("{0:00.0000}".format(3.42555) == "03.42555");
368			should("{0:00.0000}".format(1234.42555) == "1234.42555");
369		}
370
371		it should_not_require_a_index_with_a_zero_placeholder {
372			should("{00.0000}".format(1500.42) == "1500.4200");
373			should("{00.0000}".format(3.42) == "03.4200");
374			should("{00.0000}".format(3.42555) == "03.42555");
375			should("{00.0000}".format(1234.42555) == "1234.42555");
376		}
377
378		it should_work_with_currency_specifier {
379			should("{c}".format(1500.42) == (new Currency(150042,2)).toString());
380		}
381	}
382
383	describe uppercase {
384		it should_work_on_empty_strings {
385			should("".uppercase() == "");
386		}
387
388		it should_work_as_expected {
389			string foo = "abc123dEFg";
390			should(foo.uppercase() == "ABC123DEFG");
391			should(foo == "abc123dEFg");
392			should("123".uppercase() == "123");
393		}
394	}
395
396	describe lowercase {
397		it should_work_on_empty_strings {
398			should("".lowercase() == "");
399		}
400
401		it should_work_as_expected {
402			string foo = "aBC123dEFg";
403			should(foo.lowercase() == "abc123defg");
404			should(foo == "aBC123dEFg");
405			should("123".uppercase() == "123");
406		}
407	}
408
409	describe charAt {
410		it should_fail_on_empty_strings {
411			string foo = "".charAt(0);
412			should(foo is null);
413		}
414
415		it should_work_for_normal_strings {
416			string foo = "abc";
417			should(foo.charAt(0) == "a");
418			should(foo.charAt(1) == "b");
419			should(foo.charAt(2) == "c");
420			should(foo.charAt(3) is null);
421		}
422
423		it should_account_for_combining_marks {
424			string foo = "he\u0364llo";
425			should(foo.charAt(0) == "h");
426			should(foo.charAt(1) == "e\u0364");
427			should(foo.charAt(2) == "l");
428			should(foo.charAt(3) == "l");
429			should(foo.charAt(4) == "o");
430			should(foo.charAt(5) is null);
431		}
432	}
433
434	describe insertAt {
435		it should_work_on_empty_strings {
436			string foo = "";
437			string f2 = foo.insertAt("abc", 0);
438			should(foo == "");
439			should(f2 == "abc");
440		}
441
442		it should_fail_when_index_is_out_of_bounds {
443			string foo = "abc";
444			string f2 = foo.insertAt("def", 4);
445			should(foo == "abc");
446			should(f2 is null);
447		}
448
449		it should_work_when_index_is_zero {
450			string foo = "abc";
451			string f2 = foo.insertAt("def", 0);
452			should(foo == "abc");
453			should(f2 == "defabc");
454		}
455
456		it should_work_when_index_is_utflen {
457			string foo = "abc";
458			string f2 = foo.insertAt("def", foo.utflen());
459			should(foo == "abc");
460			should(f2 == "abcdef");
461		}
462
463		it should_work_when_index_is_within_string {
464			string foo = "abc";
465			string f2 = foo.insertAt("def", 1);
466			string f3 = foo.insertAt("def", 2);
467			should(foo == "abc");
468			should(f2 == "adefbc");
469			should(f3 == "abdefc");
470		}
471
472		it should_account_for_combining_marks {
473			string foo = "he\u0364llo";
474			string f1 = foo.insertAt("def", 0);
475			string f2 = foo.insertAt("def", 1);
476			string f3 = foo.insertAt("def", 2);
477			string f4 = foo.insertAt("def", 3);
478			string f5 = foo.insertAt("def", 4);
479			string f6 = foo.insertAt("def", 5);
480			string f7 = foo.insertAt("def", 6);
481
482			should(foo == "he\u0364llo");
483			should(f1 == "defhe\u0364llo");
484			should(f2 == "hdefe\u0364llo");
485			should(f3 == "he\u0364defllo");
486			should(f4 == "he\u0364ldeflo");
487			should(f5 == "he\u0364lldefo");
488			should(f6 == "he\u0364llodef");
489			should(f7 is null);
490		}
491	}
492
493	describe utflen {
494		it should_work_on_empty_strings {
495			should("".utflen() == 0);
496		}
497
498		it should_work_on_normal_strings {
499			should("abc".utflen() == 3);
500		}
501
502		it should_account_for_combining_marks {
503			string foo = "hello\u0364world";
504			should(foo.utflen() == 10);
505		}
506	}
507}