|
| 1 | + |
| 2 | +import StringBuilder from '../../../core/util/StringBuilder'; |
| 3 | +import { assertEquals } from './AssertUtils'; |
| 4 | + |
| 5 | +describe('StringBuilder tests', () => { |
| 6 | + |
| 7 | + it('initializes empty strings', () => { |
| 8 | + |
| 9 | + const expected = ''; |
| 10 | + const sb = new StringBuilder(); |
| 11 | + |
| 12 | + assertEquals(sb.toString(), expected); |
| 13 | + }); |
| 14 | + |
| 15 | + it('initializes strings', () => { |
| 16 | + |
| 17 | + const expected = 'xyz'; |
| 18 | + const sb = new StringBuilder('xyz'); |
| 19 | + |
| 20 | + assertEquals(sb.toString(), expected); |
| 21 | + }); |
| 22 | + |
| 23 | + it('appends strings', () => { |
| 24 | + |
| 25 | + const expected1 = 'abcdef'; |
| 26 | + const sb1 = new StringBuilder(); |
| 27 | + const expected2 = '-abcdef'; |
| 28 | + const sb2 = new StringBuilder('-'); |
| 29 | + |
| 30 | + sb1.append('abc'); |
| 31 | + sb1.append('def'); |
| 32 | + |
| 33 | + sb2.append('abc'); |
| 34 | + sb2.append('def'); |
| 35 | + |
| 36 | + assertEquals(sb1.toString(), expected1); |
| 37 | + assertEquals(sb2.toString(), expected2); |
| 38 | + }); |
| 39 | + |
| 40 | + it('apends chars', () => { |
| 41 | + |
| 42 | + const expected = '-&8xyzxy'; |
| 43 | + const sb = new StringBuilder('-&8'); |
| 44 | + |
| 45 | + sb.appendChars([120, 121, 122], 0, 1); |
| 46 | + sb.appendChars([120, 121, 122], 1, 1); |
| 47 | + sb.appendChars([120, 121, 122], 2, 1); |
| 48 | + sb.appendChars([120, 121, 122], 0, 2); |
| 49 | + |
| 50 | + assertEquals(sb.toString(), expected); |
| 51 | + }); |
| 52 | + |
| 53 | + it('correctly normalizes UTF8 strings', () => { |
| 54 | + |
| 55 | + const expected120 = 'x'; |
| 56 | + const input120 = 120; |
| 57 | + |
| 58 | + const expected54 = '6'; |
| 59 | + const input54 = 54; |
| 60 | + |
| 61 | + const expected80 = 'P'; |
| 62 | + const input80 = 80; |
| 63 | + |
| 64 | + const expected37 = '%'; |
| 65 | + const input37 = 37; |
| 66 | + |
| 67 | + const expected64 = '@'; |
| 68 | + const input64 = 64; |
| 69 | + |
| 70 | + const sb = new StringBuilder(); |
| 71 | + |
| 72 | + const actual120 = sb.normalizeString(input120); |
| 73 | + assertEquals(actual120, expected120); |
| 74 | + |
| 75 | + const actual54 = sb.normalizeString(input54); |
| 76 | + assertEquals(actual54, expected54); |
| 77 | + |
| 78 | + const actual80 = sb.normalizeString(input80); |
| 79 | + assertEquals(actual80, expected80); |
| 80 | + |
| 81 | + const actual37 = sb.normalizeString(input37); |
| 82 | + assertEquals(actual37, expected37); |
| 83 | + |
| 84 | + const actual64 = sb.normalizeString(input64); |
| 85 | + assertEquals(actual64, expected64); |
| 86 | + }); |
| 87 | + |
| 88 | + it('correctly returns lentgh', () => { |
| 89 | + const expected = 10; |
| 90 | + const sb = new StringBuilder('----------'); |
| 91 | + assertEquals(sb.length(), expected); |
| 92 | + }); |
| 93 | + |
| 94 | + it('sets lentgh to zero', () => { |
| 95 | + const expected = 0; |
| 96 | + const sb = new StringBuilder('----------'); |
| 97 | + |
| 98 | + sb.setLengthToZero(); |
| 99 | + |
| 100 | + assertEquals(sb.length(), expected); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns the char at index', () => { |
| 104 | + const expected = 'a'; |
| 105 | + const sb = new StringBuilder('----a----'); |
| 106 | + assertEquals(sb.charAt(4), expected); |
| 107 | + }); |
| 108 | + |
| 109 | + it('changes the char at index', () => { |
| 110 | + const expected = 'a'; |
| 111 | + const sb = new StringBuilder('---------'); |
| 112 | + |
| 113 | + sb.setCharAt(4, 'a'); |
| 114 | + |
| 115 | + assertEquals(sb.charAt(4), expected); |
| 116 | + }); |
| 117 | + |
| 118 | + it('inserts and replaces chars', () => { |
| 119 | + const expected1 = '----aaaa-----'; |
| 120 | + const expected2 = '----bbbb-----'; |
| 121 | + const expected3 = '----cccc-----'; |
| 122 | + const sb = new StringBuilder('---------'); |
| 123 | + |
| 124 | + sb.insert(4, 'aaaa'); |
| 125 | + assertEquals(sb.toString(), expected1); |
| 126 | + |
| 127 | + sb.insert(4, 'bbbb', 4); |
| 128 | + assertEquals(sb.toString(), expected2); |
| 129 | + |
| 130 | + sb.insert(4, 'cccc', null); |
| 131 | + assertEquals(sb.toString(), expected3); |
| 132 | + }); |
| 133 | + |
| 134 | +}); |
0 commit comments