Skip to content

Commit 4e85b9e

Browse files
Add a test
1 parent b390344 commit 4e85b9e

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

Diff for: crates/c-api/tests/CMakeLists.txt

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ FetchContent_MakeAvailable(googletest)
99

1010
include(GoogleTest)
1111

12-
function(add_capi_test name)
13-
add_executable(test-${name} ${name}.cc)
12+
function(add_capi_test name path)
13+
add_executable(test-${name} ${path})
1414
target_link_libraries(test-${name} PRIVATE wasmtime-cpp gtest_main)
1515
gtest_discover_tests(test-${name})
1616
endfunction()
1717

18-
add_capi_test(simple)
19-
add_capi_test(types)
20-
add_capi_test(func)
18+
add_capi_test(simple simple.cc)
19+
add_capi_test(types types.cc)
20+
add_capi_test(func func.cc)
21+
add_capi_test(component-instantiate component/instantiate.cc)
2122

2223
# Add a custom test where two files include `wasmtime.hh` and are compiled into
2324
# the same executable (basically makes sure any defined functions in the header

Diff for: crates/c-api/tests/component/instantiate.cc

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <gtest/gtest.h>
2+
#include <wasmtime.h>
3+
4+
TEST(component, instantiate) {
5+
static constexpr auto bytes = std::string_view{
6+
R"END(
7+
(component
8+
(core module)
9+
)
10+
)END",
11+
};
12+
13+
const auto engine = wasm_engine_new();
14+
EXPECT_NE(engine, nullptr);
15+
16+
const auto store = wasmtime_store_new(engine, nullptr, nullptr);
17+
EXPECT_NE(store, nullptr);
18+
const auto context = wasmtime_store_context(store);
19+
EXPECT_NE(context, nullptr);
20+
21+
wasmtime_component_t *component = nullptr;
22+
23+
auto error = wasmtime_component_new(
24+
engine, reinterpret_cast<const uint8_t *>(bytes.data()), bytes.size(),
25+
&component);
26+
27+
EXPECT_EQ(error, nullptr);
28+
EXPECT_NE(component, nullptr);
29+
30+
const auto linker = wasmtime_component_linker_new(engine);
31+
EXPECT_NE(linker, nullptr);
32+
33+
wasmtime_component_linker_instance_t *linker_instance = nullptr;
34+
error = wasmtime_component_linker_instance(linker, "a:b/c", &linker_instance);
35+
36+
EXPECT_EQ(error, nullptr);
37+
EXPECT_NE(linker_instance, nullptr);
38+
39+
wasmtime_component_linker_instance_delete(linker_instance);
40+
41+
wasmtime_component_instance_t *instance = nullptr;
42+
error = wasmtime_component_linker_instantiate(linker, context, component,
43+
&instance);
44+
EXPECT_EQ(error, nullptr);
45+
EXPECT_NE(instance, nullptr);
46+
47+
wasmtime_component_instance_delete(instance);
48+
49+
wasmtime_component_linker_delete(linker);
50+
51+
wasmtime_store_delete(store);
52+
wasm_engine_delete(engine);
53+
}

0 commit comments

Comments
 (0)