| 1 |
QUnit.module( 'mediawiki.String', () => { |
| 2 |
const { byteLength } = require( 'mediawiki.String' ); |
| 3 |
|
| 4 |
QUnit.test.each( 'byteLength()', { |
| 5 |
|
| 6 |
'lowercase a-z': [ 'abcdefghijklmnopqrstuvwxyz', 26 ], |
| 7 |
'uppercase A-Z': [ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 26 ], |
| 8 |
'numbers 0-9': [ '0123456789', 10 ], |
| 9 |
'an asterisk': [ '*', 1 ], |
| 10 |
'3 spaces': [ ' ', 3 ], |
| 11 |
|
| 12 |
|
| 13 |
'U+0024 Dollar': [ '$', 1 ], |
| 14 |
'U+00A2 Cent': [ '\u00A2', 2 ], |
| 15 |
'U+20AC Euro': [ '\u20AC', 3 ], |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
'U+24B62 Han surrogate': [ '\uD852\uDF62', 4 ] |
| 21 |
}, ( assert, [ input, expected ] ) => { |
| 22 |
assert.strictEqual( byteLength( input ), expected ); |
| 23 |
} ); |
| 24 |
|
| 25 |
const { charAt } = require( 'mediawiki.String' ); |
| 26 |
|
| 27 |
QUnit.test( 'charAt() [simple]', ( assert ) => { |
| 28 |
const azLc = 'abcdefghijklmnopqrstuvwxyz'; |
| 29 |
|
| 30 |
assert.strictEqual( charAt( azLc, 0 ), 'a', 'First char' ); |
| 31 |
assert.strictEqual( charAt( azLc, 25 ), 'z', 'Last char' ); |
| 32 |
assert.strictEqual( charAt( azLc, -1 ), '', 'Negative offset' ); |
| 33 |
assert.strictEqual( charAt( azLc, 26 ), '', 'Big offset' ); |
| 34 |
} ); |
| 35 |
|
| 36 |
QUnit.test( 'charAt() UTF-16 text', ( assert ) => { |
| 37 |
assert.strictEqual( charAt( '\uD803\uDC80', 0 ), '\uD803\uDC80', 'U+10C80' ); |
| 38 |
assert.strictEqual( charAt( '\uD803', 0 ), '\uD803', 'First surrogate only' ); |
| 39 |
assert.strictEqual( charAt( '\uD803x', 0 ), '\uD803', 'First surrogate with char' ); |
| 40 |
assert.strictEqual( charAt( '\uD803x', 1 ), 'x', 'Char after first surrogate' ); |
| 41 |
assert.strictEqual( charAt( '\uD803\uDC80', 1 ), '\uDC80', 'Second surrogate only' ); |
| 42 |
assert.strictEqual( charAt( '\uD803\uDC80x', 1 ), '\uDC80', 'Second surrogate with char' ); |
| 43 |
assert.strictEqual( charAt( '\uD803\uDC80x', 2 ), 'x', 'Char after second surrogate' ); |
| 44 |
} ); |
| 45 |
|
| 46 |
const { lcFirst } = require( 'mediawiki.String' ); |
| 47 |
|
| 48 |
QUnit.test.each( 'lcFirst()', { |
| 49 |
'empty string': [ '', '' ], |
| 50 |
'slash is unchanged': [ '/', '/' ], |
| 51 |
'ASCII uppercase': [ 'AB', 'aB' ], |
| 52 |
'ASCII lowercase unchanged': [ 'ab', 'ab' ], |
| 53 |
'first surrogate only': [ '\uD803', '\uD803' ], |
| 54 |
'first surrogate with char': [ '\uD803x', '\uD803x' ], |
| 55 |
'second surrogate only': [ '\uDC80', '\uDC80' ], |
| 56 |
'second surrogate with char': [ '\uDC80x', '\uDC80x' ], |
| 57 |
'from U+10C80 (Old Hungarian capital A) to U+10CC0 (Old Hungarian small A)': [ |
| 58 |
'\uD803\uDC80\uD803\uDCC0', |
| 59 |
'\uD803\uDCC0\uD803\uDCC0' |
| 60 |
] |
| 61 |
}, ( assert, [ input, expected ] ) => { |
| 62 |
assert.strictEqual( lcFirst( input ), expected ); |
| 63 |
} ); |
| 64 |
|
| 65 |
const { ucFirst } = require( 'mediawiki.String' ); |
| 66 |
|
| 67 |
QUnit.test.each( 'ucFirst()', { |
| 68 |
'empty string': [ '', '' ], |
| 69 |
'slash is unchanged': [ '/', '/' ], |
| 70 |
'ASCII uppercase unchanged': [ 'AB', 'AB' ], |
| 71 |
'ASCII lowercase': [ 'ab', 'Ab' ], |
| 72 |
'first surrogate only': [ '\uD803', '\uD803' ], |
| 73 |
'first surrogate with char': [ '\uD803x', '\uD803x' ], |
| 74 |
'second surrogate only': [ '\uDC80', '\uDC80' ], |
| 75 |
'second surrogate with char': [ '\uDC80x', '\uDC80x' ], |
| 76 |
'from U+10CC0 (Old Hungarian small A) to U+10C80 (Old Hungarian capital A)': [ |
| 77 |
'\uD803\uDCC0\uD803\uDCC0', |
| 78 |
'\uD803\uDC80\uD803\uDCC0' |
| 79 |
] |
| 80 |
}, ( assert, [ input, expected ] ) => { |
| 81 |
assert.strictEqual( ucFirst( input ), expected ); |
| 82 |
} ); |
| 83 |
|
| 84 |
const { trimByteLength } = require( 'mediawiki.String' ); |
| 85 |
|
| 86 |
QUnit.test.each( 'trimByteLength()', { |
| 87 |
'simple ASCII': { |
| 88 |
limit: 10, |
| 89 |
sample: '12345678901234567890', |
| 90 |
expected: '1234567890' |
| 91 |
}, |
| 92 |
'multibyte input': { |
| 93 |
limit: 14, |
| 94 |
|
| 95 |
sample: '1234567890\u20AC1234567890\u20AC', |
| 96 |
expected: '1234567890\u20AC1' |
| 97 |
}, |
| 98 |
'multibyte outside BMP': { |
| 99 |
limit: 3, |
| 100 |
sample: '\uD83D\uDCA9', 💩 |
| 101 |
expected: '' |
| 102 |
}, |
| 103 |
'multibyte overlapping a byte': { |
| 104 |
limit: 12, |
| 105 |
sample: '1234567890\u20AC1234567890\u20AC', |
| 106 |
expected: '1234567890' |
| 107 |
}, |
| 108 |
'at limit with shortening input filter': { |
| 109 |
limit: 6, |
| 110 |
sample: 'User:Sample', |
| 111 |
fn: function ( val ) { |
| 112 |
return val.replace( 'User:', '' ); |
| 113 |
}, |
| 114 |
expected: 'User:Sample' |
| 115 |
}, |
| 116 |
'exceed limit with shortening input filter': { |
| 117 |
limit: 6, |
| 118 |
sample: 'User:Example', |
| 119 |
fn: function ( val ) { |
| 120 |
return val.replace( 'User:', '' ); |
| 121 |
}, |
| 122 |
|
| 123 |
expected: 'User:Exampl' |
| 124 |
}, |
| 125 |
'below limit with expanding input filter': { |
| 126 |
limit: 10, |
| 127 |
sample: '12345678901234567890', |
| 128 |
fn: function ( text ) { |
| 129 |
return 'prefix' + text; |
| 130 |
}, |
| 131 |
|
| 132 |
expected: '1234' |
| 133 |
}, |
| 134 |
'trim from insertion point 0': { |
| 135 |
limit: 3, |
| 136 |
initial: 'abc', |
| 137 |
sample: 'zabc', |
| 138 |
expected: 'abc' |
| 139 |
}, |
| 140 |
'trim from insertion point 1': { |
| 141 |
limit: 3, |
| 142 |
initial: 'abc', |
| 143 |
sample: 'azbc', |
| 144 |
expected: 'abc' |
| 145 |
}, |
| 146 |
'Do not cut up false matching substrings in emoji insertions': { |
| 147 |
limit: 12, |
| 148 |
initial: '\uD83D\uDCA9\uD83D\uDCA9', 💩" |
| 149 |
sample: '\uD83D\uDCA9\uD83D\uDCB9\uD83E\uDCA9\uD83D\uDCA9', 🢩💩" |
| 150 |
expected: '\uD83D\uDCA9\uD83D\uDCB9\uD83D\uDCA9' 💹💩" |
| 151 |
}, |
| 152 |
'Unpaired surrogates do not crash': { |
| 153 |
limit: 4, |
| 154 |
sample: '\uD800\uD800\uDFFF', |
| 155 |
expected: '\uD800' |
| 156 |
} |
| 157 |
}, ( assert, opt ) => { |
| 158 |
const res = trimByteLength( opt.initial || '', opt.sample, opt.limit, opt.fn || null ); |
| 159 |
assert.strictEqual( |
| 160 |
res.newVal, |
| 161 |
opt.expected, |
| 162 |
'New value matches the expected string' |
| 163 |
); |
| 164 |
} ); |
| 165 |
} ); |