Skip to content

Commit fd3f328

Browse files
committed
unit test for issue 660
1 parent 36d918e commit fd3f328

File tree

3 files changed

+95
-62
lines changed

3 files changed

+95
-62
lines changed

tests/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ set(BT_TESTS
88
gtest_blackboard.cpp
99
gtest_coroutines.cpp
1010
gtest_decorator.cpp
11+
gtest_enums.cpp
1112
gtest_factory.cpp
1213
gtest_fallback.cpp
1314
gtest_parallel.cpp
1415
gtest_preconditions.cpp
16+
gtest_ports.cpp
1517
gtest_postconditions.cpp
1618
gtest_match.cpp
17-
gtest_ports.cpp
1819
gtest_json.cpp
1920
gtest_reactive.cpp
2021
gtest_sequence.cpp
@@ -56,7 +57,8 @@ else()
5657
find_package(GTest)
5758
enable_testing()
5859

59-
add_executable(${BTCPP_LIBRARY}_test ${BT_TESTS})
60+
add_executable(${BTCPP_LIBRARY}_test ${BT_TESTS}
61+
gtest_enums.cpp)
6062
target_link_libraries(${PROJECT_NAME}_test
6163
${TEST_DEPENDECIES}
6264
Threads::Threads

tests/gtest_enums.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <gtest/gtest.h>
2+
#include "behaviortree_cpp/bt_factory.h"
3+
4+
using namespace BT;
5+
6+
enum class Color
7+
{
8+
Red = 0,
9+
Blue = 1,
10+
Green = 2,
11+
Undefined
12+
};
13+
14+
static const char* ToStr(const Color& c)
15+
{
16+
switch (c) {
17+
case Color::Red: return "Red";
18+
case Color::Blue: return "Blue";
19+
case Color::Green: return "Green";
20+
case Color::Undefined: return "Undefined";
21+
}
22+
return nullptr;
23+
}
24+
25+
26+
class ActionEnum : public SyncActionNode
27+
{
28+
public:
29+
ActionEnum(const std::string& name, const NodeConfig& config) :
30+
SyncActionNode(name, config)
31+
{}
32+
33+
NodeStatus tick() override
34+
{
35+
getInput("color", color);
36+
std::cout << "Node: " << name() << " has color : " << ToStr(color) << std::endl;
37+
return NodeStatus::SUCCESS;
38+
}
39+
40+
static PortsList providedPorts()
41+
{
42+
return {BT::InputPort<Color>("color")};
43+
}
44+
45+
Color color = Color::Undefined;
46+
};
47+
48+
TEST(Enums, StrintToEnum)
49+
{
50+
std::string xml_txt = R"(
51+
<root BTCPP_format="4" >
52+
<BehaviorTree ID="Main">
53+
<Sequence>
54+
<Script code=" my_color := Red "/>
55+
<ActionEnum name="maybe_blue" color="Blue"/>
56+
<ActionEnum name="maybe_green" color="2"/>
57+
<ActionEnum name="maybe_red" color="{my_color}"/>
58+
</Sequence>
59+
</BehaviorTree>
60+
</root>)";
61+
62+
BehaviorTreeFactory factory;
63+
factory.registerNodeType<ActionEnum>("ActionEnum");
64+
factory.registerScriptingEnums<Color>();
65+
66+
auto tree = factory.createTreeFromText(xml_txt);
67+
68+
NodeStatus status = tree.tickWhileRunning();
69+
70+
ASSERT_EQ(status, NodeStatus::SUCCESS);
71+
72+
for(const auto& node: tree.subtrees.front()->nodes)
73+
{
74+
if(auto enum_node = dynamic_cast<ActionEnum*>(node.get()))
75+
{
76+
if(enum_node->name() == "maybe_red")
77+
{
78+
ASSERT_EQ(Color::Red, enum_node->color);
79+
}
80+
else if(enum_node->name() == "maybe_green")
81+
{
82+
ASSERT_EQ(Color::Green, enum_node->color);
83+
}
84+
else if(enum_node->name() == "maybe_blue")
85+
{
86+
ASSERT_EQ(Color::Blue, enum_node->color);
87+
}
88+
}
89+
}
90+
}
91+

tests/gtest_ports.cpp

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -231,64 +231,6 @@ TEST(PortTest, SubtreeStringInput_Issue489)
231231
ASSERT_EQ(7, states[1]);
232232
}
233233

234-
enum class Color
235-
{
236-
Red = 0,
237-
Blue = 1,
238-
Green = 2,
239-
Undefined
240-
};
241-
242-
class ActionEnum : public SyncActionNode
243-
{
244-
public:
245-
ActionEnum(const std::string& name, const NodeConfig& config) :
246-
SyncActionNode(name, config)
247-
{}
248-
249-
NodeStatus tick() override
250-
{
251-
getInput("color", color);
252-
return NodeStatus::SUCCESS;
253-
}
254-
255-
static PortsList providedPorts()
256-
{
257-
return {BT::InputPort<Color>("color")};
258-
}
259-
260-
Color color = Color::Undefined;
261-
};
262-
263-
TEST(PortTest, StrintToEnum)
264-
{
265-
std::string xml_txt = R"(
266-
<root BTCPP_format="4" >
267-
<BehaviorTree ID="Main">
268-
<Sequence>
269-
<ActionEnum color="Blue"/>
270-
<ActionEnum color="2"/>
271-
</Sequence>
272-
</BehaviorTree>
273-
</root>)";
274-
275-
BehaviorTreeFactory factory;
276-
factory.registerNodeType<ActionEnum>("ActionEnum");
277-
factory.registerScriptingEnums<Color>();
278-
279-
auto tree = factory.createTreeFromText(xml_txt);
280-
281-
NodeStatus status = tree.tickWhileRunning();
282-
283-
ASSERT_EQ(status, NodeStatus::SUCCESS);
284-
285-
auto first_node = dynamic_cast<ActionEnum*>(tree.subtrees.front()->nodes[1].get());
286-
auto second_node = dynamic_cast<ActionEnum*>(tree.subtrees.front()->nodes[2].get());
287-
288-
ASSERT_EQ(Color::Blue, first_node->color);
289-
ASSERT_EQ(Color::Green, second_node->color);
290-
}
291-
292234

293235
class DefaultTestAction : public SyncActionNode
294236
{
@@ -328,8 +270,6 @@ class DefaultTestAction : public SyncActionNode
328270
BT::InputPort<std::string>("greeting", "hello", "be polite"),
329271
BT::InputPort<Point2D>("pos", {1,2}, "where")};
330272
}
331-
332-
Color color = Color::Undefined;
333273
};
334274

335275
TEST(PortTest, DefaultInput)

0 commit comments

Comments
 (0)