Skip to content

Commit

Permalink
Merge pull request #1 from dylibso/add-some-more-tests
Browse files Browse the repository at this point in the history
Add some more tests
  • Loading branch information
bhelx authored Jul 18, 2024
2 parents 88cb8ac + df3aecc commit d3d5c1b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
28 changes: 26 additions & 2 deletions mock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,39 @@ import (
"github.com/extism/go-pdk"
)

//go:export reflectObjectHost
func reflectObjectHost(kPtr uint64) uint64 {
//go:export reflectJsonObjectHost
func reflectJsonObjectHost(kPtr uint64) uint64 {
kMem := pdk.FindMemory(kPtr)
k := string(kMem.ReadBytes())

// TODO should validate that we get json by trying to parse it
fmt.Println(k)

kRet := pdk.AllocateString(k)
return kRet.Offset()
}

//go:export reflectUtf8StringHost
func reflectUtf8StringHost(kPtr uint64) uint64 {
kMem := pdk.FindMemory(kPtr)
k := string(kMem.ReadBytes())

// TODO should validate that we get utf8 string
fmt.Println(k)

kRet := pdk.AllocateString(k)
return kRet.Offset()
}

//go:export reflectByteBufferHost
func reflectByteBufferHost(kPtr uint64) uint64 {
kMem := pdk.FindMemory(kPtr)
k := kMem.ReadBytes()

// TODO should validate that we get bytes somehow?

kRet := pdk.AllocateBytes(k)
return kRet.Offset()
}

func main() {}
17 changes: 15 additions & 2 deletions runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ const KitchenSink = {

export function test() {
let input = JSON.stringify(KitchenSink)
let output = JSON.parse(Test.callString("reflectObject", input))
let output = JSON.parse(Test.callString("reflectJsonObject", input))
// assuming if we re-stringify them here the formatting should be the same
Test.assertEqual("reflectObject preserved the KitchenSink", JSON.stringify(output), JSON.stringify(KitchenSink))
Test.assertEqual("reflectJsonObject preserved the KitchenSink JSON object", JSON.stringify(output), JSON.stringify(KitchenSink))

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

let inputB = (new TextEncoder()).encode(KitchenSink.aString).buffer
let outputBs = Test.call("reflectByteBuffer", inputB)
// @ts-ignore TODO fix this when new xtp-test-js is pushed
let outputB = outputBs.readBytes()

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

return 0;
}
58 changes: 55 additions & 3 deletions schema.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
version: v1-draft
exports:
- name: reflectObject
- name: reflectJsonObject
description: |
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 reflectObjectHost(input)
return reflectJsonObjectHost(input)
input:
$ref: "#/schemas/KitchenSinkObject"
output:
$ref: "#/schemas/KitchenSinkObject"
- name: reflectUtf8String
description: |
This function takes a string and returns it.
Should come out the same way it came in.
codeSamples:
- lang: typescript
source: |
return reflectUtf8StringHost(input)
input:
type: string
description: The input string
contentType: text/plain; charset=utf-8
output:
type: string
description: The output string
contentType: text/plain; charset=utf-8
- name: reflectByteBuffer
description: |
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)
input:
type: buffer
description: The input byte buffer
output:
type: buffer
description: The output byte buffer
imports:
- name: reflectObjectHost
- name: reflectJsonObjectHost
description: |
This function takes a KitchenSinkObject and returns a KitchenSinkObject.
It should come out the same way it came in. It's the same as the export.
Expand All @@ -23,6 +53,28 @@ imports:
$ref: "#/schemas/KitchenSinkObject"
output:
$ref: "#/schemas/KitchenSinkObject"
- name: reflectUtf8StringHost
description: |
This function takes a string and returns it.
Should come out the same way it came in. Same as export.
input:
type: string
description: The input string
contentType: text/plain; charset=utf-8
output:
type: string
description: The output string
contentType: text/plain; charset=utf-8
- name: reflectByteBufferHost
description: |
This function takes a bugger and returns it.
Should come out the same way it came in. Same as export.
input:
type: buffer
description: The input byte buffer
output:
type: buffer
description: The output byte buffer
schemas:
- name: EmbeddedObject
description: An embedded object, has some arrays too
Expand Down

0 comments on commit d3d5c1b

Please sign in to comment.