Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
node-version: 24.x
- run: npm ci
- run: npm run test-cookbook
test-validstrings:
test-exhaustive:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -57,3 +57,4 @@ jobs:
cd polyfill
npm install
node test/validStrings.mjs
npm run test-thorough
2 changes: 2 additions & 0 deletions polyfill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"types": "index.d.ts",
"scripts": {
"test": "node ./test/all.mjs",
"test-thorough": "test/thorough/all.sh",
"test-cookbook": "npm run build && TEST=all npm run test-cookbook-one",
"test-cookbook-one": "node --require=./script.js --require=../docs/cookbook/assert.js ../docs/cookbook/$TEST.mjs",
"test262": "npm run build262 && node runtest262.mjs",
Expand Down Expand Up @@ -78,6 +79,7 @@
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-replace": "^5.0.2",
"c8": "^7.14.0",
"progress": "^2.0.3",
"rollup": "^3.23.1",
"rollup-plugin-node-polyfills": "^0.2.1"
},
Expand Down
3 changes: 1 addition & 2 deletions polyfill/test/all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import Demitasse from '@pipobscure/demitasse';
import Pretty from '@pipobscure/demitasse-pretty';

// exhaustive date arithmetic tests, not suitable for test262
import './datemath.mjs';
import '../lib/temporal.mjs';

// tests of internals, not suitable for test262
import './ecmascript.mjs';
Expand Down
104 changes: 0 additions & 104 deletions polyfill/test/datemath.mjs

This file was deleted.

29 changes: 29 additions & 0 deletions polyfill/test/thorough/all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

# ./all.sh to test the polyfill with Node.js in your path
#
# Other interpreters:
# Boa: ./all.sh "$HOME/.esvu/bin/boa -m"
# Graal: ./all.sh "$HOME/.esvu/bin/graaljs --experimental-options --js.temporal"
# JavaScriptCore: ./all.sh "$HOME/.esvu/bin/jsc --useTemporal=1"
# Kiesel: ./all.sh "$HOME/.esvu/bin/kiesel -m"
# LadybirdJS: ./all.sh "$HOME/.esvu/bin/ladybird-js -m"
# SpiderMonkey: ./all.sh "$HOME/.esvu/bin/sm -m"
# V8: ./all.sh "$HOME/.esvu/bin/v8 --harmony-temporal"

cd $(dirname $0)
interpreter=${1-node}
there_were_errors=0
for test in \
datedifference \
datetimedifference \
gregorian \
instantdifference \
timedifference \
yearmonthdifference \
zoneddifference
do
echo "== Running $test.mjs =="
$interpreter "$test.mjs" || there_were_errors=1
done
exit $there_were_errors
87 changes: 87 additions & 0 deletions polyfill/test/thorough/datedifference.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
** Copyright (C) 2018-2019 Bloomberg LP. All rights reserved.
** This code is governed by the license found in the LICENSE file.
*/

import {
assertDurationsEqual,
assertTemporalEqual,
createDateSkippingInvalidCombinations,
getProgressBar,
interestingMonthDays,
interestingYears,
temporalImpl as T,
time,
withSnapshotsFromFile
} from './support.mjs';

// With months and years, the reversibility only holds with overflow constrain,
// because adding 1 month or 1 year may land on leap day.
const constrain = { overflow: 'constrain' };
const reject = { overflow: 'reject' };
const largestUnits = [
['years', constrain],
['months', constrain],
['weeks', reject],
['days', reject]
];

const interestingCases = [];
for (const year of interestingYears) {
for (const [month, day] of interestingMonthDays) {
const date = createDateSkippingInvalidCombinations(year, month, day);
if (!date) continue;

// Pre-compute toString so it's not done repeatedly in each test
interestingCases.push([date, date.toString()]);
}
}

const total = (interestingCases.length * (interestingCases.length - 1)) / 2;

await time(async (start) => {
const progress = getProgressBar(start, total);

await withSnapshotsFromFile('./datedifference.snapshot.json', (matchSnapshot) => {
for (const [one, str1] of interestingCases) {
for (const [two, str2] of interestingCases) {
if (T.PlainDate.compare(one, two) === 1) continue;

const testName = `${str1} : ${str2}`;
progress.tick(1, { test: testName });

for (const [largestUnit, opt] of largestUnits) {
const dif1 = one.until(two, { largestUnit });
const sDif1 = dif1.toString();
matchSnapshot(sDif1, `${testName} ${largestUnit} until`);

const dif2 = two.since(one, { largestUnit });
const sDif2 = dif2.toString();
// For months and years, `until` and `since` won't agree because the
// starting point is always `this` and month-aware arithmetic behavior
// varies based on the starting point.
if (largestUnit === 'years' || largestUnit === 'months') {
// If it isn't equal to dif1, it should be snapshotted
matchSnapshot(sDif2, `${testName} ${largestUnit} since`);
} else {
assertDurationsEqual(dif2, dif1, 'until() and since() agree');
}

const dif3 = one.since(two, { largestUnit });
assertDurationsEqual(dif3, dif1.negated(), 'swapping arg and receiver of since() gives -until()');

const dif4 = two.until(one, { largestUnit });
assertDurationsEqual(dif4, dif2.negated(), 'swapping arg and receiver of until() gives -since()');

assertTemporalEqual(one.add(dif1, opt), two, `${str1} + ${sDif1} = ${str2}`);
assertTemporalEqual(one.subtract(dif1.negated(), opt), two, `${str1} - -(${sDif1}) = ${str2}`);

assertTemporalEqual(two.subtract(dif2, opt), one, `${str2} - ${sDif2} = ${str1}`);
assertTemporalEqual(two.add(dif2.negated(), opt), one, `${str2} + -(${sDif2}) = ${str1}`);
}
}
}
});

return total;
});
Loading