Skip to content

Commit

Permalink
test(patterns): Increase coverage for setIsSuperset
Browse files Browse the repository at this point in the history
Ref #2588
  • Loading branch information
gibson042 committed Oct 21, 2024
1 parent de28fd8 commit 209f96e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/pass-style/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export {
exampleBob,
exampleCarol,
arbString,
arbKeyLeaf,
arbLeaf,
arbKey,
arbPassable,
} from './tools/arb-passable.js';
32 changes: 29 additions & 3 deletions packages/pass-style/tools/arb-passable.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const exampleCarol = Far('carol', {});

export const arbString = fc.oneof(fc.string(), fc.fullUnicodeString());

export const arbLeaf = fc.oneof(
const keyableLeaves = [
fc.constantFrom(null, undefined, false, true),
arbString,
arbString.map(s => Symbol.for(s)),
Expand All @@ -31,22 +31,43 @@ export const arbLeaf = fc.oneof(
fc.constantFrom(-0, NaN, Infinity, -Infinity),
fc.record({}),
fc.constantFrom(exampleAlice, exampleBob, exampleCarol),
];

export const arbKeyLeaf = fc.oneof(...keyableLeaves);

export const arbLeaf = fc.oneof(
...keyableLeaves,
arbString.map(s => Error(s)),
// unresolved promise
fc.constant(new Promise(() => {})),
);

const { keyDag } = fc.letrec(tie => {
return {
keyDag: fc.oneof(
{ withCrossShrink: true },
arbKeyLeaf,
fc.array(tie('keyDag')),
fc.dictionary(
arbString.filter(s => s !== 'then'),
tie('keyDag'),
),
),
};
});

const { arbDag } = fc.letrec(tie => {
return {
arbDag: fc.oneof(
{ withCrossShrink: true },
arbLeaf,
tie('arbDag').map(v => Promise.resolve(v)),
fc.array(tie('arbDag')),
fc.dictionary(
arbString.filter(s => s !== 'then'),
tie('arbDag'),
),
// A promise for a passable.
tie('arbDag').map(v => Promise.resolve(v)),
// A tagged value, either of arbitrary type with arbitrary payload
// or of known type with arbitrary or explicitly valid payload.
// Ordered by increasing complexity.
Expand Down Expand Up @@ -110,6 +131,11 @@ const { arbDag } = fc.letrec(tie => {
});

/**
* A factory for arbitrary passables
* A factory for arbitrary keys.
*/
export const arbKey = keyDag.map(x => harden(x));

/**
* A factory for arbitrary passables.
*/
export const arbPassable = arbDag.map(x => harden(x));
2 changes: 2 additions & 0 deletions packages/patterns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
},
"devDependencies": {
"@endo/init": "workspace:^",
"@endo/pass-style": "workspace:^",
"@endo/ses-ava": "workspace:^",
"@fast-check/ava": "^1.1.5",
"ava": "^6.1.3",
"babel-eslint": "^10.1.0",
"eslint": "^8.57.0",
Expand Down
38 changes: 37 additions & 1 deletion packages/patterns/test/copySet.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import test from '@endo/ses-ava/prepare-endo.js';

import { fc } from '@fast-check/ava';
import { makeTagged, getTag, passStyleOf } from '@endo/marshal';
import {
arbKey,
exampleAlice,
exampleBob,
exampleCarol,
} from '@endo/pass-style/tools.js';
import { Fail, q } from '@endo/errors';
import {
isCopySet,
assertCopySet,
makeCopySet,
getCopySetKeys,
} from '../src/keys/checkKey.js';
import { keyEQ } from '../src/keys/compareKeys.js';
import {
setIsSuperset,
setIsDisjoint,
Expand Down Expand Up @@ -85,7 +94,7 @@ test('key uniqueness', t => {
// TODO: incorporate fast-check for property-based testing that construction
// reverse rank sorts keys and validation rejects any other key order.

test('operations', t => {
test('operations on golden inputs', t => {
const x = makeCopySet(['b', 'a', 'c']);
const y = makeCopySet(['a', 'b']);
const z = makeCopySet(['c', 'b']);
Expand All @@ -104,6 +113,16 @@ test('operations', t => {
t.assert(setIsDisjoint(xMy, y));

t.assert(setIsSuperset(x, y));
const twoCohorts = [
[exampleAlice, 'z'],
[exampleBob, 'z'],
[exampleCarol, 'a'],
];
t.assert(
setIsSuperset(makeCopySet(twoCohorts), makeCopySet(twoCohorts.slice(-1))),
'superset with many items in one rank cohort (issue #2588)',
);

t.assert(matches(x, yUz));
t.assert(matches(x, M.gt(y)));
t.assert(matches(x, M.gt(z)));
Expand All @@ -119,6 +138,23 @@ test('operations', t => {
t.deepEqual(yIz, makeTagged('copySet', ['b']));
});

test('setIsSuperset', async t => {
await fc.assert(
fc.property(
fc.uniqueArray(arbKey, { comparator: keyEQ }),
fc.infiniteStream(fc.boolean()),
(arr, keepSeq) => {
const sub = arr.filter(() => keepSeq.next().value);
setIsSuperset(makeCopySet(arr), makeCopySet(sub)) ||
Fail`${q(sub)} of ${q(arr)}`;
},
),
);

// Ensure at least one ava assertion.
t.pass();
});

test('matching', t => {
const copySet = makeCopySet(['z', 'c', 'b', 'a']);
const missingKey = makeCopySet(['z', 'c', 'b']);
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,10 @@ __metadata:
"@endo/eventual-send": "workspace:^"
"@endo/init": "workspace:^"
"@endo/marshal": "workspace:^"
"@endo/pass-style": "workspace:^"
"@endo/promise-kit": "workspace:^"
"@endo/ses-ava": "workspace:^"
"@fast-check/ava": "npm:^1.1.5"
ava: "npm:^6.1.3"
babel-eslint: "npm:^10.1.0"
eslint: "npm:^8.57.0"
Expand Down

0 comments on commit 209f96e

Please sign in to comment.