Skip to content

Commit 423e970

Browse files
committed
Add unit test for upcasting base type chain in PortInfo
1 parent f90f822 commit 423e970

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

tests/gtest_ports.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "behaviortree_cpp/bt_factory.h"
44
#include "behaviortree_cpp/xml_parsing.h"
55
#include "behaviortree_cpp/json_export.h"
6+
#include "greeter_test.h" // upcasting tests
67

78
using namespace BT;
89

@@ -730,3 +731,60 @@ TEST(PortTest, DefaultWronglyOverriden)
730731
// This is correct
731732
ASSERT_NO_THROW(auto tree = factory.createTreeFromText(xml_txt_correct));
732733
}
734+
735+
TEST(PortTest, Upcasting_Issue943)
736+
{
737+
using namespace BT;
738+
739+
auto plant = PortInfo::Create<Plant::Ptr>(); // not registered
740+
auto animal = PortInfo::Create<Animal::Ptr>();
741+
auto cat = PortInfo::Create<Cat::Ptr>();
742+
auto dog = PortInfo::Create<Dog::Ptr>();
743+
auto sphynx = PortInfo::Create<SphynxCat::Ptr>();
744+
745+
ASSERT_EQ(0, plant.baseChain().size());
746+
ASSERT_EQ(1, animal.baseChain().size());
747+
ASSERT_EQ(2, cat.baseChain().size());
748+
ASSERT_EQ(2, dog.baseChain().size());
749+
ASSERT_EQ(3, sphynx.baseChain().size());
750+
751+
ASSERT_EQ(std::type_index(typeid(Animal)), animal.baseChain()[0]);
752+
ASSERT_EQ(std::type_index(typeid(Cat)), cat.baseChain()[0]);
753+
ASSERT_EQ(std::type_index(typeid(Animal)), cat.baseChain()[1]);
754+
ASSERT_EQ(std::type_index(typeid(Dog)), dog.baseChain()[0]);
755+
ASSERT_EQ(std::type_index(typeid(Animal)), dog.baseChain()[1]);
756+
757+
ASSERT_EQ(std::type_index(typeid(SphynxCat)), sphynx.baseChain()[0]);
758+
ASSERT_EQ(std::type_index(typeid(Cat)), sphynx.baseChain()[1]);
759+
ASSERT_EQ(std::type_index(typeid(Animal)), sphynx.baseChain()[2]);
760+
761+
ASSERT_FALSE(plant.isConvertibleFrom(std::type_index(typeid(Plant))));
762+
ASSERT_FALSE(plant.isConvertibleFrom(std::type_index(typeid(Animal))));
763+
ASSERT_FALSE(plant.isConvertibleFrom(std::type_index(typeid(Cat))));
764+
ASSERT_FALSE(plant.isConvertibleFrom(std::type_index(typeid(Dog))));
765+
ASSERT_FALSE(plant.isConvertibleFrom(std::type_index(typeid(SphynxCat))));
766+
767+
ASSERT_FALSE(animal.isConvertibleFrom(std::type_index(typeid(Plant))));
768+
ASSERT_TRUE(animal.isConvertibleFrom(std::type_index(typeid(Animal))));
769+
ASSERT_FALSE(animal.isConvertibleFrom(std::type_index(typeid(Cat))));
770+
ASSERT_FALSE(animal.isConvertibleFrom(std::type_index(typeid(Dog))));
771+
ASSERT_FALSE(animal.isConvertibleFrom(std::type_index(typeid(SphynxCat))));
772+
773+
ASSERT_FALSE(cat.isConvertibleFrom(std::type_index(typeid(Plant))));
774+
ASSERT_TRUE(cat.isConvertibleFrom(std::type_index(typeid(Animal))));
775+
ASSERT_TRUE(cat.isConvertibleFrom(std::type_index(typeid(Cat))));
776+
ASSERT_FALSE(cat.isConvertibleFrom(std::type_index(typeid(Dog))));
777+
ASSERT_FALSE(cat.isConvertibleFrom(std::type_index(typeid(SphynxCat))));
778+
779+
ASSERT_FALSE(dog.isConvertibleFrom(std::type_index(typeid(Plant))));
780+
ASSERT_TRUE(dog.isConvertibleFrom(std::type_index(typeid(Animal))));
781+
ASSERT_FALSE(dog.isConvertibleFrom(std::type_index(typeid(Cat))));
782+
ASSERT_TRUE(dog.isConvertibleFrom(std::type_index(typeid(Dog))));
783+
ASSERT_FALSE(dog.isConvertibleFrom(std::type_index(typeid(SphynxCat))));
784+
785+
ASSERT_FALSE(sphynx.isConvertibleFrom(std::type_index(typeid(Plant))));
786+
ASSERT_TRUE(sphynx.isConvertibleFrom(std::type_index(typeid(Animal))));
787+
ASSERT_TRUE(sphynx.isConvertibleFrom(std::type_index(typeid(Cat))));
788+
ASSERT_FALSE(sphynx.isConvertibleFrom(std::type_index(typeid(Dog))));
789+
ASSERT_TRUE(sphynx.isConvertibleFrom(std::type_index(typeid(SphynxCat))));
790+
}

tests/include/greeter_test.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,62 @@
33

44
#include <behaviortree_cpp/utils/safe_any.hpp>
55

6+
class Animal
7+
{
8+
public:
9+
using Ptr = std::shared_ptr<Animal>;
10+
virtual ~Animal() = default;
11+
};
12+
13+
class Cat : public Animal
14+
{
15+
public:
16+
using Ptr = std::shared_ptr<Cat>;
17+
};
18+
19+
class SphynxCat : public Cat
20+
{
21+
public:
22+
using Ptr = std::shared_ptr<SphynxCat>;
23+
};
24+
25+
class Dog : public Animal
26+
{
27+
public:
28+
using Ptr = std::shared_ptr<Dog>;
29+
};
30+
31+
// Not registered
32+
class Plant
33+
{
34+
public:
35+
using Ptr = std::shared_ptr<Plant>;
36+
};
37+
38+
template <>
39+
struct BT::any_cast_base<Animal>
40+
{
41+
using type = Animal;
42+
};
43+
44+
template <>
45+
struct BT::any_cast_base<Cat>
46+
{
47+
using type = Animal;
48+
};
49+
50+
template <>
51+
struct BT::any_cast_base<SphynxCat>
52+
{
53+
using type = Cat;
54+
};
55+
56+
template <>
57+
struct BT::any_cast_base<Dog>
58+
{
59+
using type = Animal;
60+
};
61+
662
/**
763
* +-------------------+------------+-------------+-----------------------------+
864
* | Class | Base Class | Polymorphic | Type Trait Registered |

0 commit comments

Comments
 (0)