| 32 | def capitalized as String |
| 33 | """ |
| 34 | Returns the string with the first character capitalized. |
| 35 | Returns a blank string for a blank string. |
| 36 | """ |
| 37 | ensure |
| 38 | result.length == .length |
| 39 | result.length implies result[0] == result[0].toUpper |
| 40 | test |
| 41 | assert 'chuck'.capitalized == 'Chuck' |
| 42 | assert 'Chuck'.capitalized == 'Chuck' |
| 43 | assert ''.capitalized == '' |
| 44 | assert ' foo'.capitalized == ' foo' |
| 45 | assert 'f'.capitalized == 'F' |
| 46 | assert '1aoeu'.capitalized == '1aoeu' |
| 47 | |
| 48 | def count(c as char) as int |
| 49 | test |
| 50 | assert ''.count(c'x')==0 |
| 51 | assert 'x'.count(c'x')==1 |
| 52 | assert 'X'.count(c'x')==0 # case sensitive |
| 53 | assert ' ! ! '.count(c'!')==2 |
| 54 | |
| 55 | def isCapitalized as bool |
| 56 | test |
| 57 | assert 'Aoeu'.isCapitalized |
| 58 | assert 'Zaoeu'.isCapitalized |
| 59 | assert not 'aoeu'.isCapitalized |
| 60 | assert not ''.isCapitalized |
| 61 | assert not '1234'.isCapitalized |
| 62 | |
| 63 | def md5HashInHex as String |
| 64 | ensure |
| 65 | result.length == 32 |
| 66 | test |
| 67 | assert 'Black holes and revelations.'.md5HashInHex == '95b141d670c19f2f20a820751897b9c6' |
| 68 | |
| 76 | def splitLines as List<of String> |
| 77 | return .splitLines(false) |
| 78 | |
| 79 | def splitLines(keepEnds as bool) as List<of String> |
| 80 | """ |
| 81 | Returns the string split into lines, recognizing the various line endings (posix, dos/http, old mac) even if mixed within the same string. |
| 82 | """ |
| 83 | test |
| 84 | cases = [ |
| 85 | ['', false, []], |
| 86 | [' ', false, [' ']], |
| 87 | ['x', false, ['x']], |
| 88 | ['x y', false, ['x y']], |
| 89 | |
| 90 | ['a\n', false, ['a']], |
| 91 | ['a\n', true, ['a\n']], |
| 92 | ['a\nb', false, ['a', 'b']], |
| 93 | ['a\nb', true, ['a\n', 'b']], |
| 94 | ['a\nb\n', false, ['a', 'b']], |
| 95 | ['a\nb\n', true, ['a\n', 'b\n']], |
| 96 | |
| 97 | ['a\r', false, ['a']], |
| 98 | ['a\r', true, ['a\r']], |
| 99 | ['a\rb', false, ['a', 'b']], |
| 100 | ['a\rb', true, ['a\r', 'b']], |
| 101 | ['a\rb\r', false, ['a', 'b']], |
| 102 | ['a\rb\r', true, ['a\r', 'b\r']], |
| 103 | |
| 104 | ['a\r\n', false, ['a']], |
| 105 | ['a\r\n', true, ['a\r\n']], |
| 106 | ['a\r\nb', false, ['a', 'b']], |
| 107 | ['a\r\nb', true, ['a\r\n', 'b']], |
| 108 | ['a\r\nb\r\n', false, ['a', 'b']], |
| 109 | ['a\r\nb\r\n', true, ['a\r\n', 'b\r\n']], |
| 110 | |
| 111 | ['a\r\n\r\n', false, ['a', '']], |
| 112 | ['a\r\n\r\n', true, ['a\r\n', '\r\n']], |
| 113 | ['a\r\n\r\n\r\n', false, ['a', '', '']], |
| 114 | ['a\r\n\r\n\r\n', true, ['a\r\n', '\r\n', '\r\n']], |
| 115 | |
| 116 | ['a\rb\nc\r\nd', false, ['a', 'b', 'c', 'd']], |
| 117 | ['a\rb\nc\r\nd', true, ['a\r', 'b\n', 'c\r\n', 'd']], |
| 118 | ] |
| 119 | for a, b, c in cases |
| 120 | input = a to String |
| 121 | keepEnds = b to bool |
| 122 | expected = c |
| 123 | # trace input, keepEnds, expected |
| 124 | actual = input.splitLines(keepEnds) |
| 125 | assert actual == expected |