Skip to content

Commit

Permalink
test: add tests for zoeHelper assertions (#1771)
Browse files Browse the repository at this point in the history
* test: add tests for zoeHelper assertions

Remove skipped tests for remaining deleted helpers

fixes: #1652

* test: drop an unintended test.only
  • Loading branch information
Chris-Hibbert authored Sep 16, 2020
1 parent 2409eb5 commit a74eb98
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 86 deletions.
85 changes: 0 additions & 85 deletions packages/zoe/test/unitTests/contractSupport/test-zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,91 +43,6 @@ function makeMockTradingZcfBuilder() {
});
}

test.skip('ZoeHelpers assertKeywords', t => {
t.plan(5);
const { moolaR, simoleanR } = setup();
const mockZCFBuilder = makeMockTradingZcfBuilder();
mockZCFBuilder.setInstanceRecord({
issuerKeywordRecord: {
Asset: moolaR.issuer,
Price: simoleanR.issuer,
},
});

const mockZCF = mockZCFBuilder.build();
// eslint-disable-next-line no-undef
const { assertKeywords } = makeZoeHelpers(mockZCF);
t.doesNotThrow(
() => assertKeywords(['Asset', 'Price']),
`Asset and Price are the correct keywords`,
);
t.doesNotThrow(
() => assertKeywords(['Price', 'Asset']),
`Order doesn't matter`,
);
t.throws(
() => assertKeywords(['TokenA', 'TokenB']),
/were not as expected/,
`The wrong keywords will throw`,
);
t.throws(
() => assertKeywords(['Asset', 'Price', 'Price2']),
/were not as expected/,
`An extra keyword will throw`,
);
t.throws(
() => assertKeywords(['Asset']),
/were not as expected/,
`a missing keyword will throw`,
);
});

test.skip('ZoeHelpers isOfferSafe', t => {
t.plan(5);
const { moolaR, simoleanR, moola, simoleans } = setup();
const leftOfferHandle = harden({});
const rightOfferHandle = harden({});
const cantTradeRightOfferHandle = harden({});
const reallocatedHandles = [];
const reallocatedAmountObjs = [];
const completedHandles = [];
const mockZCFBuilder = makeMockTradingZcfBuilder();
mockZCFBuilder.addBrand(moolaR);
mockZCFBuilder.addBrand(simoleanR);
mockZCFBuilder.addAllocation(leftOfferHandle, { Asset: moola(10) });
mockZCFBuilder.addAllocation(rightOfferHandle, { Price: simoleans(6) });
mockZCFBuilder.addAllocation(cantTradeRightOfferHandle, {
Price: simoleans(6),
});
mockZCFBuilder.addOffer(leftOfferHandle, {
proposal: {
give: { Asset: moola(10) },
want: { Price: simoleans(4) },
exit: { onDemand: null },
},
});
const mockZCF = mockZCFBuilder.build();
// eslint-disable-next-line no-undef
const { isOfferSafe } = makeZoeHelpers(mockZCF);
t.truthy(
isOfferSafe(leftOfferHandle, {
Asset: moola(0),
Price: simoleans(4),
}),
`giving someone exactly what they want is offer safe`,
);
t.falsy(
isOfferSafe(leftOfferHandle, {
Asset: moola(0),
Price: simoleans(3),
}),
`giving someone less than what they want and not what they gave is not offer safe`,
);
t.deepEqual(reallocatedHandles, harden([]), `nothing reallocated`);
t.deepEqual(reallocatedAmountObjs, harden([]), `no amounts reallocated`);
t.deepEqual(completedHandles, harden([]), `no offers completed`);
});

