Skip to content

Commit e128b24

Browse files
committed
Add unit test: Any upcasting
1 parent 14589e5 commit e128b24

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/gtest_any.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,27 @@ TEST(Any, Cast)
249249
Any a(v);
250250
EXPECT_EQ(a.cast<std::vector<int>>(), v);
251251
}
252+
253+
// Upcasting.
254+
{
255+
// Base class
256+
class Greeter
257+
{
258+
virtual void print_msg() const {};
259+
};
260+
261+
// Derived class
262+
class HelloGreeter : public Greeter
263+
{
264+
void print_msg() const override{};
265+
};
266+
267+
auto hg = std::make_shared<HelloGreeter>();
268+
269+
Any a(hg);
270+
EXPECT_NO_THROW(auto res = a.cast<std::shared_ptr<HelloGreeter>>());
271+
EXPECT_NO_THROW(auto res = a.cast<std::shared_ptr<Greeter>>());
272+
EXPECT_TRUE(a.castPtr<std::shared_ptr<HelloGreeter>>());
273+
EXPECT_TRUE(a.castPtr<std::shared_ptr<Greeter>>());
274+
}
252275
}

0 commit comments

Comments
 (0)