Skip to content

Commit

Permalink
feat: find and filter, iterator helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinketer22 authored Nov 5, 2023
1 parent 358d1d1 commit 47c9cab
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ export {
FlatTransactionComparable,
compareTransaction,
flattenTransaction,
findTransaction,
filterTransactions,
} from './test/transaction';

import './test/jest';
import './test/chai';

export {
randomAddress,
} from './utils/randomAddress';
} from './utils/randomAddress';

export {
executeTill,
executeFrom,
} from './utils/stepByStep';
24 changes: 23 additions & 1 deletion src/test/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,26 @@ export function compareTransactionForTest(subject: any, cmp: FlatTransactionComp
throw e;
}
}
}
}

export function findTransaction<T extends Transaction>(txs: T | T[], match: FlatTransactionComparable) {
let res: T | undefined;
if (Array.isArray(txs)) {
res = txs.find(x => compareTransaction(flattenTransaction(x), match));
} else {
res = compareTransaction(flattenTransaction(txs), match) ? txs : undefined;
}
return res;
}

export function findTransactionRequired<T extends Transaction>(txs: T | T[], match: FlatTransactionComparable) {
const res = findTransaction(txs, match);
if (res === undefined) {
throw new Error(`Expected ${inspect(Array.isArray(txs) ? txs.map(x => flattenTransaction(x)) : flattenTransaction(txs))} to contain a transaction that matches pattern ${inspect(match)}`);
}
return res;
}

export function filterTransactions<T extends Transaction>(txs: T[], match: FlatTransactionComparable) {
return txs.filter(x => compareTransaction(flattenTransaction(x), match));
}
34 changes: 34 additions & 0 deletions src/utils/stepByStep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Transaction } from "@ton/core";
import { FlatTransactionComparable, flattenTransaction, compareTransaction } from "../test/transaction";
import { inspect } from "node-inspect-extracted";

export async function executeTill<T extends Transaction>(txs: AsyncIterator<T>, match: FlatTransactionComparable) {
let executed: T[] = [];
let iterResult = await txs.next();
let found = false;
while (!iterResult.done) {
executed.push(iterResult.value);
found = compareTransaction(flattenTransaction(iterResult.value), match);
if (found) {
break;
}
iterResult = await txs.next();
}
if (!found) {
throw new Error(`Expected ${inspect(executed.map(x => flattenTransaction(x)))} to contain a transaction that matches pattern ${inspect(match)}`);
}

return executed;
}

export async function executeFrom<T extends Transaction>(txs: AsyncIterator<T>) {
let executed: T[] = [];
let iterResult = await txs.next();

while (!iterResult.done) {
executed.push(iterResult.value);
iterResult = await txs.next();
}

return executed;
}

0 comments on commit 47c9cab

Please sign in to comment.