Skip to content

Commit

Permalink
fix(patterns,exo)!: args, splitArray, and splitRecord now default to …
Browse files Browse the repository at this point in the history
…rejecting extra parts
  • Loading branch information
erights committed Sep 12, 2023
1 parent 5feb9ed commit f66768c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
15 changes: 15 additions & 0 deletions packages/exo/test/test-heap-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ import {
} from '../src/exo-makers.js';
import { GET_INTERFACE_GUARD } from '../src/exo-tools.js';

const NoExtraI = M.interface('NoExtra', {
foo: M.call().returns(),
});

test('what happens with extra arguments', t => {
const exo = makeExo('WithExtra', NoExtraI, {
foo(x) {
t.is(x, 8);
},
});
t.throws(() => exo.foo(8), {
message: 'In "foo" method of (WithExtra): ...rest: [8] - Must be: []',
});
});

const UpCounterI = M.interface('UpCounter', {
incr: M.call()
// TODO M.number() should not be needed to get a better error message
Expand Down
4 changes: 2 additions & 2 deletions packages/patterns/src/patterns/patternMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ const makePatternKit = () => {
const matchSplitArrayHelper = Far('match:splitArray helper', {
checkMatches: (
specimen,
[requiredPatt, optionalPatt = [], restPatt = MM.any()],
[requiredPatt, optionalPatt = harden([]), restPatt = harden([])],
check,
) => {
if (!checkKind(specimen, 'copyArray', check)) {
Expand Down Expand Up @@ -1412,7 +1412,7 @@ const makePatternKit = () => {
const matchSplitRecordHelper = Far('match:splitRecord helper', {
checkMatches: (
specimen,
[requiredPatt, optionalPatt = {}, restPatt = MM.any()],
[requiredPatt, optionalPatt = harden({}), restPatt = harden({})],
check,
) => {
if (!checkKind(specimen, 'copyRecord', check)) {
Expand Down
35 changes: 25 additions & 10 deletions packages/patterns/test/test-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const runTests = (t, successCase, failCase) => {
successCase(specimen, M.lte([3, 4, 1]));

successCase(specimen, M.split([3], [4]));
successCase(specimen, M.split([3]));
successCase(specimen, M.split([3], M.any()));
successCase(specimen, M.split([3], M.array()));
successCase(specimen, M.split([3, 4], []));
successCase(specimen, M.split([], [3, 4]));
Expand All @@ -211,6 +211,7 @@ const runTests = (t, successCase, failCase) => {
failCase(specimen, M.lte([3]), '[3,4] - Must be <= [3]');
failCase(specimen, M.gte([3, 4, 1]), '[3,4] - Must be >= [3,4,1]');

failCase(specimen, M.split([3]), '...rest: [4] - Must be: []');
failCase(
specimen,
M.split([3, 4, 5, 6]),
Expand Down Expand Up @@ -249,13 +250,13 @@ const runTests = (t, successCase, failCase) => {
// Records compare pareto
successCase(specimen, M.gte({ foo: 3, bar: 3 }));
successCase(specimen, M.lte({ foo: 4, bar: 4 }));
successCase(
specimen,
M.split(
{ foo: M.number() },
M.and(M.partial({ bar: M.number() }), M.partial({ baz: M.number() })),
),
);
// successCase(
// specimen,
// M.split(
// { foo: M.number() },
// M.and(M.partial({ bar: M.number() }), M.partial({ baz: M.number() })),
// ),
// );
successCase(
specimen,
M.split(
Expand All @@ -266,12 +267,12 @@ const runTests = (t, successCase, failCase) => {

successCase(specimen, M.split({ foo: 3 }, { bar: 4 }));
successCase(specimen, M.split({ bar: 4 }, { foo: 3 }));
successCase(specimen, M.split({ foo: 3 }));
successCase(specimen, M.split({ foo: 3 }, M.any()));
successCase(specimen, M.split({ foo: 3 }, M.record()));
successCase(specimen, M.split({}, { foo: 3, bar: 4 }));
successCase(specimen, M.split({ foo: 3, bar: 4 }, {}));

successCase(specimen, M.partial({ zip: 5, zap: 6 }));
successCase(specimen, M.partial({ zip: 5, zap: 6 }, M.any()));
successCase(specimen, M.partial({ zip: 5, zap: 6 }, { foo: 3, bar: 4 }));
successCase(specimen, M.partial({ foo: 3, zip: 5 }, { bar: 4 }));

Expand Down Expand Up @@ -336,6 +337,15 @@ const runTests = (t, successCase, failCase) => {
'bar?: number 4 - Must be a string',
);

failCase(
specimen,
M.split(
{ foo: M.number() },
M.and(M.partial({ bar: M.number() }), M.partial({ baz: M.number() })),
),
'...rest: ...rest: {"bar":4} - Must be: {}',
);
failCase(specimen, M.split({ foo: 3 }), '...rest: {"bar":4} - Must be: {}');
failCase(
specimen,
M.split(
Expand Down Expand Up @@ -365,6 +375,11 @@ const runTests = (t, successCase, failCase) => {
M.split({ foo: 3 }, { foo: M.any(), bar: 4 }),
'...rest: {"bar":4} - Must have missing properties ["foo"]',
);
failCase(
specimen,
M.partial({ zip: 5, zap: 6 }),
'...rest: {"bar":4,"foo":3} - Must be: {}',
);
failCase(
specimen,
M.partial({ foo: 7, zip: 5 }, { bar: 4 }),
Expand Down

0 comments on commit f66768c

Please sign in to comment.