Skip to content

Commit

Permalink
chore: integrate with amountMath changes and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Mar 29, 2021
1 parent e591ac7 commit 37d1a21
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ test('percentMath - ALL', t => {
/** @param {bigint} value */
const moe = value => amountMath.make(value, brand);

assertAmountsEqual(t, moe(100000), makeAll(brand).scale(moe(100000n)));
assertAmountsEqual(t, moe(100000n), makeAll(brand).scale(moe(100000n)));
});

test('percentMath - NONE', t => {
Expand All @@ -74,7 +74,7 @@ test('percentMath - NONE', t => {

assertAmountsEqual(
t,
amountMath.getEmpty(),
amountMath.makeEmpty(brand),
makeNone(brand).scale(moe(100000n)),
);
});
Expand Down
8 changes: 5 additions & 3 deletions packages/zoe/test/unitTests/contracts/loan/test-borrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '@agoric/zoe/tools/prepare-test-env-ava';
import test from 'ava'; // TODO ses-ava doesn't yet have test.todo
import '../../../../exported';

import { amountMath } from '@agoric/ertp';
import { amountMath, MathKind } from '@agoric/ertp';
import { E } from '@agoric/eventual-send';
import { makeNotifierKit } from '@agoric/notifier';

Expand Down Expand Up @@ -214,7 +214,7 @@ test('borrow getLiquidationPromise', async t => {
const { quoteAmount, quotePayment } = await liquidationPromise;
const quoteAmount2 = await E(quoteIssuer).getAmountOf(quotePayment);

assertAmountsEqual(t, quoteAmount, quoteAmount2);
assertAmountsEqual(t, quoteAmount, quoteAmount2, MathKind.SET);
assertAmountsEqual(
t,
quoteAmount,
Expand All @@ -229,6 +229,7 @@ test('borrow getLiquidationPromise', async t => {
],
quoteBrand,
),
MathKind.SET,
);
});

Expand Down Expand Up @@ -278,7 +279,7 @@ test('borrow, then addCollateral, then getLiquidationPromise', async t => {

const quoteBrand = await E(quoteIssuer).getBrand();

assertAmountsEqual(t, quoteAmount, quoteAmount2);
assertAmountsEqual(t, quoteAmount, quoteAmount2, MathKind.SET);
assertAmountsEqual(
t,
quoteAmount,
Expand All @@ -293,6 +294,7 @@ test('borrow, then addCollateral, then getLiquidationPromise', async t => {
],
quoteBrand,
),
MathKind.SET,
);

await checkPayouts(
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/contracts/test-atomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ test('zoe - non-fungible atomicSwap', async t => {

// Checking handling of duplicate issuers. I'd have preferred a raffle contract
test('zoe - atomicSwap like-for-like', async t => {
t.plan(13);
t.plan(14);
const { moolaIssuer, moolaMint, moola, zoe } = setup();
const invitationIssuer = zoe.getInvitationIssuer();

Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/contracts/test-autoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { assertOfferResult, assertPayoutAmount } from '../../zoeTestHelpers';
const autoswap = `${__dirname}/../../../src/contracts/autoswap`;

test('autoSwap API interactions, no jig', async t => {
t.plan(20);
t.plan(28);
const {
moolaIssuer,
simoleanIssuer,
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/contracts/test-barter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { assertPayoutAmount, assertOfferResult } from '../../zoeTestHelpers';
const barter = `${__dirname}/../../../src/contracts/barterExchange`;

test('barter with valid offers', async t => {
t.plan(10);
t.plan(14);
const {
moolaIssuer,
simoleanIssuer,
Expand Down
42 changes: 20 additions & 22 deletions packages/zoe/test/unitTests/contracts/test-coveredCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { sameStructure } from '@agoric/same-structure';
import buildManualTimer from '../../../tools/manualTimer';
import { setup } from '../setupBasicMints';
import { setupNonFungible } from '../setupNonFungibleMints';
import { assertAmountsEqual } from '../../zoeTestHelpers';

const coveredCallRoot = `${__dirname}/../../../src/contracts/coveredCall`;
const atomicSwapRoot = `${__dirname}/../../../src/contracts/atomicSwap`;
Expand Down Expand Up @@ -590,7 +591,7 @@ test('zoe - coveredCall with swap for invitation', async t => {
// call. Can Bob trick Dave? Can Dave describe what it is that he
// wants in his offer description in the second covered call?
test('zoe - coveredCall with coveredCall for invitation', async t => {
t.plan(31);
t.plan(33);
// Setup the environment
const timer = buildManualTimer(console.log);
const { moolaR, simoleanR, bucksR, moola, simoleans, bucks, zoe } = setup();
Expand Down Expand Up @@ -721,9 +722,7 @@ test('zoe - coveredCall with coveredCall for invitation', async t => {
const daveOptionValue = await E(zoe).getInvitationDetails(daveExclOption);
t.is(daveOptionValue.installation, coveredCallInstallation);
t.is(daveOptionValue.description, 'exerciseOption');
t.truthy(
amountMath.isEqual(daveOptionValue.strikePrice.StrikePrice, bucks(1)),
);
assertAmountsEqual(t, daveOptionValue.strikePrice.StrikePrice, bucks(1));
t.is(daveOptionValue.expirationDate, 100n);
t.deepEqual(daveOptionValue.timeAuthority, timer);

Expand All @@ -736,12 +735,11 @@ test('zoe - coveredCall with coveredCall for invitation', async t => {
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].expirationDate,
100n,
);
t.truthy(
amountMath.isEqual(
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].strikePrice
.StrikePrice,
simoleans(7),
),
assertAmountsEqual(
t,
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].strikePrice
.StrikePrice,
simoleans(7),
);
t.deepEqual(
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].timeAuthority,
Expand Down Expand Up @@ -859,7 +857,7 @@ test('zoe - coveredCall with coveredCall for invitation', async t => {
// Alice uses a covered call to sell a cryptoCat to Bob for the
// 'Glorious shield' she has wanted for a long time.
test('zoe - coveredCall non-fungible', async t => {
t.plan(13);
t.plan(15);
const {
ccIssuer,
rpgIssuer,
Expand Down Expand Up @@ -932,17 +930,17 @@ test('zoe - coveredCall non-fungible', async t => {
const optionValue = await E(zoe).getInvitationDetails(bobExclOption);
t.is(optionValue.installation, coveredCallInstallation);
t.is(optionValue.description, 'exerciseOption');
t.truthy(
amountMath.isEqual(
optionValue.underlyingAssets.UnderlyingAsset,
growlTigerAmount,
),
);
t.truthy(
amountMath.isEqual(
optionValue.strikePrice.StrikePrice,
aGloriousShieldAmount,
),
assertAmountsEqual(
t,
optionValue.underlyingAssets.UnderlyingAsset,
growlTigerAmount,
MathKind.SET,
);
assertAmountsEqual(
t,
optionValue.strikePrice.StrikePrice,
aGloriousShieldAmount,
MathKind.SET,
);
t.is(optionValue.expirationDate, 1n);
t.deepEqual(optionValue.timeAuthority, timer);
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/test/unitTests/contracts/test-simpleExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { assertPayoutAmount, assertOfferResult } from '../../zoeTestHelpers';
const simpleExchange = `${__dirname}/../../../src/contracts/simpleExchange`;

test('simpleExchange with valid offers', async t => {
t.plan(17);
t.plan(21);
const {
moolaIssuer,
simoleanIssuer,
Expand Down Expand Up @@ -294,7 +294,7 @@ test('simpleExchange with multiple sell offers', async t => {
});

test('simpleExchange with non-fungible assets', async t => {
t.plan(9);
t.plan(13);
const {
ccIssuer,
rpgIssuer,
Expand Down
19 changes: 12 additions & 7 deletions packages/zoe/test/unitTests/test-testHelpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-check
// eslint-disable-next-line import/no-extraneous-dependencies
import '@agoric/install-ses';

import test from 'ava';
import { test } from '@agoric/zoe/tools/prepare-test-env-ava';

import { MathKind } from '@agoric/ertp';
import { assertAmountsEqual } from '../zoeTestHelpers';
Expand All @@ -14,6 +13,7 @@ function makeFakeT() {
return harden({
fail: msg => (error = msg),
pass: msg => (message = msg),
truthy: () => {},
getError: () => error,
getMessage: () => message,
});
Expand All @@ -35,7 +35,7 @@ test('assertAmountsEqual - Nat manual', t => {
} = setup();

const fakeT = makeFakeT();
assertAmountsEqual(fakeT, moola(0), { value: 0, brand: moolaBrand });
assertAmountsEqual(fakeT, moola(0), { value: 0n, brand: moolaBrand });
t.is(fakeT.getMessage(), 'values are equal');
t.falsy(fakeT.getError());
});
Expand All @@ -45,7 +45,7 @@ test('assertAmountsEqual - false Nat', t => {
const fakeT = makeFakeT();

assertAmountsEqual(fakeT, moola(0), moola(1));
t.is(fakeT.getError(), 'value (0) expected to equal 1');
t.is(fakeT.getError(), 'value ("[0n]") expected to equal "[1n]"');
});

test('assertAmountsEqual - Set', t => {
Expand Down Expand Up @@ -120,9 +120,14 @@ test('assertAmountsEqual - both mismatch', t => {
const { cryptoCats } = setupNonFungible();

const fakeT = makeFakeT();
assertAmountsEqual(fakeT, moola(0), cryptoCats(harden(['Garfield'])));
assertAmountsEqual(
fakeT,
moola(0),
cryptoCats(harden(['Garfield'])),
MathKind.SET,
);
t.is(
fakeT.getError(),
'Neither brand nor value matched: {"brand":{},"value":0}, {"brand":{},"value":["Garfield"]}',
'Neither brand nor value matched: {"brand":"[Alleged: moola brand]","value":"[0n]"}, {"brand":"[Alleged: CryptoCats brand]","value":["Garfield"]}',
);
});
8 changes: 4 additions & 4 deletions packages/zoe/test/unitTests/zcf/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const testTerms = async (t, zcf, expected) => {
const zcfTerms = zcf.getTerms();
const zcfTermsMinusAmountMath = { ...zcfTerms, maths: {} };
const expectedMinusAmountMath = { ...expected, maths: {} };
assertAmountsEqual(t, zcfTermsMinusAmountMath, expectedMinusAmountMath);
t.deepEqual(zcfTermsMinusAmountMath, expectedMinusAmountMath);

compareAmountMaths(t, zcfTerms.maths, expected.maths);
};
Expand Down Expand Up @@ -226,7 +226,7 @@ test(`zcf.saveIssuer & zoe.getTerms`, async t => {
const zoeTerms = await E(zoe).getTerms(instance);
const zoeTermsMinusAmountMath = { ...zoeTerms, maths: {} };
const expectedMinusAmountMath = { ...expected, maths: {} };
assertAmountsEqual(t, zoeTermsMinusAmountMath, expectedMinusAmountMath);
t.deepEqual(zoeTermsMinusAmountMath, expectedMinusAmountMath);

compareAmountMaths(t, zoeTerms.maths, expected.maths);
});
Expand Down Expand Up @@ -346,7 +346,7 @@ test(`zcf.makeInvitation - customProperties`, async t => {
timer,
});
const details = await E(zoe).getInvitationDetails(invitationP);
assertAmountsEqual(t, details, {
t.deepEqual(details, {
description: 'myInvitation',
handle: details.handle,
installation,
Expand All @@ -364,7 +364,7 @@ test(`zcf.makeInvitation - customProperties overwritten`, async t => {
instance: 'whatever',
});
const details = await E(zoe).getInvitationDetails(invitationP);
assertAmountsEqual(t, details, {
t.deepEqual(details, {
description: 'myInvitation',
handle: details.handle,
installation,
Expand Down
17 changes: 12 additions & 5 deletions packages/zoe/test/zoeTestHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import { E } from '@agoric/eventual-send';

import '../exported';
import strSetMathHelpers from '@agoric/ertp/src/mathHelpers/strSetMathHelpers';
import setMathHelpers from '@agoric/ertp/src/mathHelpers/setMathHelpers';
import { MathKind } from '@agoric/ertp';
import { MathKind, amountMath } from '@agoric/ertp';

import { q } from '@agoric/assert';

Expand All @@ -23,7 +22,7 @@ export const assertAmountsEqual = (
valuesEqual = amount.value === expected.value;
break;
case MathKind.STRING_SET:
valuesEqual = strSetMathHelpers.doIsEqual(amount.value, expected.value);
valuesEqual = setMathHelpers.doIsEqual(amount.value, expected.value);
break;
case MathKind.SET:
valuesEqual = setMathHelpers.doIsEqual(amount.value, expected.value);
Expand All @@ -33,7 +32,9 @@ export const assertAmountsEqual = (
}

const l = label ? `${label} ` : '';
if (brandsEqual && !valuesEqual) {
if (brandsEqual && valuesEqual) {
t.truthy(amountMath.isEqual(amount, expected));
} else if (brandsEqual && !valuesEqual) {
t.fail(
`${l}value (${q(amount.value)}) expected to equal ${q(expected.value)}`,
);
Expand Down Expand Up @@ -66,7 +67,13 @@ export const assertPayoutDeposit = (t, payout, purse, amount) => {
E(purse)
.deposit(payment)
.then(payoutAmount =>
assertAmountsEqual(t, payoutAmount, amount, MathKind.NAT, 'payout'),
assertAmountsEqual(
t,
payoutAmount,
amount,
MathKind.NAT,
`payout was ${payoutAmount.value}, expected ${amount}.value`,
),
);
});
};
Expand Down

0 comments on commit 37d1a21

Please sign in to comment.