Skip to content

Commit c1fae10

Browse files
Ms2ger12wrigja
authored andcommitted
Port some Duration tests to test262.
UPSTREAM_COMMIT=cb3ea1461076fe4d84937e0a0da980c3170c5306
1 parent 3397b91 commit c1fae10

File tree

2 files changed

+1
-135
lines changed

2 files changed

+1
-135
lines changed

test/duration.mjs

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -107,37 +107,10 @@ describe('Duration', () => {
107107
});
108108
});
109109
describe('Duration.add()', () => {
110-
const duration = Duration.from({ days: 1, minutes: 5 });
111110
const max = new Duration(0, 0, 0, ...Array(7).fill(Number.MAX_VALUE));
112111
it('always throws when addition overflows', () => {
113112
throws(() => max.add(max), RangeError);
114113
});
115-
it('incorrectly-spelled properties are ignored', () => {
116-
equal(`${duration.add({ month: 1, days: 1 })}`, 'P2DT5M');
117-
});
118-
it('casts argument', () => {
119-
equal(`${duration.add(Temporal.Duration.from('P2DT5M'))}`, 'P3DT10M');
120-
equal(`${duration.add('P2DT5M')}`, 'P3DT10M');
121-
});
122-
it('relativeTo affects year length', () => {
123-
const oneYear = new Duration(1);
124-
const days365 = new Duration(0, 0, 0, 365);
125-
equal(`${oneYear.add(days365, { relativeTo: Temporal.PlainDate.from('2016-01-01') })}`, 'P2Y');
126-
equal(`${oneYear.add(days365, { relativeTo: Temporal.PlainDate.from('2015-01-01') })}`, 'P1Y11M30D');
127-
});
128-
it('relativeTo affects month length', () => {
129-
const oneMonth = new Duration(0, 1);
130-
const days30 = new Duration(0, 0, 0, 30);
131-
equal(`${oneMonth.add(days30, { relativeTo: Temporal.PlainDate.from('2018-01-01') })}`, 'P2M2D');
132-
equal(`${oneMonth.add(days30, { relativeTo: Temporal.PlainDate.from('2018-02-01') })}`, 'P1M30D');
133-
equal(`${oneMonth.add(days30, { relativeTo: Temporal.PlainDate.from('2018-03-01') })}`, 'P2M');
134-
});
135-
it('first this is resolved against relativeTo, then the argument against relativeTo + this', () => {
136-
const d1 = new Duration(0, 1, 0, 1);
137-
const d2 = new Duration(0, 1, 0, 1);
138-
const relativeTo = new Temporal.PlainDate(2000, 1, 1);
139-
equal(`${d1.add(d2, { relativeTo })}`, 'P2M2D');
140-
});
141114
const oneDay = new Duration(0, 0, 0, 1);
142115
const hours24 = new Duration(0, 0, 0, 0, 24);
143116
it('relativeTo does not affect days if PlainDate', () => {
@@ -257,33 +230,6 @@ describe('Duration', () => {
257230
});
258231
});
259232
describe('Duration.subtract()', () => {
260-
const duration = Duration.from({ days: 3, hours: 1, minutes: 10 });
261-
it('incorrectly-spelled properties are ignored', () => {
262-
equal(`${duration.subtract({ month: 1, days: 1 })}`, 'P2DT1H10M');
263-
});
264-
it('casts argument', () => {
265-
equal(`${duration.subtract(Temporal.Duration.from('P1DT5M'))}`, 'P2DT1H5M');
266-
equal(`${duration.subtract('P1DT5M')}`, 'P2DT1H5M');
267-
});
268-
it('relativeTo affects year length', () => {
269-
const oneYear = new Duration(1);
270-
const days365 = new Duration(0, 0, 0, 365);
271-
equal(`${oneYear.subtract(days365, { relativeTo: Temporal.PlainDate.from('2017-01-01') })}`, 'PT0S');
272-
equal(`${oneYear.subtract(days365, { relativeTo: Temporal.PlainDate.from('2016-01-01') })}`, 'P1D');
273-
});
274-
it('relativeTo affects month length', () => {
275-
const oneMonth = new Duration(0, 1);
276-
const days30 = new Duration(0, 0, 0, 30);
277-
equal(`${oneMonth.subtract(days30, { relativeTo: Temporal.PlainDate.from('2018-02-01') })}`, '-P2D');
278-
equal(`${oneMonth.subtract(days30, { relativeTo: Temporal.PlainDate.from('2018-03-01') })}`, 'P1D');
279-
equal(`${oneMonth.subtract(days30, { relativeTo: Temporal.PlainDate.from('2018-04-01') })}`, 'PT0S');
280-
});
281-
it('first this is resolved against relativeTo, then the argument against relativeTo + this', () => {
282-
const d1 = new Duration(0, 2, 1, 4);
283-
const d2 = new Duration(0, 1, 1, 1);
284-
const relativeTo = new Temporal.PlainDate(2000, 1, 1);
285-
equal(`${d1.subtract(d2, { relativeTo })}`, 'P1M3D');
286-
});
287233
const oneDay = new Duration(0, 0, 0, 1);
288234
const hours24 = new Duration(0, 0, 0, 0, 24);
289235
it('relativeTo does not affect days if PlainDate', () => {
@@ -380,74 +326,9 @@ describe('Duration', () => {
380326
const d = new Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
381327
const d2 = new Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5);
382328
const relativeTo = Temporal.PlainDate.from('2020-01-01');
383-
it('throws without parameter', () => {
384-
throws(() => d.round(), TypeError);
385-
});
386-
it('throws with empty object', () => {
387-
throws(() => d.round({}), RangeError);
388-
});
389329
it("succeeds with largestUnit: 'auto'", () => {
390330
equal(`${Duration.from({ hours: 25 }).round({ largestUnit: 'auto' })}`, 'PT25H');
391331
});
392-
it('throws if smallestUnit is larger than largestUnit', () => {
393-
const units = [
394-
'years',
395-
'months',
396-
'weeks',
397-
'days',
398-
'hours',
399-
'minutes',
400-
'seconds',
401-
'milliseconds',
402-
'microseconds',
403-
'nanoseconds'
404-
];
405-
for (let largestIdx = 1; largestIdx < units.length; largestIdx++) {
406-
for (let smallestIdx = 0; smallestIdx < largestIdx; smallestIdx++) {
407-
const largestUnit = units[largestIdx];
408-
const smallestUnit = units[smallestIdx];
409-
throws(() => d.round({ largestUnit, smallestUnit, relativeTo }), RangeError);
410-
}
411-
}
412-
});
413-
it('accepts string parameter as a shortcut for {smallestUnit}', () => {
414-
const d = Temporal.Duration.from({
415-
days: 1,
416-
hours: 2,
417-
minutes: 3,
418-
seconds: 4,
419-
milliseconds: 5,
420-
microseconds: 6,
421-
nanoseconds: 7
422-
});
423-
equal(d.round('day').toString(), 'P1D');
424-
equal(d.round('hour').toString(), 'P1DT2H');
425-
equal(d.round('minute').toString(), 'P1DT2H3M');
426-
equal(d.round('second').toString(), 'P1DT2H3M4S');
427-
equal(d.round('millisecond').toString(), 'P1DT2H3M4.005S');
428-
equal(d.round('microsecond').toString(), 'P1DT2H3M4.005006S');
429-
equal(d.round('nanosecond').toString(), 'P1DT2H3M4.005006007S');
430-
});
431-
it('assumes a different default for largestUnit if smallestUnit is larger than the default', () => {
432-
const almostYear = Duration.from({ days: 364 });
433-
equal(`${almostYear.round({ smallestUnit: 'years', relativeTo })}`, 'P1Y');
434-
const almostMonth = Duration.from({ days: 27 });
435-
equal(`${almostMonth.round({ smallestUnit: 'months', relativeTo })}`, 'P1M');
436-
const almostWeek = Duration.from({ days: 6 });
437-
equal(`${almostWeek.round({ smallestUnit: 'weeks', relativeTo })}`, 'P1W');
438-
const almostDay = Duration.from({ seconds: 86399 });
439-
equal(`${almostDay.round({ smallestUnit: 'days' })}`, 'P1D');
440-
const almostHour = Duration.from({ seconds: 3599 });
441-
equal(`${almostHour.round({ smallestUnit: 'hours' })}`, 'PT1H');
442-
const almostMinute = Duration.from({ seconds: 59 });
443-
equal(`${almostMinute.round({ smallestUnit: 'minutes' })}`, 'PT1M');
444-
const almostSecond = Duration.from({ nanoseconds: 999999999 });
445-
equal(`${almostSecond.round({ smallestUnit: 'seconds' })}`, 'PT1S');
446-
const almostMillisecond = Duration.from({ nanoseconds: 999999 });
447-
equal(`${almostMillisecond.round({ smallestUnit: 'milliseconds' })}`, 'PT0.001S');
448-
const almostMicrosecond = Duration.from({ nanoseconds: 999 });
449-
equal(`${almostMicrosecond.round({ smallestUnit: 'microseconds' })}`, 'PT0.000001S');
450-
});
451332
const hours25 = new Duration(0, 0, 0, 0, 25);
452333
it('days are 24 hours if relativeTo not given', () => {
453334
equal(`${hours25.round({ largestUnit: 'days' })}`, 'P1DT1H');
@@ -583,9 +464,6 @@ describe('Duration', () => {
583464
'P31D'
584465
);
585466
});
586-
it('throws on invalid roundingMode', () => {
587-
throws(() => d2.round({ smallestUnit: 'nanoseconds', roundingMode: 'cile' }), RangeError);
588-
});
589467
it('throws if neither one of largestUnit or smallestUnit is given', () => {
590468
const hoursOnly = new Duration(0, 0, 0, 0, 1);
591469
[{}, () => {}, { roundingMode: 'ceil' }].forEach((roundTo) => {
@@ -909,18 +787,6 @@ describe('Duration', () => {
909787
const d = new Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
910788
const d2 = new Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5);
911789
const relativeTo = Temporal.PlainDate.from('2020-01-01');
912-
it('parameter may only be an object or string', () => {
913-
[null, 1, true, Symbol('foo'), 1n].forEach((badOptions) => throws(() => d.total(badOptions), TypeError));
914-
});
915-
it('accepts string parameter as shortcut for {unit}', () => {
916-
equal(d2.total({ unit: 'days' }).toString(), d2.total('days').toString());
917-
equal(d2.total({ unit: 'hours' }).toString(), d2.total('hours').toString());
918-
equal(d2.total({ unit: 'minutes' }).toString(), d2.total('minutes').toString());
919-
equal(d2.total({ unit: 'seconds' }).toString(), d2.total('seconds').toString());
920-
equal(d2.total({ unit: 'milliseconds' }).toString(), d2.total('milliseconds').toString());
921-
equal(d2.total({ unit: 'microseconds' }).toString(), d2.total('microseconds').toString());
922-
equal(d2.total({ unit: 'nanoseconds' }).toString(), d2.total('nanoseconds').toString());
923-
});
924790
it('throws on disallowed or invalid unit (object param)', () => {
925791
['era', 'nonsense'].forEach((unit) => {
926792
throws(() => d.total({ unit }), RangeError);

0 commit comments

Comments
 (0)