From 82c2ca070976329e822a466e2d6bafa88b87bc4d Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Wed, 18 Jan 2017 15:56:44 -0800 Subject: [PATCH] Add Intl.PluralRules tests (#782) * Add Intl.PluralRules tests * document the tests --- test/intl402/PluralRules/builtin.js | 14 ++++++++ test/intl402/PluralRules/can-be-subclassed.js | 28 +++++++++++++++ test/intl402/PluralRules/internals.js | 21 ++++++++++++ test/intl402/PluralRules/length.js | 15 ++++++++ test/intl402/PluralRules/name.js | 15 ++++++++ test/intl402/PluralRules/prototype/bind.js | 34 +++++++++++++++++++ .../intl402/PluralRules/prototype/builtins.js | 14 ++++++++ .../PluralRules/prototype/constructor/main.js | 15 ++++++++ .../PluralRules/prototype/properties.js | 22 ++++++++++++ .../PluralRules/prototype/prototype.js | 17 ++++++++++ .../prototype/resolvedOptions/builtins.js | 14 ++++++++ .../prototype/resolvedOptions/name.js | 15 ++++++++ .../prototype/resolvedOptions/properties.js | 30 ++++++++++++++++ .../PluralRules/prototype/select/name.js | 17 ++++++++++ .../prototype/select/non-finite.js | 22 ++++++++++++ .../PluralRules/prototype/select/tainting.js | 20 +++++++++++ .../supportedLocalesOf/arguments.js | 14 ++++++++ .../PluralRules/supportedLocalesOf/main.js | 29 ++++++++++++++++ .../PluralRules/supportedLocalesOf/name.js | 14 ++++++++ .../supportedLocalesOf/supportedLocalesOf.js | 14 ++++++++ test/intl402/PluralRules/this-not-ignored.js | 29 ++++++++++++++++ 21 files changed, 413 insertions(+) create mode 100644 test/intl402/PluralRules/builtin.js create mode 100644 test/intl402/PluralRules/can-be-subclassed.js create mode 100644 test/intl402/PluralRules/internals.js create mode 100644 test/intl402/PluralRules/length.js create mode 100644 test/intl402/PluralRules/name.js create mode 100644 test/intl402/PluralRules/prototype/bind.js create mode 100644 test/intl402/PluralRules/prototype/builtins.js create mode 100644 test/intl402/PluralRules/prototype/constructor/main.js create mode 100644 test/intl402/PluralRules/prototype/properties.js create mode 100644 test/intl402/PluralRules/prototype/prototype.js create mode 100644 test/intl402/PluralRules/prototype/resolvedOptions/builtins.js create mode 100755 test/intl402/PluralRules/prototype/resolvedOptions/name.js create mode 100644 test/intl402/PluralRules/prototype/resolvedOptions/properties.js create mode 100755 test/intl402/PluralRules/prototype/select/name.js create mode 100644 test/intl402/PluralRules/prototype/select/non-finite.js create mode 100644 test/intl402/PluralRules/prototype/select/tainting.js create mode 100644 test/intl402/PluralRules/supportedLocalesOf/arguments.js create mode 100644 test/intl402/PluralRules/supportedLocalesOf/main.js create mode 100755 test/intl402/PluralRules/supportedLocalesOf/name.js create mode 100644 test/intl402/PluralRules/supportedLocalesOf/supportedLocalesOf.js create mode 100644 test/intl402/PluralRules/this-not-ignored.js diff --git a/test/intl402/PluralRules/builtin.js b/test/intl402/PluralRules/builtin.js new file mode 100644 index 0000000000..8a887772d0 --- /dev/null +++ b/test/intl402/PluralRules/builtin.js @@ -0,0 +1,14 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules +description: > + Tests that Intl.PluralRules meets the requirements for + built-in objects defined by the introduction of chapter 17 of the + ECMAScript Language Specification. +author: Zibi Braniecki +includes: [testBuiltInObject.js] +---*/ + +testBuiltInObject(Intl.PluralRules, true, true, ["supportedLocalesOf"], 0); diff --git a/test/intl402/PluralRules/can-be-subclassed.js b/test/intl402/PluralRules/can-be-subclassed.js new file mode 100644 index 0000000000..57a0562f2c --- /dev/null +++ b/test/intl402/PluralRules/can-be-subclassed.js @@ -0,0 +1,28 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-intl-pluralrules-constructor +description: Tests that Intl.PluralRules can be subclassed. +author: Zibi Braniecki +includes: [testIntl.js] +---*/ + +// get a plural-rules and have it format an array of dates for comparison with the subclass +var locales = ["tlh", "id", "en"]; +var a = [1, 5, 12]; + +var referencePluralRules = new Intl.PluralRules(locales); +var referenceSelected = a.map(referencePluralRules.select.bind(referencePluralRules)); + +class MyPluralRules extends Intl.PluralRules { + constructor(locales, options) { + super(locales, options); + // could initialize MyPluralRules properties + } + // could add methods to MyPluralRules.prototype +} + +var pr = new MyPluralRules(locales); +var actual = a.map(pr.select.bind(pr)); +testArraysAreSame(referenceSelected, actual); diff --git a/test/intl402/PluralRules/internals.js b/test/intl402/PluralRules/internals.js new file mode 100644 index 0000000000..1de97b3cdf --- /dev/null +++ b/test/intl402/PluralRules/internals.js @@ -0,0 +1,21 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-intl-pluralrules-constructor +description: > + Tests that objects constructed by Intl.PluralRules have the specified + internal properties. +author: Zibi Braniecki +---*/ + +var obj = new Intl.PluralRules(); + +var actualPrototype = Object.getPrototypeOf(obj); +if (actualPrototype !== Intl.PluralRules.prototype) { + $ERROR("Prototype of object constructed by Intl.PluralRules isn't Intl.PluralRules.prototype; got " + actualPrototype); +} + +if (!Object.isExtensible(obj)) { + $ERROR("Object constructed by Intl.PluralRules must be extensible."); +} diff --git a/test/intl402/PluralRules/length.js b/test/intl402/PluralRules/length.js new file mode 100644 index 0000000000..9f701deff4 --- /dev/null +++ b/test/intl402/PluralRules/length.js @@ -0,0 +1,15 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules +description: Intl.PluralRules.length. +author: Zibi Braniecki +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Intl.PluralRules.length, 0); + +verifyNotEnumerable(Intl.PluralRules, "length"); +verifyNotWritable(Intl.PluralRules, "length"); +verifyConfigurable(Intl.PluralRules, "length"); diff --git a/test/intl402/PluralRules/name.js b/test/intl402/PluralRules/name.js new file mode 100644 index 0000000000..c5126aa88f --- /dev/null +++ b/test/intl402/PluralRules/name.js @@ -0,0 +1,15 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules +description: Intl.PluralRules.name is "PluralRules" +author: Zibi Braniecki +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Intl.PluralRules.name, "PluralRules"); + +verifyNotEnumerable(Intl.PluralRules, "name"); +verifyNotWritable(Intl.PluralRules, "name"); +verifyConfigurable(Intl.PluralRules, "name"); diff --git a/test/intl402/PluralRules/prototype/bind.js b/test/intl402/PluralRules/prototype/bind.js new file mode 100644 index 0000000000..fb07bd6e9f --- /dev/null +++ b/test/intl402/PluralRules/prototype/bind.js @@ -0,0 +1,34 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-properties-of-intl-pluralrules-prototype-object +description: > + Tests that Intl.PluralRules.prototype functions throw a TypeError if + called on a non-object value or an object that hasn't been + initialized as a PluralRules. +author: Zibi Braniecki +---*/ + +var functions = { + select: Intl.PluralRules.prototype.select, + resolvedOptions: Intl.PluralRules.prototype.resolvedOptions +}; +var invalidTargets = [undefined, null, true, 0, "PluralRules", [], {}]; + +Object.getOwnPropertyNames(functions).forEach(function (functionName) { + var f = functions[functionName]; + invalidTargets.forEach(function (target) { + var error; + try { + f.call(target); + } catch (e) { + error = e; + } + if (error === undefined) { + $ERROR("Calling " + functionName + " on " + target + " was not rejected."); + } else if (error.name !== "TypeError") { + $ERROR("Calling " + functionName + " on " + target + " was rejected with wrong error " + error.name + "."); + } + }); +}); diff --git a/test/intl402/PluralRules/prototype/builtins.js b/test/intl402/PluralRules/prototype/builtins.js new file mode 100644 index 0000000000..8e66ba4aa2 --- /dev/null +++ b/test/intl402/PluralRules/prototype/builtins.js @@ -0,0 +1,14 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-properties-of-intl-pluralrules-prototype-object +description: > + Tests that Intl.PluralRules.prototype meets the requirements for + built-in objects defined by the introduction of chapter 17 of the + ECMAScript Language Specification. +author: Zibi Braniecki +includes: [testBuiltInObject.js] +---*/ + +testBuiltInObject(Intl.PluralRules.prototype, false, false, ["constructor", "select", "resolvedOptions"]); diff --git a/test/intl402/PluralRules/prototype/constructor/main.js b/test/intl402/PluralRules/prototype/constructor/main.js new file mode 100644 index 0000000000..b76316ebd9 --- /dev/null +++ b/test/intl402/PluralRules/prototype/constructor/main.js @@ -0,0 +1,15 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.prototype.constructor +description: > + Tests that Intl.PluralRules.prototype is an object that has been + initialized as an Intl.PluralRules. +author: Zibi Braniecki +---*/ + +if (Intl.PluralRules.prototype.constructor !== Intl.PluralRules) { + $ERROR("Intl.PluralRules.prototype.constructor is not the same as " + + "Intl.PluralRules"); +} diff --git a/test/intl402/PluralRules/prototype/properties.js b/test/intl402/PluralRules/prototype/properties.js new file mode 100644 index 0000000000..7c05f838ee --- /dev/null +++ b/test/intl402/PluralRules/prototype/properties.js @@ -0,0 +1,22 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-properties-of-intl-pluralrules-prototype-object +description: Tests that Intl.PluralRules.prototype has the required attributes. +author: Zibi Braniecki +---*/ + +var desc = Object.getOwnPropertyDescriptor(Intl.PluralRules, "prototype"); +if (desc === undefined) { + $ERROR("Intl.PluralRules.prototype is not defined."); +} +if (desc.writable) { + $ERROR("Intl.PluralRules.prototype must not be writable."); +} +if (desc.enumerable) { + $ERROR("Intl.PluralRules.prototype must not be enumerable."); +} +if (desc.configurable) { + $ERROR("Intl.PluralRules.prototype must not be configurable."); +} diff --git a/test/intl402/PluralRules/prototype/prototype.js b/test/intl402/PluralRules/prototype/prototype.js new file mode 100644 index 0000000000..c3b9bcae58 --- /dev/null +++ b/test/intl402/PluralRules/prototype/prototype.js @@ -0,0 +1,17 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-properties-of-intl-pluralrules-prototype-object +description: > + Tests that Intl.PluralRules.prototype is an object that has been + initialized as an Intl.PluralRules. +author: Zibi Braniecki +---*/ + +// test by calling a function that would fail if "this" were not an object +// initialized as an Intl.PluralRules +if (typeof Intl.PluralRules.prototype.select(0) !== "string") { + $ERROR("Intl.PluralRules's prototype is not an object that has been " + + "initialized as an Intl.PluralRules"); +} diff --git a/test/intl402/PluralRules/prototype/resolvedOptions/builtins.js b/test/intl402/PluralRules/prototype/resolvedOptions/builtins.js new file mode 100644 index 0000000000..4e192530f0 --- /dev/null +++ b/test/intl402/PluralRules/prototype/resolvedOptions/builtins.js @@ -0,0 +1,14 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.prototype.resolvedOptions +description: > + Tests that Intl.PluralRules.prototype.resolvedOptions meets the requirements for + built-in objects defined by the introduction of chapter 17 of the + ECMAScript Language Specification. +author: Zibi Braniecki +includes: [testBuiltInObject.js] +---*/ + +testBuiltInObject(Intl.PluralRules.prototype.resolvedOptions, true, false, [], 0); diff --git a/test/intl402/PluralRules/prototype/resolvedOptions/name.js b/test/intl402/PluralRules/prototype/resolvedOptions/name.js new file mode 100755 index 0000000000..3510af8bad --- /dev/null +++ b/test/intl402/PluralRules/prototype/resolvedOptions/name.js @@ -0,0 +1,15 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.resolvedOptions.name +description: Intl.PluralRules.resolvedOptions.name is "resolvedOptions" +author: Zibi Braniecki +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Intl.PluralRules.prototype.resolvedOptions.name, "resolvedOptions"); + +verifyNotEnumerable(Intl.PluralRules.prototype.resolvedOptions, "name"); +verifyNotWritable(Intl.PluralRules.prototype.resolvedOptions, "name"); +verifyConfigurable(Intl.PluralRules.prototype.resolvedOptions, "name"); diff --git a/test/intl402/PluralRules/prototype/resolvedOptions/properties.js b/test/intl402/PluralRules/prototype/resolvedOptions/properties.js new file mode 100644 index 0000000000..05f7ea37a3 --- /dev/null +++ b/test/intl402/PluralRules/prototype/resolvedOptions/properties.js @@ -0,0 +1,30 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.prototype.resolvedOptions +description: > + Tests that the object returned by + Intl.PluralRules.prototype.resolvedOptions has the right + properties. +author: Zibi Braniecki +includes: [testIntl.js] +---*/ + +var actual = new Intl.PluralRules().resolvedOptions(); + +var actual2 = new Intl.PluralRules().resolvedOptions(); +if (actual2 === actual) { + $ERROR("resolvedOptions returned the same object twice."); +} + +// this assumes the default values where the specification provides them +mustHaveProperty(actual, "locale", isCanonicalizedStructurallyValidLanguageTag); +mustHaveProperty(actual, "type", ["cardinal"]); +mustNotHaveProperty(actual, "currency"); +mustNotHaveProperty(actual, "currencyDisplay"); +mustHaveProperty(actual, "minimumIntegerDigits", [1]); +mustHaveProperty(actual, "minimumFractionDigits", [0]); +mustHaveProperty(actual, "maximumFractionDigits", [3]); +mustNotHaveProperty(actual, "minimumSignificantDigits"); +mustNotHaveProperty(actual, "maximumSignificantDigits"); diff --git a/test/intl402/PluralRules/prototype/select/name.js b/test/intl402/PluralRules/prototype/select/name.js new file mode 100755 index 0000000000..83ab966d7d --- /dev/null +++ b/test/intl402/PluralRules/prototype/select/name.js @@ -0,0 +1,17 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.prototype.select +description: Intl.PluralRules.prototype.select.name is "select" +author: Zibi Braniecki +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Intl.PluralRules.prototype.select.name, 'select', + 'The value of `Intl.PluralRules.prototype.select.name` is `"select"`' +); + +verifyNotEnumerable(Intl.PluralRules.prototype.select, 'name'); +verifyNotWritable(Intl.PluralRules.prototype.select, 'name'); +verifyConfigurable(Intl.PluralRules.prototype.select, 'name'); diff --git a/test/intl402/PluralRules/prototype/select/non-finite.js b/test/intl402/PluralRules/prototype/select/non-finite.js new file mode 100644 index 0000000000..1539edeb6b --- /dev/null +++ b/test/intl402/PluralRules/prototype/select/non-finite.js @@ -0,0 +1,22 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.prototype.select +description: Tests that select function returns "other" for non finite values. +info: + 1.1.4. ResolvePlural (pluralRules, n) + (...) + 1.1.4_3. If isFinite(n) is false, then + 1.1.4_3.a. Return "other". +author: Zibi Braniecki + +---*/ + +var invalidValues = [NaN, Infinity, -Infinity]; + +var pr = new Intl.PluralRules(); + +invalidValues.forEach(function (value) { + assert.sameValue(pr.select(value), "other"); +}); diff --git a/test/intl402/PluralRules/prototype/select/tainting.js b/test/intl402/PluralRules/prototype/select/tainting.js new file mode 100644 index 0000000000..0decb708f6 --- /dev/null +++ b/test/intl402/PluralRules/prototype/select/tainting.js @@ -0,0 +1,20 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-intl-pluralrules-abstracts +description: > + Tests that the behavior of a Record is not affected by + adversarial changes to Object.prototype. +info: + 1.1.1. InitializePluralRules (pluralRules, locales, options) + (...) + 1.1.1_6. Let t be ? GetOption(options, "type", "string", « "cardinal", "ordinal" », "cardinal"). +author: Zibi Braniecki +includes: [testIntl.js] +---*/ + +taintProperties(["type"]); + +var pr = new Intl.PluralRules(); +var time = pr.select(9); diff --git a/test/intl402/PluralRules/supportedLocalesOf/arguments.js b/test/intl402/PluralRules/supportedLocalesOf/arguments.js new file mode 100644 index 0000000000..d48c184d1f --- /dev/null +++ b/test/intl402/PluralRules/supportedLocalesOf/arguments.js @@ -0,0 +1,14 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.supportedLocalesOf +description: > + Tests that Intl.PluralRules.supportedLocalesOf doesn't access + arguments that it's not given. +author: Zibi Braniecki +includes: [testIntl.js] +---*/ + +taintDataProperty(Object.prototype, "1"); +new Intl.PluralRules("und"); diff --git a/test/intl402/PluralRules/supportedLocalesOf/main.js b/test/intl402/PluralRules/supportedLocalesOf/main.js new file mode 100644 index 0000000000..f9888165c2 --- /dev/null +++ b/test/intl402/PluralRules/supportedLocalesOf/main.js @@ -0,0 +1,29 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.supportedLocalesOf +description: > + Tests that Intl.PluralRules has a supportedLocalesOf property, and + it works as planned. +author: Zibi Braniecki +---*/ + +var defaultLocale = new Intl.PluralRules().resolvedOptions().locale; +var notSupported = 'zxx'; // "no linguistic content" +var requestedLocales = [defaultLocale, notSupported]; + +var supportedLocales; + +if (!Intl.PluralRules.hasOwnProperty('supportedLocalesOf')) { + $ERROR("Intl.PluralRules doesn't have a supportedLocalesOf property."); +} + +supportedLocales = Intl.PluralRules.supportedLocalesOf(requestedLocales); +if (supportedLocales.length !== 1) { + $ERROR('The length of supported locales list is not 1.'); +} + +if (supportedLocales[0] !== defaultLocale) { + $ERROR('The default locale is not returned in the supported list.'); +} diff --git a/test/intl402/PluralRules/supportedLocalesOf/name.js b/test/intl402/PluralRules/supportedLocalesOf/name.js new file mode 100755 index 0000000000..42fb331104 --- /dev/null +++ b/test/intl402/PluralRules/supportedLocalesOf/name.js @@ -0,0 +1,14 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.supportedLocalesOf +description: Tests that Intl.PluralRules.supportedLocalesOf.name is "supportedLocalesOf" +author: Zibi Braniecki +includes: [propertyHelper.js] +---*/ +assert.sameValue(Intl.PluralRules.supportedLocalesOf.name, "supportedLocalesOf"); + +verifyNotEnumerable(Intl.PluralRules.supportedLocalesOf, "name"); +verifyNotWritable(Intl.PluralRules.supportedLocalesOf, "name"); +verifyConfigurable(Intl.PluralRules.supportedLocalesOf, "name"); diff --git a/test/intl402/PluralRules/supportedLocalesOf/supportedLocalesOf.js b/test/intl402/PluralRules/supportedLocalesOf/supportedLocalesOf.js new file mode 100644 index 0000000000..10c4ba2a9a --- /dev/null +++ b/test/intl402/PluralRules/supportedLocalesOf/supportedLocalesOf.js @@ -0,0 +1,14 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules.supportedLocalesOf +description: > + Tests that Intl.PluralRules.supportedLocalesOf meets the requirements for + built-in objects defined by the introduction of chapter 17 of the + ECMAScript Language Specification. +author: Zibi Braniecki +includes: [testBuiltInObject.js] +---*/ + +testBuiltInObject(Intl.PluralRules.supportedLocalesOf, true, false, [], 1); diff --git a/test/intl402/PluralRules/this-not-ignored.js b/test/intl402/PluralRules/this-not-ignored.js new file mode 100644 index 0000000000..922fcc51b5 --- /dev/null +++ b/test/intl402/PluralRules/this-not-ignored.js @@ -0,0 +1,29 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-Intl.PluralRules +description: Tests that the this-value is ignored in PluralRules +author: Zibi Braniecki +includes: [testIntl.js] +---*/ + +testWithIntlConstructors(function (Constructor) { + var obj, newObj; + + // variant 1: use constructor in a "new" expression + obj = new Constructor(); + newObj = Intl.PluralRules.call(obj); + if (obj === newObj) { + $ERROR("PluralRules object created with \"new\" was not ignored as this-value."); + } + + // variant 2: use constructor as a function + obj = Constructor(); + newObj = Intl.PluralRules.call(obj); + if (obj === newObj) { + $ERROR("PluralRules object created with constructor as function was not ignored as this-value."); + } + + return true; +});