Skip to content

Commit

Permalink
cleanup: rename MemoryData to MemoryView
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Aug 26, 2024
1 parent 44912aa commit 308062f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Test } from "@dylibso/xtp-test";

export function test() {
// call a function from some Extism plugin (you'll link these up in the CLI command to run the test),
// passing in some data and getting back `MemoryData`, which we convert to JSON using the `MemoryData.json`
// passing in some data and getting back `MemoryView`, which we convert to JSON using the `MemoryView.json`
// method
const res = Test.call("count_vowels", "some input").json();
const count = res["count"];
Expand Down Expand Up @@ -52,13 +52,13 @@ behavior.

```ts
export class Test {
// call a function from the Extism plugin being tested, passing in `Input` and returning the output as `MemoryData`, which
// call a function from the Extism plugin being tested, passing in `Input` and returning the output as `MemoryView`, which
// can be used to convert the type to a JavaScript native value.
static call(funcName: string, input: Input): MemoryData { ... }
static call(funcName: string, input: Input): MemoryView { ... }

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

// Run a test group, resetting the plugin before and after the group is run.
static group(name: string, callback: () => void) { .. }
Expand Down Expand Up @@ -102,12 +102,12 @@ export class Test {
type Input = string | ArrayBuffer | object | undefined;
```

`MemoryData` wraps an Extism memory handle, allowing you to convert between multiple types without dealing directly
`MemoryView` wraps an Extism memory handle, allowing you to convert between multiple types without dealing directly
with low-level memory access.

```ts
// Provides access to data in Extism memory
export class MemoryData {
export class MemoryView {
...

// Returns true if the underlying memory handle is empty or undefined.
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function test() {
const total = resetOutput["total"];
Test.assertEqual("reset plugin has vars cleared", total, 4);

// test using `MemoryData.arrayBuffer` to read an ArrayBuffer from Extism memory
// test using `MemoryView.arrayBuffer` to read an ArrayBuffer from Extism memory
const outputBytes = Test.call("count_vowels", "hello, world").arrayBuffer();
Test.assertEqual(
"bytes from output are expected length",
Expand Down
14 changes: 7 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface MemoryHandle {
}

// Provides access to data in Extism memory
export class MemoryData {
export class MemoryView {
memory: MemoryHandle | null = null;

constructor(memory: MemoryHandle | undefined) {
Expand Down Expand Up @@ -91,33 +91,33 @@ function convertInput(input: Input): MemoryHandle {
}

export class Test {
// call a function from the Extism plugin being tested, passing in `Input` and returning the output as `MemoryData`, which
// call a function from the Extism plugin being tested, passing in `Input` and returning the output as `MemoryView`, which
// can be used to convert the type to a JavaScript native value.
static call(
funcName: string,
input: Input,
): MemoryData {
): MemoryView {
// @ts-ignore: Memory
const a = Memory.fromString(funcName);
const b = convertInput(input);
const c = call(a.offset, b.offset);
a.free();
b.free();
// @ts-ignore: Memory
return new MemoryData(Memory.find(c));
return new MemoryView(Memory.find(c));
}

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

// call a function from the Extism plugin being tested, passing in `Input` and get the number of nanoseconds spent in the function.
Expand Down

0 comments on commit 308062f

Please sign in to comment.