Skip to content

Commit

Permalink
Add some basic functionality tests for Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomato authored and Ms2ger committed Sep 29, 2021
1 parent acfedb7 commit d3de474
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 128 deletions.
14 changes: 14 additions & 0 deletions polyfill/test/Instant/constructor/fromEpochMicroseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.fromepochmicroseconds
description: Basic tests for Instant.fromEpochMicroseconds().
features: [BigInt, Temporal]
---*/

const afterEpoch = Temporal.Instant.fromEpochMicroseconds(217175010_123_456n);
assert.sameValue(afterEpoch.epochNanoseconds, 217175010_123_456_000n, "fromEpochMicroseconds post epoch");

const beforeEpoch = Temporal.Instant.fromEpochMicroseconds(-217175010_876_543n);
assert.sameValue(beforeEpoch.epochNanoseconds, -217175010_876_543_000n, "fromEpochMicroseconds pre epoch");
14 changes: 14 additions & 0 deletions polyfill/test/Instant/constructor/fromEpochMilliseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.fromepochmilliseconds
description: Basic tests for Instant.fromEpochMilliseconds().
features: [BigInt, Temporal]
---*/

const afterEpoch = Temporal.Instant.fromEpochMilliseconds(217175010_123);
assert.sameValue(afterEpoch.epochNanoseconds, 217175010_123_000_000n, "fromEpochMilliseconds post epoch");

const beforeEpoch = Temporal.Instant.fromEpochMilliseconds(-217175010_876);
assert.sameValue(beforeEpoch.epochNanoseconds, -217175010_876_000_000n, "fromEpochMilliseconds pre epoch");
14 changes: 14 additions & 0 deletions polyfill/test/Instant/constructor/fromEpochNanoseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.fromepochnanoseconds
description: Basic tests for Instant.fromEpochNanoseconds().
features: [BigInt, Temporal]
---*/

const afterEpoch = Temporal.Instant.fromEpochNanoseconds(217175010_123_456_789n);
assert.sameValue(afterEpoch.epochNanoseconds, 217175010_123_456_789n, "fromEpochNanoseconds post epoch");

const beforeEpoch = Temporal.Instant.fromEpochNanoseconds(-217175010_876_543_211n);
assert.sameValue(beforeEpoch.epochNanoseconds, -217175010_876_543_211n, "fromEpochNanoseconds pre epoch");
14 changes: 14 additions & 0 deletions polyfill/test/Instant/constructor/fromEpochSeconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.fromepochseconds
description: Basic tests for Instant.fromEpochSeconds().
features: [BigInt, Temporal]
---*/

const afterEpoch = Temporal.Instant.fromEpochSeconds(217175010);
assert.sameValue(afterEpoch.epochNanoseconds, 217175010_000_000_000n, "fromEpochSeconds post epoch");

const beforeEpoch = Temporal.Instant.fromEpochSeconds(-217175010);
assert.sameValue(beforeEpoch.epochNanoseconds, -217175010_000_000_000n, "fromEpochSeconds pre epoch");
16 changes: 16 additions & 0 deletions polyfill/test/Instant/prototype/epochMicroseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.instant.prototype.epochmicroseconds
description: Basic tests for epochMicroseconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.Instant(217175010_123_456_789n);
assert.sameValue(afterEpoch.epochMicroseconds, 217175010_123_456n, "epochMicroseconds post epoch");
assert.sameValue(typeof afterEpoch.epochMicroseconds, "bigint", "epochMicroseconds value is a bigint");

