From 585dad42e4e64985e999ec33e28b110e4063a434 Mon Sep 17 00:00:00 2001 From: zekexiao Date: Sat, 18 Oct 2025 20:07:52 +0800 Subject: [PATCH] add catch2 test --- CMakeLists.txt | 43 ++++++++++++++-------- example/Point.cpp | 5 --- example/Point.h => test/test_class.cpp | 38 ++++++++++++++------ test/test_engine.cpp | 50 ++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 30 deletions(-) delete mode 100644 example/Point.cpp rename example/Point.h => test/test_class.cpp (60%) create mode 100644 test/test_engine.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bbad3e3..660c7f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,33 +5,48 @@ project(mrubypp LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +option(MRUBYPP_BUILD_TEST "Build Catch2 Test" ON) + if (MSVC) add_compile_options("$<$:/utf-8>") add_compile_options("$<$:/utf-8>") add_compile_definitions(NOMINMAX) endif () -add_executable(mrubypp main.cpp +add_library(mrubypp INTERFACE mrubypp.h mrubypp_converters.h mrubypp_arena_guard.h - mrubypp_bind_class.h - example/Point.cpp - example/Point.h) + mrubypp_bind_class.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}) +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) install(TARGETS mrubypp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/example/Point.cpp b/example/Point.cpp deleted file mode 100644 index 075643d..0000000 --- a/example/Point.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by ZekeXiao on 2025/10/18. -// - -#include "Point.h" diff --git a/example/Point.h b/test/test_class.cpp similarity index 60% rename from example/Point.h rename to test/test_class.cpp index 26cd191..17e3087 100644 --- a/example/Point.h +++ b/test/test_class.cpp @@ -2,14 +2,9 @@ // Created by ZekeXiao on 2025/10/18. // -#ifndef MRUBYPP_POINT_H -#define MRUBYPP_POINT_H +#include -#include "mrubypp_converters.h" - -#include "mruby/data.h" - -#include "mrubypp_bind_class.h" +#include "mrubypp.h" class Point { public: @@ -39,7 +34,6 @@ private: template <> struct mrubypp_converter { static mrb_value to_mrb(mrb_state *mrb, const Point &var) { - // 创建一个新的 Point 对象数据结构 mrb_value obj = mrb_obj_value( mrb_data_object_alloc(mrb, mrb->object_class, new Point(var), &mrubypp_class_builder::data_type)); @@ -47,12 +41,34 @@ template <> struct mrubypp_converter { } static Point from_mrb(mrb_state *mrb, mrb_value value) { - // 从 mrb_value 中提取 Point 对象 if (mrb_type(value) == MRB_TT_DATA) { Point *point = static_cast(DATA_PTR(value)); return *point; } - return Point(0, 0); // 默认构造 + return Point(0, 0); } }; -#endif // MRUBYPP_POINT_H + +TEST_CASE("Point", "[class]") { + mrubypp engine; + engine.class_builder("Point") + .def_constructor() + .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("test_point"); + + REQUIRE(point_result.get_x() == 21); + REQUIRE(point_result.get_y() == 8); +} \ No newline at end of file diff --git a/test/test_engine.cpp b/test/test_engine.cpp new file mode 100644 index 0000000..a9ca097 --- /dev/null +++ b/test/test_engine.cpp @@ -0,0 +1,50 @@ +// +// Created by ZekeXiao on 2025/10/18. +// +// +// Created by ZekeXiao on 2025/10/18. +// + +#include + +#include "mrubypp.h" + +TEST_CASE("none args call", "[engine]") { + mrubypp engine; + engine.load(R"( + def get_1() + 1 + end + )"); + + auto b = engine.call("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 a{3, 1, 2}; + auto b = engine.call("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("get_same", 1); + REQUIRE(b == 1); + }; +} \ No newline at end of file