194 lines
7.2 KiB
C++
194 lines
7.2 KiB
C++
// P4-26 官方 OBJ 插件写能力测试(format:obj.write)。
|
||
//
|
||
// 覆盖:
|
||
// 1) 发现/加载:com.vde.official.obj 具备 format:obj.read + format:obj.write;
|
||
// 2) 读→写→读往返:OBJ 四边形导入 → host tessellate → o/v/f(1-based)
|
||
// 写出 → 重新导入成功;
|
||
// 3) 多实体写出:两个 box 实体 → 两个 o body_N 组;
|
||
// 4) 空实体导出失败、非法输出路径失败(write_memory/file 一致性由 fake-host harness 覆盖)。
|
||
//
|
||
// 注:shared CMake 由 Lead 注册;本文件按现有官方插件测试模式编写。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cstdint>
|
||
#include <filesystem>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/plugin/geometry_host_services.h"
|
||
#include "vde/plugin/plugin_manager.h"
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
#ifndef VDE_PLUGIN_OFFICIAL_DIR
|
||
#define VDE_PLUGIN_OFFICIAL_DIR ""
|
||
#endif
|
||
#ifndef VDE_PLUGIN_SAMPLE_DIR
|
||
#define VDE_PLUGIN_SAMPLE_DIR ""
|
||
#endif
|
||
#ifndef VDE_TEST_TEMP_DIR
|
||
#define VDE_TEST_TEMP_DIR ""
|
||
#endif
|
||
|
||
namespace {
|
||
|
||
using vde::plugin::FormatImportResult;
|
||
using vde::plugin::PluginManager;
|
||
|
||
constexpr const char* kObjPluginId = "com.vde.official.obj";
|
||
|
||
std::string temp_path(const char* name) {
|
||
const std::string dir = VDE_TEST_TEMP_DIR[0] != '\0'
|
||
? std::string(VDE_TEST_TEMP_DIR)
|
||
: (std::filesystem::temp_directory_path().string() + "/");
|
||
return dir + name;
|
||
}
|
||
|
||
void write_text(const std::string& path, const std::string& text) {
|
||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||
ASSERT_TRUE(out.good()) << path;
|
||
out << text;
|
||
}
|
||
|
||
std::string read_text(const std::string& path) {
|
||
std::ifstream in(path, std::ios::binary);
|
||
return std::string((std::istreambuf_iterator<char>(in)),
|
||
std::istreambuf_iterator<char>());
|
||
}
|
||
|
||
std::size_t count_prefix_lines(const std::string& text, const std::string& prefix) {
|
||
std::size_t count = 0;
|
||
std::size_t pos = 0;
|
||
while (pos < text.size()) {
|
||
std::size_t end = text.find('\n', pos);
|
||
if (end == std::string::npos) end = text.size();
|
||
if (text.compare(pos, prefix.size(), prefix) == 0) ++count;
|
||
pos = end + 1;
|
||
}
|
||
return count;
|
||
}
|
||
|
||
class ObjWriteFixture : public ::testing::Test {
|
||
protected:
|
||
void SetUp() override {
|
||
host_ = vde::plugin::make_geometry_host_services();
|
||
PluginManager& manager = PluginManager::instance();
|
||
manager.clear();
|
||
manager.set_host_services(host_);
|
||
manager.clear_search_paths();
|
||
if (VDE_PLUGIN_OFFICIAL_DIR[0] != '\0')
|
||
manager.add_search_path(VDE_PLUGIN_OFFICIAL_DIR);
|
||
if (VDE_PLUGIN_SAMPLE_DIR[0] != '\0')
|
||
manager.add_search_path(VDE_PLUGIN_SAMPLE_DIR);
|
||
manager.scan_search_paths();
|
||
}
|
||
void TearDown() override {
|
||
for (const auto& path : files_) std::filesystem::remove(path);
|
||
PluginManager::instance().clear();
|
||
vde::plugin::release_geometry_host_services(&host_);
|
||
}
|
||
void remember(const std::string& path) { files_.push_back(path); }
|
||
|
||
bool load_obj(std::string* error = nullptr) {
|
||
PluginManager& manager = PluginManager::instance();
|
||
if (manager.is_loaded(kObjPluginId)) return true;
|
||
return manager.load(kObjPluginId, error);
|
||
}
|
||
|
||
VdePluginHost host_{};
|
||
std::vector<std::string> files_;
|
||
};
|
||
|
||
TEST_F(ObjWriteFixture, DiscoveredWithReadWriteCapabilities) {
|
||
ASSERT_TRUE(load_obj()) << "OBJ plugin not discovered/loaded";
|
||
EXPECT_TRUE(PluginManager::instance().is_loaded(kObjPluginId));
|
||
const auto manifest = PluginManager::instance().manifest(kObjPluginId);
|
||
ASSERT_TRUE(manifest.has_value());
|
||
bool read_capability = false;
|
||
bool write_capability = false;
|
||
for (const auto& capability : manifest->capabilities) {
|
||
if (capability == "format:obj.read") read_capability = true;
|
||
if (capability == "format:obj.write") write_capability = true;
|
||
}
|
||
EXPECT_TRUE(read_capability);
|
||
EXPECT_TRUE(write_capability);
|
||
bool format_present = false;
|
||
for (const auto& format : PluginManager::instance().formats()) {
|
||
if (format.plugin_id == kObjPluginId && format.format_id == "obj")
|
||
format_present = true;
|
||
}
|
||
EXPECT_TRUE(format_present);
|
||
}
|
||
|
||
TEST_F(ObjWriteFixture, ReadWriteReadRoundTrip) {
|
||
ASSERT_TRUE(load_obj());
|
||
const std::string input = temp_path("vde_obj_write_input.obj");
|
||
const std::string output = temp_path("vde_obj_write_output.obj");
|
||
remember(input);
|
||
remember(output);
|
||
write_text(input,
|
||
"# VDE OBJ write test\n"
|
||
"v 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\n"
|
||
"f 1 2 3 4\n");
|
||
FormatImportResult imported;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(input, &imported))
|
||
<< PluginManager::instance().last_error();
|
||
ASSERT_EQ(imported.bodies.size(), 1u);
|
||
ASSERT_TRUE(PluginManager::instance().export_file(output, imported.bodies, "obj"))
|
||
<< PluginManager::instance().last_error();
|
||
const std::string text = read_text(output);
|
||
EXPECT_GE(count_prefix_lines(text, "o body_"), 1u);
|
||
EXPECT_GE(count_prefix_lines(text, "v "), 4u);
|
||
EXPECT_GE(count_prefix_lines(text, "f "), 2u);
|
||
|
||
FormatImportResult reimported;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(output, &reimported))
|
||
<< PluginManager::instance().last_error();
|
||
EXPECT_EQ(reimported.format_id, "obj");
|
||
ASSERT_EQ(reimported.bodies.size(), 1u);
|
||
}
|
||
|
||
TEST_F(ObjWriteFixture, ExportsMultipleBodiesAsGroups) {
|
||
ASSERT_TRUE(load_obj());
|
||
const VdePluginHost& host = PluginManager::instance().host_services();
|
||
ASSERT_TRUE(host.body_create_box != nullptr);
|
||
const VdeBody box_a = host.body_create_box(1.0, 1.0, 1.0);
|
||
const VdeBody box_b = host.body_create_box(2.0, 2.0, 2.0);
|
||
ASSERT_NE(box_a, nullptr);
|
||
ASSERT_NE(box_b, nullptr);
|
||
const std::string output = temp_path("vde_obj_write_multi.obj");
|
||
remember(output);
|
||
std::vector<VdeBody> bodies{box_a, box_b};
|
||
const bool exported = PluginManager::instance().export_file(output, bodies, "obj");
|
||
if (host.body_destroy) {
|
||
host.body_destroy(box_a);
|
||
host.body_destroy(box_b);
|
||
}
|
||
ASSERT_TRUE(exported) << PluginManager::instance().last_error();
|
||
const std::string text = read_text(output);
|
||
EXPECT_GE(count_prefix_lines(text, "o body_"), 2u);
|
||
EXPECT_GE(count_prefix_lines(text, "f "), 12u);
|
||
}
|
||
|
||
TEST_F(ObjWriteFixture, EmptyBodiesRejected) {
|
||
ASSERT_TRUE(load_obj());
|
||
const std::string output = temp_path("vde_obj_write_empty.obj");
|
||
remember(output);
|
||
EXPECT_FALSE(PluginManager::instance().export_file(output, {}, "obj"));
|
||
EXPECT_FALSE(PluginManager::instance().last_error().empty());
|
||
}
|
||
|
||
TEST_F(ObjWriteFixture, InvalidOutputPathRejected) {
|
||
ASSERT_TRUE(load_obj());
|
||
const VdePluginHost& host = PluginManager::instance().host_services();
|
||
const VdeBody box = host.body_create_box(1.0, 1.0, 1.0);
|
||
ASSERT_NE(box, nullptr);
|
||
const std::string output = temp_path("vde_obj_write_missing_dir/sub/out.obj");
|
||
const bool exported = PluginManager::instance().export_file(output, {box}, "obj");
|
||
if (host.body_destroy) host.body_destroy(box);
|
||
EXPECT_FALSE(exported);
|
||
EXPECT_FALSE(PluginManager::instance().last_error().empty());
|
||
}
|
||
|
||
} // namespace
|