calendar-possibly-required.js (2405B)
1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally 2 // Copyright (C) 2021 Igalia, S.L. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 5 /*--- 6 esid: sec-temporal.duration.compare 7 description: relativeTo argument needed if days = 0 but years/months/weeks non-zero 8 features: [Temporal] 9 ---*/ 10 const duration1a = new Temporal.Duration(1); 11 const duration1b = new Temporal.Duration(1, 0, 0, 0, 0, 0, 0, 0, 0, 1); 12 const duration2a = new Temporal.Duration(0, 12); 13 const duration2b = new Temporal.Duration(0, 12, 0, 0, 0, 0, 0, 0, 0, 1); 14 const duration3a = new Temporal.Duration(0, 0, 5); 15 const duration3b = new Temporal.Duration(0, 0, 5, 0, 0, 0, 0, 0, 0, 1); 16 const duration4a = new Temporal.Duration(0, 0, 0, 42); 17 const duration4b = new Temporal.Duration(0, 0, 0, 42, 0, 0, 0, 0, 0, 1); 18 const relativeTo = new Temporal.PlainDate(2021, 12, 15); 19 assert.throws( 20 RangeError, 21 () => { Temporal.Duration.compare(duration1a, duration1b); }, 22 "cannot compare Duration values without relativeTo if year is non-zero" 23 ); 24 assert.sameValue(-1, 25 Temporal.Duration.compare(duration1a, duration1b, { relativeTo }), 26 "compare succeeds for year-only Duration provided relativeTo is supplied"); 27 assert.throws( 28 RangeError, 29 () => { Temporal.Duration.compare(duration2a, duration2b); }, 30 "cannot compare Duration values without relativeTo if month is non-zero" 31 ); 32 assert.sameValue(-1, 33 Temporal.Duration.compare(duration2a, duration2b, { relativeTo }), 34 "compare succeeds for year-and-month Duration provided relativeTo is supplied"); 35 assert.throws( 36 RangeError, 37 () => { Temporal.Duration.compare(duration3a, duration3b); }, 38 "cannot compare Duration values without relativeTo if week is non-zero" 39 ); 40 assert.sameValue(-1, 41 Temporal.Duration.compare(duration3a, duration3b, { relativeTo }), 42 "compare succeeds for year-and-month-and-week Duration provided relativeTo is supplied" 43 ); 44 45 assert.sameValue(-1, 46 Temporal.Duration.compare(duration4a, duration4b), 47 "compare succeeds for zero year-month-week non-zero day Duration even without relativeTo"); 48 49 // Double-check that the comparison also works with a relative-to argument 50 assert.sameValue(-1, 51 Temporal.Duration.compare(duration4a, duration4b, { relativeTo }), 52 "compare succeeds for zero year-month-week non-zero day Duration with relativeTo" 53 ); 54 55 reportCompare(0, 0);