Skip to content

Commit

Permalink
feat: support mock_input, update tests (#9)
Browse files Browse the repository at this point in the history
* feat: support mock_input, update tests

* fix: free memory

* fix: throw if no mock input data provided when requested

* ci: use xtp.toml to drive tests
  • Loading branch information
nilslice authored May 22, 2024
1 parent 306bef0 commit 8c891e1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ jobs:
run: cd examples/basic && npm i && npm run build

- name: Test example
run: xtp plugin test https://raw.githubusercontent.com/extism/extism/main/wasm/code.wasm --with examples/basic/test.wasm
run: |
# this is configured with the xtp.toml file in the root
xtp plugin test https://raw.githubusercontent.com/extism/extism/main/wasm/code.wasm
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ export class Test {
// call a function from the Extism plugin being tested, passing in `Input` and returning the output as a `ArrayBuffer`.
static callBuffer(funcName: string, input: Input): ArrayBuffer { ... }

// read the mock test input provided by the test runner, returns a `MemoryHandle`.
// this input is defined in an xtp.toml file, or by the --mock-input-data or --mock-input-file flags.
static mockInput(): MemoryHandle { ... }

// read the mock test input provided by the test runner, returns an `ArrayBuffer`.
// this input is defined in an xtp.toml file, or by the --mock-input-data or --mock-input-file flags.
static mockInputBuffer(): ArrayBuffer { ... }

// read the mock test input provided by the test runner, returns an `string`.
// this input is defined in an xtp.toml file, or by the --mock-input-data or --mock-input-file flags.
static mockInputString(): string { ... }

// Run a test group, resetting the plugin before and after the group is run.
static group(name: string, callback: () => void) { .. }

Expand Down Expand Up @@ -150,6 +162,7 @@ declare module "xtp:test" {
time(func: PTR, input: PTR): I64;
group(name: PTR): void;
reset(): void;
mock_input(): PTR;
}
}
```
Expand Down
3 changes: 3 additions & 0 deletions examples/basic/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Test } from "../../index.ts";

export function test() {
const notEmpty = Test.callString("count_vowels", Test.mockInputString());
Test.assertNotEqual("with mock, not empty", notEmpty, "");

Test.assertGreaterThan("gt test", 100, 1);
Test.assertLessThan("lt test", Number.MIN_VALUE, Number.MAX_VALUE);
Test.assertGreaterThanOrEqualTo("gte test", Math.PI, 3.14);
Expand Down
1 change: 1 addition & 0 deletions examples/basic/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ declare module "xtp:test" {
time(func: PTR, input: PTR): I64;
group(name: PTR): void;
reset(): void;
mock_input(): PTR;
}
}
Binary file modified examples/basic/test.wasm
Binary file not shown.
33 changes: 32 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare const Host: {
};

// @ts-ignore
const { call, time, assert, group, reset } = Host.getFunctions();
const { call, time, assert, group, reset, mock_input } = Host.getFunctions();

interface MemoryHandle {
offset: number;
Expand Down Expand Up @@ -61,6 +61,37 @@ export class Test {
return Memory.find(c);
}

// read the mock test input provided by the test runner, returns a `MemoryHandle`.
// this input is defined in an xtp.toml file, or by the --mock-input-data or --mock-input-file flags.
static mockInput(): MemoryHandle {
const offset = mock_input();
if (offset === 0) {
throw new Error(
"Failed to fetch mock input, not provided by test runner.",
);
}
// @ts-ignore: Memory
return Memory.find(offset);
}

// read the mock test input provided by the test runner, returns an `ArrayBuffer`.
// this input is defined in an xtp.toml file, or by the --mock-input-data or --mock-input-file flags.
static mockInputBuffer(): ArrayBuffer {
const inputMem = Test.mockInput();
const buf = inputMem.readBuffer();
inputMem.free();
return buf;
}

// read the mock test input provided by the test runner, returns an `string`.
// this input is defined in an xtp.toml file, or by the --mock-input-data or --mock-input-file flags.
static mockInputString(): string {
const inputMem = Test.mockInput();
const str = inputMem.readString();
inputMem.free();
return str;
}

// call a function from the Extism plugin being tested, passing in `Input` and get the number of nanoseconds spent in the function.
static timeNanoseconds(funcName: string, input: Input): number {
// @ts-ignore: Memory
Expand Down
1 change: 1 addition & 0 deletions test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ declare module "xtp:test" {
time(func: PTR, input: PTR): I64;
group(name: PTR): void;
reset(): void;
mock_input(): PTR;
}
}
11 changes: 11 additions & 0 deletions xtp.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[test]]
name = "basic"
build = "cd examples/basic && npm run build"
with = "examples/basic/test.wasm"
mock_input = { data = "this is my mock input data" }

[[test]]
name = "basic - file input"
build = "cd examples/basic && npm run build"
with = "examples/basic/test.wasm"
mock_input = { file = "examples/basic/index.ts" }

0 comments on commit 8c891e1

Please sign in to comment.