Skip to content

Commit

Permalink
Test harness running in node
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Mar 13, 2021
1 parent 527ca58 commit a6f1dcb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/asbind-instance/asbind-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ async function compileStreaming(source) {
if (WebAssembly.compileStreaming) {
return WebAssembly.compileStreaming(source);
}
source = await Promise.resolve(souce);
if (source instanceof Response) {
source = await Promise.resolve(source);
if (typeof Response === "object" && source instanceof Response) {
source = await source.arrayBuffer();
}
return WebAssembly.compile(source);
Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm
29 changes: 22 additions & 7 deletions test/test-runner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const asc = require("assemblyscript/cli/asc");
const { promisify } = require("util");
const glob = promisify(require("glob"));
const fs = require("fs/promises");

const Mocha = require("mocha");
const assert = require("assert");
const glob = promisify(require("glob"));

const asc = require("assemblyscript/cli/asc");

globalThis.AsBind = require("../dist/as-bind.cjs.js");

async function main() {
await asc.ready;
Expand Down Expand Up @@ -31,20 +37,29 @@ async function compileAllAsc() {
async function runTestsInNode() {
const mocha = new Mocha();

mocha.globalSetup(() => {
console.log("GLOBAL SETUP");
});

const testFiles = await glob("./tests/**/test.js");
for (const testFile of testFiles) {
mocha.addFile(testFile);
}

mocha.globalSetup(() => {
this.assert = assert;
});

mocha.rootHooks({
async beforeEach() {
const { file } = this.currentTest;
const wasmFile = file.replace(/test\.js$/, "asc.wasm");
this.rawModule = await fs.readFile(wasmFile);
}
});

const failures = await runMochaAsync(mocha);
console.log({ failures });
}

function runMochaAsync(mocha) {
async function runMochaAsync(mocha) {
await mocha.loadFilesAsync();
return new Promise(resolve => {
const runner = mocha.run(resolve);
});
Expand Down
Binary file removed test/tests/arraybufferview/asc.wasm
Binary file not shown.
20 changes: 17 additions & 3 deletions test/tests/arraybufferview/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
describe("as-bind", function() {
it("should handle ArrayBufferViews", function(done) {
console.log("AAAH2");
done("NAH2");
it("should handle Uint8Arrays", async function() {
const instance = await AsBind.instantiate(this.rawModule, {
asc: {
imported(a, b) {
const result = new Uint8Array(a.length + b.length);
result.set(b, 0);
result.set(a, b.length);
return result;
}
}
});
assert.strictEqual(
instance.exports
.exported(new Uint8Array([1, 2, 3]), new Uint8Array([10, 11, 12]))
.join(","),
new Uint8Array([255, 10, 11, 12, 1, 2, 3, 255]).join(",")
);
});
});
Binary file removed test/tests/strings/asc.wasm
Binary file not shown.
12 changes: 9 additions & 3 deletions test/tests/strings/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
describe("as-bind", function() {
it("should handle strings", function(done) {
console.log("AAAH");
done("NAH");
it("should handle strings", async function() {
const instance = await AsBind.instantiate(this.rawModule, {
asc: {
imported(a, b) {
return b + a;
}
}
});
assert.strictEqual(instance.exports.exported("a", "b"), "!ba!");
});
});
9 changes: 8 additions & 1 deletion transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ class AsBindTransform extends Transform {
);
const importedFunctions = {};
for (const importedFunction of flatImportedFunctions) {
const moduleName = containingModule(importedFunction).internalName;
// To know under what module name an imported function will be expected,
// we have to find the containing module of the given function, take the
// internal name (which is effectively the file path without extension)
// and only take the part after the last `/`
// (i.e. the file name without extension).
const moduleName = containingModule(importedFunction)
.internalName.split("/")
.slice(-1)[0];
if (!importedFunctions.hasOwnProperty(moduleName)) {
importedFunctions[moduleName] = {};
}
Expand Down

0 comments on commit a6f1dcb

Please sign in to comment.