Skip to content

Commit

Permalink
Merge pull request #7 from dylibso/expand-tests
Browse files Browse the repository at this point in the history
feat: expand test coverage
  • Loading branch information
nilslice authored Aug 26, 2024
2 parents ee571db + 97749bf commit a3a3042
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 84 deletions.
2 changes: 0 additions & 2 deletions mock/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
build:
tinygo build -target wasi -o plugin.wasm main.go

test:
extism call plugin.wasm greet --input "world" --wasi
18 changes: 18 additions & 0 deletions mock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ func reflectByteBufferHost(kPtr uint64) uint64 {
return kRet.Offset()
}

//go:export noInputWithOutputHost
func noInputWithOutputHost() uint64 {
mem := pdk.AllocateString("noInputWithOutputHost")
return mem.Offset()
}

//go:export withInputNoOutputHost
func withInputNoOutputHost(ptr uint64) {
mem := pdk.FindMemory(ptr)
data := string(mem.ReadBytes())
if data != "42" { // JSON-encoded string value from the caller
panic("failed to read expected value")
}
}

//go:export noInputNoOutputHost
func noInputNoOutputHost() {}

func main() {}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions runner/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"@extism/js-pdk": "^1.0.0"
},
"dependencies": {
"@dylibso/xtp-test": "^0.0.8"
"@dylibso/xtp-test": "^0.0.9"
}
}
95 changes: 70 additions & 25 deletions runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,82 @@ const KitchenSink = {
};

export function test() {
let input = JSON.stringify(KitchenSink);
let output = JSON.parse(Test.callString("reflectJsonObject", input));
Test.group("schema v1-draft encoding types", () => {
let input = JSON.stringify(KitchenSink);
let output: typeof KitchenSink = Test.call("reflectJsonObject", input)
.json();

matchIdenticalTopLevel(output);
matchIdenticalEmbedded(output.anEmbeddedObject);
output.anEmbeddedObjectArray.forEach(matchIdenticalEmbedded);
matchIdenticalTopLevel(output);
matchIdenticalEmbedded(output.anEmbeddedObject);
output.anEmbeddedObjectArray.forEach(matchIdenticalEmbedded);

// dates and JSON encodings between languages are a little fuzzy.
// so, rather than test stringified equality, we test the value of
// the date in various forms.
matchDate(output);
// dates and JSON encodings between languages are a little fuzzy.
// so, rather than test stringified equality, we test the value of
// the date in various forms.
matchDate(output);

Test.assertEqual(
"reflectJsonObject preserved optional field semantics",
output.anOptionalString,
KitchenSink.anOptionalString,
);
Test.assertEqual(
"reflectJsonObject preserved optional field semantics",
output.anOptionalString,
KitchenSink.anOptionalString,
);

let inputS = KitchenSink.aString;
let outputS = Test.callString("reflectUtf8String", inputS);
Test.assertEqual("reflectUtf8String preserved the string", outputS, inputS);
let inputS = KitchenSink.aString;
let outputS = Test.call("reflectUtf8String", inputS).text();
Test.assertEqual("reflectUtf8String preserved the string", outputS, inputS);

let inputB = (new TextEncoder()).encode(KitchenSink.aString).buffer;
let outputB = Test.callBuffer("reflectByteBuffer", inputB);
let inputB = (new TextEncoder()).encode(KitchenSink.aString).buffer;
let outputB = Test.call("reflectByteBuffer", inputB).arrayBuffer();

// TODO compare the bytes
Test.assertEqual(
"reflectByteBuffer preserved the buffer length",
outputB.byteLength,
inputB.byteLength,
);
// TODO compare the bytes
Test.assertEqual(
"reflectByteBuffer preserved the buffer length",
outputB.byteLength,
inputB.byteLength,
);
});

Test.group("check signature and type variations", () => {
// should call a the `noInputWithOutputHost` host function passing it
// a string "noInputWithOutputHost" which it should return
let noInputWithOutputOutput = Test.call(
"noInputWithOutput",
undefined,
).text();
Test.assertEqual(
"noInputWithOutput returns expected output",
noInputWithOutputOutput,
"noInputWithOutputHost",
);

// should call the `withInputNoOutputHost` host function passing it
// JSON-encoded number 42, which the host function checks for and panics
// if it is not that JSON value
try {
let withInputNoOutputOutput = Test.call(
"withInputNoOutput",
JSON.stringify(42),
);
Test.assert(
"withInputNoOutput runs and returns no output",
withInputNoOutputOutput.isEmpty(),
`expected empty output, got: ${withInputNoOutputOutput.text()}`,
);
} catch (e: any) {
Test.assert(
"withInputNoOutput runs without panic",
false,
`host function (withInputNoOutputHost) panic with unexpected argument, must be JSON-encoded 42: ${e.message}`,
);
}

const noInputNoOutput = Test.call("noInputNoOutput", undefined);
Test.assert(
"noInputNoOutput is called successfully",
noInputNoOutput.isEmpty(),
`expected empty output, got: ${noInputNoOutput.text()}`,
);
});

return 0;
}
Expand Down
175 changes: 123 additions & 52 deletions schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ exports:
This function takes a KitchenSinkObject and returns a KitchenSinkObject.
It should come out the same way it came in.
codeSamples:
- lang: typescript
source: |
// pass this through the host function and return it back
return reflectJsonObjectHost(input)
- lang: go
source: |-
reflect, err := ReflectJsonObjectHost(input)
if err != nil {
return KitchenSinkObject{}, err
}
- lang: typescript
source: |
// pass this through the host function and return it back
return reflectJsonObjectHost(input)
- lang: go
source: |-
reflect, err := ReflectJsonObjectHost(input)
if err != nil {
return input, err
}
return *reflect, err
- lang: csharp
source: |-
return Host.ReflectJsonObjectHost(input);
- lang: zig
source: |
return Host.reflectJsonObjectHost(input);
return *reflect, err
- lang: csharp
source: |-
return Host.ReflectJsonObjectHost(input);
- lang: zig
source: |
return Host.reflectJsonObjectHost(input);
input:
contentType: application/json
$ref: "#/components/schemas/KitchenSinkObject"
Expand All @@ -34,23 +34,23 @@ exports:
This function takes a string and returns it.
Should come out the same way it came in.
codeSamples:
- lang: typescript
source: |
return reflectUtf8StringHost(input)
- lang: go
source: |-
reflect, err := ReflectUtf8StringHost(input)
if err != nil {
return "", err
}
- lang: typescript
source: |
return reflectUtf8StringHost(input)
- lang: go
source: |-
reflect, err := ReflectUtf8StringHost(input)
if err != nil {
return "", err
}
return *reflect, nil
- lang: csharp
source: |-
return Host.ReflectUtf8StringHost(input);
- lang: zig
source: |
return Host.reflectUtf8StringHost(input);
return *reflect, nil
- lang: csharp
source: |-
return Host.ReflectUtf8StringHost(input);
- lang: zig
source: |
return Host.reflectUtf8StringHost(input);
input:
type: string
description: The input string
Expand All @@ -64,23 +64,23 @@ exports:
This function takes a byte buffer and returns it.
Should come out the same way it came in.
codeSamples:
- lang: typescript
source: |
return reflectByteBufferHost(input)
- lang: go
source: |-
reflect, err := ReflectByteBufferHost(input)
if err != nil {
return nil, err
}
- lang: typescript
source: |
return reflectByteBufferHost(input)
- lang: go
source: |-
reflect, err := ReflectByteBufferHost(input)
if err != nil {
return nil, err
}
return reflect, nil
- lang: csharp
source: |-
return Host.ReflectByteBufferHost(input);
- lang: zig
source: |
return Host.reflectByteBufferHost(input);
return reflect, nil
- lang: csharp
source: |-
return Host.ReflectByteBufferHost(input);
- lang: zig
source: |
return Host.reflectByteBufferHost(input);
input:
contentType: application/x-binary
type: buffer
Expand All @@ -89,6 +89,61 @@ exports:
contentType: application/x-binary
type: buffer
description: The output byte buffer

