From f45c69e8d860f6a8086e9e0c13e95c5319a47b8f Mon Sep 17 00:00:00 2001 From: Adam Shaw Date: Wed, 28 Feb 2024 19:13:10 -0500 Subject: [PATCH] more tests --- .../src/funcApi/duration.test.ts | 2 +- .../src/funcApi/instant.test.ts | 2 +- .../src/funcApi/plainDate.test.ts | 6 +- .../src/funcApi/plainDateTime.test.ts | 4 +- .../src/funcApi/plainYearMonth.test.ts | 283 ++++++++++++++++++ .../src/funcApi/plainYearMonth.ts | 37 ++- .../src/funcApi/zonedDateTime.test.ts | 4 +- 7 files changed, 310 insertions(+), 28 deletions(-) create mode 100644 packages/temporal-polyfill/src/funcApi/plainYearMonth.test.ts diff --git a/packages/temporal-polyfill/src/funcApi/duration.test.ts b/packages/temporal-polyfill/src/funcApi/duration.test.ts index b6446cfc..540f081a 100644 --- a/packages/temporal-polyfill/src/funcApi/duration.test.ts +++ b/packages/temporal-polyfill/src/funcApi/duration.test.ts @@ -6,7 +6,7 @@ import { expectDurationEquals } from './testUtils' import * as ZonedDateTimeFns from './zonedDateTime' describe('create', () => { - it('called with no args', () => { + it('called without args', () => { const d = DurationFns.create() expectDurationEquals(d, {}) }) diff --git a/packages/temporal-polyfill/src/funcApi/instant.test.ts b/packages/temporal-polyfill/src/funcApi/instant.test.ts index eb1880dc..24795731 100644 --- a/packages/temporal-polyfill/src/funcApi/instant.test.ts +++ b/packages/temporal-polyfill/src/funcApi/instant.test.ts @@ -122,7 +122,7 @@ describe('since', () => { }) describe('round', () => { - it('works with no simple string unit', () => { + it('works without simple string unit', () => { const inst0 = InstantFns.fromString('2024-01-01T00:30:00+01:00') const inst1 = InstantFns.round(inst0, 'hour') expectInstantEquals(inst1, 1704067200000000000n) diff --git a/packages/temporal-polyfill/src/funcApi/plainDate.test.ts b/packages/temporal-polyfill/src/funcApi/plainDate.test.ts index 47e6936e..5f17ecc0 100644 --- a/packages/temporal-polyfill/src/funcApi/plainDate.test.ts +++ b/packages/temporal-polyfill/src/funcApi/plainDate.test.ts @@ -224,7 +224,7 @@ describe('subtract', () => { }) describe('until', () => { - it('works with no options', () => { + it('works without options', () => { const pd0 = PlainDateFns.create(2023, 1, 25) const pd1 = PlainDateFns.create(2024, 2, 27) const d = PlainDateFns.until(pd0, pd1) @@ -246,7 +246,7 @@ describe('until', () => { }) describe('since', () => { - it('works with no options', () => { + it('works without options', () => { const pd0 = PlainDateFns.create(2023, 1, 25) const pd1 = PlainDateFns.create(2024, 2, 27) const d = PlainDateFns.since(pd0, pd1) @@ -329,7 +329,7 @@ describe('toZonedDateTime', () => { }) describe('toPlainDateTime', () => { - it('works with no arg', () => { + it('works without arg', () => { const pd = PlainDateFns.create(2024, 2, 27, 'hebrew') const pdt = PlainDateFns.toPlainDateTime(pd) expectPlainDateTimeEquals(pdt, { diff --git a/packages/temporal-polyfill/src/funcApi/plainDateTime.test.ts b/packages/temporal-polyfill/src/funcApi/plainDateTime.test.ts index 6e0fb926..fdb40c70 100644 --- a/packages/temporal-polyfill/src/funcApi/plainDateTime.test.ts +++ b/packages/temporal-polyfill/src/funcApi/plainDateTime.test.ts @@ -290,7 +290,7 @@ describe('subtract', () => { }) describe('until', () => { - it('works with no options', () => { + it('works without options', () => { const pdt0 = PlainDateTimeFns.create(2023, 1, 25, 10) const pdt1 = PlainDateTimeFns.create(2024, 2, 27, 12) const d = PlainDateTimeFns.until(pdt0, pdt1) @@ -314,7 +314,7 @@ describe('until', () => { }) describe('since', () => { - it('works with no options', () => { + it('works without options', () => { const pdt0 = PlainDateTimeFns.create(2023, 1, 25, 10) const pdt1 = PlainDateTimeFns.create(2024, 2, 27, 12) const d = PlainDateTimeFns.since(pdt0, pdt1) diff --git a/packages/temporal-polyfill/src/funcApi/plainYearMonth.test.ts b/packages/temporal-polyfill/src/funcApi/plainYearMonth.test.ts new file mode 100644 index 00000000..caf7f2e1 --- /dev/null +++ b/packages/temporal-polyfill/src/funcApi/plainYearMonth.test.ts @@ -0,0 +1,283 @@ +import { describe, expect, it } from 'vitest' +import * as DurationFns from './duration' +import * as PlainYearMonthFns from './plainYearMonth' +import { + expectDurationEquals, + expectPlainDateEquals, + expectPlainYearMonthEquals, + testHotCache, +} from './testUtils' + +describe('create', () => { + it('works with a referenceDay', () => { + const pym = PlainYearMonthFns.create(2024, 6, 'gregory', 5) + expectPlainYearMonthEquals(pym, { + calendar: 'gregory', + isoYear: 2024, + isoMonth: 6, + isoDay: 5, + }) + }) + + it('works without a referenceDay', () => { + const pym = PlainYearMonthFns.create(2024, 6, 'gregory') + expectPlainYearMonthEquals(pym, { + calendar: 'gregory', + isoYear: 2024, + isoMonth: 6, + }) + }) +}) + +describe('fromString', () => { + it('works', () => { + const pym = PlainYearMonthFns.fromString('2024-06-01[u-ca=gregory]') + expectPlainYearMonthEquals(pym, { + calendar: 'gregory', + isoYear: 2024, + isoMonth: 6, + }) + }) +}) + +describe('fromFields', () => { + it('works without options', () => { + const pym = PlainYearMonthFns.fromFields({ + calendar: 'gregory', + year: 2024, + month: 6, + }) + expectPlainYearMonthEquals(pym, { + calendar: 'gregory', + isoYear: 2024, + isoMonth: 6, + }) + }) +}) + +describe('getFields', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2024, 6, 'gregory') + const fields = PlainYearMonthFns.getFields(pym) + expect(fields).toEqual({ + era: 'ce', + eraYear: 2024, + year: 2024, + monthCode: 'M06', + month: 6, + }) + }) +}) + +describe('withFields', () => { + it('works', () => { + const pym0 = PlainYearMonthFns.create(2024, 6, 'gregory') + const pym1 = PlainYearMonthFns.withFields(pym0, { + year: 2009, + }) + expectPlainYearMonthEquals(pym1, { + calendar: 'gregory', + isoYear: 2009, + isoMonth: 6, + }) + }) +}) + +describe('daysInMonth', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2024, 2) + expect(PlainYearMonthFns.daysInMonth(pym)).toBe(29) + }) +}) + +describe('daysInYear', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2024, 2) + expect(PlainYearMonthFns.daysInYear(pym)).toBe(366) + }) +}) + +describe('monthsInYear', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2024, 2) + expect(PlainYearMonthFns.monthsInYear(pym)).toBe(12) + }) +}) + +describe('inLeapYear', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2024, 2) + expect(PlainYearMonthFns.inLeapYear(pym)).toBe(true) + }) +}) + +describe('add', () => { + it('works', () => { + const pym0 = PlainYearMonthFns.create(2024, 2) + const pym1 = PlainYearMonthFns.add(pym0, DurationFns.create(1, 1)) + expectPlainYearMonthEquals(pym1, { + isoYear: 2025, + isoMonth: 3, + }) + }) +}) + +describe('subtract', () => { + it('works', () => { + const pym0 = PlainYearMonthFns.create(2024, 2) + const pym1 = PlainYearMonthFns.subtract(pym0, DurationFns.create(1, 1)) + expectPlainYearMonthEquals(pym1, { + isoYear: 2023, + isoMonth: 1, + }) + }) +}) + +describe('until', () => { + it('works without options', () => { + const pym0 = PlainYearMonthFns.create(2024, 4) + const pym1 = PlainYearMonthFns.create(2028, 2) + const d = PlainYearMonthFns.until(pym0, pym1) + expectDurationEquals(d, { + years: 3, + months: 10, + }) + }) +}) + +describe('since', () => { + it('works without options', () => { + const pym0 = PlainYearMonthFns.create(2024, 4) + const pym1 = PlainYearMonthFns.create(2028, 2) + const d = PlainYearMonthFns.since(pym0, pym1) + expectDurationEquals(d, { + years: -3, + months: -10, + }) + }) +}) + +describe('equals', () => { + it('works', () => { + const pym0 = PlainYearMonthFns.create(2024, 4) + const pym1 = PlainYearMonthFns.create(2028, 2) + expect(PlainYearMonthFns.equals(pym0, pym1)).toBe(false) + expect(PlainYearMonthFns.equals(pym0, pym0)).toBe(true) + }) +}) + +describe('compare', () => { + it('works', () => { + const pym0 = PlainYearMonthFns.create(2024, 4) + const pym1 = PlainYearMonthFns.create(2028, 2) + expect(PlainYearMonthFns.compare(pym0, pym1)).toBe(-1) + expect(PlainYearMonthFns.compare(pym1, pym0)).toBe(1) + expect(PlainYearMonthFns.compare(pym0, pym0)).toBe(0) + }) +}) + +describe('toPlainDate', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2024, 2) + const pd = PlainYearMonthFns.toPlainDate(pym, { day: 10 }) + expectPlainDateEquals(pd, { + isoYear: 2024, + isoMonth: 2, + isoDay: 10, + }) + }) +}) + +describe('toString', () => { + it('works without options', () => { + const pym = PlainYearMonthFns.create(2024, 2) + const s = PlainYearMonthFns.toString(pym) + expect(s).toBe('2024-02') + }) + + it('works with options', () => { + const pym = PlainYearMonthFns.create(2024, 2) + const s = PlainYearMonthFns.toString(pym, { calendarName: 'always' }) + expect(s).toBe('2024-02-01[u-ca=iso8601]') + }) +}) + +describe('toLocaleString', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2023, 12) + const locale = 'en' + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: 'long', + timeZone: 'America/New_York', + calendar: 'iso8601', // required unfortunately + } + const s = testHotCache(() => + PlainYearMonthFns.toLocaleString(pym, locale, options), + ) + expect(s).toBe('December 2023') + }) +}) + +describe('toLocaleStringParts', () => { + it('works', () => { + const pym = PlainYearMonthFns.create(2023, 12) + const locale = 'en' + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: 'long', + timeZone: 'America/New_York', + calendar: 'iso8601', // required unfortunately + } + const parts = testHotCache(() => + PlainYearMonthFns.toLocaleStringParts(pym, locale, options), + ) + expect(parts).toEqual([ + { type: 'month', value: 'December' }, + { type: 'literal', value: ' ' }, + { type: 'year', value: '2023' }, + ]) + }) +}) + +describe('rangeToLocaleString', () => { + it('works', () => { + const pym0 = PlainYearMonthFns.create(2023, 10) + const pym1 = PlainYearMonthFns.create(2023, 12) + const locale = 'en' + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: 'long', + timeZone: 'America/New_York', + calendar: 'iso8601', // required unfortunately + } + const s = testHotCache(() => + PlainYearMonthFns.rangeToLocaleString(pym0, pym1, locale, options), + ) + expect(s).toBe('October – December 2023') + }) +}) + +describe('rangeToLocaleStringParts', () => { + it('works', () => { + const pym0 = PlainYearMonthFns.create(2023, 10) + const pym1 = PlainYearMonthFns.create(2023, 12) + const locale = 'en' + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: 'long', + timeZone: 'America/New_York', + calendar: 'iso8601', // required unfortunately + } + const parts = testHotCache(() => + PlainYearMonthFns.rangeToLocaleStringParts(pym0, pym1, locale, options), + ) + expect(parts).toEqual([ + { source: 'startRange', type: 'month', value: 'October' }, + { source: 'shared', type: 'literal', value: ' – ' }, + { source: 'endRange', type: 'month', value: 'December' }, + { source: 'shared', type: 'literal', value: ' ' }, + { source: 'shared', type: 'year', value: '2023' }, + ]) + }) +}) diff --git a/packages/temporal-polyfill/src/funcApi/plainYearMonth.ts b/packages/temporal-polyfill/src/funcApi/plainYearMonth.ts index 6e9edfda..6bfe58fb 100644 --- a/packages/temporal-polyfill/src/funcApi/plainYearMonth.ts +++ b/packages/temporal-polyfill/src/funcApi/plainYearMonth.ts @@ -70,19 +70,6 @@ export const getFields = memoize(computeYearMonthFields, WeakMap) as ( slots: PlainYearMonthSlots, ) => YearMonthFields -export const daysInMonth = computeDaysInMonth as ( - slots: PlainYearMonthSlots, -) => number -export const daysInYear = computeDaysInYear as ( - slots: PlainYearMonthSlots, -) => number -export const monthsInYear = computeMonthsInYear as ( - slots: PlainYearMonthSlots, -) => number -export const inLeapYear = computeInLeapYear as ( - slots: PlainYearMonthSlots, -) => boolean - export function withFields( slots: PlainYearMonthSlots, fields: YearMonthBag, @@ -97,6 +84,19 @@ export function withFields( ) } +export const daysInMonth = computeDaysInMonth as ( + slots: PlainYearMonthSlots, +) => number +export const daysInYear = computeDaysInYear as ( + slots: PlainYearMonthSlots, +) => number +export const monthsInYear = computeMonthsInYear as ( + slots: PlainYearMonthSlots, +) => number +export const inLeapYear = computeInLeapYear as ( + slots: PlainYearMonthSlots, +) => boolean + export const add = bindArgs( movePlainYearMonth, createNativeYearMonthMoveOps, @@ -125,21 +125,20 @@ export const compare = compareIsoDateFields as ( slots1: PlainYearMonthSlots, ) => NumberSign -export const toString = formatPlainYearMonthIso - export function toPlainDate( - plainYearMonthSlots: PlainYearMonthSlots, - plainYearMonthFields: YearMonthFields, + slots: PlainYearMonthSlots, bag: { day: number }, ): PlainDateSlots { return plainYearMonthToPlainDate( createNativeDateModOps, - plainYearMonthSlots, - plainYearMonthFields, + slots, + getFields(slots), bag, ) } +export const toString = formatPlainYearMonthIso + export function toLocaleString( slots: PlainYearMonthSlots, locales?: LocalesArg, diff --git a/packages/temporal-polyfill/src/funcApi/zonedDateTime.test.ts b/packages/temporal-polyfill/src/funcApi/zonedDateTime.test.ts index bb75777c..86fc8d3c 100644 --- a/packages/temporal-polyfill/src/funcApi/zonedDateTime.test.ts +++ b/packages/temporal-polyfill/src/funcApi/zonedDateTime.test.ts @@ -431,7 +431,7 @@ describe('subtract', () => { }) describe('until', () => { - it('works with no options', () => { + it('works without options', () => { const zdt0 = ZonedDateTimeFns.fromString( '2024-02-27T12:30:00[America/New_York]', ) @@ -463,7 +463,7 @@ describe('until', () => { }) describe('since', () => { - it('works with no options', () => { + it('works without options', () => { const zdt0 = ZonedDateTimeFns.fromString( '2024-02-27T12:30:00[America/New_York]', )