David Aurelio 2426b3b5ec Prevent tests with invalid UTF-8 from failing when jest reads them from the cache
Summary: The utf-8 test was failing when run from Jest’s cache, because it contained unicode escape sequences for stray high and low surrogates. Somewhere in the process of transpiling, caching on disk, and reading from the cache, those stray surrogates are changed to U+FFFD (REPLACEMENT CHARACTER / �). This diffs changes the method of creating these strings from literals to `String.fromCharCode`, which survives the process as intended.

Reviewed By: bestander

Differential Revision: D3534711

fbshipit-source-id: 365bace77a1f914e6e2fbb3430b3e0ea6cec5e83
2016-07-08 12:35:53 -07:00

64 lines
2.1 KiB
JavaScript

/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
jest.disableAutomock();
const {encode} = require('../utf8');
describe('UTF-8 encoding:', () => {
it('can encode code points < U+80', () => {
const arrayBuffer = encode('\u0000abcDEF\u007f');
expect(new Uint8Array(arrayBuffer)).toEqual(
new Uint8Array([0x00, 0x61, 0x62, 0x63, 0x44, 0x45, 0x46, 0x7f]));
});
it('can encode code points < U+800', () => {
const arrayBuffer = encode('\u0080\u0548\u07ff');
expect(new Uint8Array(arrayBuffer)).toEqual(
new Uint8Array([0xc2, 0x80, 0xd5, 0x88, 0xdf, 0xbf]));
});
it('can encode code points < U+10000', () => {
const arrayBuffer = encode('\u0800\uac48\uffff');
expect(new Uint8Array(arrayBuffer)).toEqual(
new Uint8Array([0xe0, 0xa0, 0x80, 0xea, 0xb1, 0x88, 0xef, 0xbf, 0xbf]));
});
it('can encode code points in the Supplementary Planes (surrogate pairs)', () => {
const arrayBuffer = encode([
'\ud800\udc00',
'\ud800\ude89',
'\ud83d\ude3b',
'\udbff\udfff'
].join(''));
expect(new Uint8Array(arrayBuffer)).toEqual(
new Uint8Array([
0xf0, 0x90, 0x80, 0x80,
0xf0, 0x90, 0x8a, 0x89,
0xf0, 0x9f, 0x98, 0xbb,
0xf4, 0x8f, 0xbf, 0xbf,
])
);
});
it('allows for stray high surrogates', () => {
const arrayBuffer = encode(String.fromCharCode(0x61, 0xd8c6, 0x62));
expect(new Uint8Array(arrayBuffer)).toEqual(
new Uint8Array([0x61, 0xed, 0xa3, 0x86, 0x62]));
});
it('allows for stray low surrogates', () => {
const arrayBuffer = encode(String.fromCharCode(0x61, 0xde19, 0x62));
expect(new Uint8Array(arrayBuffer)).toEqual(
new Uint8Array([0x61, 0xed, 0xb8, 0x99, 0x62]));
});
});