noInputWithOutput:
description: a function that takes no input, but returns an output
output:
contentType: text/plain; charset=utf-8
type: string
codeSamples:
- lang: typescript
source: |
return noInputWithOutputHost();
- lang: zig
source: |-
return Host.noInputWithOutputHost();
- lang: go
source: |
output, err := NoInputWithOutputHost()
if err != nil {
return *output, err
}
return *output, nil
withInputNoOutput:
description: a function that takes input, but returns no output
input:
contentType: application/json
type: number
codeSamples:
- lang: typescript
source: |
return withInputNoOutputHost(input);
- lang: zig
source: |-
return Host.withInputNoOutputHost(input);
- lang: go
source: |
err := WithInputNoOutputHost(input)
if err != nil {
return err
}
return nil
noInputNoOutput:
description: a function that takes no input, and returns no output
codeSamples:
- lang: typescript
source: |-
noInputNoOutputHost();
- lang: zig
source: |-
return Host.noInputNoOutputHost();
- lang: go
source: |
return NoInputNoOutputHost()
imports:
reflectJsonObjectHost:
description: |
Expand Down Expand Up @@ -125,6 +180,22 @@ imports:
contentType: application/x-binary
type: buffer
description: The output byte buffer

noInputWithOutputHost:
description: a function that takes no input, but returns an output
output:
contentType: text/plain; charset=utf-8
type: string

withInputNoOutputHost:
description: a function that takes input, but returns no output
input:
contentType: application/json
type: number

noInputNoOutputHost:
description: a function that takes no input, and returns no output

components:
schemas:
EmbeddedObject:
Expand Down Expand Up @@ -158,9 +229,9 @@ components:
description: A string enum
type: string
enum:
- option1
- option2
- option3
- option1
- option2
- option3
KitchenSinkObject:
description: A json object with every type of property
properties:
Expand Down

0 comments on commit a3a3042

Please sign in to comment.