default before/after in FunctionAssertion

This commit is contained in:
Michael Zhu
2019-10-31 15:49:12 -07:00
parent af0de72bc3
commit 09d13b2bfa
2 changed files with 14 additions and 17 deletions

View File

@@ -26,7 +26,6 @@ blockchainTests.resets('FunctionAssertion Unit Tests', env => {
before: async (_input: BigNumber) => {
sideEffectTarget = randomInput;
},
after: async (_beforeInfo: any, _result: Result, _input: BigNumber) => null,
});
const randomInput = getRandomInteger(ZERO_AMOUNT, MAX_UINT256);
await assertion.executeAsync(randomInput);
@@ -36,7 +35,6 @@ blockchainTests.resets('FunctionAssertion Unit Tests', env => {
it('should call the after function with the provided arguments', async () => {
let sideEffectTarget = ZERO_AMOUNT;
const assertion = new FunctionAssertion(exampleContract.returnInteger, {
before: async (_input: BigNumber) => null,
after: async (_beforeInfo: any, _result: Result, input: BigNumber) => {
sideEffectTarget = input;
},
@@ -47,10 +45,7 @@ blockchainTests.resets('FunctionAssertion Unit Tests', env => {
});
it('should not fail immediately if the wrapped function fails', async () => {
const assertion = new FunctionAssertion(exampleContract.emptyRevert, {
before: async () => null,
after: async (_beforeInfo: any, _result: Result) => null,
});
const assertion = new FunctionAssertion<{}>(exampleContract.emptyRevert);
await assertion.executeAsync();
});
@@ -72,7 +67,6 @@ blockchainTests.resets('FunctionAssertion Unit Tests', env => {
it('should pass the result from the function call to "after"', async () => {
let sideEffectTarget = ZERO_AMOUNT;
const assertion = new FunctionAssertion(exampleContract.returnInteger, {
before: async (_input: BigNumber) => null,
after: async (_beforeInfo: any, result: Result, _input: BigNumber) => {
sideEffectTarget = result.data;
},
@@ -85,7 +79,6 @@ blockchainTests.resets('FunctionAssertion Unit Tests', env => {
it('should pass the receipt from the function call to "after"', async () => {
let sideEffectTarget: TransactionReceiptWithDecodedLogs;
const assertion = new FunctionAssertion(exampleContract.emitEvent, {
before: async (_input: string) => null,
after: async (_beforeInfo: any, result: Result, _input: string) => {
if (result.receipt) {
sideEffectTarget = result.receipt;
@@ -107,7 +100,6 @@ blockchainTests.resets('FunctionAssertion Unit Tests', env => {
it('should pass the error to "after" if the function call fails', async () => {
let sideEffectTarget: Error;
const assertion = new FunctionAssertion(exampleContract.stringRevert, {
before: async _string => null,
after: async (_beforeInfo: any, result: Result, _input: string) => {
sideEffectTarget = result.data;
},

View File

@@ -1,9 +1,9 @@
import { PromiseWithTransactionHash } from '@0x/base-contract';
import { BlockchainTestsEnvironment } from '@0x/contracts-test-utils';
import { decodeThrownErrorAsRevertError } from '@0x/utils';
import { BlockParam, CallData, TransactionReceiptWithDecodedLogs, TxData } from 'ethereum-types';
import { TransactionReceiptWithDecodedLogs } from 'ethereum-types';
import * as _ from 'lodash';
// tslint:disable:max-classes-per-file
export interface ContractGetterFunction {
callAsync: (...args: any[]) => Promise<any>;
}
@@ -60,8 +60,12 @@ export class FunctionAssertion<TBefore> implements Assertion {
// The wrapper function that will be wrapped in assertions.
public wrapperFunction: ContractWrapperFunction;
constructor(wrapperFunction: ContractWrapperFunction, condition: Condition<TBefore>) {
this.condition = condition;
constructor(wrapperFunction: ContractWrapperFunction, condition: Partial<Condition<TBefore>> = {}) {
this.condition = {
before: _.noop.bind(this),
after: _.noop.bind(this),
...condition,
};
this.wrapperFunction = wrapperFunction;
}
@@ -123,10 +127,11 @@ class MetaAssertion implements Assertion {
) {}
public async executeAsync(): Promise<void> {
let idx: number;
while ((idx = this.indexGenerator()) > 0) {
let idx = this.indexGenerator();
while (idx > 0) {
const args = await this.assertionGenerators[idx].generator();
this.assertionGenerators[idx].assertion.executeAsync(...args);
await this.assertionGenerators[idx].assertion.executeAsync(...args);
idx = this.indexGenerator();
}
}
}