Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

chore: add codegen codec tests #316

Merged
merged 4 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _tasks/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ await Promise.all([
}],
outDir,
mappings: {
"https://deno.land/x/[email protected].1/mod.ts": {
"https://deno.land/x/[email protected].2/mod.ts": {
name: "parity-scale-codec",
version: "^0.6.1",
version: "^0.6.2",
},
"https://deno.land/x/[email protected]/mod.ts": {
name: "zones",
Expand Down
55 changes: 55 additions & 0 deletions codegen/codecVisitor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as M from "../frame_metadata/mod.ts";
import * as C from "../mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

for (const config of T.configs) {
Deno.test(config.runtimeName, async () => {
const metadata = U.throwIfError(await C.run(new C.Metadata(config)));
const codegen = await T.importCodegen(config);
const deriveCodec = M.DeriveCodec(metadata.tys);
const derivedCodecs = metadata.tys.map(deriveCodec);
const codegenCodecs = codegen._metadata.types;
assertCodecEquals(derivedCodecs, codegenCodecs);
});
}

const memo = new Map<any, Set<any>>();
function assertCodecEquals(a: any, b: any, path = "") {
const set = U.getOr(memo, a, () => new Set());
if (set.has(b)) return;
set.add(b);
if (a === b) return;
if (!a || !b || typeof a !== "object" || typeof b !== "object") {
throw new Error(`${path}: ${Deno.inspect(a)} !== ${Deno.inspect(b)}`);
}
if ("_metadata" in a !== "_metadata" in b) {
throw new Error(`${path}: codec-ness mismatch`);
}
if ("_metadata" in a) {
if (a._metadata[0] === b._metadata[0] && a._metadata[0] === C.$.deferred) {
assertCodecEquals(a._metadata[1](), b._metadata[1](), path + "._metadata[1]()");
return;
}
assertCodecEquals(a._metadata, b._metadata, path + "._metadata");
return;
}
if ((a instanceof Array) !== (b instanceof Array)) {
throw new Error(`${path}: Array-ness mismatch`);
}
if (a instanceof Array) {
assertCodecEquals(a.length, b.length, path + ".length");
for (let i = 0; i < a.length; i++) {
assertCodecEquals(a[i], b[i], path + `[${i}]`);
}
return;
}
assertCodecEquals(Object.keys(a), Object.keys(b), path + "#keys");
for (const key of Object.keys(a)) {
assertCodecEquals(
a[key],
b[key],
path + (/^\p{ID_Start}\p{ID_Continue}*$/u.test(key) ? "." + key : `[${JSON.stringify(key)}]`),
);
}
}
5 changes: 5 additions & 0 deletions codegen/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { Decl, Files, importSource, printDecls, S } from "./utils.ts";
export async function run(metadataFile: string, outputDir: string) {
const metadata = M.fromPrefixedHex(await Deno.readTextFile(metadataFile));
const output = codegen(metadata);
await writeOutput(outputDir, output);
}

export async function writeOutput(outputDir: string, output: Files) {
const errors = [];
try {
await Deno.remove(outputDir, { recursive: true });
Expand Down Expand Up @@ -47,6 +51,7 @@ export function codegen(metadata: M.Metadata): Files {
"\n",
["import { ChainError, BitSequence, Era, $ } from", S.string(importSource)],
[`import * as _codec from "./codecs.ts"`],
[`export { _metadata }`],
],
});

Expand Down
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"Cargo.lock",
"deno.lock",
"frame_metadata/raw_erc20_metadata.json",
"target"
"target",
"**/__snapshots__/*.snap"
]
}
2 changes: 1 addition & 1 deletion deps/scale.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/[email protected].1/mod.ts";
export * from "https://deno.land/x/[email protected].2/mod.ts";
4 changes: 1 addition & 3 deletions frame_metadata/Codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export function DeriveCodec(tys: M.Ty[]): DeriveCodec {
const memberFields = fields.map((field, i) => {
return [
field.name || i,
$.deferred(() => {
return this.visit(field.ty);
}),
this.visit(field.ty),
] as [string, $.Codec<unknown>];
});
member = [type, ...memberFields];
Expand Down
Loading