Skip to content

Commit

Permalink
more centralized test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Feb 18, 2024
1 parent 9becc49 commit 51d6266
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 172 deletions.
24 changes: 1 addition & 23 deletions packages/temporal-polyfill/src/funcApi/duration.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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 })
}
44 changes: 1 addition & 43 deletions packages/temporal-polyfill/src/funcApi/instant.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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 })
}
110 changes: 10 additions & 100 deletions packages/temporal-polyfill/src/funcApi/now.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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<string, string> {
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<string, string>,
zdt1: ZonedDateTimeFns.ZonedDateTimeSlots<string, string>,
): 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<string>,
pdt1: PlainDateTimeFns.PlainDateTimeSlots<string>,
): 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<string>,
pd1: PlainDateFns.PlainDateSlots<string>,
): 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,
}
150 changes: 150 additions & 0 deletions packages/temporal-polyfill/src/funcApi/testUtils.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> {
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<string, string>,
zdt1: ZonedDateTimeFns.ZonedDateTimeSlots<string, string>,
): 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<string>,
pdt1: PlainDateTimeFns.PlainDateTimeSlots<string>,
): 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<string>,
pd1: PlainDateFns.PlainDateSlots<string>,
): 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
}
6 changes: 0 additions & 6 deletions packages/temporal-polyfill/src/internal/testUtils.ts

This file was deleted.

0 comments on commit 51d6266

Please sign in to comment.