|
| 1 | +import { assertDurationsEqual, getProgressBar, time, temporalImpl as T } from './support.mjs'; |
| 2 | + |
| 3 | +const day = [ |
| 4 | + ['', {}], |
| 5 | + ['1Y', { y: 1 }], |
| 6 | + ['2M', { mon: 2 }], |
| 7 | + ['4W', { w: 4 }], |
| 8 | + ['3D', { d: 3 }], |
| 9 | + ['1Y2M', { y: 1, mon: 2 }], |
| 10 | + ['1Y4W', { y: 1, w: 4 }], |
| 11 | + ['1Y3D', { y: 1, d: 3 }], |
| 12 | + ['2M4W', { mon: 2, w: 4 }], |
| 13 | + ['2M3D', { mon: 2, d: 3 }], |
| 14 | + ['4W3D', { w: 4, d: 3 }], |
| 15 | + ['1Y2M4W', { y: 1, mon: 2, w: 4 }], |
| 16 | + ['1Y2M3D', { y: 1, mon: 2, d: 3 }], |
| 17 | + ['1Y4W3D', { y: 1, w: 4, d: 3 }], |
| 18 | + ['2M4W3D', { mon: 2, w: 4, d: 3 }], |
| 19 | + ['1Y2M4W3D', { y: 1, mon: 2, w: 4, d: 3 }] |
| 20 | +]; |
| 21 | +const times = [ |
| 22 | + ['', {}], |
| 23 | + ['4H', { h: 4 }], |
| 24 | + ['5M', { min: 5 }], |
| 25 | + ['4H5M', { h: 4, min: 5 }] |
| 26 | +]; |
| 27 | +const sec = [ |
| 28 | + ['', {}], |
| 29 | + ['6S', { s: 6 }], |
| 30 | + ['7.1S', { s: 7, ms: 100 }], |
| 31 | + ['7.12S', { s: 7, ms: 120 }], |
| 32 | + ['7.123S', { s: 7, ms: 123 }], |
| 33 | + ['8.1234S', { s: 8, ms: 123, µs: 400 }], |
| 34 | + ['8.12345S', { s: 8, ms: 123, µs: 450 }], |
| 35 | + ['8.123456S', { s: 8, ms: 123, µs: 456 }], |
| 36 | + ['9.1234567S', { s: 9, ms: 123, µs: 456, ns: 700 }], |
| 37 | + ['9.12345678S', { s: 9, ms: 123, µs: 456, ns: 780 }], |
| 38 | + ['9.123456789S', { s: 9, ms: 123, µs: 456, ns: 789 }], |
| 39 | + ['0.123S', { ms: 123 }], |
| 40 | + ['0.000456S', { µs: 456 }], |
| 41 | + ['0.000000789S', { ns: 789 }], |
| 42 | + ['0.123456S', { ms: 123, µs: 456 }], |
| 43 | + ['0.123000789S', { ms: 123, ns: 789 }], |
| 44 | + ['0.000456789S', { µs: 456, ns: 789 }], |
| 45 | + ['0.123456789S', { ms: 123, µs: 456, ns: 789 }] |
| 46 | +]; |
| 47 | +for (const [str, expect] of sec) { |
| 48 | + if (str.includes('.')) sec.push([str.replace('.', ','), expect]); |
| 49 | +} |
| 50 | + |
| 51 | +var tim = sec |
| 52 | + .reduce((arr, [s, add]) => arr.concat(times.map(([p, expect]) => [`${p}${s}`, { ...expect, ...add }])), []) |
| 53 | + .slice(1); |
| 54 | + |
| 55 | +const testCases = []; |
| 56 | + |
| 57 | +for (const [str, expectation] of day.slice(1)) { |
| 58 | + const { y = 0, mon = 0, w = 0, d = 0 } = expectation; |
| 59 | + const expected = new T.Duration(y, mon, w, d); |
| 60 | + |
| 61 | + for (const s of [str, str.toLowerCase()]) { |
| 62 | + for (const p of ['P', 'p']) { |
| 63 | + testCases.push([`${p}${s}`, expected]); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +for (const [str, expectation] of tim) { |
| 68 | + const { h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0 } = expectation; |
| 69 | + const expected = new T.Duration(0, 0, 0, 0, h, min, s, ms, µs, ns); |
| 70 | + |
| 71 | + for (const s of [str, str.toLowerCase()]) { |
| 72 | + for (const p of ['P', 'p']) { |
| 73 | + for (const t of ['T', 't']) { |
| 74 | + testCases.push([`${p}${t}${s}`, expected]); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +for (const [dstr, { y = 0, mon = 0, w = 0, d = 0 }] of day) { |
| 80 | + for (const [tstr, { h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0 }] of tim) { |
| 81 | + const expected = new T.Duration(y, mon, w, d, h, min, s, ms, µs, ns); |
| 82 | + |
| 83 | + for (const ds of [dstr, dstr.toLowerCase()]) { |
| 84 | + for (const ts of [tstr, tstr.toLowerCase()]) { |
| 85 | + for (const p of ['P', 'p']) { |
| 86 | + for (const t of ['T', 't']) { |
| 87 | + testCases.push([`${p}${ds}${t}${ts}`, expected]); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +const total = testCases.length; |
| 96 | + |
| 97 | +await time(async (start) => { |
| 98 | + const progress = getProgressBar(start, total); |
| 99 | + |
| 100 | + for (const [isoString, expected] of testCases) { |
| 101 | + progress.tick(1, { test: isoString }); |
| 102 | + const actual = T.Duration.from(isoString); |
| 103 | + assertDurationsEqual(actual, expected, isoString); |
| 104 | + } |
| 105 | + |
| 106 | + return total; |
| 107 | +}); |
0 commit comments