Skip to content

Commit

Permalink
fix: better split errors
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Oct 11, 2022
1 parent b83b479 commit b7312bb
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 65 deletions.
15 changes: 7 additions & 8 deletions packages/ERTP/test/unitTests/test-inputValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ test('makeIssuerKit bad displayInfo.decimalPlaces', async t => {
harden({ decimalPlaces: 'hello' }),
),
{
message:
'displayInfo: optional: decimalPlaces: "hello" - Must be >= -100',
message: 'displayInfo: decimalPlaces?: "hello" - Must be >= -100',
},
);

Expand Down Expand Up @@ -62,15 +61,15 @@ test('makeIssuerKit bad displayInfo.decimalPlaces', async t => {
() =>
makeIssuerKit('myTokens', AssetKind.NAT, harden({ decimalPlaces: 101 })),
{
message: 'displayInfo: optional: decimalPlaces: 101 - Must be <= 100',
message: 'displayInfo: decimalPlaces?: 101 - Must be <= 100',
},
);

t.throws(
() =>
makeIssuerKit('myTokens', AssetKind.NAT, harden({ decimalPlaces: -101 })),
{
message: 'displayInfo: optional: decimalPlaces: -101 - Must be >= -100',
message: 'displayInfo: decimalPlaces?: -101 - Must be >= -100',
},
);
});
Expand All @@ -88,7 +87,7 @@ test('makeIssuerKit bad displayInfo.assetKind', async t => {
),
{
message:
'displayInfo: optional: assetKind: "something" - Must match one of ["nat","set","copySet","copyBag"]',
'displayInfo: assetKind?: "something" - Must match one of ["nat","set","copySet","copyBag"]',
},
);
});
Expand All @@ -105,7 +104,7 @@ test('makeIssuerKit bad displayInfo.whatever', async t => {
}),
),
{
message: 'displayInfo: rest: {"whatever":"something"} - Must be: {}',
message: 'displayInfo: ...rest: {"whatever":"something"} - Must be: {}',
},
);
});
Expand Down Expand Up @@ -142,7 +141,7 @@ test('brand.isMyIssuer bad issuer', async t => {
// @ts-expect-error Intentional wrong type for testing
t.throwsAsync(() => brand.isMyIssuer('not an issuer'), {
message:
'In "isMyIssuer" method of (myTokens brand): args: [0]: string "not an issuer" - Must be a remotable (Issuer)',
'In "isMyIssuer" method of (myTokens brand): arg 0: string "not an issuer" - Must be a remotable (Issuer)',
});
const fakeIssuer = /** @type {Issuer} */ (
/** @type {unknown} */ (Far('myTokens issuer', {}))
Expand Down Expand Up @@ -195,7 +194,7 @@ test('issuer.combine bad payments array', async t => {
// @ts-expect-error Intentional wrong type for testing
await t.throwsAsync(() => E(issuer).combine(notAnArray2), {
message:
'In "combine" method of (fungible issuer): args: [0]: remotable "[Alleged: notAnArray2]" - Must be a copyArray',
'In "combine" method of (fungible issuer): arg 0: remotable "[Alleged: notAnArray2]" - Must be a copyArray',
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/ERTP/test/unitTests/test-issuerObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test('bad display info', t => {
const displayInfo = harden({ somethingUnexpected: 3 });
// @ts-expect-error deliberate invalid arguments for testing
t.throws(() => makeIssuerKit('fungible', AssetKind.NAT, displayInfo), {
message: 'displayInfo: rest: {"somethingUnexpected":3} - Must be: {}',
message: 'displayInfo: ...rest: {"somethingUnexpected":3} - Must be: {}',
});
});

Expand Down Expand Up @@ -200,7 +200,7 @@ test('purse.deposit promise', async t => {
() => E(purse).deposit(exclusivePaymentP, fungible25),
{
message:
'In "deposit" method of (fungible Purse purse): args: [0]: promise "[Promise]" - Must be a remotable (Payment)',
'In "deposit" method of (fungible Purse purse): arg 0: promise "[Promise]" - Must be a remotable (Payment)',
},
'failed to reject a promise for a payment',
);
Expand Down Expand Up @@ -335,7 +335,7 @@ test('issuer.split bad amount', async t => {
_ => E(issuer).split(payment, AmountMath.make(otherBrand, 10n)),
{
message:
'In "split" method of (fungible issuer): args: [1]: brand: "[Alleged: other fungible brand]" - Must be: "[Alleged: fungible brand]"',
'In "split" method of (fungible issuer): arg 1: brand: "[Alleged: other fungible brand]" - Must be: "[Alleged: fungible brand]"',
},
'throws for bad amount',
);
Expand Down
2 changes: 1 addition & 1 deletion packages/ERTP/test/unitTests/test-mintObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('mint.mintPayment set w strings AssetKind', async t => {
const badAmount = AmountMath.make(brand, harden([['badElement']]));
t.throws(() => mint.mintPayment(badAmount), {
message:
'In "mintPayment" method of (items mint): args: [0]: value: [0]: copyArray ["badElement"] - Must be a string',
'In "mintPayment" method of (items mint): arg 0: value: [0]: copyArray ["badElement"] - Must be a string',
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/governance/test/unitTests/test-paramGovernance.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ test('multiple params bad change', async t => {
),
{
message:
'In "getAmountOf" method of (Zoe Invitation issuer): args: [0]: bigint "[13n]" - Must be a remotable (Payment)',
'In "getAmountOf" method of (Zoe Invitation issuer): arg 0: bigint "[13n]" - Must be a remotable (Payment)',
},
);
});
Expand Down
31 changes: 26 additions & 5 deletions packages/store/src/patterns/patternMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1579,10 +1579,24 @@ const makePatternKit = () => {
optionalPatt,
optionalSpecimen.length,
);
let argNum = 0;
return (
checkMatches(requiredSpecimen, requiredPatt, check, 'args') &&
checkMatches(optionalSpecimen, partialPatt, check, 'optional args') &&
checkMatches(restSpecimen, restPatt, check, 'rest args')
(requiredSpecimen.length === requiredPatt.length ||
check(
false,
X`Expected at least ${q(
requiredPatt.length,
)} arguments: ${specimen}`,
)) &&
requiredPatt.every((p, i) =>
// eslint-disable-next-line no-plusplus
checkMatches(requiredSpecimen[i], p, check, `arg ${argNum++}`),
) &&
partialPatt.every((p, i) =>
// eslint-disable-next-line no-plusplus
checkMatches(optionalSpecimen[i], p, check, `arg ${argNum++}?`),
) &&
checkMatches(restSpecimen, restPatt, check, '...rest')
);
},

Expand Down Expand Up @@ -1728,8 +1742,15 @@ const makePatternKit = () => {
const partialPatt = adaptRecordPattern(optionalPatt, partialNames);
return (
checkMatches(requiredSpecimen, requiredPatt, check) &&
checkMatches(optionalSpecimen, partialPatt, check, 'optional') &&
checkMatches(restSpecimen, restPatt, check, 'rest')
partialNames.every(name =>
checkMatches(
optionalSpecimen[name],
partialPatt[name],
check,
`${name}?`,
),
) &&
checkMatches(restSpecimen, restPatt, check, '...rest')
);
},

Expand Down
15 changes: 6 additions & 9 deletions packages/store/test/test-heap-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ test('test defineHeapFarClass', t => {
t.is(upCounter.incr(5), 8);
t.is(upCounter.incr(1), 9);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (UpCounter): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (UpCounter): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error bad arg
t.throws(() => upCounter.incr('foo'), {
message:
'In "incr" method of (UpCounter): optional args: [0]: string "foo" - Must be a number',
'In "incr" method of (UpCounter): arg 0?: string "foo" - Must be a number',
});
});

Expand Down Expand Up @@ -79,13 +78,12 @@ test('test defineHeapFarClassKit', t => {
t.is(downCounter.decr(), 7);
t.is(upCounter.incr(3), 10);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (Counter up): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (Counter up): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error the type violation is what we're testing
t.throws(() => downCounter.decr('foo'), {
message:
'In "decr" method of (Counter down): optional args: [0]: string "foo" - Must be a number',
'In "decr" method of (Counter down): arg 0?: string "foo" - Must be a number',
});
// @ts-expect-error bad arg
t.throws(() => upCounter.decr(3), {
Expand All @@ -104,12 +102,11 @@ test('test makeHeapFarInstance', t => {
t.is(upCounter.incr(5), 8);
t.is(upCounter.incr(1), 9);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (upCounter): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (upCounter): arg 0?: -3 - Must be >= 0',
});
t.throws(() => upCounter.incr('foo'), {
message:
'In "incr" method of (upCounter): optional args: [0]: string "foo" - Must be a number',
'In "incr" method of (upCounter): arg 0?: string "foo" - Must be a number',
});
});

Expand Down
18 changes: 9 additions & 9 deletions packages/store/test/test-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ const runTests = (successCase, failCase) => {
failCase(
specimen,
M.split([3, 4, 5, 6]),
'args: [3,4] - Must be: [3,4,5,6]',
'Expected at least 4 arguments: [3,4]',
);
failCase(specimen, M.split([5]), 'args: [3] - Must be: [5]');
failCase(specimen, M.split([5]), 'arg 0: 3 - Must be: 5');
failCase(specimen, M.split({}), 'copyArray [3,4] - Must be a copyRecord');
failCase(specimen, M.split([3], 'x'), 'rest args: [4] - Must be: "x"');
failCase(specimen, M.split([3], 'x'), '...rest: [4] - Must be: "x"');

failCase(specimen, M.partial([5]), 'optional args: [0]: 3 - Must be: 5');
failCase(specimen, M.partial([5]), 'arg 0?: 3 - Must be: 5');

failCase(
specimen,
Expand Down Expand Up @@ -199,7 +199,7 @@ const runTests = (successCase, failCase) => {
failCase(
specimen,
M.splitRecord({ foo: M.number() }, { bar: M.string(), baz: M.number() }),
'optional: bar: number 4 - Must be a string',
'bar?: number 4 - Must be a string',
);

failCase(
Expand All @@ -208,7 +208,7 @@ const runTests = (successCase, failCase) => {
{ foo: M.number() },
M.and(M.partial({ bar: M.string() }), M.partial({ baz: M.number() })),
),
'rest: optional: bar: number 4 - Must be a string',
'...rest: bar?: number 4 - Must be a string',
);

failCase(
Expand All @@ -224,17 +224,17 @@ const runTests = (successCase, failCase) => {
failCase(
specimen,
M.split({ foo: 3 }, { foo: 3, bar: 4 }),
'rest: {"bar":4} - Must be: {"foo":3,"bar":4}',
'...rest: {"bar":4} - Must be: {"foo":3,"bar":4}',
);
failCase(
specimen,
M.split({ foo: 3 }, { foo: M.any(), bar: 4 }),
'rest: {"bar":4} - Must have missing properties ["foo"]',
'...rest: {"bar":4} - Must have missing properties ["foo"]',
);
failCase(
specimen,
M.partial({ foo: 7, zip: 5 }, { bar: 4 }),
'optional: foo: 3 - Must be: 7',
'foo?: 3 - Must be: 7',
);

failCase(
Expand Down
10 changes: 4 additions & 6 deletions packages/vat-data/test/test-durable-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ test('test defineDurableFarClass', t => {
t.is(upCounter.incr(5), 8);
t.is(upCounter.incr(1), 9);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (UpCounter): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (UpCounter): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error the type violation is what we're testing
t.throws(() => upCounter.incr('foo'), {
message:
'In "incr" method of (UpCounter): optional args: [0]: string "foo" - Must be a number',
'In "incr" method of (UpCounter): arg 0?: string "foo" - Must be a number',
});
});

Expand Down Expand Up @@ -83,13 +82,12 @@ test('test defineDurableFarClassKit', t => {
t.is(downCounter.decr(), 7);
t.is(upCounter.incr(3), 10);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (Counter up): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (Counter up): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error the type violation is what we're testing
t.throws(() => downCounter.decr('foo'), {
message:
'In "decr" method of (Counter down): optional args: [0]: string "foo" - Must be a number',
'In "decr" method of (Counter down): arg 0?: string "foo" - Must be a number',
});
t.throws(() => upCounter.decr(3), {
message: 'upCounter.decr is not a function',
Expand Down
10 changes: 4 additions & 6 deletions packages/vat-data/test/test-virtual-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ test('test defineVirtualFarClass', t => {
t.is(upCounter.incr(5), 8);
t.is(upCounter.incr(1), 9);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (UpCounter): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (UpCounter): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error the type violation is what we're testing
t.throws(() => upCounter.incr('foo'), {
message:
'In "incr" method of (UpCounter): optional args: [0]: string "foo" - Must be a number',
'In "incr" method of (UpCounter): arg 0?: string "foo" - Must be a number',
});
});

Expand Down Expand Up @@ -78,13 +77,12 @@ test('test defineVirtualFarClassKit', t => {
t.is(downCounter.decr(), 7);
t.is(upCounter.incr(3), 10);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (Counter up): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (Counter up): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error the type violation is what we're testing
t.throws(() => downCounter.decr('foo'), {
message:
'In "decr" method of (Counter down): optional args: [0]: string "foo" - Must be a number',
'In "decr" method of (Counter down): arg 0?: string "foo" - Must be a number',
});
t.throws(() => upCounter.decr(3), {
message: 'upCounter.decr is not a function',
Expand Down
15 changes: 6 additions & 9 deletions packages/vat-data/test/test-vivify.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ test('test vivifyFarClass', t => {
t.is(upCounter.incr(5), 8);
t.is(upCounter.incr(1), 9);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (UpCounter): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (UpCounter): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error the type violation is what we're testing
t.throws(() => upCounter.incr('foo'), {
message:
'In "incr" method of (UpCounter): optional args: [0]: string "foo" - Must be a number',
'In "incr" method of (UpCounter): arg 0?: string "foo" - Must be a number',
});
});

Expand Down Expand Up @@ -86,13 +85,12 @@ test('test vivifyFarClassKit', t => {
t.is(downCounter.decr(), 7);
t.is(upCounter.incr(3), 10);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (Counter up): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (Counter up): arg 0?: -3 - Must be >= 0',
});
// @ts-expect-error the type violation is what we're testing
t.throws(() => downCounter.decr('foo'), {
message:
'In "decr" method of (Counter down): optional args: [0]: string "foo" - Must be a number',
'In "decr" method of (Counter down): arg 0?: string "foo" - Must be a number',
});
t.throws(() => upCounter.decr(3), {
message: 'upCounter.decr is not a function',
Expand All @@ -112,12 +110,11 @@ test('test vivifyFarInstance', t => {
t.is(upCounter.incr(5), 8);
t.is(upCounter.incr(1), 9);
t.throws(() => upCounter.incr(-3), {
message:
'In "incr" method of (upCounter): optional args: [0]: -3 - Must be >= 0',
message: 'In "incr" method of (upCounter): arg 0?: -3 - Must be >= 0',
});
t.throws(() => upCounter.incr('foo'), {
message:
'In "incr" method of (upCounter): optional args: [0]: string "foo" - Must be a number',
'In "incr" method of (upCounter): arg 0?: string "foo" - Must be a number',
});
});

Expand Down
Loading

0 comments on commit b7312bb

Please sign in to comment.