Ensure explicit default locale before each test

This commit is contained in:
Ron Buckton 2017-10-26 14:29:03 -07:00
parent bfba32b71d
commit d9775cd822
3 changed files with 55 additions and 18 deletions

View file

@ -1613,21 +1613,17 @@ namespace ts {
* Creates a string comparer for use with string collation in the UI.
*/
const createStringComparer = (function () {
// If the host supports Intl, we use it for comparisons using the default locale.
if (typeof Intl === "object" && typeof Intl.Collator === "function") {
return createIntlCollatorStringComparer;
type CachedLocale = "en-US" | undefined;
interface StringComparerCache {
default?: Comparer<string>;
"en-US"?: Comparer<string>;
}
// If the host does not support Intl, we fall back to localeCompare.
// localeCompare in Node v0.10 is just an ordinal comparison, so don't use it.
if (typeof String.prototype.localeCompare === "function" &&
typeof String.prototype.toLocaleUpperCase === "function" &&
"a".localeCompare("B") < 0) {
return createLocaleCompareStringComparer;
}
// Otherwise, fall back to ordinal comparison:
return createFallbackStringComparer;
let caseInsensitiveCache: StringComparerCache | undefined;
let caseSensitiveCache: StringComparerCache | undefined;
const createStringComparerNoCache = getStringComparerFactory();
return createStringComparer;
function compareWithCallback(a: string | undefined, b: string | undefined, comparer: (a: string, b: string) => number) {
if (a === b) return Comparison.EqualTo;
@ -1688,6 +1684,47 @@ namespace ts {
return compareCaseInsensitive(a, b) || compareCaseSensitive(a, b);
}
}
function getStringComparerFactory() {
// If the host supports Intl, we use it for comparisons using the default locale.
if (typeof Intl === "object" && typeof Intl.Collator === "function") {
return createIntlCollatorStringComparer;
}
// If the host does not support Intl, we fall back to localeCompare.
// localeCompare in Node v0.10 is just an ordinal comparison, so don't use it.
if (typeof String.prototype.localeCompare === "function" &&
typeof String.prototype.toLocaleUpperCase === "function" &&
"a".localeCompare("B") < 0) {
return createLocaleCompareStringComparer;
}
// Otherwise, fall back to ordinal comparison:
return createFallbackStringComparer;
}
// Hold onto common string comparers. This avoids constantly reallocating comparers during
// tests.
function createStringComparerCached(locale: CachedLocale, caseInsensitive: boolean) {
const cacheKey = locale || "default";
const cache = caseInsensitive
? caseInsensitiveCache || (caseInsensitiveCache = {})
: caseSensitiveCache || (caseSensitiveCache = {});
let comparer = cache[cacheKey];
if (!comparer) {
comparer = createStringComparerNoCache(locale, caseInsensitive);
cache[cacheKey] = comparer;
}
return comparer;
}
function createStringComparer(locale: string | undefined, caseInsensitive: boolean) {
return locale === undefined || locale === "en-US"
? createStringComparerCached(locale as CachedLocale, caseInsensitive)
: createStringComparerNoCache(locale, caseInsensitive);
}
})();
let uiCS: Comparer<string> | undefined;

View file

@ -3128,8 +3128,6 @@ Actual: ${stringify(fullActual)}`);
`(function(test, goTo, verify, edit, debug, format, cancellation, classification, verifyOperationIsCancelled) {
${code}
})`;
const savedUILocale = ts.getUILocale();
ts.setUILocale("en-US"); // run tests in en-US by default.
try {
const test = new FourSlashInterface.Test(state);
@ -3145,9 +3143,6 @@ ${code}
catch (err) {
throw err;
}
finally {
ts.setUILocale(savedUILocale);
}
}
function chompLeadingSpace(content: string) {

View file

@ -207,6 +207,11 @@ function beginTests() {
ts.Debug.enableDebugInfo();
}
// run tests in en-US by default.
const savedUILocale = ts.getUILocale();
beforeEach(() => ts.setUILocale("en-US"));
afterEach(() => ts.setUILocale(savedUILocale));
runTests(runners);
if (!runUnitTests) {