11#include < gtest/gtest.h>
22#include " behaviortree_cpp/bt_factory.h"
3+ #include " behaviortree_cpp/xml_parsing.h"
4+ #include " behaviortree_cpp/json_export.h"
35
46using namespace BT ;
57
@@ -274,6 +276,11 @@ struct Point2D {
274276template <> [[nodiscard]]
275277Point2D BT::convertFromString<Point2D>(StringView str)
276278{
279+ if (StartWith (str, " json:" ))
280+ {
281+ str.remove_prefix (5 );
282+ return convertFromJSON<Point2D>(str);
283+ }
277284 const auto parts = BT::splitString (str, ' ,' );
278285 if (parts.size () != 2 )
279286 {
@@ -284,6 +291,18 @@ Point2D BT::convertFromString<Point2D>(StringView str)
284291 return {x, y};
285292}
286293
294+ template <> [[nodiscard]]
295+ std::string BT::toStr<Point2D>(const Point2D& point)
296+ {
297+ return std::to_string (point.x ) + " ," + std::to_string (point.y );
298+ }
299+
300+ BT_JSON_CONVERTER (Point2D, point)
301+ {
302+ add_field (" x" , &point.x );
303+ add_field (" y" , &point.y );
304+ }
305+
287306
288307class DefaultTestAction : public SyncActionNode
289308{
@@ -428,20 +447,24 @@ class NodeWithDefaultPoints : public SyncActionNode
428447
429448 NodeStatus tick () override
430449 {
431- Point2D vectA, vectB, vectC, vectD, input;
432- if (!getInput (" pointA" , vectA) || vectA != Point2D{1 , 2 }) {
450+ Point2D pointA, pointB, pointC, pointD, pointE, input;
451+
452+ if (!getInput (" pointA" , pointA) || pointA != Point2D{1 , 2 }) {
433453 throw std::runtime_error (" failed pointA" );
434454 }
435- if (!getInput (" pointB" , vectB ) || vectB != Point2D{3 , 4 }) {
455+ if (!getInput (" pointB" , pointB ) || pointB != Point2D{3 , 4 }) {
436456 throw std::runtime_error (" failed pointB" );
437457 }
438- if (!getInput (" pointC" , vectC ) || vectC != Point2D{5 , 6 }) {
458+ if (!getInput (" pointC" , pointC ) || pointC != Point2D{5 , 6 }) {
439459 throw std::runtime_error (" failed pointC" );
440460 }
441- if (!getInput (" pointD" , vectD) || vectD != Point2D{7 , 8 }) {
461+ if (!getInput (" pointD" , pointD) || pointD != Point2D{7 , 8 }) {
462+ throw std::runtime_error (" failed pointD" );
463+ }
464+ if (!getInput (" pointE" , pointE) || pointE != Point2D{9 , 10 }) {
442465 throw std::runtime_error (" failed pointD" );
443466 }
444- if (!getInput (" input" , input) || input != Point2D{9 , 10 }) {
467+ if (!getInput (" input" , input) || input != Point2D{- 1 , - 2 }) {
445468 throw std::runtime_error (" failed input" );
446469 }
447470 return NodeStatus::SUCCESS;
@@ -453,20 +476,24 @@ class NodeWithDefaultPoints : public SyncActionNode
453476 BT::InputPort<Point2D>(" pointA" , Point2D{1 , 2 }, " default value is [1,2]" ),
454477 BT::InputPort<Point2D>(" pointB" , " {point}" , " default value inside blackboard {point}" ),
455478 BT::InputPort<Point2D>(" pointC" , " 5,6" , " default value is [5,6]" ),
456- BT::InputPort<Point2D>(" pointD" , " {=}" , " default value inside blackboard {pointD}" )};
479+ BT::InputPort<Point2D>(" pointD" , " {=}" , " default value inside blackboard {pointD}" ),
480+ BT::InputPort<Point2D>(" pointE" , R"( json:{"x":9,"y":10})" ,
481+ " default value is [9,10]" )};
457482 }
458483};
459484
460485
461- TEST (PortTest, DefaultInputVectors )
486+ TEST (PortTest, DefaultInputPoint2D )
462487{
463488 std::string xml_txt = R"(
464489 <root BTCPP_format="4" >
465490 <BehaviorTree>
466- <NodeWithDefaultPoints input="9,10 "/>
491+ <NodeWithDefaultPoints input="-1,-2 "/>
467492 </BehaviorTree>
468493 </root>)" ;
469494
495+ JsonExporter::get ().addConverter <Point2D>();
496+
470497 BehaviorTreeFactory factory;
471498 factory.registerNodeType <NodeWithDefaultPoints>(" NodeWithDefaultPoints" );
472499 auto tree = factory.createTreeFromText (xml_txt);
@@ -477,6 +504,8 @@ TEST(PortTest, DefaultInputVectors)
477504 BT::NodeStatus status;
478505 ASSERT_NO_THROW (status = tree.tickOnce ());
479506 ASSERT_EQ (status, NodeStatus::SUCCESS);
507+
508+ std::cout << writeTreeNodesModelXML (factory) << std::endl;
480509}
481510
482511class NodeWithDefaultStrings : public SyncActionNode
@@ -531,6 +560,8 @@ TEST(PortTest, DefaultInputStrings)
531560 BT::NodeStatus status;
532561 ASSERT_NO_THROW (status = tree.tickOnce ());
533562 ASSERT_EQ (status, NodeStatus::SUCCESS);
563+
564+ std::cout << writeTreeNodesModelXML (factory) << std::endl;
534565}
535566
536567struct TestStruct
0 commit comments