Skip to content

Commit

Permalink
feat(store): M.splitArray and M.splitRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Nov 24, 2022
1 parent e5ed233 commit 6405c35
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 205 deletions.
20 changes: 8 additions & 12 deletions packages/ERTP/test/unitTests/test-inputValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ test('makeIssuerKit bad displayInfo.decimalPlaces', async t => {
harden({ decimalPlaces: 'hello' }),
),
{
message:
'displayInfo: optional-parts: decimalPlaces: "hello" - Must be >= -100',
message: 'displayInfo: decimalPlaces?: "hello" - Must be >= -100',
},
);

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

t.throws(
() =>
makeIssuerKit('myTokens', AssetKind.NAT, harden({ decimalPlaces: -101 })),
{
message:
'displayInfo: optional-parts: decimalPlaces: -101 - Must be >= -100',
message: 'displayInfo: decimalPlaces?: -101 - Must be >= -100',
},
);
});
Expand All @@ -89,7 +86,7 @@ test('makeIssuerKit bad displayInfo.assetKind', async t => {
),
{
message:
'displayInfo: optional-parts: assetKind: "something" - Must match one of ["nat","set","copySet","copyBag"]',
'displayInfo: assetKind?: "something" - Must match one of ["nat","set","copySet","copyBag"]',
},
);
});
Expand All @@ -106,8 +103,7 @@ test('makeIssuerKit bad displayInfo.whatever', async t => {
}),
),
{
message:
'displayInfo: rest-parts: {"whatever":"something"} - Must be: {}',
message: 'displayInfo: ...rest: {"whatever":"something"} - Must be: {}',
},
);
});
Expand Down Expand Up @@ -144,7 +140,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) arg 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 @@ -187,7 +183,7 @@ test('issuer.combine bad payments array', async t => {
// @ts-expect-error Intentional wrong type for testing
await t.throwsAsync(() => E(issuer).combine(notAnArray), {
message:
'In "combine" method of (fungible issuer) arg 0: cannot serialize Remotables with non-methods like "length" in {"length":2,"split":"[Function split]"}',
'In "combine" method of (fungible issuer): cannot serialize Remotables with non-methods like "length" in {"length":2,"split":"[Function split]"}',
});

const notAnArray2 = Far('notAnArray2', {
Expand All @@ -197,7 +193,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) arg 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 @@ -43,7 +43,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-parts: {"somethingUnexpected":3} - Must be: {}',
message: 'displayInfo: ...rest: {"somethingUnexpected":3} - Must be: {}',
});
});

