Repository navigation
Expand file tree
/
Copy pathgtest_plugin_issue953.cpp
More file actions
137 lines (111 loc) · 4.07 KB
/
Copy pathgtest_plugin_issue953.cpp
File metadata and controls
137 lines (111 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Test for Issue #953: convertFromString specialization in plugins not visible
* to main application.
*
* This test loads a plugin (plugin_issue953.so) that defines:
* - A custom type (Issue953Type)
* - The convertFromString<Issue953Type> specialization (ONLY in the plugin)
* - An action node that uses getInput<Issue953Type>()
*
* The key point: this test file does NOT have access to the convertFromString
* specialization. Before the fix, getInput() would fail. After the fix, it
* works because the StringConverter is stored in PortInfo.
*/
#include "behaviortree_cpp/bt_factory.h"
#include <filesystem>
#include <gtest/gtest.h>
using namespace BT;
// Plugin path is defined at compile time via CMake
#ifndef BT_PLUGIN_ISSUE953_PATH
#define BT_PLUGIN_ISSUE953_PATH "plugin_issue953.so"
#endif
class PluginIssue953Test : public testing::Test
{
protected:
void SetUp() override
{
plugin_path_ = BT_PLUGIN_ISSUE953_PATH;
if(!std::filesystem::exists(plugin_path_))
{
GTEST_SKIP() << "Plugin not found at: " << plugin_path_ << ". "
<< "Make sure it's built before running this test.";
}
}
std::string plugin_path_;
};
// Test that getInput works for a custom type defined only in the plugin
TEST_F(PluginIssue953Test, GetInputUsesStoredConverter)
{
// This XML uses a literal string value for the input port
const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Issue953Action input="42;hello_world;3.14159"/>
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
// Load the plugin - this registers Issue953Action
// The plugin has the convertFromString<Issue953Type> specialization,
// but THIS file does not.
factory.registerFromPlugin(plugin_path_);
auto tree = factory.createTreeFromText(xml_text);
// This should work because:
// 1. InputPort<Issue953Type>() was called in the plugin
// 2. GetAnyFromStringFunctor captured convertFromString at that point
// 3. The fix makes getInput() use that stored converter
auto status = tree.tickWhileRunning();
ASSERT_EQ(status, NodeStatus::SUCCESS);
// Verify the parsed values via output ports
auto bb = tree.rootBlackboard();
EXPECT_EQ(bb->get<int>("out_id"), 42);
EXPECT_EQ(bb->get<std::string>("out_name"), "hello_world");
EXPECT_DOUBLE_EQ(bb->get<double>("out_value"), 3.14159);
}
// Test with blackboard - value stored as string, then parsed on read
TEST_F(PluginIssue953Test, GetInputFromBlackboardString)
{
const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Sequence>
<Script code="my_data := '99;from_script;2.718'" />
<Issue953Action input="{my_data}"/>
</Sequence>
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
factory.registerFromPlugin(plugin_path_);
auto tree = factory.createTreeFromText(xml_text);
auto status = tree.tickWhileRunning();
ASSERT_EQ(status, NodeStatus::SUCCESS);
auto bb = tree.rootBlackboard();
EXPECT_EQ(bb->get<int>("out_id"), 99);
EXPECT_EQ(bb->get<std::string>("out_name"), "from_script");
EXPECT_DOUBLE_EQ(bb->get<double>("out_value"), 2.718);
}
// Test with SubTree port remapping
TEST_F(PluginIssue953Test, GetInputViaSubtreeRemapping)
{
const char* xml_text = R"(
<root BTCPP_format="4" main_tree_to_execute="MainTree">
<BehaviorTree ID="MainTree">
<SubTree ID="Issue953SubTree" data="123;subtree_test;1.5"/>
</BehaviorTree>
<BehaviorTree ID="Issue953SubTree">
<Issue953Action input="{data}"/>
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
factory.registerFromPlugin(plugin_path_);
auto tree = factory.createTreeFromText(xml_text);
auto status = tree.tickWhileRunning();
ASSERT_EQ(status, NodeStatus::SUCCESS);
// Get the subtree's blackboard to check output
auto subtree_bb = tree.subtrees[1]->blackboard;
EXPECT_EQ(subtree_bb->get<int>("out_id"), 123);
EXPECT_EQ(subtree_bb->get<std::string>("out_name"), "subtree_test");
EXPECT_DOUBLE_EQ(subtree_bb->get<double>("out_value"), 1.5);
}