const beforeEpoch = new Temporal.Instant(-217175010_876_543_211n);
assert.sameValue(beforeEpoch.epochMicroseconds, -217175010_876_543n, "epochMicroseconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochMicroseconds, "bigint", "epochMicroseconds value is a bigint");
16 changes: 16 additions & 0 deletions polyfill/test/Instant/prototype/epochMilliseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.instant.prototype.epochmilliseconds
description: Basic tests for epochMilliseconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.Instant(217175010_123_456_789n);
assert.sameValue(afterEpoch.epochMilliseconds, 217175010_123, "epochMilliseconds post epoch");
assert.sameValue(typeof afterEpoch.epochMilliseconds, "number", "epochMilliseconds value is a number");

const beforeEpoch = new Temporal.Instant(-217175010_876_543_211n);
assert.sameValue(beforeEpoch.epochMilliseconds, -217175010_876, "epochMilliseconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochMilliseconds, "number", "epochMilliseconds value is a number");
16 changes: 16 additions & 0 deletions polyfill/test/Instant/prototype/epochNanoseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.instant.prototype.epochnanoseconds
description: Basic tests for epochNanoseconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.Instant(217175010_123_456_789n);
assert.sameValue(afterEpoch.epochNanoseconds, 217175010_123_456_789n, "epochNanoseconds post epoch");
assert.sameValue(typeof afterEpoch.epochNanoseconds, "bigint", "epochNanoseconds value is a bigint");

const beforeEpoch = new Temporal.Instant(-217175010_876_543_211n);
assert.sameValue(beforeEpoch.epochNanoseconds, -217175010_876_543_211n, "epochNanoseconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochNanoseconds, "bigint", "epochNanoseconds value is a bigint");
16 changes: 16 additions & 0 deletions polyfill/test/Instant/prototype/epochSeconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.instant.prototype.epochseconds
description: Basic tests for epochSeconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.Instant(217175010_123_456_789n);
assert.sameValue(afterEpoch.epochSeconds, 217175010, "epochSeconds post epoch");
assert.sameValue(typeof afterEpoch.epochSeconds, "number", "epochSeconds value is a number");

const beforeEpoch = new Temporal.Instant(-217175010_876_543_211n);
assert.sameValue(beforeEpoch.epochSeconds, -217175010, "epochSeconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochSeconds, "number", "epochSeconds value is a number");
14 changes: 14 additions & 0 deletions polyfill/test/Instant/prototype/toString/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

const afterEpoch = new Temporal.Instant(217175010_123_456_789n);
assert.sameValue(afterEpoch.toString(), "1976-11-18T14:23:30.123456789Z", "basic toString() after epoch");

const beforeEpoch = new Temporal.Instant(-217175010_876_543_211n);
assert.sameValue(beforeEpoch.toString(), "1963-02-13T09:36:29.123456789Z", "basic toString() before epoch");
16 changes: 16 additions & 0 deletions polyfill/test/ZonedDateTime/prototype/epochMicroseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.zoneddatetime.prototype.epochmicroseconds
description: Basic tests for epochMicroseconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.ZonedDateTime(217175010_123_456_789n, "UTC");
assert.sameValue(afterEpoch.epochMicroseconds, 217175010_123_456n, "epochMicroseconds post epoch");
assert.sameValue(typeof afterEpoch.epochMicroseconds, "bigint", "epochMicroseconds value is a bigint");

const beforeEpoch = new Temporal.ZonedDateTime(-217175010_876_543_211n, "UTC");
assert.sameValue(beforeEpoch.epochMicroseconds, -217175010_876_543n, "epochMicroseconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochMicroseconds, "bigint", "epochMicroseconds value is a bigint");
16 changes: 16 additions & 0 deletions polyfill/test/ZonedDateTime/prototype/epochMilliseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.zoneddatetime.prototype.epochmilliseconds
description: Basic tests for epochMilliseconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.ZonedDateTime(217175010_123_456_789n, "UTC");
assert.sameValue(afterEpoch.epochMilliseconds, 217175010_123, "epochMilliseconds post epoch");
assert.sameValue(typeof afterEpoch.epochMilliseconds, "number", "epochMilliseconds value is a number");

const beforeEpoch = new Temporal.ZonedDateTime(-217175010_876_543_211n, "UTC");
assert.sameValue(beforeEpoch.epochMilliseconds, -217175010_876, "epochMilliseconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochMilliseconds, "number", "epochMilliseconds value is a number");
16 changes: 16 additions & 0 deletions polyfill/test/ZonedDateTime/prototype/epochNanoseconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.zoneddatetime.prototype.epochnanoseconds
description: Basic tests for epochNanoseconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.ZonedDateTime(217175010_123_456_789n, "UTC");
assert.sameValue(afterEpoch.epochNanoseconds, 217175010_123_456_789n, "epochNanoseconds post epoch");
assert.sameValue(typeof afterEpoch.epochNanoseconds, "bigint", "epochNanoseconds value is a bigint");

const beforeEpoch = new Temporal.ZonedDateTime(-217175010_876_543_211n, "UTC");
assert.sameValue(beforeEpoch.epochNanoseconds, -217175010_876_543_211n, "epochNanoseconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochNanoseconds, "bigint", "epochNanoseconds value is a bigint");
16 changes: 16 additions & 0 deletions polyfill/test/ZonedDateTime/prototype/epochSeconds/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-get-temporal.zoneddatetime.prototype.epochseconds
description: Basic tests for epochSeconds.
features: [BigInt, Temporal]
---*/

const afterEpoch = new Temporal.ZonedDateTime(217175010_123_456_789n, "UTC");
assert.sameValue(afterEpoch.epochSeconds, 217175010, "epochSeconds post epoch");
assert.sameValue(typeof afterEpoch.epochSeconds, "number", "epochSeconds value is a number");

const beforeEpoch = new Temporal.ZonedDateTime(-217175010_876_543_211n, "UTC");
assert.sameValue(beforeEpoch.epochSeconds, -217175010, "epochSeconds pre epoch");
assert.sameValue(typeof beforeEpoch.epochSeconds, "number", "epochSeconds value is a number");
128 changes: 0 additions & 128 deletions polyfill/test/instant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ const { Instant } = Temporal;

describe('Instant', () => {
describe('instant.toString() works', () => {
it('`1976-11-18T14:23:30.123456789Z`.toString()', () => {
const iso = '1976-11-18T14:23:30.123456789Z';
const instant = Instant.from(iso);
assert(instant);
equal(`${instant}`, iso);
});
it('`1963-02-13T09:36:29.123456789Z`.toString()', () => {
const iso = '1963-02-13T09:36:29.123456789Z';
const instant = Instant.from(iso);
assert(instant);
equal(`${instant}`, iso);
});
it('optional time zone parameter UTC', () => {
const inst = Instant.from('1976-11-18T14:23:30.123456789Z');
const timeZone = Temporal.TimeZone.from('UTC');
Expand Down Expand Up @@ -100,122 +88,6 @@ describe('Instant', () => {
[{}, () => {}, undefined].forEach((options) => equal(i1.toString(options), '1976-11-18T15:23:00Z'));
});
});
describe('Instant.epochSeconds works', () => {
it('post-epoch', () => {
const epochMs = Date.UTC(1976, 10, 18, 15, 23, 30, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochSeconds, Math.trunc(epochMs / 1e3));
equal(typeof inst.epochSeconds, 'number');
});
it('pre-epoch', () => {
const epochMs = Date.UTC(1963, 1, 13, 9, 36, 29, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochSeconds, Math.trunc(epochMs / 1e3));
equal(typeof inst.epochSeconds, 'number');
});
});
describe('Instant.fromEpochSeconds() works', () => {
it('1976-11-18T15:23:30', () => {
const epochSeconds = Math.floor(Date.UTC(1976, 10, 18, 15, 23, 30, 123) / 1e3);
const instant = Instant.fromEpochSeconds(epochSeconds);
equal(instant.epochSeconds, epochSeconds);
});
it('1963-02-13T09:36:29', () => {
const epochSeconds = Math.floor(Date.UTC(1963, 1, 13, 9, 36, 29, 123) / 1e3);
const instant = Instant.fromEpochSeconds(epochSeconds);
equal(instant.epochSeconds, epochSeconds);
});
});
describe('Instant.epochMilliseconds() works', () => {
it('post-epoch', () => {
const epochMs = Date.UTC(1976, 10, 18, 15, 23, 30, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochMilliseconds, epochMs);
equal(typeof inst.epochMilliseconds, 'number');
});
it('pre-epoch', () => {
const epochMs = Date.UTC(1963, 1, 13, 9, 36, 29, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochMilliseconds, epochMs);
equal(typeof inst.epochMilliseconds, 'number');
});
});
describe('Instant.fromEpochMilliseconds() works', () => {
it('1976-11-18T15:23:30.123', () => {
const epochMilliseconds = Date.UTC(1976, 10, 18, 15, 23, 30, 123);
const instant = Instant.fromEpochMilliseconds(epochMilliseconds);
equal(instant.epochMilliseconds, epochMilliseconds);
});
it('1963-02-13T09:36:29.123', () => {
const epochMilliseconds = Date.UTC(1963, 1, 13, 9, 36, 29, 123);
const instant = Instant.fromEpochMilliseconds(epochMilliseconds);
equal(instant.epochMilliseconds, epochMilliseconds);
});
});
describe('Instant.epochMicroseconds works', () => {
it('post-epoch', () => {
const epochMs = Date.UTC(1976, 10, 18, 15, 23, 30, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochMicroseconds, BigInt(epochMs) * BigInt(1e3));
equal(typeof inst.epochMicroseconds, 'bigint');
});
it('pre-epoch', () => {
const epochMs = Date.UTC(1963, 1, 13, 9, 36, 29, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochMicroseconds, BigInt(epochMs) * BigInt(1e3));
equal(typeof inst.epochMicroseconds, 'bigint');
});
});
describe('Instant.fromEpochMicroseconds() works', () => {
it('1976-11-18T15:23:30.123456', () => {
const epochMicroseconds = BigInt(Date.UTC(1976, 10, 18, 15, 23, 30, 123)) * BigInt(1e3) + BigInt(456);
const instant = Instant.fromEpochMicroseconds(epochMicroseconds);
equal(instant.epochMicroseconds, epochMicroseconds);
});
it('1963-02-13T09:36:29.123456', () => {
const epochMicroseconds = BigInt(Date.UTC(1963, 1, 13, 9, 36, 29, 123)) * BigInt(1e3) + BigInt(456);
const instant = Instant.fromEpochMicroseconds(epochMicroseconds);
equal(instant.epochMicroseconds, epochMicroseconds);
});
});
describe('Instant.epochNanoseconds works', () => {
it('post-epoch', () => {
const epochMs = Date.UTC(1976, 10, 18, 15, 23, 30, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochNanoseconds, epochNs);
equal(typeof inst.epochNanoseconds, 'bigint');
});
it('pre-epoch', () => {
const epochMs = Date.UTC(1963, 1, 13, 9, 36, 29, 123);
const epochNs = BigInt(epochMs) * BigInt(1e6);
const inst = new Instant(epochNs);
equal(inst.epochNanoseconds, epochNs);
equal(typeof inst.epochNanoseconds, 'bigint');
});
});
describe('Instant.fromEpochNanoseconds() works', () => {
it('1976-11-18T15:23:30.123456789', () => {
const epochNanoseconds = BigInt(Date.UTC(1976, 10, 18, 15, 23, 30, 123)) * BigInt(1e6) + BigInt(456789);
const instant = Instant.fromEpochNanoseconds(epochNanoseconds);
equal(instant.epochNanoseconds, epochNanoseconds);
});
it('1963-02-13T09:36:29.123456789', () => {
const epochNanoseconds = BigInt(Date.UTC(1963, 1, 13, 9, 36, 29, 123)) * BigInt(1e6) + BigInt(456789);
const instant = Instant.fromEpochNanoseconds(epochNanoseconds);
equal(instant.epochNanoseconds, epochNanoseconds);
});
it('-1n', () => {
const instant = Instant.fromEpochNanoseconds(-1n);
equal(`${instant}`, '1969-12-31T23:59:59.999999999Z');
});
});
describe('Instant.from() works', () => {
it('1976-11-18T15:23Z', () => {
equal(Instant.from('1976-11-18T15:23Z').epochMilliseconds, Date.UTC(1976, 10, 18, 15, 23));
Expand Down

0 comments on commit d3de474

Please sign in to comment.