test('ZoeHelpers satisfies blank proposal', t => {
const { moolaR, moola } = setup();
const fakeZcfSeat = harden({
Expand Down
139 changes: 138 additions & 1 deletion packages/zoe/test/unitTests/zcf/test-zoeHelpersWZcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { E } from '@agoric/eventual-send';
import test from 'ava';

import { setup } from '../setupBasicMints';
import { swap } from '../../../src/contractSupport';
import {
swap,
assertIssuerKeywords,
assertProposalShape,
} from '../../../src/contractSupport';
import { assertPayoutAmount } from '../../zoeTestHelpers';
import { setupZCFTest } from './setupZcfTest';

Expand Down Expand Up @@ -95,3 +99,136 @@ test(`zoeHelper with zcf - swap no match`, async t => {
assertPayoutAmount(t, simoleanIssuer, seat2PayoutB, simoleans(0));
assertPayoutAmount(t, moolaIssuer, await bUserSeat.getPayout('A'), moola(5));
});

test(`zoeHelper with zcf - assertIssuerKeywords`, async t => {
const {
moolaIssuer,
moola,
simoleanIssuer,
simoleanMint,
simoleans,
} = setup();
const issuerKeywordRecord = { A: moolaIssuer, B: simoleanIssuer };
const { zoe, zcf } = await setupZCFTest(issuerKeywordRecord);

await makeOffer(
zoe,
zcf,
harden({ want: { A: moola(20) }, give: { B: simoleans(3) } }),
{ B: simoleanMint.mintPayment(simoleans(3)) },
);

t.throws(
() => assertIssuerKeywords(zcf, []),
{
message:
'keywords: (an object) were not as expected: (an object)\nSee console for error data.',
},
'empty keywordRecord does not match',
);
t.throws(
() => assertIssuerKeywords(zcf, ['A']),
{
message:
'keywords: (an object) were not as expected: (an object)\nSee console for error data.',
},
'missing keyword from keywordRecord does not match',
);
t.throws(
() => assertIssuerKeywords(zcf, ['A', 'b']),
{
message:
'keywords: (an object) were not as expected: (an object)\nSee console for error data.',
},
'wrong keyword in keywordRecord does not match',
);
t.throws(
() => assertIssuerKeywords(zcf, ['A', 'B', 'C']),
{
message:
'keywords: (an object) were not as expected: (an object)\nSee console for error data.',
},
'extra keywords in keywordRecord does not match',
);
t.throws(
() => assertIssuerKeywords(zcf),
{
message:
'undefined is not iterable (cannot read property Symbol(Symbol.iterator))',
},
'no expected keywordRecord gets an error',
);
t.falsy(assertIssuerKeywords(zcf, ['A', 'B']));
});

test(`zoeHelper with zcf - assertProposalShape`, async t => {
const {
moolaIssuer,
moola,
simoleanIssuer,
simoleanMint,
simoleans,
} = setup();
const issuerKeywordRecord = { A: moolaIssuer, B: simoleanIssuer };
const { zoe, zcf } = await setupZCFTest(issuerKeywordRecord);

const { zcfSeat } = await makeOffer(
zoe,
zcf,
harden({ want: { A: moola(20) }, give: { B: simoleans(3) } }),
{ B: simoleanMint.mintPayment(simoleans(3)) },
);

t.falsy(assertProposalShape(zcfSeat, []), 'empty expectation matches');
t.throws(
() => assertProposalShape(zcfSeat, { want: { C: undefined } }),
{
message:
'actual (an object) did not match expected (an object)\nSee console for error data.',
},
'empty keywordRecord does not match',
);
t.falsy(assertProposalShape(zcfSeat, { want: { A: null } }));
t.falsy(assertProposalShape(zcfSeat, { give: { B: null } }));
t.throws(
() => assertProposalShape(zcfSeat, { give: { c: undefined } }),
{
message:
'actual (an object) did not match expected (an object)\nSee console for error data.',
},
'wrong key in keywordRecord does not match',
);
t.throws(
() => assertProposalShape(zcfSeat, { exit: { onDemaind: undefined } }),
{
message:
'actual (an object) did not match expected (an object)\nSee console for error data.',
},
'missing exit rule',
);
});

test.failing(`zcf/zoeHelper - assertProposalShape w/bad Expected`, async t => {
const {
moolaIssuer,
moola,
simoleanIssuer,
simoleanMint,
simoleans,
} = setup();
const issuerKeywordRecord = { A: moolaIssuer, B: simoleanIssuer };
const { zoe, zcf } = await setupZCFTest(issuerKeywordRecord);

const { zcfSeat } = await makeOffer(
zoe,
zcf,
harden({ want: { A: moola(20) }, give: { B: simoleans(3) } }),
{ B: simoleanMint.mintPayment(simoleans(3)) },
);

// TODO: providing an amount in the expected record should throw
// https://github.com/Agoric/agoric-sdk/issues/1769
t.throws(() => assertProposalShape(zcfSeat, { give: { B: moola(3) } }), {
message: 'expected record should have null values',
});
});

0 comments on commit a74eb98

Please sign in to comment.