Skip to content

Commit

Permalink
Temporal: Extend toJSON/toString tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger authored and ptomato committed May 20, 2022
1 parent 2de2fe3 commit 7fca663
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 51 deletions.
25 changes: 16 additions & 9 deletions test/built-ins/Temporal/Instant/prototype/toJSON/basic.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.tojson
description: Basic tests for toJSON()
features: [Temporal]
description: Basic behavior for toJSON
features: [BigInt, Temporal]
---*/

const instantBeforeEpoch = Temporal.Instant.from("1963-02-13T10:36:29.123456789+01:00");
assert.sameValue(instantBeforeEpoch.toJSON(), "1963-02-13T09:36:29.123456789Z");
const tests = [
[new Temporal.Instant(192_258_181_000_000_000n), "1976-02-04T05:03:01Z"],
[new Temporal.Instant(0n), "1970-01-01T00:00:00Z"],
[new Temporal.Instant(30_000_000_000n), "1970-01-01T00:00:30Z"],
[new Temporal.Instant(30_123_400_000n), "1970-01-01T00:00:30.1234Z"],
];

const instantAfterEpoch = Temporal.Instant.from("1976-11-18T15:23:30.123456789+01:00");
assert.sameValue(instantAfterEpoch.toJSON(), "1976-11-18T14:23:30.123456789Z");
assert.sameValue(instantAfterEpoch.toJSON("+01:00"), "1976-11-18T14:23:30.123456789Z",
"after epoch with ignored argument");
const options = new Proxy({}, {
get() { throw new Test262Error("should not get properties off argument") }
});
for (const [instant, expected] of tests) {
assert.sameValue(instant.toJSON(), expected, "toJSON without argument");
assert.sameValue(instant.toJSON(options), expected, "toJSON with argument");
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ description: auto value for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/

const zeroSeconds = new Temporal.Instant(0n);
const wholeSeconds = new Temporal.Instant(30_000_000_000n);
const subSeconds = new Temporal.Instant(30_123_400_000n);

const tests = [
[zeroSeconds, "1970-01-01T00:00:00Z"],
[wholeSeconds, "1970-01-01T00:00:30Z"],
[subSeconds, "1970-01-01T00:00:30.1234Z"],
[new Temporal.Instant(192_258_181_000_000_000n), "1976-02-04T05:03:01Z"],
[new Temporal.Instant(0n), "1970-01-01T00:00:00Z"],
[new Temporal.Instant(30_000_000_000n), "1970-01-01T00:00:30Z"],
[new Temporal.Instant(30_123_400_000n), "1970-01-01T00:00:30.1234Z"],
];

for (const [instant, expected] of tests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ description: Number for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/

const fewSeconds = new Temporal.Instant(192_258_181_000_000_000n);
const zeroSeconds = new Temporal.Instant(0n);
const wholeSeconds = new Temporal.Instant(30_000_000_000n);
const subSeconds = new Temporal.Instant(30_123_400_000n);

assert.sameValue(fewSeconds.toString({ fractionalSecondDigits: 0 }), "1976-02-04T05:03:01Z",
"pads parts with 0");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1970-01-01T00:00:30Z",
"truncates 4 decimal places to 0");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:00.00Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ info: |
features: [Temporal]
---*/

const zeroSeconds = new Temporal.Instant(0n);
const wholeSeconds = new Temporal.Instant(30_000_000_000n);
const subSeconds = new Temporal.Instant(30_123_400_000n);

const tests = [
[zeroSeconds, "1970-01-01T00:00:00Z"],
[wholeSeconds, "1970-01-01T00:00:30Z"],
[subSeconds, "1970-01-01T00:00:30.1234Z"],
[new Temporal.Instant(192_258_181_000_000_000n), "1976-02-04T05:03:01Z"],
[new Temporal.Instant(0n), "1970-01-01T00:00:00Z"],
[new Temporal.Instant(30_000_000_000n), "1970-01-01T00:00:30Z"],
[new Temporal.Instant(30_123_400_000n), "1970-01-01T00:00:30.1234Z"],
];

for (const [instant, expected] of tests) {
Expand Down
23 changes: 23 additions & 0 deletions test/built-ins/Temporal/PlainDateTime/prototype/toJSON/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindatetime.prototype.tojson
description: Basic behavior for toJSON
features: [Temporal]
---*/

const tests = [
[new Temporal.PlainDateTime(1976, 2, 4, 5, 3, 1), "1976-02-04T05:03:01"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23), "1976-11-18T15:23:00"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30), "1976-11-18T15:23:30"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400), "1976-11-18T15:23:30.1234"],
];

const options = new Proxy({}, {
get() { throw new Test262Error("should not get properties off argument") }
});
for (const [datetime, expected] of tests) {
assert.sameValue(datetime.toJSON(), expected, "toJSON without argument");
assert.sameValue(datetime.toJSON(options), expected, "toJSON with argument");
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ description: auto value for fractionalSecondDigits option
features: [Temporal]
---*/

const zeroSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
const wholeSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30);
const subSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400);

const tests = [
[zeroSeconds, "1976-11-18T15:23:00"],
[wholeSeconds, "1976-11-18T15:23:30"],
[subSeconds, "1976-11-18T15:23:30.1234"],
[new Temporal.PlainDateTime(1976, 2, 4, 5, 3, 1), "1976-02-04T05:03:01"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23), "1976-11-18T15:23:00"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30), "1976-11-18T15:23:30"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400), "1976-11-18T15:23:30.1234"],
];

