Add ABI tests.

This commit is contained in:
Richard Moore 2022-04-19 03:34:47 -04:00
parent 35cf107485
commit fd026eb335
4 changed files with 90 additions and 0 deletions

@ -0,0 +1,43 @@
import assert from "assert";
import { loadTests } from "./utils.js";
import { TestCaseAbi, TestCaseAbiVerbose } from "./types.js";
import { defaultAbiCoder } from "../index.js";
function equal(actual: any, expected: TestCaseAbiVerbose): void {
switch (expected.type) {
case "address": case "boolean": case "hexstring": case "string":
assert.equal(actual, expected.value);
return;
case "number":
assert.equal(actual, BigInt(expected.value));
return
case "array": case "object":
assert.ok(Array.isArray(actual), "!array");
assert.equal(actual.length, expected.value.length, ".length mismatch");
for (let i = 0; i < actual.length; i++) {
equal(actual[i], expected.value[i]);
}
return;
}
throw new Error(`unsupported: ${ expected }`);
}
describe("Tests ABI Coder", function() {
const tests = loadTests<TestCaseAbi>("abi");
for (const test of tests) {
it(`tests ABI encoding: (${ test.name })`, function() {
const encoded = defaultAbiCoder.encode([ test.type ], [ test.value ]);
assert.equal(encoded, test.encoded, "encoded");
});
}
for (const test of tests) {
it(`tests ABI decoding: (${ test.name })`, function() {
const decoded = defaultAbiCoder.decode([ test.type ], test.encoded)[0];
equal(decoded, test.verbose);
});
}
});

@ -0,0 +1,22 @@
export type TestCaseAbiVerbose = {
type: "address" | "hexstring" | "number" | "string",
value: string
} | {
type: "boolean",
value: boolean
} | {
type: "array",
value: Array<TestCaseAbiVerbose>
} | {
type: "object",
value: Array<TestCaseAbiVerbose>
}
export interface TestCaseAbi {
name: string;
type: string;
value: any;
verbose: TestCaseAbiVerbose;
bytecode: string;
encoded: string;
}

@ -0,0 +1,25 @@
import fs from "fs"
import path from "path";
import zlib from 'zlib';
// Find the package root (based on the nyc output/ folder)
const root = (function() {
let root = process.cwd();
while (true) {
if (fs.existsSync(path.join(root, "output"))) { return root; }
const parent = path.join(root, "..");
if (parent === root) { break; }
root = parent;
}
throw new Error("could not find root");
})();
// Load the tests
export function loadTests<T>(tag: string): Array<T> {
const filename = path.resolve(root, "testcases", tag + '.json.gz');
return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)).toString());
}

BIN
testcases/abi.json.gz Normal file

Binary file not shown.