Skip to content

Commit

Permalink
[Test] Decouple plugin content and plugin test
Browse files Browse the repository at this point in the history
Signed-off-by: Shen-Ta Hsieh <[email protected]>
  • Loading branch information
ibmibmibm authored and hydai committed Sep 3, 2024
1 parent c096b24 commit 9fd25aa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
1 change: 0 additions & 1 deletion test/plugins/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ target_include_directories(wasmedgePluginUnittestsCPP

target_link_libraries(wasmedgePluginUnittestsCPP
PRIVATE
wasmedgePluginTestModuleCPP
${GTEST_BOTH_LIBRARIES}
)

Expand Down
6 changes: 3 additions & 3 deletions test/plugins/unittest/testplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace Host {

using namespace std::literals::string_view_literals;

WASMEDGE_EXPORT PO::List<std::string>
PO::List<std::string>
WasmEdgePluginTestEnv::CmdArgs(PO::Description("Test for args."sv),
PO::MetaVar("ARG"sv));

WASMEDGE_EXPORT PO::Option<std::string>
PO::Option<std::string>
WasmEdgePluginTestEnv::CmdName(PO::Description("Test for input name."sv),
PO::DefaultValue(std::string("")));

WASMEDGE_EXPORT PO::Option<PO::Toggle>
PO::Option<PO::Toggle>
WasmEdgePluginTestEnv::CmdOpt(PO::Description("Test for option."sv));

namespace {
Expand Down
6 changes: 3 additions & 3 deletions test/plugins/unittest/testplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class WasmEdgePluginTestEnv {
public:
WasmEdgePluginTestEnv() noexcept = default;

WASMEDGE_EXPORT static PO::List<std::string> CmdArgs;
WASMEDGE_EXPORT static PO::Option<std::string> CmdName;
WASMEDGE_EXPORT static PO::Option<PO::Toggle> CmdOpt;
static PO::List<std::string> CmdArgs;
static PO::Option<std::string> CmdName;
static PO::Option<PO::Toggle> CmdOpt;
};

template <typename T>
Expand Down
30 changes: 6 additions & 24 deletions test/plugins/unittest/unittest_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// SPDX-FileCopyrightText: 2019-2024 Second State INC

#include "common/defines.h"
#include "plugin/plugin.h"
#include "runtime/callingframe.h"
#include "runtime/instance/module.h"

#include "testplugin.h"

#include <algorithm>
#include <array>
#include <cstdint>
Expand All @@ -17,16 +16,6 @@

namespace {

template <typename T, typename U>
inline std::unique_ptr<T> dynamicPointerCast(std::unique_ptr<U> &&R) noexcept {
static_assert(std::has_virtual_destructor_v<T>);
T *P = dynamic_cast<T *>(R.get());
if (P) {
R.release();
}
return std::unique_ptr<T>(P);
}

std::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance> createModuleC() {
using namespace std::literals::string_view_literals;
WasmEdge::Plugin::Plugin::load(std::filesystem::u8path(
Expand All @@ -42,7 +31,7 @@ std::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance> createModuleC() {
return {};
}

std::unique_ptr<WasmEdge::Host::WasmEdgePluginTestModule> createModuleCPP() {
std::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance> createModuleCPP() {
using namespace std::literals::string_view_literals;
WasmEdge::Plugin::Plugin::load(std::filesystem::u8path(
"./" WASMEDGE_LIB_PREFIX
Expand All @@ -57,8 +46,7 @@ std::unique_ptr<WasmEdge::Host::WasmEdgePluginTestModule> createModuleCPP() {
Parser.set_raw_value("opt"sv);
if (const auto *Module =
Plugin->findModule("wasmedge_plugintest_cpp_module"sv)) {
return dynamicPointerCast<WasmEdge::Host::WasmEdgePluginTestModule>(
Module->create());
return Module->create();
}
}
return {};
Expand All @@ -78,9 +66,7 @@ TEST(wasmedgePluginTests, CPP_Run) {
auto *FuncInst1 = TestModCPP->findFuncExports("arg_len");
EXPECT_NE(FuncInst1, nullptr);
EXPECT_TRUE(FuncInst1->isHostFunction());
auto &HostFuncInst1 =
dynamic_cast<WasmEdge::Host::WasmEdgePluginTestFuncArgLen &>(
FuncInst1->getHostFunc());
auto &HostFuncInst1 = FuncInst1->getHostFunc();

// Test: Run function successfully.
EXPECT_TRUE(HostFuncInst1.run(CallFrame, {}, RetVal));
Expand All @@ -90,9 +76,7 @@ TEST(wasmedgePluginTests, CPP_Run) {
auto *FuncInst2 = TestModCPP->findFuncExports("name_size");
EXPECT_NE(FuncInst2, nullptr);
EXPECT_TRUE(FuncInst2->isHostFunction());
auto &HostFuncInst2 =
dynamic_cast<WasmEdge::Host::WasmEdgePluginTestFuncNameSize &>(
FuncInst2->getHostFunc());
auto &HostFuncInst2 = FuncInst2->getHostFunc();

// Test: Run function successfully.
EXPECT_TRUE(HostFuncInst2.run(CallFrame, {}, RetVal));
Expand All @@ -102,9 +86,7 @@ TEST(wasmedgePluginTests, CPP_Run) {
auto *FuncInst3 = TestModCPP->findFuncExports("opt");
EXPECT_NE(FuncInst3, nullptr);
EXPECT_TRUE(FuncInst3->isHostFunction());
auto &HostFuncInst3 =
dynamic_cast<WasmEdge::Host::WasmEdgePluginTestFuncOpt &>(
FuncInst3->getHostFunc());
auto &HostFuncInst3 = FuncInst3->getHostFunc();

// Test: Run function successfully.
EXPECT_TRUE(HostFuncInst3.run(CallFrame, {}, RetVal));
Expand Down

0 comments on commit 9fd25aa

Please sign in to comment.