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 ffe32c6 commit 3b56933
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 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
38 changes: 18 additions & 20 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 @@ -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 @@ -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
27 changes: 12 additions & 15 deletions packages/zoe/test/unitTests/test-testHelpers.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// @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';
import { setup } from './setupBasicMints';
import { setupNonFungible } from './setupNonFungibleMints';

function makeFakeT() {
let message;
let error;
return harden({
fail: msg => (error = msg),
pass: msg => (message = msg),
truthy: () => {},
getError: () => error,
getMessage: () => message,
});
}

Expand All @@ -24,7 +21,6 @@ test('assertAmountsEqual - Nat dup', t => {

const fakeT = makeFakeT();
assertAmountsEqual(fakeT, moola(0), moola(0));
t.is(fakeT.getMessage(), 'values are equal');
t.falsy(fakeT.getError());
});

Expand All @@ -35,8 +31,7 @@ test('assertAmountsEqual - Nat manual', t => {
} = setup();

const fakeT = makeFakeT();
assertAmountsEqual(fakeT, moola(0), { value: 0, brand: moolaBrand });
t.is(fakeT.getMessage(), 'values are equal');
assertAmountsEqual(fakeT, moola(0), { value: 0n, brand: moolaBrand });
t.falsy(fakeT.getError());
});

Expand All @@ -45,7 +40,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 All @@ -55,7 +50,6 @@ test('assertAmountsEqual - Set', t => {
const shinyAmount = rpgItems(shinyHat);
const fakeT = makeFakeT();
assertAmountsEqual(fakeT, shinyAmount, shinyAmount, MathKind.SET);
t.is(fakeT.getMessage(), 'values are equal');
t.falsy(fakeT.getError());
});

Expand All @@ -80,7 +74,6 @@ test('assertAmountsEqual - StrSet dupe', t => {
const felix = cryptoCats(harden(['Felix']));
const fakeT = makeFakeT();
assertAmountsEqual(fakeT, felix, felix, MathKind.STRING_SET);
t.is(fakeT.getMessage(), 'values are equal');
t.falsy(fakeT.getError());
});

Expand All @@ -91,7 +84,6 @@ test('assertAmountsEqual - StrSet copy', t => {
const felixAgain = cryptoCats(harden(['Felix']));
const fakeT = makeFakeT();
assertAmountsEqual(fakeT, felix, felixAgain, MathKind.STRING_SET);
t.is(fakeT.getMessage(), 'values are equal');
t.falsy(fakeT.getError());
});

Expand Down Expand Up @@ -120,9 +112,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
23 changes: 13 additions & 10 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,19 +32,17 @@ export const assertAmountsEqual = (
}

const l = label ? `${label} ` : '';
if (brandsEqual && !valuesEqual) {
if (brandsEqual && valuesEqual) {
t.truthy(amountMath.isEqual(amount, expected), l);
} else if (brandsEqual && !valuesEqual) {
t.fail(
`${l}value (${q(amount.value)}) expected to equal ${q(expected.value)}`,
);
} else if (!brandsEqual && valuesEqual) {
t.fail(`${l}brand (${amount.brand}) expected to equal ${expected.brand}`);
} else if (!brandsEqual && !valuesEqual) {
} else {
t.fail(`${l}Neither brand nor value matched: ${q(amount)}, ${q(expected)}`);
}

// In tests with a fakeT, we'll get here even after failing, otherwise not.
// assert pass() in case there are no other tests.
t.pass(`values are equal`);
};

export const assertPayoutAmount = async (
Expand All @@ -66,7 +63,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 3b56933

Please sign in to comment.