for (const [datetime, expected] of tests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ description: Number for fractionalSecondDigits option
features: [Temporal]
---*/

const fewSeconds = new Temporal.PlainDateTime(1976, 2, 4, 5, 3, 1);
const zeroSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
const wholeSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30);
const subSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400);

assert.sameValue(fewSeconds.toString({ fractionalSecondDigits: 0 }), "1976-02-04T05:03:01",
"pads parts with 0");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1976-11-18T15:23:30",
"truncates 4 decimal places to 0");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1976-11-18T15:23:00.00",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ info: |
features: [Temporal]
---*/

const zeroSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
const wholeSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30);
const subSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400);

const tests = [
[zeroSeconds, "1976-11-18T15:23:00"],
[wholeSeconds, "1976-11-18T15:23:30"],
[subSeconds, "1976-11-18T15:23:30.1234"],
[new Temporal.PlainDateTime(1976, 2, 4, 5, 3, 1), "1976-02-04T05:03:01"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23), "1976-11-18T15:23:00"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30), "1976-11-18T15:23:30"],
[new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400), "1976-11-18T15:23:30.1234"],
];

for (const [datetime, expected] of tests) {
Expand Down
23 changes: 23 additions & 0 deletions test/built-ins/Temporal/ZonedDateTime/prototype/toJSON/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.zoneddatetime.prototype.tojson
description: Basic behavior for toJSON
features: [BigInt, Temporal]
---*/

const tests = [
[new Temporal.ZonedDateTime(192_258_181_000_000_000n, "UTC"), "1976-02-04T05:03:01+00:00[UTC]"],
[new Temporal.ZonedDateTime(0n, "UTC"), "1970-01-01T00:00:00+00:00[UTC]"],
[new Temporal.ZonedDateTime(30_000_000_000n, "UTC"), "1970-01-01T00:00:30+00:00[UTC]"],
[new Temporal.ZonedDateTime(30_123_400_000n, "UTC"), "1970-01-01T00:00:30.1234+00:00[UTC]"],
];

const options = new Proxy({}, {
get() { throw new Test262Error("should not get properties off argument") }
});
for (const [datetime, expected] of tests) {
assert.sameValue(datetime.toJSON(), expected, "toJSON without argument");
assert.sameValue(datetime.toJSON(options), expected, "toJSON with argument");
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ description: auto value for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/

const zeroSeconds = new Temporal.ZonedDateTime(0n, "UTC");
const wholeSeconds = new Temporal.ZonedDateTime(30_000_000_000n, "UTC");
const subSeconds = new Temporal.ZonedDateTime(30_123_400_000n, "UTC");

const tests = [
[zeroSeconds, "1970-01-01T00:00:00+00:00[UTC]"],
[wholeSeconds, "1970-01-01T00:00:30+00:00[UTC]"],
[subSeconds, "1970-01-01T00:00:30.1234+00:00[UTC]"],
[new Temporal.ZonedDateTime(192_258_181_000_000_000n, "UTC"), "1976-02-04T05:03:01+00:00[UTC]"],
[new Temporal.ZonedDateTime(0n, "UTC"), "1970-01-01T00:00:00+00:00[UTC]"],
[new Temporal.ZonedDateTime(30_000_000_000n, "UTC"), "1970-01-01T00:00:30+00:00[UTC]"],
[new Temporal.ZonedDateTime(30_123_400_000n, "UTC"), "1970-01-01T00:00:30.1234+00:00[UTC]"],
];

for (const [datetime, expected] of tests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ description: Number for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/

const fewSeconds = new Temporal.ZonedDateTime(192_258_181_000_000_000n, "UTC");
const zeroSeconds = new Temporal.ZonedDateTime(0n, "UTC");
const wholeSeconds = new Temporal.ZonedDateTime(30_000_000_000n, "UTC");
const subSeconds = new Temporal.ZonedDateTime(30_123_400_000n, "UTC");

assert.sameValue(fewSeconds.toString({ fractionalSecondDigits: 0 }), "1976-02-04T05:03:01+00:00[UTC]",
"pads parts with 0");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1970-01-01T00:00:30+00:00[UTC]",
"truncates 4 decimal places to 0");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:00.00+00:00[UTC]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ info: |
features: [Temporal]
---*/

const zeroSeconds = new Temporal.ZonedDateTime(0n, "UTC");
const wholeSeconds = new Temporal.ZonedDateTime(30_000_000_000n, "UTC");
const subSeconds = new Temporal.ZonedDateTime(30_123_400_000n, "UTC");

const tests = [
[zeroSeconds, "1970-01-01T00:00:00+00:00[UTC]"],
[wholeSeconds, "1970-01-01T00:00:30+00:00[UTC]"],
[subSeconds, "1970-01-01T00:00:30.1234+00:00[UTC]"],
[new Temporal.ZonedDateTime(192_258_181_000_000_000n, "UTC"), "1976-02-04T05:03:01+00:00[UTC]"],
[new Temporal.ZonedDateTime(0n, "UTC"), "1970-01-01T00:00:00+00:00[UTC]"],
[new Temporal.ZonedDateTime(30_000_000_000n, "UTC"), "1970-01-01T00:00:30+00:00[UTC]"],
[new Temporal.ZonedDateTime(30_123_400_000n, "UTC"), "1970-01-01T00:00:30.1234+00:00[UTC]"],
];

for (const [datetime, expected] of tests) {
Expand Down

0 comments on commit 7fca663

Please sign in to comment.