From b35a023342e0cd2b4a1c3b499b92ede3ab860467 Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Tue, 16 Feb 2021 16:55:30 -0800 Subject: [PATCH 1/4] test: add assertAmountsEqual() helper --- .../contractSupport/test-percentMath.js | 9 +- .../test/unitTests/contracts/loan/helpers.js | 3 +- .../unitTests/contracts/loan/test-borrow.js | 82 ++++++++--- .../unitTests/contracts/test-atomicSwap.js | 7 +- .../test/unitTests/test-fakePriceAuthority.js | 33 ++--- .../zoe/test/unitTests/test-testHelpers.js | 128 ++++++++++++++++++ packages/zoe/test/unitTests/zcf/test-zcf.js | 36 +++-- packages/zoe/test/zoeTestHelpers.js | 53 +++++++- 8 files changed, 290 insertions(+), 61 deletions(-) create mode 100644 packages/zoe/test/unitTests/test-testHelpers.js diff --git a/packages/zoe/test/unitTests/contractSupport/test-percentMath.js b/packages/zoe/test/unitTests/contractSupport/test-percentMath.js index 03892c54fbc..5cbb44ed5e0 100644 --- a/packages/zoe/test/unitTests/contractSupport/test-percentMath.js +++ b/packages/zoe/test/unitTests/contractSupport/test-percentMath.js @@ -11,6 +11,7 @@ import { makeNone, makeAll, } from '../../../src/contractSupport/percentMath'; +import { assertAmountsEqual } from '../../zoeTestHelpers'; test('percentMath - basic', t => { const { brand } = makeIssuerKit('moe'); @@ -63,7 +64,7 @@ test('percentMath - ALL', t => { /** @param {bigint} value */ const moe = value => amountMath.make(value, brand); - t.deepEqual(moe(100000n), makeAll(brand).scale(moe(100000n))); + assertAmountsEqual(t, moe(100000), makeAll(brand).scale(moe(100000n))); }); test('percentMath - NONE', t => { @@ -71,7 +72,11 @@ test('percentMath - NONE', t => { /** @param {bigint} value */ const moe = value => amountMath.make(value, brand); - t.deepEqual(amountMath.makeEmpty(brand), makeNone(brand).scale(moe(100000n))); + assertAmountsEqual( + t, + amountMath.getEmpty(), + makeNone(brand).scale(moe(100000n)), + ); }); test('percentMath - complement', t => { diff --git a/packages/zoe/test/unitTests/contracts/loan/helpers.js b/packages/zoe/test/unitTests/contracts/loan/helpers.js index f3e296c1f93..79c0028798a 100644 --- a/packages/zoe/test/unitTests/contracts/loan/helpers.js +++ b/packages/zoe/test/unitTests/contracts/loan/helpers.js @@ -12,6 +12,7 @@ import { amountMath } from '@agoric/ertp'; import { setup } from '../../setupBasicMints'; import { setupZCFTest } from '../../zcf/setupZcfTest'; import { makeRatio } from '../../../../src/contractSupport'; +import { assertAmountsEqual } from '../../../zoeTestHelpers'; /** * @param {import("ava").ExecutionContext} t @@ -78,7 +79,7 @@ export const checkPayouts = async ( const kit = kitKeywordRecord[keyword]; const amount = await kit.issuer.getAmountOf(paymentP); const expected = expectedKeywordRecord[keyword]; - t.deepEqual(amount, expected); + assertAmountsEqual(t, amount, expected); t.truthy( amountMath.isEqual(amount, expected), `amount value: ${amount.value}, expected value: ${expected.value}, message: ${message}`, diff --git a/packages/zoe/test/unitTests/contracts/loan/test-borrow.js b/packages/zoe/test/unitTests/contracts/loan/test-borrow.js index bd1432114fc..ff4bc456356 100644 --- a/packages/zoe/test/unitTests/contracts/loan/test-borrow.js +++ b/packages/zoe/test/unitTests/contracts/loan/test-borrow.js @@ -27,6 +27,7 @@ import { makeBorrowInvitation } from '../../../../src/contracts/loan/borrow'; import { makeAddCollateralInvitation } from '../../../../src/contracts/loan/addCollateral'; import { makeCloseLoanInvitation } from '../../../../src/contracts/loan/close'; import { makeRatio } from '../../../../src/contractSupport'; +import { assertAmountsEqual } from '../../../zoeTestHelpers'; const BASIS_POINTS = 10000n; @@ -179,13 +180,13 @@ test('borrow getDebtNotifier', async t => { const { borrowFacet, maxLoan } = await setupBorrowFacet(); const debtNotifier = await E(borrowFacet).getDebtNotifier(); const state = await debtNotifier.getUpdateSince(); - t.deepEqual(state.value, maxLoan); + assertAmountsEqual(t, state.value, maxLoan); }); test('borrow getRecentCollateralAmount', async t => { const { borrowFacet, collateral } = await setupBorrowFacet(); const collateralAmount = await E(borrowFacet).getRecentCollateralAmount(); - t.deepEqual(collateralAmount, collateral); + assertAmountsEqual(t, collateralAmount, collateral); }); test('borrow getLiquidationPromise', async t => { @@ -213,8 +214,9 @@ test('borrow getLiquidationPromise', async t => { const { quoteAmount, quotePayment } = await liquidationPromise; const quoteAmount2 = await E(quoteIssuer).getAmountOf(quotePayment); - t.deepEqual(quoteAmount, quoteAmount2); - t.deepEqual( + assertAmountsEqual(t, quoteAmount, quoteAmount2); + assertAmountsEqual( + t, quoteAmount, amountMath.make( [ @@ -276,9 +278,9 @@ test('borrow, then addCollateral, then getLiquidationPromise', async t => { const quoteBrand = await E(quoteIssuer).getBrand(); - t.deepEqual(quoteAmount, quoteAmount2); - - t.deepEqual( + assertAmountsEqual(t, quoteAmount, quoteAmount2); + assertAmountsEqual( + t, quoteAmount, amountMath.make( [ @@ -324,21 +326,29 @@ test('getDebtNotifier with interest', async t => { const { value: originalDebt, updateCount } = await E( debtNotifier, ).getUpdateSince(); - t.deepEqual(originalDebt, maxLoan); + assertAmountsEqual(t, originalDebt, maxLoan); periodUpdater.updateState(6); const { value: debtCompounded1, updateCount: updateCount1 } = await E( debtNotifier, ).getUpdateSince(updateCount); - t.deepEqual(debtCompounded1, amountMath.make(40020n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded1, + amountMath.make(40020n, loanKit.brand), + ); periodUpdater.updateState(11); const { value: debtCompounded2 } = await E(debtNotifier).getUpdateSince( updateCount1, ); - t.deepEqual(debtCompounded2, amountMath.make(40040n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded2, + amountMath.make(40040n, loanKit.brand), + ); const closeLoanInvitation = E(borrowFacet).makeCloseLoanInvitation(); await checkDescription(t, zoe, closeLoanInvitation, 'repayAndClose'); @@ -387,14 +397,18 @@ test('aperiodic interest', async t => { const { value: originalDebt, updateCount } = await E( debtNotifier, ).getUpdateSince(); - t.deepEqual(originalDebt, maxLoan); + assertAmountsEqual(t, originalDebt, maxLoan); periodUpdater.updateState(6); const { value: debtCompounded1, updateCount: updateCount1 } = await E( debtNotifier, ).getUpdateSince(updateCount); - t.deepEqual(debtCompounded1, amountMath.make(40020n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded1, + amountMath.make(40020n, loanKit.brand), + ); // skip ahead a notification periodUpdater.updateState(16); @@ -404,13 +418,21 @@ test('aperiodic interest', async t => { debtNotifier, ).getUpdateSince(updateCount1); t.is(await E(borrowFacet).getLastCalculationTimestamp(), 16n); - t.deepEqual(debtCompounded2, amountMath.make(40060n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded2, + amountMath.make(40060n, loanKit.brand), + ); periodUpdater.updateState(21); const { value: debtCompounded3 } = await E(debtNotifier).getUpdateSince( updateCount2, ); - t.deepEqual(debtCompounded3, amountMath.make(40080n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded3, + amountMath.make(40080n, loanKit.brand), + ); }); // Show that interest is charged from the time the loan was created. @@ -479,7 +501,7 @@ test('short periods', async t => { const { value: originalDebt, updateCount } = await E( debtNotifier, ).getUpdateSince(); - t.deepEqual(originalDebt, maxLoan); + assertAmountsEqual(t, originalDebt, maxLoan); periodUpdater.updateState(5); t.is(await E(borrowFacet).getLastCalculationTimestamp(), 1n); @@ -488,28 +510,44 @@ test('short periods', async t => { const { value: debtCompounded1, updateCount: updateCount1 } = await E( debtNotifier, ).getUpdateSince(updateCount); - t.deepEqual(debtCompounded1, amountMath.make(40020n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded1, + amountMath.make(40020n, loanKit.brand), + ); t.is(await E(borrowFacet).getLastCalculationTimestamp(), 6n); periodUpdater.updateState(14); const { value: debtCompounded2, updateCount: updateCount2 } = await E( debtNotifier, ).getUpdateSince(updateCount1); - t.deepEqual(debtCompounded2, amountMath.make(40040n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded2, + amountMath.make(40040n, loanKit.brand), + ); t.is(await E(borrowFacet).getLastCalculationTimestamp(), 11n); periodUpdater.updateState(17); const { value: debtCompounded3, updateCount: updateCount3 } = await E( debtNotifier, ).getUpdateSince(updateCount2); - t.deepEqual(debtCompounded3, amountMath.make(40060n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded3, + amountMath.make(40060n, loanKit.brand), + ); t.is(await E(borrowFacet).getLastCalculationTimestamp(), 16n); periodUpdater.updateState(21); const { value: debtCompounded4, updateCount: updateCount4 } = await E( debtNotifier, ).getUpdateSince(updateCount3); - t.deepEqual(debtCompounded4, amountMath.make(40080n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded4, + amountMath.make(40080n, loanKit.brand), + ); t.is(await E(borrowFacet).getLastCalculationTimestamp(), 21n); periodUpdater.updateState(25); @@ -519,7 +557,11 @@ test('short periods', async t => { const { value: debtCompounded5 } = await E(debtNotifier).getUpdateSince( updateCount4, ); - t.deepEqual(debtCompounded5, amountMath.make(40100n, loanKit.brand)); + assertAmountsEqual( + t, + debtCompounded5, + amountMath.make(40100n, loanKit.brand), + ); t.is(await E(borrowFacet).getLastCalculationTimestamp(), 26n); }); diff --git a/packages/zoe/test/unitTests/contracts/test-atomicSwap.js b/packages/zoe/test/unitTests/contracts/test-atomicSwap.js index 8be63d1fc6e..81461dac423 100644 --- a/packages/zoe/test/unitTests/contracts/test-atomicSwap.js +++ b/packages/zoe/test/unitTests/contracts/test-atomicSwap.js @@ -9,6 +9,7 @@ import { Far } from '@agoric/marshal'; import { setup } from '../setupBasicMints'; import { setupNonFungible } from '../setupNonFungibleMints'; +import { assertAmountsEqual } from '../../zoeTestHelpers'; const atomicSwapRoot = `${__dirname}/../../../src/contracts/atomicSwap`; @@ -424,7 +425,11 @@ test('zoe - atomicSwap like-for-like', async t => { ); // Alice didn't get any of what Alice put in - t.deepEqual(await moolaIssuer.getAmountOf(aliceAssetPayout), moola(0n)); + assertAmountsEqual( + t, + await moolaIssuer.getAmountOf(aliceAssetPayout), + moola(0n), + ); // Alice deposits her payout to ensure she can const aliceAssetAmount = await aliceMoolaPurse.deposit(aliceAssetPayout); diff --git a/packages/zoe/test/unitTests/test-fakePriceAuthority.js b/packages/zoe/test/unitTests/test-fakePriceAuthority.js index 8d7da0faf36..aa9ccd444ba 100644 --- a/packages/zoe/test/unitTests/test-fakePriceAuthority.js +++ b/packages/zoe/test/unitTests/test-fakePriceAuthority.js @@ -14,6 +14,7 @@ import { getAmountIn, getQuoteValues, } from '../../src/contractSupport'; +import { assertAmountsEqual } from '../zoeTestHelpers'; const makeTestPriceAuthority = (brands, priceList, timer) => makeFakePriceAuthority({ @@ -37,8 +38,8 @@ test('priceAuthority quoteAtTime', async t => { const done = E(priceAuthority) .quoteAtTime(3n, moola(5), bucksBrand) .then(async quote => { - t.deepEqual(moola(5), getAmountIn(quote), 'amountIn match'); - t.deepEqual(bucks(55 * 5), getAmountOut(quote)); + assertAmountsEqual(t, moola(5), getAmountIn(quote)); + assertAmountsEqual(t, bucks(55 * 5), getAmountOut(quote)); t.is(3n, getTimestamp(quote)); }); @@ -82,8 +83,8 @@ test('priceAuthority quoteWanted', async t => { const quote = await E(priceAuthority).quoteWanted(moolaBrand, bucks(400)); const quoteAmount = quote.quoteAmount.value[0]; t.is(1n, quoteAmount.timestamp); - t.deepEqual(bucks(400), quoteAmount.amountOut); - t.deepEqual(moola(20), quoteAmount.amountIn); + assertAmountsEqual(t, bucks(400), quoteAmount.amountOut); + assertAmountsEqual(t, moola(20), quoteAmount.amountIn); }); test('priceAuthority paired quotes', async t => { @@ -104,14 +105,14 @@ test('priceAuthority paired quotes', async t => { const quoteOut = await E(priceAuthority).quoteWanted(moolaBrand, bucks(400)); const quoteOutAmount = quoteOut.quoteAmount.value[0]; t.is(1n, quoteOutAmount.timestamp); - t.deepEqual(bucks(400), quoteOutAmount.amountOut); - t.deepEqual(moola(20), quoteOutAmount.amountIn); + assertAmountsEqual(t, bucks(400), quoteOutAmount.amountOut); + assertAmountsEqual(t, moola(20), quoteOutAmount.amountIn); const quoteIn = await E(priceAuthority).quoteGiven(moola(22), bucksBrand); const quoteInAmount = quoteIn.quoteAmount.value[0]; t.is(1n, quoteInAmount.timestamp); - t.deepEqual(bucks(20 * 22), quoteInAmount.amountOut); - t.deepEqual(moola(22), quoteInAmount.amountIn); + assertAmountsEqual(t, bucks(20 * 22), quoteInAmount.amountOut); + assertAmountsEqual(t, moola(22), quoteInAmount.amountIn); }); test('priceAuthority quoteWhenGTE', async t => { @@ -129,8 +130,8 @@ test('priceAuthority quoteWhenGTE', async t => { const quoteInAmount = quote.quoteAmount.value[0]; t.is(4n, manualTimer.getCurrentTimestamp()); t.is(4n, quoteInAmount.timestamp); - t.deepEqual(bucks(40), quoteInAmount.amountOut); - t.deepEqual(moola(1), quoteInAmount.amountIn); + assertAmountsEqual(t, bucks(40), quoteInAmount.amountOut); + assertAmountsEqual(t, moola(1), quoteInAmount.amountIn); }); await E(manualTimer).tick(); @@ -155,8 +156,8 @@ test('priceAuthority quoteWhenLT', async t => { const quoteInAmount = quote.quoteAmount.value[0]; t.is(3n, manualTimer.getCurrentTimestamp()); t.is(3n, quoteInAmount.timestamp); - t.deepEqual(bucks(29), quoteInAmount.amountOut); - t.deepEqual(moola(1), quoteInAmount.amountIn); + assertAmountsEqual(t, bucks(29), quoteInAmount.amountOut); + assertAmountsEqual(t, moola(1), quoteInAmount.amountIn); }); await E(manualTimer).tick(); @@ -180,8 +181,8 @@ test('priceAuthority quoteWhenGT', async t => { const quoteInAmount = quote.quoteAmount.value[0]; t.is(3n, manualTimer.getCurrentTimestamp()); t.is(3n, quoteInAmount.timestamp); - t.deepEqual(bucks(41), quoteInAmount.amountOut); - t.deepEqual(moola(1), quoteInAmount.amountIn); + assertAmountsEqual(t, bucks(41), quoteInAmount.amountOut); + assertAmountsEqual(t, moola(1), quoteInAmount.amountIn); }); await E(manualTimer).tick(); @@ -205,8 +206,8 @@ test('priceAuthority quoteWhenLTE', async t => { const quoteInAmount = quote.quoteAmount.value[0]; t.is(4n, quoteInAmount.timestamp); t.is(4n, manualTimer.getCurrentTimestamp()); - t.deepEqual(bucks(25), quoteInAmount.amountOut); - t.deepEqual(moola(1), quoteInAmount.amountIn); + assertAmountsEqual(t, bucks(25), quoteInAmount.amountOut); + assertAmountsEqual(t, moola(1), quoteInAmount.amountIn); }); await E(manualTimer).tick(); diff --git a/packages/zoe/test/unitTests/test-testHelpers.js b/packages/zoe/test/unitTests/test-testHelpers.js new file mode 100644 index 00000000000..558b7be21e1 --- /dev/null +++ b/packages/zoe/test/unitTests/test-testHelpers.js @@ -0,0 +1,128 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import '@agoric/install-ses'; + +import test from '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), + getError: () => error, + getMessage: () => message, + }); +} + +test('assertAmountsEqual - Nat dup', t => { + const { moola } = setup(); + + const fakeT = makeFakeT(); + assertAmountsEqual(fakeT, moola(0), moola(0)); + t.is(fakeT.getMessage(), 'values are equal'); + t.falsy(fakeT.getError()); +}); + +test('assertAmountsEqual - Nat manual', t => { + const { + moola, + moolaR: { brand: moolaBrand }, + } = setup(); + + const fakeT = makeFakeT(); + assertAmountsEqual(fakeT, moola(0), { value: 0, brand: moolaBrand }); + t.is(fakeT.getMessage(), 'values are equal'); + t.falsy(fakeT.getError()); +}); + +test('assertAmountsEqual - false Nat', t => { + const { moola } = setup(); + const fakeT = makeFakeT(); + + assertAmountsEqual(fakeT, moola(0), moola(1)); + t.is(fakeT.getError(), 'value (0) expected to equal 1'); +}); + +test('assertAmountsEqual - Set', t => { + const { createRpgItem, rpgItems } = setupNonFungible(); + + const shinyHat = createRpgItem('hat', 'shiny'); + const shinyAmount = rpgItems(shinyHat); + const fakeT = makeFakeT(); + assertAmountsEqual(fakeT, shinyAmount, shinyAmount, MathKind.SET); + t.is(fakeT.getMessage(), 'values are equal'); + t.falsy(fakeT.getError()); +}); + +test('assertAmountsEqual - false Set', t => { + const { createRpgItem, rpgItems } = setupNonFungible(); + + const shinyHat = createRpgItem('hat', 'shiny'); + const shinyAmount = rpgItems(shinyHat); + const sparklyHat = createRpgItem('hat', 'sparkly'); + const sparklyAmount = rpgItems(sparklyHat); + const fakeT = makeFakeT(); + assertAmountsEqual(fakeT, shinyAmount, sparklyAmount, MathKind.SET); + t.is( + fakeT.getError(), + 'value ([{"name":"hat","description":"hat","power":"shiny"}]) expected to equal [{"name":"hat","description":"hat","power":"sparkly"}]', + ); +}); + +test('assertAmountsEqual - StrSet dupe', t => { + const { cryptoCats } = setupNonFungible(); + + 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()); +}); + +test('assertAmountsEqual - StrSet copy', t => { + const { cryptoCats } = setupNonFungible(); + + const felix = cryptoCats(harden(['Felix'])); + 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()); +}); + +test('assertAmountsEqual - false StrSet', t => { + const { cryptoCats } = setupNonFungible(); + + const felix = cryptoCats(harden(['Felix'])); + const sylvester = cryptoCats(harden(['Sylvester'])); + const fakeT = makeFakeT(); + assertAmountsEqual(fakeT, felix, sylvester, MathKind.STRING_SET); + t.is(fakeT.getError(), 'value (["Felix"]) expected to equal ["Sylvester"]'); +}); + +test('assertAmountsEqual - brand mismatch', t => { + const { moola, bucks } = setup(); + const fakeT = makeFakeT(); + assertAmountsEqual(fakeT, moola(0), bucks(0)); + t.is( + fakeT.getError(), + 'brand ([Alleged: moola brand]) expected to equal [Alleged: bucks brand]', + ); +}); + +test('assertAmountsEqual - both mismatch', t => { + const { moola } = setup(); + const { cryptoCats } = setupNonFungible(); + + const fakeT = makeFakeT(); + assertAmountsEqual(fakeT, moola(0), cryptoCats(harden(['Garfield']))); + t.is( + fakeT.getError(), + 'Neither brand nor value matched: {"brand":{},"value":0}, {"brand":{},"value":["Garfield"]}', + ); +}); diff --git a/packages/zoe/test/unitTests/zcf/test-zcf.js b/packages/zoe/test/unitTests/zcf/test-zcf.js index 6d004d8e239..7c1e9fa2bdb 100644 --- a/packages/zoe/test/unitTests/zcf/test-zcf.js +++ b/packages/zoe/test/unitTests/zcf/test-zcf.js @@ -11,6 +11,7 @@ import { setup } from '../setupBasicMints'; import buildManualTimer from '../../../tools/manualTimer'; import { setupZCFTest } from './setupZcfTest'; +import { assertAmountsEqual } from '../../zoeTestHelpers'; test(`zcf.getZoeService`, async t => { const { zoe, zcf } = await setupZCFTest(); @@ -44,7 +45,7 @@ const testTerms = async (t, zcf, expected) => { const zcfTerms = zcf.getTerms(); const zcfTermsMinusAmountMath = { ...zcfTerms, maths: {} }; const expectedMinusAmountMath = { ...expected, maths: {} }; - t.deepEqual(zcfTermsMinusAmountMath, expectedMinusAmountMath); + assertAmountsEqual(t, zcfTermsMinusAmountMath, expectedMinusAmountMath); compareAmountMaths(t, zcfTerms.maths, expected.maths); }; @@ -225,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: {} }; - t.deepEqual(zoeTermsMinusAmountMath, expectedMinusAmountMath); + assertAmountsEqual(t, zoeTermsMinusAmountMath, expectedMinusAmountMath); compareAmountMaths(t, zoeTerms.maths, expected.maths); }); @@ -329,7 +330,7 @@ test(`zcf.makeInvitation - no customProperties`, async t => { const { zcf, zoe, instance, installation } = await setupZCFTest(); const invitationP = zcf.makeInvitation(() => {}, 'myInvitation'); const details = await E(zoe).getInvitationDetails(invitationP); - t.deepEqual(details, { + assertAmountsEqual(t, details, { description: 'myInvitation', handle: details.handle, installation, @@ -345,7 +346,7 @@ test(`zcf.makeInvitation - customProperties`, async t => { timer, }); const details = await E(zoe).getInvitationDetails(invitationP); - t.deepEqual(details, { + assertAmountsEqual(t, details, { description: 'myInvitation', handle: details.handle, installation, @@ -363,7 +364,7 @@ test(`zcf.makeInvitation - customProperties overwritten`, async t => { instance: 'whatever', }); const details = await E(zoe).getInvitationDetails(invitationP); - t.deepEqual(details, { + assertAmountsEqual(t, details, { description: 'myInvitation', handle: details.handle, installation, @@ -470,7 +471,8 @@ test(`zcf.makeZCFMint - mintGains - no seat`, async t => { const { brand } = zcfMint.getIssuerRecord(); const zcfSeat = zcfMint.mintGains({ A: amountMath.make(4n, brand) }); t.truthy(zcfSeat); - t.deepEqual( + assertAmountsEqual( + t, zcfSeat.getAmountAllocated('A', brand), amountMath.make(4n, brand), ); @@ -540,7 +542,8 @@ test(`zcf.makeZCFMint - mintGains - right issuer`, async t => { zcfSeat, ); t.is(zcfSeat2, zcfSeat); - t.deepEqual( + assertAmountsEqual( + t, zcfSeat.getAmountAllocated('A', brand), amountMath.make(4n, brand), ); @@ -557,7 +560,8 @@ test(`zcf.makeZCFMint - burnLosses - right issuer`, async t => { zcfSeat, ); t.is(zcfSeat2, zcfSeat); - t.deepEqual( + assertAmountsEqual( + t, zcfSeat.getAmountAllocated('A', brand), amountMath.make(4n, brand), ); @@ -565,7 +569,8 @@ test(`zcf.makeZCFMint - burnLosses - right issuer`, async t => { // https://github.com/Agoric/agoric-sdk/issues/1709 const result = zcfMint.burnLosses({ A: amountMath.make(1n, brand) }, zcfSeat); t.is(result, undefined); - t.deepEqual( + assertAmountsEqual( + t, zcfSeat.getAmountAllocated('A', brand), amountMath.make(3n, brand), ); @@ -595,7 +600,8 @@ test(`zcf.makeZCFMint - burnLosses - seat exited`, async t => { zcfSeat, ); t.is(zcfSeat2, zcfSeat); - t.deepEqual( + assertAmountsEqual( + t, zcfSeat.getAmountAllocated('A', brand), amountMath.make(4n, brand), ); @@ -853,7 +859,7 @@ test(`zcfSeat.getAmountAllocated from zcf.makeEmptySeatKit`, async t => { // Mint some gains to change the allocation. const { brand: brand1 } = await allocateEasy(zcf, 'Stuff', zcfSeat, 'A', 3); - t.deepEqual(zcfSeat.getAmountAllocated('A', brand1), { + assertAmountsEqual(t, zcfSeat.getAmountAllocated('A', brand1), { brand: brand1, value: 3n, }); @@ -861,12 +867,12 @@ test(`zcfSeat.getAmountAllocated from zcf.makeEmptySeatKit`, async t => { // Again, mint some gains to change the allocation. const { brand: brand2 } = await allocateEasy(zcf, 'Stuff2', zcfSeat, 'B', 6); - t.deepEqual(zcfSeat.getAmountAllocated('B'), { + assertAmountsEqual(t, zcfSeat.getAmountAllocated('B'), { brand: brand2, value: 6n, }); - t.deepEqual(zcfSeat.getAmountAllocated('B', brand2), { + assertAmountsEqual(t, zcfSeat.getAmountAllocated('B', brand2), { brand: brand2, value: 6n, }); @@ -1102,11 +1108,11 @@ test(`userSeat.getPayouts, getPayout from zcf.makeEmptySeatKit`, async t => { t.deepEqual(await payoutPs.A, await payoutAP); t.deepEqual(await payoutPs.B, await payoutBP); - t.deepEqual(await E(issuer1).getAmountOf(payoutAP), { + assertAmountsEqual(t, await E(issuer1).getAmountOf(payoutAP), { brand: brand1, value: 3n, }); - t.deepEqual(await E(issuer2).getAmountOf(payoutBP), { + assertAmountsEqual(t, await E(issuer2).getAmountOf(payoutBP), { brand: brand2, value: 6n, }); diff --git a/packages/zoe/test/zoeTestHelpers.js b/packages/zoe/test/zoeTestHelpers.js index e11c163647b..79856cda47d 100644 --- a/packages/zoe/test/zoeTestHelpers.js +++ b/packages/zoe/test/zoeTestHelpers.js @@ -3,6 +3,50 @@ 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 { q } from '@agoric/assert'; + +export const assertAmountsEqual = ( + t, + amount, + expected, + mathKind = MathKind.NAT, + label = '', +) => { + const brandsEqual = amount.brand === expected.brand; + let valuesEqual; + switch (mathKind) { + case MathKind.NAT: + valuesEqual = amount.value === expected.value; + break; + case MathKind.STRING_SET: + valuesEqual = strSetMathHelpers.doIsEqual(amount.value, expected.value); + break; + case MathKind.SET: + valuesEqual = setMathHelpers.doIsEqual(amount.value, expected.value); + break; + default: + valuesEqual = false; + } + + const l = label ? `${label} ` : ''; + 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) { + 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 ( t, @@ -12,7 +56,8 @@ export const assertPayoutAmount = async ( label = '', ) => { const amount = await issuer.getAmountOf(payout); - t.deepEqual(amount, expectedAmount, `${label} payout was ${amount.value}`); + const amountMathKind = issuer.getAmountMathKind(); + assertAmountsEqual(t, amount, expectedAmount, amountMathKind, label); }; // Returns a promise that can be awaited in tests to ensure the check completes. @@ -21,11 +66,7 @@ export const assertPayoutDeposit = (t, payout, purse, amount) => { E(purse) .deposit(payment) .then(payoutAmount => - t.deepEqual( - payoutAmount, - amount, - `payout was ${payoutAmount.value}, expected ${amount}.value`, - ), + assertAmountsEqual(t, payoutAmount, amount, MathKind.NAT, 'payout'), ); }); }; From ffe32c64bafee0b54b4ab077d4416d18ec1b97c7 Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Wed, 17 Feb 2021 09:12:43 -0800 Subject: [PATCH 2/4] chore: revert one incorrect (but illuminating) switch of deepEqual switched from deepEqual() to assertAmountsEqual() on a non-amount. --- packages/zoe/test/unitTests/zcf/test-zcf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/zoe/test/unitTests/zcf/test-zcf.js b/packages/zoe/test/unitTests/zcf/test-zcf.js index 7c1e9fa2bdb..1118c185f65 100644 --- a/packages/zoe/test/unitTests/zcf/test-zcf.js +++ b/packages/zoe/test/unitTests/zcf/test-zcf.js @@ -330,7 +330,7 @@ test(`zcf.makeInvitation - no customProperties`, async t => { const { zcf, zoe, instance, installation } = await setupZCFTest(); const invitationP = zcf.makeInvitation(() => {}, 'myInvitation'); const details = await E(zoe).getInvitationDetails(invitationP); - assertAmountsEqual(t, details, { + t.deepEqual(details, { description: 'myInvitation', handle: details.handle, installation, From 3b56933df199a8da5773b88c2f459e6091e0bd5b Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Mon, 29 Mar 2021 12:37:56 -0700 Subject: [PATCH 3/4] chore: integrate with amountMath changes and improve tests --- .../contractSupport/test-percentMath.js | 4 +- .../unitTests/contracts/loan/test-borrow.js | 8 ++-- .../unitTests/contracts/test-coveredCall.js | 38 +++++++++---------- .../zoe/test/unitTests/test-testHelpers.js | 27 ++++++------- packages/zoe/test/unitTests/zcf/test-zcf.js | 8 ++-- packages/zoe/test/zoeTestHelpers.js | 23 ++++++----- 6 files changed, 54 insertions(+), 54 deletions(-) diff --git a/packages/zoe/test/unitTests/contractSupport/test-percentMath.js b/packages/zoe/test/unitTests/contractSupport/test-percentMath.js index 5cbb44ed5e0..34a6fd26caa 100644 --- a/packages/zoe/test/unitTests/contractSupport/test-percentMath.js +++ b/packages/zoe/test/unitTests/contractSupport/test-percentMath.js @@ -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 => { @@ -74,7 +74,7 @@ test('percentMath - NONE', t => { assertAmountsEqual( t, - amountMath.getEmpty(), + amountMath.makeEmpty(brand), makeNone(brand).scale(moe(100000n)), ); }); diff --git a/packages/zoe/test/unitTests/contracts/loan/test-borrow.js b/packages/zoe/test/unitTests/contracts/loan/test-borrow.js index ff4bc456356..8d269715780 100644 --- a/packages/zoe/test/unitTests/contracts/loan/test-borrow.js +++ b/packages/zoe/test/unitTests/contracts/loan/test-borrow.js @@ -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'; @@ -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, @@ -229,6 +229,7 @@ test('borrow getLiquidationPromise', async t => { ], quoteBrand, ), + MathKind.SET, ); }); @@ -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, @@ -293,6 +294,7 @@ test('borrow, then addCollateral, then getLiquidationPromise', async t => { ], quoteBrand, ), + MathKind.SET, ); await checkPayouts( diff --git a/packages/zoe/test/unitTests/contracts/test-coveredCall.js b/packages/zoe/test/unitTests/contracts/test-coveredCall.js index a412f44afaa..b18009b54a8 100644 --- a/packages/zoe/test/unitTests/contracts/test-coveredCall.js +++ b/packages/zoe/test/unitTests/contracts/test-coveredCall.js @@ -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`; @@ -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); @@ -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, @@ -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); diff --git a/packages/zoe/test/unitTests/test-testHelpers.js b/packages/zoe/test/unitTests/test-testHelpers.js index 558b7be21e1..3696a59fd84 100644 --- a/packages/zoe/test/unitTests/test-testHelpers.js +++ b/packages/zoe/test/unitTests/test-testHelpers.js @@ -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'; @@ -9,13 +8,11 @@ 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, }); } @@ -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()); }); @@ -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()); }); @@ -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 => { @@ -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()); }); @@ -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()); }); @@ -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()); }); @@ -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"]}', ); }); diff --git a/packages/zoe/test/unitTests/zcf/test-zcf.js b/packages/zoe/test/unitTests/zcf/test-zcf.js index 1118c185f65..05d7b610786 100644 --- a/packages/zoe/test/unitTests/zcf/test-zcf.js +++ b/packages/zoe/test/unitTests/zcf/test-zcf.js @@ -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); }; @@ -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); }); @@ -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, @@ -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, diff --git a/packages/zoe/test/zoeTestHelpers.js b/packages/zoe/test/zoeTestHelpers.js index 79856cda47d..0b917959705 100644 --- a/packages/zoe/test/zoeTestHelpers.js +++ b/packages/zoe/test/zoeTestHelpers.js @@ -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'; @@ -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); @@ -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 ( @@ -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`, + ), ); }); }; From e72898fa78417c554dff67f1218a33943555a6c8 Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Mon, 29 Mar 2021 15:01:14 -0700 Subject: [PATCH 4/4] refactor: drop MathKind parameter to assertAmountsEqual() --- .../unitTests/contracts/loan/test-borrow.js | 8 ++--- .../unitTests/contracts/test-coveredCall.js | 2 -- .../zoe/test/unitTests/test-testHelpers.js | 18 ++++------ packages/zoe/test/zoeTestHelpers.js | 34 ++++++------------- 4 files changed, 19 insertions(+), 43 deletions(-) diff --git a/packages/zoe/test/unitTests/contracts/loan/test-borrow.js b/packages/zoe/test/unitTests/contracts/loan/test-borrow.js index 8d269715780..ff4bc456356 100644 --- a/packages/zoe/test/unitTests/contracts/loan/test-borrow.js +++ b/packages/zoe/test/unitTests/contracts/loan/test-borrow.js @@ -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, MathKind } from '@agoric/ertp'; +import { amountMath } from '@agoric/ertp'; import { E } from '@agoric/eventual-send'; import { makeNotifierKit } from '@agoric/notifier'; @@ -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, MathKind.SET); + assertAmountsEqual(t, quoteAmount, quoteAmount2); assertAmountsEqual( t, quoteAmount, @@ -229,7 +229,6 @@ test('borrow getLiquidationPromise', async t => { ], quoteBrand, ), - MathKind.SET, ); }); @@ -279,7 +278,7 @@ test('borrow, then addCollateral, then getLiquidationPromise', async t => { const quoteBrand = await E(quoteIssuer).getBrand(); - assertAmountsEqual(t, quoteAmount, quoteAmount2, MathKind.SET); + assertAmountsEqual(t, quoteAmount, quoteAmount2); assertAmountsEqual( t, quoteAmount, @@ -294,7 +293,6 @@ test('borrow, then addCollateral, then getLiquidationPromise', async t => { ], quoteBrand, ), - MathKind.SET, ); await checkPayouts( diff --git a/packages/zoe/test/unitTests/contracts/test-coveredCall.js b/packages/zoe/test/unitTests/contracts/test-coveredCall.js index b18009b54a8..e8324c42cd1 100644 --- a/packages/zoe/test/unitTests/contracts/test-coveredCall.js +++ b/packages/zoe/test/unitTests/contracts/test-coveredCall.js @@ -934,13 +934,11 @@ test('zoe - coveredCall non-fungible', async t => { 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); diff --git a/packages/zoe/test/unitTests/test-testHelpers.js b/packages/zoe/test/unitTests/test-testHelpers.js index 3696a59fd84..94239036863 100644 --- a/packages/zoe/test/unitTests/test-testHelpers.js +++ b/packages/zoe/test/unitTests/test-testHelpers.js @@ -2,7 +2,6 @@ // eslint-disable-next-line import/no-extraneous-dependencies 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'; @@ -49,7 +48,7 @@ test('assertAmountsEqual - Set', t => { const shinyHat = createRpgItem('hat', 'shiny'); const shinyAmount = rpgItems(shinyHat); const fakeT = makeFakeT(); - assertAmountsEqual(fakeT, shinyAmount, shinyAmount, MathKind.SET); + assertAmountsEqual(fakeT, shinyAmount, shinyAmount); t.falsy(fakeT.getError()); }); @@ -61,7 +60,7 @@ test('assertAmountsEqual - false Set', t => { const sparklyHat = createRpgItem('hat', 'sparkly'); const sparklyAmount = rpgItems(sparklyHat); const fakeT = makeFakeT(); - assertAmountsEqual(fakeT, shinyAmount, sparklyAmount, MathKind.SET); + assertAmountsEqual(fakeT, shinyAmount, sparklyAmount); t.is( fakeT.getError(), 'value ([{"name":"hat","description":"hat","power":"shiny"}]) expected to equal [{"name":"hat","description":"hat","power":"sparkly"}]', @@ -73,7 +72,7 @@ test('assertAmountsEqual - StrSet dupe', t => { const felix = cryptoCats(harden(['Felix'])); const fakeT = makeFakeT(); - assertAmountsEqual(fakeT, felix, felix, MathKind.STRING_SET); + assertAmountsEqual(fakeT, felix, felix); t.falsy(fakeT.getError()); }); @@ -83,7 +82,7 @@ test('assertAmountsEqual - StrSet copy', t => { const felix = cryptoCats(harden(['Felix'])); const felixAgain = cryptoCats(harden(['Felix'])); const fakeT = makeFakeT(); - assertAmountsEqual(fakeT, felix, felixAgain, MathKind.STRING_SET); + assertAmountsEqual(fakeT, felix, felixAgain); t.falsy(fakeT.getError()); }); @@ -93,7 +92,7 @@ test('assertAmountsEqual - false StrSet', t => { const felix = cryptoCats(harden(['Felix'])); const sylvester = cryptoCats(harden(['Sylvester'])); const fakeT = makeFakeT(); - assertAmountsEqual(fakeT, felix, sylvester, MathKind.STRING_SET); + assertAmountsEqual(fakeT, felix, sylvester); t.is(fakeT.getError(), 'value (["Felix"]) expected to equal ["Sylvester"]'); }); @@ -112,12 +111,7 @@ test('assertAmountsEqual - both mismatch', t => { const { cryptoCats } = setupNonFungible(); const fakeT = makeFakeT(); - assertAmountsEqual( - fakeT, - moola(0), - cryptoCats(harden(['Garfield'])), - MathKind.SET, - ); + assertAmountsEqual(fakeT, moola(0), cryptoCats(harden(['Garfield']))); t.is( fakeT.getError(), 'Neither brand nor value matched: {"brand":"[Alleged: moola brand]","value":"[0n]"}, {"brand":"[Alleged: CryptoCats brand]","value":["Garfield"]}', diff --git a/packages/zoe/test/zoeTestHelpers.js b/packages/zoe/test/zoeTestHelpers.js index 0b917959705..367449dbcd6 100644 --- a/packages/zoe/test/zoeTestHelpers.js +++ b/packages/zoe/test/zoeTestHelpers.js @@ -4,34 +4,22 @@ import { E } from '@agoric/eventual-send'; import '../exported'; import setMathHelpers from '@agoric/ertp/src/mathHelpers/setMathHelpers'; -import { MathKind, amountMath } from '@agoric/ertp'; +import { amountMath, isSetValue, isNatValue } from '@agoric/ertp'; import { q } from '@agoric/assert'; -export const assertAmountsEqual = ( - t, - amount, - expected, - mathKind = MathKind.NAT, - label = '', -) => { +export const assertAmountsEqual = (t, amount, expected, label = '') => { const brandsEqual = amount.brand === expected.brand; + const l = label ? `${label} ` : ''; let valuesEqual; - switch (mathKind) { - case MathKind.NAT: - valuesEqual = amount.value === expected.value; - break; - case MathKind.STRING_SET: - valuesEqual = setMathHelpers.doIsEqual(amount.value, expected.value); - break; - case MathKind.SET: - valuesEqual = setMathHelpers.doIsEqual(amount.value, expected.value); - break; - default: - valuesEqual = false; + if (isSetValue(expected.value)) { + valuesEqual = setMathHelpers.doIsEqual(amount.value, expected.value); + } else if (isNatValue(expected.value)) { + valuesEqual = amount.value === expected.value; + } else { + t.fail(`${l} illegal value; neither isNat() or isSet() was true`); } - const l = label ? `${label} ` : ''; if (brandsEqual && valuesEqual) { t.truthy(amountMath.isEqual(amount, expected), l); } else if (brandsEqual && !valuesEqual) { @@ -53,8 +41,7 @@ export const assertPayoutAmount = async ( label = '', ) => { const amount = await issuer.getAmountOf(payout); - const amountMathKind = issuer.getAmountMathKind(); - assertAmountsEqual(t, amount, expectedAmount, amountMathKind, label); + assertAmountsEqual(t, amount, expectedAmount, label); }; // Returns a promise that can be awaited in tests to ensure the check completes. @@ -67,7 +54,6 @@ export const assertPayoutDeposit = (t, payout, purse, amount) => { t, payoutAmount, amount, - MathKind.NAT, `payout was ${payoutAmount.value}, expected ${amount}.value`, ), );