Merge "resourceloader: Simplify StringSet fallback"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.String.trimByteLength.test.js
1 ( function () {
2 var simpleSample, U_20AC, poop, mbSample,
3 trimByteLength = require( 'mediawiki.String' ).trimByteLength;
4
5 QUnit.module( 'mediawiki.String.trimByteLength', QUnit.newMwEnvironment() );
6
7 // Simple sample (20 chars, 20 bytes)
8 simpleSample = '12345678901234567890';
9
10 // 3 bytes (euro-symbol)
11 U_20AC = '\u20AC';
12
13 // Outside of the BMP (pile of poo emoji)
14 poop = '\uD83D\uDCA9'; // "💩"
15
16 // Multi-byte sample (22 chars, 26 bytes)
17 mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
18
19 /**
20 * Test factory for mw.String#trimByteLength
21 *
22 * @param {Object} options
23 * @param {string} options.description Test name
24 * @param {string} options.sample Sequence of characters to trim
25 * @param {string} [options.initial] Previous value of the sequence of characters, if any
26 * @param {Number} options.limit Length to trim to
27 * @param {Function} [options.fn] Filter function
28 * @param {string} options.expected Expected final value
29 */
30 function byteLimitTest( options ) {
31 var opt = $.extend( {
32 description: '',
33 sample: '',
34 initial: '',
35 limit: 0,
36 fn: function ( a ) { return a; },
37 expected: ''
38 }, options );
39
40 QUnit.test( opt.description, function ( assert ) {
41 var res = trimByteLength( opt.initial, opt.sample, opt.limit, opt.fn );
42
43 assert.strictEqual(
44 res.newVal,
45 opt.expected,
46 'New value matches the expected string'
47 );
48 } );
49 }
50
51 byteLimitTest( {
52 description: 'Limit using the maxlength attribute',
53 limit: 10,
54 sample: simpleSample,
55 expected: '1234567890'
56 } );
57
58 byteLimitTest( {
59 description: 'Limit using a custom value (multibyte)',
60 limit: 14,
61 sample: mbSample,
62 expected: '1234567890' + U_20AC + '1'
63 } );
64
65 byteLimitTest( {
66 description: 'Limit using a custom value (multibyte, outside BMP)',
67 limit: 3,
68 sample: poop,
69 expected: ''
70 } );
71
72 byteLimitTest( {
73 description: 'Limit using a custom value (multibyte) overlapping a byte',
74 limit: 12,
75 sample: mbSample,
76 expected: '1234567890'
77 } );
78
79 byteLimitTest( {
80 description: 'Pass the limit and a callback as input filter',
81 limit: 6,
82 fn: function ( val ) {
83 var title = mw.Title.newFromText( String( val ) );
84 // Return without namespace prefix
85 return title ? title.getMain() : '';
86 },
87 sample: 'User:Sample',
88 expected: 'User:Sample'
89 } );
90
91 byteLimitTest( {
92 description: 'Pass the limit and a callback as input filter',
93 limit: 6,
94 fn: function ( val ) {
95 var title = mw.Title.newFromText( String( val ) );
96 // Return without namespace prefix
97 return title ? title.getMain() : '';
98 },
99 sample: 'User:Example',
100 // The callback alters the value to be used to calculeate
101 // the length. The altered value is "Exampl" which has
102 // a length of 6, the "e" would exceed the limit.
103 expected: 'User:Exampl'
104 } );
105
106 byteLimitTest( {
107 description: 'Input filter that increases the length',
108 limit: 10,
109 fn: function ( text ) {
110 return 'prefix' + text;
111 },
112 sample: simpleSample,
113 // Prefix adds 6 characters, limit is reached after 4
114 expected: '1234'
115 } );
116
117 byteLimitTest( {
118 description: 'Trim from insertion when limit exceeded',
119 limit: 3,
120 initial: 'abc',
121 sample: 'zabc',
122 // Trim from the insertion point (at 0), not the end
123 expected: 'abc'
124 } );
125
126 byteLimitTest( {
127 description: 'Trim from insertion when limit exceeded',
128 limit: 3,
129 initial: 'abc',
130 sample: 'azbc',
131 // Trim from the insertion point (at 1), not the end
132 expected: 'abc'
133 } );
134
135 byteLimitTest( {
136 description: 'Do not cut up false matching substrings in emoji insertions',
137 limit: 12,
138 initial: '\uD83D\uDCA9\uD83D\uDCA9', // "💩💩"
139 sample: '\uD83D\uDCA9\uD83D\uDCB9\uD83E\uDCA9\uD83D\uDCA9', // "💩💹🢩💩"
140 expected: '\uD83D\uDCA9\uD83D\uDCB9\uD83D\uDCA9' // "💩💹💩"
141 } );
142
143 byteLimitTest( {
144 description: 'Unpaired surrogates do not crash',
145 limit: 4,
146 sample: '\uD800\uD800\uDFFF',
147 expected: '\uD800'
148 } );
149
150 }() );