From 51d62660e5e3261bb392692d7cad689817502bb3 Mon Sep 17 00:00:00 2001 From: Adam Shaw Date: Sun, 18 Feb 2024 12:29:19 -0500 Subject: [PATCH] more centralized test utils --- .../src/funcApi/duration.test.ts | 24 +-- .../src/funcApi/instant.test.ts | 44 +---- .../temporal-polyfill/src/funcApi/now.test.ts | 110 ++----------- .../src/funcApi/testUtils.ts | 150 ++++++++++++++++++ .../src/internal/testUtils.ts | 6 - 5 files changed, 162 insertions(+), 172 deletions(-) create mode 100644 packages/temporal-polyfill/src/funcApi/testUtils.ts delete mode 100644 packages/temporal-polyfill/src/internal/testUtils.ts diff --git a/packages/temporal-polyfill/src/funcApi/duration.test.ts b/packages/temporal-polyfill/src/funcApi/duration.test.ts index dda07d95..cc87c039 100644 --- a/packages/temporal-polyfill/src/funcApi/duration.test.ts +++ b/packages/temporal-polyfill/src/funcApi/duration.test.ts @@ -1,9 +1,8 @@ import { describe, expect, it } from 'vitest' -import { expectPropsEqualStrict } from '../internal/testUtils' -import { DurationBag, DurationSlots } from './duration' import * as DurationFns from './duration' import * as PlainDateFns from './plainDate' import * as PlainDateTimeFns from './plainDateTime' +import { expectDurationEquals } from './testUtils' import * as ZonedDateTimeFns from './zonedDateTime' describe('create', () => { @@ -300,24 +299,3 @@ describe('toLocaleString', () => { expect(s).toBeTruthy() }) }) - -// Utils -// ----------------------------------------------------------------------------- - -const defaultSlots = { - branding: 'Duration', - days: 0, - hours: 0, - microseconds: 0, - milliseconds: 0, - minutes: 0, - months: 0, - nanoseconds: 0, - seconds: 0, - weeks: 0, - years: 0, -} - -function expectDurationEquals(d: DurationSlots, bag: DurationBag): void { - expectPropsEqualStrict(d, { ...defaultSlots, ...bag }) -} diff --git a/packages/temporal-polyfill/src/funcApi/instant.test.ts b/packages/temporal-polyfill/src/funcApi/instant.test.ts index bac36d4a..96cd3ffc 100644 --- a/packages/temporal-polyfill/src/funcApi/instant.test.ts +++ b/packages/temporal-polyfill/src/funcApi/instant.test.ts @@ -1,8 +1,7 @@ import { describe, expect, it } from 'vitest' -import { bigIntToDayTimeNano } from '../internal/dayTimeNano' -import { expectPropsEqualStrict } from '../internal/testUtils' import * as DurationFns from './duration' import * as InstantFns from './instant' +import { expectDurationEquals, expectInstantEquals } from './testUtils' import * as ZonedDateTimeFns from './zonedDateTime' describe('create', () => { @@ -334,44 +333,3 @@ describe('rangeToLocaleStringParts', () => { expect(t2 - t1).toBeLessThan((t1 - t0) / 2) // at least twice as fast }) }) - -// Utils -// ----------------------------------------------------------------------------- - -const defaultSlots = { - branding: 'Instant', - epochNanoseconds: 0n, -} - -function expectInstantEquals( - inst: InstantFns.InstantSlots, - epochNanoseconds: bigint, -): void { - expectPropsEqualStrict(inst, { - ...defaultSlots, - epochNanoseconds: bigIntToDayTimeNano(epochNanoseconds), - }) -} - -// TODO: DRY!!!... - -const defaultDurationSlots = { - branding: 'Duration', - days: 0, - hours: 0, - microseconds: 0, - milliseconds: 0, - minutes: 0, - months: 0, - nanoseconds: 0, - seconds: 0, - weeks: 0, - years: 0, -} - -function expectDurationEquals( - d: DurationFns.DurationSlots, - bag: DurationFns.DurationBag, -): void { - expectPropsEqualStrict(d, { ...defaultDurationSlots, ...bag }) -} diff --git a/packages/temporal-polyfill/src/funcApi/now.test.ts b/packages/temporal-polyfill/src/funcApi/now.test.ts index ee9a7a54..d332fcc9 100644 --- a/packages/temporal-polyfill/src/funcApi/now.test.ts +++ b/packages/temporal-polyfill/src/funcApi/now.test.ts @@ -1,11 +1,15 @@ import { describe, expect, it } from 'vitest' -import { dayTimeNanoToBigInt } from '../internal/dayTimeNano' -import { isoToEpochNano } from '../internal/timeMath' -import * as InstantFns from './instant' import * as NowFns from './now' -import * as PlainDateFns from './plainDate' -import * as PlainDateTimeFns from './plainDateTime' -import * as PlainTimeFns from './plainTime' +import { + expectInstantsSimilar, + expectPlainDateTimesSimilar, + expectPlainDatesSimilar, + expectPlainTimesSimilar, + expectZonedDateTimesSimilar, + getCurrentInstant, + getCurrentZonedDateTime, + systemTimeZoneId, +} from './testUtils' import * as ZonedDateTimeFns from './zonedDateTime' describe('timeZoneId', () => { @@ -139,97 +143,3 @@ describe('plainTimeISO', () => { expectPlainTimesSimilar(pt0, pt1) }) }) - -// Utils -// ----------------------------------------------------------------------------- - -const systemResolvedOptions = new Intl.DateTimeFormat().resolvedOptions() -const systemTimeZoneId = systemResolvedOptions.timeZone - -function getCurrentInstant() { - return InstantFns.fromEpochMilliseconds(Date.now()) -} - -function getCurrentZonedDateTime( - calendar: string, - timeZone: string, -): ZonedDateTimeFns.ZonedDateTimeSlots { - return InstantFns.toZonedDateTime(getCurrentInstant(), { timeZone, calendar }) -} - -function expectEpochNanosSimilar( - epochNano0: bigint, - epochNano1: bigint, -): boolean { - return Math.abs(Number(epochNano0 - epochNano1)) < 1000 -} - -function expectInstantsSimilar( - inst0: InstantFns.InstantSlots, - inst1: InstantFns.InstantSlots, -): void { - expect(inst0.branding).toBe('Instant') - expect(inst1.branding).toBe('Instant') - expectEpochNanosSimilar( - InstantFns.epochNanoseconds(inst0), - InstantFns.epochNanoseconds(inst1), - ) -} - -function expectZonedDateTimesSimilar( - zdt0: ZonedDateTimeFns.ZonedDateTimeSlots, - zdt1: ZonedDateTimeFns.ZonedDateTimeSlots, -): void { - expect(zdt0.branding).toBe('ZonedDateTime') - expect(zdt1.branding).toBe('ZonedDateTime') - expect(zdt0.calendar).toBe(zdt1.calendar) - expect(zdt0.timeZone).toBe(zdt1.timeZone) - expectEpochNanosSimilar( - ZonedDateTimeFns.epochNanoseconds(zdt0), - ZonedDateTimeFns.epochNanoseconds(zdt1), - ) -} - -function expectPlainDateTimesSimilar( - pdt0: PlainDateTimeFns.PlainDateTimeSlots, - pdt1: PlainDateTimeFns.PlainDateTimeSlots, -): void { - expect(pdt0.branding).toBe('PlainDateTime') - expect(pdt1.branding).toBe('PlainDateTime') - expect(pdt0.calendar).toBe(pdt1.calendar) - expectEpochNanosSimilar( - dayTimeNanoToBigInt(isoToEpochNano(pdt0)!), - dayTimeNanoToBigInt(isoToEpochNano(pdt1)!), - ) -} - -function expectPlainDatesSimilar( - pd0: PlainDateFns.PlainDateSlots, - pd1: PlainDateFns.PlainDateSlots, -): void { - expect(pd0.branding).toBe('PlainDate') - expect(pd1.branding).toBe('PlainDate') - expect(pd0.calendar).toBe(pd1.calendar) - expectEpochNanosSimilar( - dayTimeNanoToBigInt(isoToEpochNano(pd0)!), - dayTimeNanoToBigInt(isoToEpochNano(pd1)!), - ) -} - -function expectPlainTimesSimilar( - pt0: PlainTimeFns.PlainTimeSlots, - pt1: PlainTimeFns.PlainTimeSlots, -): void { - expect(pt0.branding).toBe('PlainTime') - expect(pt1.branding).toBe('PlainTime') - expectEpochNanosSimilar( - dayTimeNanoToBigInt(isoToEpochNano({ ...isoDateDefaults, ...pt0 })!), - dayTimeNanoToBigInt(isoToEpochNano({ ...isoDateDefaults, ...pt1 })!), - ) -} - -const isoDateDefaults = { - isoYear: 0, - isoMonth: 0, - isoDay: 0, -} diff --git a/packages/temporal-polyfill/src/funcApi/testUtils.ts b/packages/temporal-polyfill/src/funcApi/testUtils.ts new file mode 100644 index 00000000..4f0389af --- /dev/null +++ b/packages/temporal-polyfill/src/funcApi/testUtils.ts @@ -0,0 +1,150 @@ +import { expect } from 'vitest' +import { + bigIntToDayTimeNano, + dayTimeNanoToBigInt, +} from '../internal/dayTimeNano' +import { isoToEpochNano } from '../internal/timeMath' +import { DurationBag, DurationSlots } from './duration' +import * as InstantFns from './instant' +import * as PlainDateFns from './plainDate' +import * as PlainDateTimeFns from './plainDateTime' +import * as PlainTimeFns from './plainTime' +import * as ZonedDateTimeFns from './zonedDateTime' + +// Current +// ----------------------------------------------------------------------------- + +const systemResolvedOptions = new Intl.DateTimeFormat().resolvedOptions() +export const systemTimeZoneId = systemResolvedOptions.timeZone + +export function getCurrentInstant() { + return InstantFns.fromEpochMilliseconds(Date.now()) +} + +export function getCurrentZonedDateTime( + calendar: string, + timeZone: string, +): ZonedDateTimeFns.ZonedDateTimeSlots { + return InstantFns.toZonedDateTime(getCurrentInstant(), { timeZone, calendar }) +} + +// Equality +// ----------------------------------------------------------------------------- + +const isoDateDefaults = { + isoYear: 0, + isoMonth: 0, + isoDay: 0, +} + +const instantSlotDefaults = { + branding: 'Instant', + epochNanoseconds: 0n, +} + +const durationSlotDefaults = { + branding: 'Duration', + days: 0, + hours: 0, + microseconds: 0, + milliseconds: 0, + minutes: 0, + months: 0, + nanoseconds: 0, + seconds: 0, + weeks: 0, + years: 0, +} + +export function expectInstantEquals( + inst: InstantFns.InstantSlots, + epochNanoseconds: bigint, +): void { + expectPropsEqualStrict(inst, { + ...instantSlotDefaults, + epochNanoseconds: bigIntToDayTimeNano(epochNanoseconds), + }) +} + +export function expectDurationEquals(d: DurationSlots, bag: DurationBag): void { + expectPropsEqualStrict(d, { ...durationSlotDefaults, ...bag }) +} + +function expectPropsEqualStrict(obj0: {}, obj1: {}): void { + expect(obj0).toStrictEqual(obj1) + expect(Object.keys(obj0)).toStrictEqual(Object.keys(obj1)) +} + +// Similarity +// ----------------------------------------------------------------------------- + +export function expectInstantsSimilar( + inst0: InstantFns.InstantSlots, + inst1: InstantFns.InstantSlots, +): void { + expect(inst0.branding).toBe('Instant') + expect(inst1.branding).toBe('Instant') + expectEpochNanosSimilar( + InstantFns.epochNanoseconds(inst0), + InstantFns.epochNanoseconds(inst1), + ) +} + +export function expectZonedDateTimesSimilar( + zdt0: ZonedDateTimeFns.ZonedDateTimeSlots, + zdt1: ZonedDateTimeFns.ZonedDateTimeSlots, +): void { + expect(zdt0.branding).toBe('ZonedDateTime') + expect(zdt1.branding).toBe('ZonedDateTime') + expect(zdt0.calendar).toBe(zdt1.calendar) + expect(zdt0.timeZone).toBe(zdt1.timeZone) + expectEpochNanosSimilar( + ZonedDateTimeFns.epochNanoseconds(zdt0), + ZonedDateTimeFns.epochNanoseconds(zdt1), + ) +} + +export function expectPlainDateTimesSimilar( + pdt0: PlainDateTimeFns.PlainDateTimeSlots, + pdt1: PlainDateTimeFns.PlainDateTimeSlots, +): void { + expect(pdt0.branding).toBe('PlainDateTime') + expect(pdt1.branding).toBe('PlainDateTime') + expect(pdt0.calendar).toBe(pdt1.calendar) + expectEpochNanosSimilar( + dayTimeNanoToBigInt(isoToEpochNano(pdt0)!), + dayTimeNanoToBigInt(isoToEpochNano(pdt1)!), + ) +} + +export function expectPlainDatesSimilar( + pd0: PlainDateFns.PlainDateSlots, + pd1: PlainDateFns.PlainDateSlots, +): void { + expect(pd0.branding).toBe('PlainDate') + expect(pd1.branding).toBe('PlainDate') + expect(pd0.calendar).toBe(pd1.calendar) + expectEpochNanosSimilar( + dayTimeNanoToBigInt(isoToEpochNano(pd0)!), + dayTimeNanoToBigInt(isoToEpochNano(pd1)!), + ) +} + +export function expectPlainTimesSimilar( + pt0: PlainTimeFns.PlainTimeSlots, + pt1: PlainTimeFns.PlainTimeSlots, +): void { + expect(pt0.branding).toBe('PlainTime') + expect(pt1.branding).toBe('PlainTime') + expectEpochNanosSimilar( + dayTimeNanoToBigInt(isoToEpochNano({ ...isoDateDefaults, ...pt0 })!), + dayTimeNanoToBigInt(isoToEpochNano({ ...isoDateDefaults, ...pt1 })!), + ) +} + +function expectEpochNanosSimilar( + epochNano0: bigint, + epochNano1: bigint, +): boolean { + return Math.abs(Number(epochNano0 - epochNano1)) < 1000 +} diff --git a/packages/temporal-polyfill/src/internal/testUtils.ts b/packages/temporal-polyfill/src/internal/testUtils.ts deleted file mode 100644 index e20291ca..00000000 --- a/packages/temporal-polyfill/src/internal/testUtils.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { expect } from 'vitest' - -export function expectPropsEqualStrict(obj0: {}, obj1: {}): void { - expect(obj0).toStrictEqual(obj1) - expect(Object.keys(obj0)).toStrictEqual(Object.keys(obj1)) -}