From cac55f2999b20917236dc30a021025fbfe2096a7 Mon Sep 17 00:00:00 2001 From: Zibi Braniecki <zibi@braniecki.net> Date: Mon, 15 Aug 2016 15:23:34 -0700 Subject: [PATCH] NumberFormat.prototype.formatToParts tests, part 1 (#744) --- .../prototype/formatToParts/formatToParts.js | 17 +++++ .../prototype/formatToParts/length.js | 13 ++++ .../prototype/formatToParts/main.js | 65 +++++++++++++++++++ .../prototype/formatToParts/name.js | 15 +++++ .../formatToParts/return-abrupt-tonumber.js | 40 ++++++++++++ .../this-has-not-internal-throws.js | 17 +++++ .../this-is-not-object-throws.js | 39 +++++++++++ 7 files changed, 206 insertions(+) create mode 100644 test/intl402/NumberFormat/prototype/formatToParts/formatToParts.js create mode 100644 test/intl402/NumberFormat/prototype/formatToParts/length.js create mode 100644 test/intl402/NumberFormat/prototype/formatToParts/main.js create mode 100644 test/intl402/NumberFormat/prototype/formatToParts/name.js create mode 100644 test/intl402/NumberFormat/prototype/formatToParts/return-abrupt-tonumber.js create mode 100644 test/intl402/NumberFormat/prototype/formatToParts/this-has-not-internal-throws.js create mode 100644 test/intl402/NumberFormat/prototype/formatToParts/this-is-not-object-throws.js diff --git a/test/intl402/NumberFormat/prototype/formatToParts/formatToParts.js b/test/intl402/NumberFormat/prototype/formatToParts/formatToParts.js new file mode 100644 index 0000000000..3d584db06c --- /dev/null +++ b/test/intl402/NumberFormat/prototype/formatToParts/formatToParts.js @@ -0,0 +1,17 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +description: Property type and descriptor. +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + typeof Intl.NumberFormat.prototype.formatToParts, + 'function', + '`typeof Intl.NumberFormat.prototype.formatToParts` is `function`' +); + +verifyNotEnumerable(Intl.NumberFormat.prototype, 'formatToParts'); +verifyWritable(Intl.NumberFormat.prototype, 'formatToParts'); +verifyConfigurable(Intl.NumberFormat.prototype, 'formatToParts'); diff --git a/test/intl402/NumberFormat/prototype/formatToParts/length.js b/test/intl402/NumberFormat/prototype/formatToParts/length.js new file mode 100644 index 0000000000..071b9d0aa2 --- /dev/null +++ b/test/intl402/NumberFormat/prototype/formatToParts/length.js @@ -0,0 +1,13 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +description: Intl.NumberFormat.prototype.formatToParts.length. +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Intl.NumberFormat.prototype.formatToParts.length, 0); + +verifyNotEnumerable(Intl.NumberFormat.prototype.formatToParts, "length"); +verifyNotWritable(Intl.NumberFormat.prototype.formatToParts, "length"); +verifyConfigurable(Intl.NumberFormat.prototype.formatToParts, "length"); diff --git a/test/intl402/NumberFormat/prototype/formatToParts/main.js b/test/intl402/NumberFormat/prototype/formatToParts/main.js new file mode 100644 index 0000000000..e4c3fd0036 --- /dev/null +++ b/test/intl402/NumberFormat/prototype/formatToParts/main.js @@ -0,0 +1,65 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +description: Tests for existance and behavior of Intl.NumberFormat.prototype.formatToParts +---*/ + +function reduce(parts) { + return parts.map(part => part.value).join(''); +} + +function compareFTPtoFormat(locales, options, value) { + const nf = new Intl.NumberFormat(locales, options); + assert.sameValue( + nf.format(value), + reduce(nf.formatToParts(value)), + `Expected the same value for value ${value}, + locales: ${locales} and options: ${options}` + ); +} + +const num1 = 123456.789; +const num2 = 0.123; + +compareFTPtoFormat(); +compareFTPtoFormat('pl'); +compareFTPtoFormat(['pl']); +compareFTPtoFormat([]); +compareFTPtoFormat(['de'], undefined, 0); +compareFTPtoFormat(['de'], undefined, -10); +compareFTPtoFormat(['de'], undefined, 25324234235); +compareFTPtoFormat(['de'], undefined, num1); +compareFTPtoFormat(['de'], { + style: 'percent' +}, num2); +compareFTPtoFormat(['de'], { + style: 'currency', + currency: 'EUR' +}, num1); +compareFTPtoFormat(['de'], { + style: 'currency', + currency: 'EUR', + currencyDisplay: 'code' +}, num1); +compareFTPtoFormat(['de'], { + useGrouping: true +}, num1); +compareFTPtoFormat(['de'], { + useGrouping: false +}, num1); +compareFTPtoFormat(['de'], { + minimumIntegerDigits: 2 +}, num2); +compareFTPtoFormat(['de'], { + minimumFractionDigits: 6 +}, num2); +compareFTPtoFormat(['de'], { + maximumFractionDigits: 1 +}, num2); +compareFTPtoFormat(['de'], { + maximumSignificantDigits: 3 +}, num1); +compareFTPtoFormat(['de'], { + maximumSignificantDigits: 5 +}, num1); diff --git a/test/intl402/NumberFormat/prototype/formatToParts/name.js b/test/intl402/NumberFormat/prototype/formatToParts/name.js new file mode 100644 index 0000000000..1cff87d093 --- /dev/null +++ b/test/intl402/NumberFormat/prototype/formatToParts/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. + +/*--- +description: Intl.NumberFormat.prototype.formatToParts.name value and descriptor. +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Intl.NumberFormat.prototype.formatToParts.name, 'formatToParts', + 'The value of `Intl.NumberFormat.prototype.formatToParts.name` is `"formatToParts"`' +); + +verifyNotEnumerable(Intl.NumberFormat.prototype.formatToParts, 'name'); +verifyNotWritable(Intl.NumberFormat.prototype.formatToParts, 'name'); +verifyConfigurable(Intl.NumberFormat.prototype.formatToParts, 'name'); diff --git a/test/intl402/NumberFormat/prototype/formatToParts/return-abrupt-tonumber.js b/test/intl402/NumberFormat/prototype/formatToParts/return-abrupt-tonumber.js new file mode 100644 index 0000000000..27cf4574ce --- /dev/null +++ b/test/intl402/NumberFormat/prototype/formatToParts/return-abrupt-tonumber.js @@ -0,0 +1,40 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. +// +/*--- +description: > + Return abrupt completions from ToNumber(date) +info: | + Intl.NumberFormat.prototype.formatToParts ([ value ]) + + 5. Let _x_ be ? ToNumber(_value_). +features: [Symbol] +---*/ + +var obj1 = { + valueOf: function() { + throw new Test262Error(); + } +}; + +var obj2 = { + toString: function() { + throw new Test262Error(); + } +}; + +var nf = new Intl.NumberFormat(["pt-BR"]); + +assert.throws(Test262Error, function() { + nf.formatToParts(obj1); +}, "valueOf"); + +assert.throws(Test262Error, function() { + nf.formatToParts(obj2); +}, "toString"); + +var s = Symbol('1'); +assert.throws(TypeError, function() { + nf.formatToParts(s); +}, "symbol"); + diff --git a/test/intl402/NumberFormat/prototype/formatToParts/this-has-not-internal-throws.js b/test/intl402/NumberFormat/prototype/formatToParts/this-has-not-internal-throws.js new file mode 100644 index 0000000000..c478c92c5c --- /dev/null +++ b/test/intl402/NumberFormat/prototype/formatToParts/this-has-not-internal-throws.js @@ -0,0 +1,17 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +description: > + Throws a TypeError if this is not a NumberFormat object +---*/ + +var formatToParts = Intl.NumberFormat.prototype.formatToParts; + +assert.throws(TypeError, function() { + formatToParts.call({}); +}, "{}"); + +assert.throws(TypeError, function() { + formatToParts.call(new Number()); +}, "new Number()"); diff --git a/test/intl402/NumberFormat/prototype/formatToParts/this-is-not-object-throws.js b/test/intl402/NumberFormat/prototype/formatToParts/this-is-not-object-throws.js new file mode 100644 index 0000000000..def58e3d54 --- /dev/null +++ b/test/intl402/NumberFormat/prototype/formatToParts/this-is-not-object-throws.js @@ -0,0 +1,39 @@ +// Copyright 2016 Mozilla Corporation. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +description: Throws a TypeError if this is not Object +features: [Symbol] +---*/ + +var formatToParts = Intl.NumberFormat.prototype.formatToParts; + +assert.throws(TypeError, function() { + formatToParts.call(undefined); +}, "undefined"); + +assert.throws(TypeError, function() { + formatToParts.call(null); +}, "null"); + +assert.throws(TypeError, function() { + formatToParts.call(42); +}, "number"); + +assert.throws(TypeError, function() { + formatToParts.call("foo"); +}, "string"); + +assert.throws(TypeError, function() { + formatToParts.call(false); +}, "false"); + +assert.throws(TypeError, function() { + formatToParts.call(true); +}, "true"); + +var s = Symbol('1'); +assert.throws(TypeError, function() { + formatToParts.call(s); +}, "symbol"); + -- GitLab