add catch2 test
This commit is contained in:
@@ -5,33 +5,48 @@ project(mrubypp LANGUAGES CXX)
|
|||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
option(MRUBYPP_BUILD_TEST "Build Catch2 Test" ON)
|
||||||
|
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
|
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
|
||||||
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
|
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
|
||||||
add_compile_definitions(NOMINMAX)
|
add_compile_definitions(NOMINMAX)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
add_executable(mrubypp main.cpp
|
add_library(mrubypp INTERFACE
|
||||||
mrubypp.h
|
mrubypp.h
|
||||||
mrubypp_converters.h
|
mrubypp_converters.h
|
||||||
mrubypp_arena_guard.h
|
mrubypp_arena_guard.h
|
||||||
mrubypp_bind_class.h
|
mrubypp_bind_class.h)
|
||||||
example/Point.cpp
|
|
||||||
example/Point.h)
|
|
||||||
|
|
||||||
|
|
||||||
if (NOT mruby_ROOT)
|
|
||||||
message(NOTICE "[mrubypp] Using default mruby build")
|
|
||||||
set(mruby_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/deps/mruby")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_include_directories(mrubypp PUBLIC "${mruby_ROOT}/include")
|
|
||||||
target_link_directories(mrubypp PUBLIC "${mruby_ROOT}/lib")
|
|
||||||
target_link_libraries(mrubypp PUBLIC
|
|
||||||
libmruby ws2_32.lib wsock32.lib ws2_32.lib)
|
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_LIST_DIR})
|
include_directories(${CMAKE_CURRENT_LIST_DIR})
|
||||||
|
|
||||||
|
if (${MRUBYPP_BUILD_TEST})
|
||||||
|
if (NOT mruby_ROOT)
|
||||||
|
message(NOTICE "[mrubypp] Using default mruby build")
|
||||||
|
set(mruby_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/deps/mruby")
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
|
||||||
|
add_subdirectory(deps/Catch2)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(mrubypp_test
|
||||||
|
test/test_class.cpp
|
||||||
|
test/test_engine.cpp)
|
||||||
|
|
||||||
|
|
||||||
|
target_include_directories(mrubypp_test PUBLIC "${mruby_ROOT}/include")
|
||||||
|
target_link_directories(mrubypp_test PUBLIC "${mruby_ROOT}/lib")
|
||||||
|
target_link_libraries(mrubypp_test PUBLIC
|
||||||
|
libmruby ws2_32.lib wsock32.lib ws2_32.lib)
|
||||||
|
|
||||||
|
target_link_libraries(mrubypp_test PRIVATE mrubypp Catch2::Catch2WithMain)
|
||||||
|
add_test(NAME mrubypp_test COMMAND mrubypp_test)
|
||||||
|
endif ()
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
install(TARGETS mrubypp
|
install(TARGETS mrubypp
|
||||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by ZekeXiao on 2025/10/18.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "Point.h"
|
|
||||||
@@ -2,14 +2,9 @@
|
|||||||
// Created by ZekeXiao on 2025/10/18.
|
// Created by ZekeXiao on 2025/10/18.
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef MRUBYPP_POINT_H
|
#include <catch2/catch_all.hpp>
|
||||||
#define MRUBYPP_POINT_H
|
|
||||||
|
|
||||||
#include "mrubypp_converters.h"
|
#include "mrubypp.h"
|
||||||
|
|
||||||
#include "mruby/data.h"
|
|
||||||
|
|
||||||
#include "mrubypp_bind_class.h"
|
|
||||||
|
|
||||||
class Point {
|
class Point {
|
||||||
public:
|
public:
|
||||||
@@ -39,7 +34,6 @@ private:
|
|||||||
|
|
||||||
template <> struct mrubypp_converter<Point> {
|
template <> struct mrubypp_converter<Point> {
|
||||||
static mrb_value to_mrb(mrb_state *mrb, const Point &var) {
|
static mrb_value to_mrb(mrb_state *mrb, const Point &var) {
|
||||||
// 创建一个新的 Point 对象数据结构
|
|
||||||
mrb_value obj = mrb_obj_value(
|
mrb_value obj = mrb_obj_value(
|
||||||
mrb_data_object_alloc(mrb, mrb->object_class, new Point(var),
|
mrb_data_object_alloc(mrb, mrb->object_class, new Point(var),
|
||||||
&mrubypp_class_builder<Point>::data_type));
|
&mrubypp_class_builder<Point>::data_type));
|
||||||
@@ -47,12 +41,34 @@ template <> struct mrubypp_converter<Point> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Point from_mrb(mrb_state *mrb, mrb_value value) {
|
static Point from_mrb(mrb_state *mrb, mrb_value value) {
|
||||||
// 从 mrb_value 中提取 Point 对象
|
|
||||||
if (mrb_type(value) == MRB_TT_DATA) {
|
if (mrb_type(value) == MRB_TT_DATA) {
|
||||||
Point *point = static_cast<Point *>(DATA_PTR(value));
|
Point *point = static_cast<Point *>(DATA_PTR(value));
|
||||||
return *point;
|
return *point;
|
||||||
}
|
}
|
||||||
return Point(0, 0); // 默认构造
|
return Point(0, 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#endif // MRUBYPP_POINT_H
|
|
||||||
|
TEST_CASE("Point", "[class]") {
|
||||||
|
mrubypp engine;
|
||||||
|
engine.class_builder<Point>("Point")
|
||||||
|
.def_constructor<int, int>()
|
||||||
|
.def_method("add", &Point::add)
|
||||||
|
.def_class_method("none", &Point::none)
|
||||||
|
.def_property("x", &Point::get_x, &Point::set_x);
|
||||||
|
|
||||||
|
engine.load(R"(
|
||||||
|
def test_point()
|
||||||
|
p = Point.new(3, 4)
|
||||||
|
p.x = 10
|
||||||
|
p.add(p)
|
||||||
|
p.x += Point::none()
|
||||||
|
return p
|
||||||
|
end
|
||||||
|
)");
|
||||||
|
|
||||||
|
auto point_result = engine.call<Point>("test_point");
|
||||||
|
|
||||||
|
REQUIRE(point_result.get_x() == 21);
|
||||||
|
REQUIRE(point_result.get_y() == 8);
|
||||||
|
}
|
||||||
50
test/test_engine.cpp
Normal file
50
test/test_engine.cpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
//
|
||||||
|
// Created by ZekeXiao on 2025/10/18.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by ZekeXiao on 2025/10/18.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <catch2/catch_all.hpp>
|
||||||
|
|
||||||
|
#include "mrubypp.h"
|
||||||
|
|
||||||
|
TEST_CASE("none args call", "[engine]") {
|
||||||
|
mrubypp engine;
|
||||||
|
engine.load(R"(
|
||||||
|
def get_1()
|
||||||
|
1
|
||||||
|
end
|
||||||
|
)");
|
||||||
|
|
||||||
|
auto b = engine.call<int>("get_1");
|
||||||
|
REQUIRE(b == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("args call", "[engine]") {
|
||||||
|
mrubypp engine;
|
||||||
|
engine.load(R"(
|
||||||
|
def add(a)
|
||||||
|
a.sort!
|
||||||
|
a[0]
|
||||||
|
end
|
||||||
|
)");
|
||||||
|
|
||||||
|
std::vector<int> a{3, 1, 2};
|
||||||
|
auto b = engine.call<int>("add", a);
|
||||||
|
REQUIRE(b == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("call benchmark", "[!benchmark]") {
|
||||||
|
mrubypp engine;
|
||||||
|
engine.load(R"(
|
||||||
|
def get_same(a)
|
||||||
|
return a
|
||||||
|
end
|
||||||
|
)");
|
||||||
|
|
||||||
|
BENCHMARK("call and return") {
|
||||||
|
auto b = engine.call<int>("get_same", 1);
|
||||||
|
REQUIRE(b == 1);
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user