Expand Down Expand Up @@ -199,7 +199,7 @@ test('purse.deposit promise', async t => {
() => E(purse).deposit(exclusivePaymentP, fungible25),
{
message:
'In "deposit" method of (fungible Purse purse) arg 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 @@ -334,7 +334,7 @@ test('issuer.split bad amount', async t => {
_ => E(issuer).split(payment, AmountMath.make(otherBrand, 10n)),
{
message:
'In "split" method of (fungible issuer) arg 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 @@ -45,7 +45,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) arg 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/SwingSet/test/stores/test-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const symbolKrusty = Symbol.for('krusty');

// prettier-ignore
const primes = [
2, 3, 5, 7, 11,
2, 3, 5, 7, 11,
13, 17, 19, 23, 29,
31, 37, 41, 43, 47,
53, 59, 61, 67, 71,
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 @@ -218,7 +218,7 @@ test('multiple params bad change', async t => {
),
{
message:
'In "getAmountOf" method of (Zoe Invitation issuer) arg 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
6 changes: 3 additions & 3 deletions packages/inter-protocol/test/psm/test-psm.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ test('wrong give giveMintedInvitation', async t => {
),
{
message:
'"giveMinted" proposal: required-parts: give: In: brand: "[Alleged: aUSD brand]" - Must be: "[Alleged: IST brand]"',
'"giveMinted" proposal: give: In: brand: "[Alleged: aUSD brand]" - Must be: "[Alleged: IST brand]"',
},
);
});
Expand Down Expand Up @@ -652,7 +652,7 @@ test('wrong give wantMintedInvitation', async t => {
),
{
message:
'"wantMinted" proposal: required-parts: give: In: brand: "[Alleged: IST brand]" - Must be: "[Alleged: aUSD brand]"',
'"wantMinted" proposal: give: In: brand: "[Alleged: IST brand]" - Must be: "[Alleged: aUSD brand]"',
},
);
});
Expand All @@ -674,7 +674,7 @@ test('extra give wantMintedInvitation', async t => {
),
{
message:
'"wantMinted" proposal: required-parts: give: {"Extra":{"brand":"[Alleged: aUSD brand]","value":"[200000000n]"},"In":{"brand":"[Seen]","value":"[200000000n]"}} - Must not have unexpected properties: ["Extra"]',
'"wantMinted" proposal: give: {"Extra":{"brand":"[Alleged: aUSD brand]","value":"[200000000n]"},"In":{"brand":"[Seen]","value":"[200000000n]"}} - Must not have unexpected properties: ["Extra"]',
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,7 @@ test('addVaultType: extra, unexpected params', async t => {
E(vaultFactory).addVaultType(chit.issuer, 'Chit', missingParams),
{
message:
/initialParamValues: required-parts: .* - Must have missing properties \["interestRate"\]/,
/initialParamValues: .* - Must have missing properties \["interestRate"\]/,
},
);

Expand Down
6 changes: 3 additions & 3 deletions packages/internal/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const { details: X, quote: q } = assert;

/**
* Throws if multiple entries use the same property name. Otherwise acts
* like `Object.fromEntries`. Use it to protect from property names
* computed from user-provided data.
* like `Object.fromEntries` but hardens the result.
* Use it to protect from property names computed from user-provided data.
*
* @template K,V
* @param {Iterable<[K,V]>} allEntries
* @returns {{[k: K]: V}}
*/
export const fromUniqueEntries = allEntries => {
const entriesArray = [...allEntries];
const result = fromEntries(entriesArray);
const result = harden(fromEntries(entriesArray));
if (ownKeys(result).length === entriesArray.length) {
return result;
}
Expand Down
40 changes: 7 additions & 33 deletions packages/store/src/patterns/interface-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,20 @@ import { Far } from '@endo/marshal';
import { E } from '@endo/eventual-send';
import { listDifference, objectMap } from '@agoric/internal';

import { fit } from './patternMatchers.js';
import { fit, M } from './patternMatchers.js';

const { details: X, quote: q } = assert;
const { apply, ownKeys } = Reflect;
const { defineProperties, seal, freeze } = Object;

const defendSyncArgs = (args, methodGuard, label) => {
const { argGuards, optionalArgGuards, restArgGuard } = methodGuard;
if (args.length < argGuards.length) {
assert.fail(
X`${label} args: ${args} - expected ${argGuards.length} arguments`,
);
}
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
const argLabel = `${label} arg ${i}`;
if (i < argGuards.length) {
fit(arg, argGuards[i], argLabel);
} else if (
optionalArgGuards &&
i < argGuards.length + optionalArgGuards.length
) {
if (arg !== undefined) {
// In the optional section, an `undefined` arg succeeds
// unconditionally
fit(arg, optionalArgGuards[i - argGuards.length], argLabel);
}
} else if (restArgGuard) {
const restArg = harden(args.slice(i));
fit(restArg, restArgGuard, `${label} rest[${i}]`);
return;
} else {
assert.fail(
X`${argLabel}: ${args} - expected fewer than ${i + 1} arguments`,
);
}
}
if (restArgGuard) {
fit(harden([]), restArgGuard, `${label} rest[]`);
}
const paramsPattern = M.splitArray(
argGuards,
optionalArgGuards,
restArgGuard,
);
fit(harden(args), paramsPattern, label);
};

/**
Expand Down
Loading

0 comments on commit 6405c35

Please sign in to comment.