Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DateTimeFormat.prototype.formatToParts tests #734

Merged
merged 1 commit into from Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,33 @@
// Copyright 2016 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.

/*---
description: >
Throws a RangeError if date arg is cast to an Infinity value
info: |
Intl.DateTimeFormat.prototype.formatToParts ([ date ])

4. If _date_ is not provided or is *undefined*, then
a. Let _x_ be *%Date_now%*().
5. Else,
a. Let _x_ be ? ToNumber(_date_).
6. Return ? FormatDateTimeToParts(_dtf_, _x_).

FormatDateTimeToParts(dateTimeFormat, x)

1. Let _parts_ be ? PartitionDateTimePattern(_dateTimeFormat_, _x_).

PartitionDateTimePattern (dateTimeFormat, x)

1. If _x_ is not a finite Number, throw a *RangeError* exception.
---*/

var dtf = new Intl.DateTimeFormat(["pt-BR"]);

assert.throws(RangeError, function() {
dtf.formatToParts(Infinity);
}, "+Infinity");

assert.throws(RangeError, function() {
dtf.formatToParts(-Infinity);
}, "-Infinity");
@@ -0,0 +1,33 @@
// Copyright 2016 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.

/*---
description: >
Throws a RangeError if date arg is cast to NaN
info: |
Intl.DateTimeFormat.prototype.formatToParts ([ date ])

4. If _date_ is not provided or is *undefined*, then
a. Let _x_ be *%Date_now%*().
5. Else,
a. Let _x_ be ? ToNumber(_date_).
6. Return ? FormatDateTimeToParts(_dtf_, _x_).

FormatDateTimeToParts(dateTimeFormat, x)

1. Let _parts_ be ? PartitionDateTimePattern(_dateTimeFormat_, _x_).

PartitionDateTimePattern (dateTimeFormat, x)

1. If _x_ is not a finite Number, throw a *RangeError* exception.
---*/

var dtf = new Intl.DateTimeFormat(["pt-BR"]);

assert.throws(RangeError, function() {
dtf.formatToParts(NaN);
});

assert.throws(RangeError, function() {
dtf.formatToParts("lol");
});
@@ -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.DateTimeFormat.prototype.formatToParts,
'function',
'`typeof Intl.DateTimeFormat.prototype.formatToParts` is `function`'
);

verifyNotEnumerable(Intl.DateTimeFormat.prototype, 'formatToParts');
verifyWritable(Intl.DateTimeFormat.prototype, 'formatToParts');
verifyConfigurable(Intl.DateTimeFormat.prototype, 'formatToParts');
13 changes: 13 additions & 0 deletions test/intl402/DateTimeFormat/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.DateTimeFormat.prototype.formatToParts.length.
includes: [propertyHelper.js]
---*/

assert.sameValue(Intl.DateTimeFormat.prototype.formatToParts.length, 0);

verifyNotEnumerable(Intl.DateTimeFormat.prototype.formatToParts, "length");
verifyNotWritable(Intl.DateTimeFormat.prototype.formatToParts, "length");
verifyConfigurable(Intl.DateTimeFormat.prototype.formatToParts, "length");
40 changes: 40 additions & 0 deletions test/intl402/DateTimeFormat/prototype/formatToParts/main.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: Tests for existance and behavior of Intl.DateTimeFormat.prototype.formatToParts
---*/

function reduce(parts) {
return parts.map(part => part.value).join('');
}

function compareFTPtoFormat(locales, options, value) {
const dtf = new Intl.DateTimeFormat(locales, options);
assert.sameValue(
dtf.format(value),
reduce(dtf.formatToParts(value)),
`Expected the same value for value ${value},
locales: ${locales} and options: ${options}`
);
}

compareFTPtoFormat();
compareFTPtoFormat('pl');
compareFTPtoFormat(['pl']);
compareFTPtoFormat([]);
compareFTPtoFormat(['de'], undefined, 0);
compareFTPtoFormat(['de'], undefined, -10);
compareFTPtoFormat(['de'], undefined, 25324234235);
compareFTPtoFormat(['de'], {
day: '2-digit'
}, Date.now());
compareFTPtoFormat(['de'], {
day: 'numeric',
year: '2-digit'
}, Date.now());
compareFTPtoFormat(['ar'], {
month: 'numeric',
day: 'numeric',
year: '2-digit'
}, Date.now());
15 changes: 15 additions & 0 deletions test/intl402/DateTimeFormat/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.DateTimeFormat.prototype.formatToParts.name value and descriptor.
includes: [propertyHelper.js]
---*/

assert.sameValue(Intl.DateTimeFormat.prototype.formatToParts.name, 'formatToParts',
'The value of `Intl.DateTimeFormat.prototype.formatToParts.name` is `"formatToParts"`'
);

verifyNotEnumerable(Intl.DateTimeFormat.prototype.formatToParts, 'name');
verifyNotWritable(Intl.DateTimeFormat.prototype.formatToParts, 'name');
verifyConfigurable(Intl.DateTimeFormat.prototype.formatToParts, 'name');
@@ -0,0 +1,42 @@
// Copyright 2016 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.

/*---
description: >
Return abrupt completions from ToNumber(date)
info: |
Intl.DateTimeFormat.prototype.formatToParts ([ date ])

4. If _date_ is not provided or is *undefined*, then
a. Let _x_ be *%Date_now%*().
5. Else,
a. Let _x_ be ? ToNumber(_date_).
features: [Symbol]
---*/

var obj1 = {
valueOf: function() {
throw new Test262Error();
}
};

var obj2 = {
toString: function() {
throw new Test262Error();
}
};

var dtf = new Intl.DateTimeFormat(["pt-BR"]);

assert.throws(Test262Error, function() {
dtf.formatToParts(obj1);
}, "valueOf");

assert.throws(Test262Error, function() {
dtf.formatToParts(obj2);
}, "toString");

var s = Symbol('1');
assert.throws(TypeError, function() {
dtf.formatToParts(s);
}, "symbol");
@@ -0,0 +1,17 @@
// Copyright 2016 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.

/*---
description: >
Throws a TypeError if this is not a DateTimeFormat object
---*/

var formatToParts = Intl.DateTimeFormat.prototype.formatToParts;

assert.throws(TypeError, function() {
formatToParts.call({});
}, "{}");

assert.throws(TypeError, function() {
formatToParts.call(new Date());
}, "new Date()");
@@ -0,0 +1,38 @@
// Copyright 2016 Leonardo Balter. 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.DateTimeFormat.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");