From 3b56933df199a8da5773b88c2f459e6091e0bd5b Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Mon, 29 Mar 2021 12:37:56 -0700 Subject: [PATCH] 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`, + ), ); }); };