test_TextEncoder.js (8927B)
1 /* 2 * test_TextEncoder.js 3 * bug 764234 tests 4 */ 5 6 /* eslint-env mozilla/testharness */ 7 8 function runTextEncoderTests() { 9 test(testEncoderEncode, "testEncoderEncode"); 10 test(testEncoderGetEncoding, "testEncoderGetEncoding"); 11 test(testInvalidSequence, "testInvalidSequence"); 12 test(testInputString, "testInputString"); 13 test(testStreamingOptions, "testStreamingOptions"); 14 } 15 16 function testEncoderEncode() { 17 var data = 18 "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09" + 19 "\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14" + 20 "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f" + 21 "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a" + 22 "\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35" + 23 "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44" + 24 "\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f" + 25 "\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a" + 26 "\u0e5b"; 27 28 var expectedString = [ 29 0xc2, 0xa0, 0xe0, 0xb8, 0x81, 0xe0, 0xb8, 0x82, 0xe0, 0xb8, 0x83, 0xe0, 30 0xb8, 0x84, 0xe0, 0xb8, 0x85, 0xe0, 0xb8, 0x86, 0xe0, 0xb8, 0x87, 0xe0, 31 0xb8, 0x88, 0xe0, 0xb8, 0x89, 0xe0, 0xb8, 0x8a, 0xe0, 0xb8, 0x8b, 0xe0, 32 0xb8, 0x8c, 0xe0, 0xb8, 0x8d, 0xe0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 0xe0, 0xb9, 0x8b, 0xe0, 47 0xb9, 0x8c, 0xe0, 0xb9, 0x8d, 0xe0, 0xb9, 0x8e, 0xe0, 0xb9, 0x8f, 0xe0, 48 0xb9, 0x90, 0xe0, 0xb9, 0x91, 0xe0, 0xb9, 0x92, 0xe0, 0xb9, 0x93, 0xe0, 49 0xb9, 0x94, 0xe0, 0xb9, 0x95, 0xe0, 0xb9, 0x96, 0xe0, 0xb9, 0x97, 0xe0, 50 0xb9, 0x98, 0xe0, 0xb9, 0x99, 0xe0, 0xb9, 0x9a, 0xe0, 0xb9, 0x9b, 51 ]; 52 53 // valid encoding passed 54 testSingleString({ 55 input: data, 56 expected: expectedString, 57 msg: "testing encoding with valid utf-8 encoding.", 58 }); 59 } 60 61 function testInvalidSequence() { 62 var data = "\u0e43\u0e44\ufffd\u0e45"; 63 var expectedString = [ 64 0xe0, 0xb9, 0x83, 0xe0, 0xb9, 0x84, 0xef, 0xbf, 0xbd, 0xe0, 0xb9, 0x85, 65 ]; 66 67 //Test null input string 68 testSingleString({ 69 input: data, 70 expected: expectedString, 71 msg: "encoder with replacement character test.", 72 }); 73 } 74 75 function testInputString() { 76 //Test null input string 77 testSingleString({ 78 input: "", 79 expected: [], 80 msg: "encoder null input string test.", 81 }); 82 83 //Test spaces as input string 84 testSingleString({ 85 input: " ", 86 expected: [32, 32], 87 msg: "spaces as input string.", 88 }); 89 } 90 91 function testSingleString(test) { 92 var outText; 93 try { 94 var stream = test.stream ? { stream: true } : null; 95 outText = new TextEncoder().encode(test.input, stream); 96 } catch (e) { 97 assert_equals( 98 e.name, 99 test.error, 100 test.msg + " error thrown from the constructor." 101 ); 102 if (test.errorMessage) { 103 assert_equals( 104 e.message, 105 test.errorMessage, 106 test.msg + " error thrown from the constructor." 107 ); 108 } 109 return; 110 } 111 assert_true(!test.error, test.msg); 112 113 if (outText.length !== test.expected.length) { 114 assert_equals( 115 outText.length, 116 test.expected.length, 117 test.msg + " length mismatch" 118 ); 119 return; 120 } 121 122 for (var i = 0; i < outText.length; i++) { 123 if (outText[i] != test.expected[i]) { 124 assert_equals( 125 escape(stringFromArray(outText.buffer)), 126 escape(stringFromArray(test.expected)), 127 test.msg + " Bytes do not match expected bytes." 128 ); 129 return; 130 } 131 } 132 } 133 134 function stringFromArray(a) { 135 return Array.map 136 .call(a, function (v) { 137 return String.fromCharCode(v); 138 }) 139 .join(""); 140 } 141 142 function testStreamingOptions() { 143 var data = [ 144 "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09\u0e0a" + 145 "\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14" + 146 "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d", 147 "\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28" + 148 "\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32" + 149 "\u0e33\u0e34\u0e35\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40" + 150 "\u0e41\u0e42", 151 "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d" + 152 "\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57" + 153 "\u0e58\u0e59\u0e5a\u0e5b", 154 ]; 155 156 var expected = [ 157 [ 158 0xc2, 0xa0, 0xe0, 0xb8, 0x81, 0xe0, 0xb8, 0x82, 0xe0, 0xb8, 0x83, 0xe0, 159 0xb8, 0x84, 0xe0, 0xb8, 0x85, 0xe0, 0xb8, 0x86, 0xe0, 0xb8, 0x87, 0xe0, 160 0xb8, 0x88, 0xe0, 0xb8, 0x89, 0xe0, 0xb8, 0x8a, 0xe0, 0xb8, 0x8b, 0xe0, 161 0xb8, 0x8c, 0xe0, 0xb8, 0x8d, 0xe0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b9, 0x89, 0xe0, 0xb9, 0x8a, 183 0xe0, 0xb9, 0x8b, 0xe0, 0xb9, 0x8c, 0xe0, 0xb9, 0x8d, 0xe0, 0xb9, 0x8e, 184 0xe0, 0xb9, 0x8f, 0xe0, 0xb9, 0x90, 0xe0, 0xb9, 0x91, 0xe0, 0xb9, 0x92, 185 0xe0, 0xb9, 0x93, 0xe0, 0xb9, 0x94, 0xe0, 0xb9, 0x95, 0xe0, 0xb9, 0x96, 186 0xe0, 0xb9, 0x97, 0xe0, 0xb9, 0x98, 0xe0, 0xb9, 0x99, 0xe0, 0xb9, 0x9a, 187 0xe0, 0xb9, 0x9b, 188 ], 189 ]; 190 191 // STREAMING TEST ONE: test streaming three valid strings with stream option 192 // set to true for all three. 193 testArrayOfStrings({ 194 array: [ 195 { input: data[0], stream: true, expected: expected[0] }, 196 { input: data[1], stream: true, expected: expected[1] }, 197 { input: data[2], stream: true, expected: expected[2] }, 198 ], 199 msg: "streaming test one.", 200 }); 201 202 // STREAMING TEST TWO: test streaming valid strings with stream option 203 // streaming option: false from constructor, string 1 stream: true, 204 // string 2 stream: false, string 3 stream: false 205 testArrayOfStrings({ 206 array: [ 207 { input: data[0], stream: true, expected: expected[0] }, 208 { input: data[1], expected: expected[1] }, 209 { input: data[2], expected: expected[2] }, 210 ], 211 msg: "streaming test two.", 212 }); 213 } 214 215 function arrayFromString(s) { 216 return s.split("").map(function (c) { 217 return c.charCodeAt(0); 218 }); 219 } 220 221 function testArrayOfStrings(test) { 222 var encoder; 223 try { 224 encoder = new TextEncoder(); 225 } catch (e) { 226 assert_equals(e.name, test.error, test.msg); 227 return; 228 } 229 assert_true(!test.error, test.msg); 230 231 var array = test.array; 232 for (var i = 0; i < array.length; i += 1) { 233 var stream = array[i].stream ? { stream: true } : null; 234 var view = encoder.encode(array[i].input, stream); 235 236 var stringLen = view.length; 237 var expected = array[i].expected; 238 for (var j = 0; j < stringLen; j++) { 239 if (view[j] !== expected[j]) { 240 assert_equals( 241 view[j], 242 expected[j], 243 "Bytes do not match expected bytes." 244 ); 245 return; 246 } 247 } 248 } 249 } 250 251 function testEncoderGetEncoding() { 252 var encoder = new TextEncoder(); 253 assert_equals(encoder.encoding, "utf-8", "TextEncoder encoding test."); 254 }