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