1
1
#include < gtest/gtest.h>
2
2
#include " behaviortree_cpp/bt_factory.h"
3
+ #include " behaviortree_cpp/xml_parsing.h"
4
+ #include " behaviortree_cpp/json_export.h"
3
5
4
6
using namespace BT ;
5
7
@@ -274,6 +276,11 @@ struct Point2D {
274
276
template <> [[nodiscard]]
275
277
Point2D BT::convertFromString<Point2D>(StringView str)
276
278
{
279
+ if (StartWith (str, " json:" ))
280
+ {
281
+ str.remove_prefix (5 );
282
+ return convertFromJSON<Point2D>(str);
283
+ }
277
284
const auto parts = BT::splitString (str, ' ,' );
278
285
if (parts.size () != 2 )
279
286
{
@@ -284,6 +291,18 @@ Point2D BT::convertFromString<Point2D>(StringView str)
284
291
return {x, y};
285
292
}
286
293
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
+
287
306
288
307
class DefaultTestAction : public SyncActionNode
289
308
{
@@ -428,20 +447,24 @@ class NodeWithDefaultPoints : public SyncActionNode
428
447
429
448
NodeStatus tick () override
430
449
{
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 }) {
433
453
throw std::runtime_error (" failed pointA" );
434
454
}
435
- if (!getInput (" pointB" , vectB ) || vectB != Point2D{3 , 4 }) {
455
+ if (!getInput (" pointB" , pointB ) || pointB != Point2D{3 , 4 }) {
436
456
throw std::runtime_error (" failed pointB" );
437
457
}
438
- if (!getInput (" pointC" , vectC ) || vectC != Point2D{5 , 6 }) {
458
+ if (!getInput (" pointC" , pointC ) || pointC != Point2D{5 , 6 }) {
439
459
throw std::runtime_error (" failed pointC" );
440
460
}
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 }) {
442
465
throw std::runtime_error (" failed pointD" );
443
466
}
444
- if (!getInput (" input" , input) || input != Point2D{9 , 10 }) {
467
+ if (!getInput (" input" , input) || input != Point2D{- 1 , - 2 }) {
445
468
throw std::runtime_error (" failed input" );
446
469
}
447
470
return NodeStatus::SUCCESS;
@@ -453,20 +476,24 @@ class NodeWithDefaultPoints : public SyncActionNode
453
476
BT::InputPort<Point2D>(" pointA" , Point2D{1 , 2 }, " default value is [1,2]" ),
454
477
BT::InputPort<Point2D>(" pointB" , " {point}" , " default value inside blackboard {point}" ),
455
478
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]" )};
457
482
}
458
483
};
459
484
460
485
461
- TEST (PortTest, DefaultInputVectors )
486
+ TEST (PortTest, DefaultInputPoint2D )
462
487
{
463
488
std::string xml_txt = R"(
464
489
<root BTCPP_format="4" >
465
490
<BehaviorTree>
466
- <NodeWithDefaultPoints input="9,10 "/>
491
+ <NodeWithDefaultPoints input="-1,-2 "/>
467
492
</BehaviorTree>
468
493
</root>)" ;
469
494
495
+ JsonExporter::get ().addConverter <Point2D>();
496
+
470
497
BehaviorTreeFactory factory;
471
498
factory.registerNodeType <NodeWithDefaultPoints>(" NodeWithDefaultPoints" );
472
499
auto tree = factory.createTreeFromText (xml_txt);
@@ -477,6 +504,8 @@ TEST(PortTest, DefaultInputVectors)
477
504
BT::NodeStatus status;
478
505
ASSERT_NO_THROW (status = tree.tickOnce ());
479
506
ASSERT_EQ (status, NodeStatus::SUCCESS);
507
+
508
+ std::cout << writeTreeNodesModelXML (factory) << std::endl;
480
509
}
481
510
482
511
class NodeWithDefaultStrings : public SyncActionNode
@@ -531,6 +560,8 @@ TEST(PortTest, DefaultInputStrings)
531
560
BT::NodeStatus status;
532
561
ASSERT_NO_THROW (status = tree.tickOnce ());
533
562
ASSERT_EQ (status, NodeStatus::SUCCESS);
563
+
564
+ std::cout << writeTreeNodesModelXML (factory) << std::endl;
534
565
}
535
566
536
567
struct TestStruct
0 commit comments