test: add more host function tests, cleanup tests to use wasm/code.wasm when possible (#219)

This commit is contained in:
zach
2023-01-19 08:09:16 -08:00
committed by GitHub
parent d73468a3ac
commit aa04fd3e5c
29 changed files with 270 additions and 42 deletions

Binary file not shown.

View File

@@ -10,6 +10,8 @@ std::vector<uint8_t> read(const char *filename) {
std::istreambuf_iterator<char>());
}
const std::string code = "../../wasm/code.wasm";
namespace {
using namespace extism;
@@ -20,7 +22,7 @@ TEST(Context, Basic) {
TEST(Plugin, Manifest) {
Context context;
Manifest manifest = Manifest::path("code.wasm");
Manifest manifest = Manifest::path(code);
manifest.set_config("a", "1");
ASSERT_NO_THROW(Plugin plugin = context.plugin(manifest));
@@ -38,7 +40,7 @@ TEST(Plugin, BadManifest) {
TEST(Plugin, Bytes) {
Context context;
auto wasm = read("code.wasm");
auto wasm = read(code.c_str());
ASSERT_NO_THROW(Plugin plugin = context.plugin(wasm));
Plugin plugin = context.plugin(wasm);
@@ -48,7 +50,7 @@ TEST(Plugin, Bytes) {
TEST(Plugin, UpdateConfig) {
Context context;
auto wasm = read("code.wasm");
auto wasm = read(code.c_str());
Plugin plugin = context.plugin(wasm);
Config config;
@@ -58,13 +60,34 @@ TEST(Plugin, UpdateConfig) {
TEST(Plugin, FunctionExists) {
Context context;
auto wasm = read("code.wasm");
auto wasm = read(code.c_str());
Plugin plugin = context.plugin(wasm);
ASSERT_FALSE(plugin.function_exists("bad_function"));
ASSERT_TRUE(plugin.function_exists("count_vowels"));
}
TEST(Plugin, HostFunction) {
Context context;
auto wasm = read("../../wasm/code-functions.wasm");
auto t = std::vector<ValType>{ValType::I64};
Function hello_world =
Function("hello_world", t, t,
[](CurrentPlugin plugin, const std::vector<Val> &params,
std::vector<Val> &results, void *user_data) {
auto offs = plugin.alloc(4);
memcpy(plugin.memory() + offs, "test", 4);
results[0].v.i64 = (int64_t)offs;
});
auto functions = std::vector<Function>{
hello_world,
};
Plugin plugin = context.plugin(wasm, true, functions);
auto buf = plugin.call("count_vowels", "aaa");
ASSERT_EQ(buf.length, 4);
ASSERT_EQ((std::string)buf, "test");
}
}; // namespace
int main(int argc, char **argv) {