Added some more test cases for dynamic_pointer_move
This commit is contained in:
parent
667151e6ed
commit
f30bec26ee
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
[requirements]
|
[requirements]
|
||||||
cryptopp/cryptopp: 9
|
cryptopp/cryptopp: 9
|
||||||
|
google/gmock: 2
|
||||||
google/gtest: 11
|
google/gtest: 11
|
||||||
messmer/cmake: 3
|
messmer/cmake: 3
|
||||||
|
|
||||||
|
63
test/pointer_destructor_test.cpp
Normal file
63
test/pointer_destructor_test.cpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#include "google/gtest/gtest.h"
|
||||||
|
#include "google/gmock/gmock.h"
|
||||||
|
#include "../pointer.h"
|
||||||
|
|
||||||
|
using namespace cpputils;
|
||||||
|
|
||||||
|
using std::unique_ptr;
|
||||||
|
using std::make_unique;
|
||||||
|
using std::function;
|
||||||
|
|
||||||
|
// Putting them in an own namespace is needed, so they don't clash with globally defined Parent/Child classes
|
||||||
|
namespace withdestructorcallback {
|
||||||
|
class DestructorCallback {
|
||||||
|
public:
|
||||||
|
MOCK_CONST_METHOD0(call, void());
|
||||||
|
};
|
||||||
|
|
||||||
|
class Parent {
|
||||||
|
public:
|
||||||
|
virtual ~Parent() { }
|
||||||
|
};
|
||||||
|
|
||||||
|
class Child : public Parent {
|
||||||
|
public:
|
||||||
|
Child(const DestructorCallback &childDestructorCallback) : _destructorCallback(childDestructorCallback) { }
|
||||||
|
|
||||||
|
~Child() {
|
||||||
|
_destructorCallback.call();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const DestructorCallback &_destructorCallback;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
using namespace withdestructorcallback;
|
||||||
|
|
||||||
|
class DynamicPointerMoveDestructorTest: public ::testing::Test {
|
||||||
|
public:
|
||||||
|
DestructorCallback childDestructorCallback;
|
||||||
|
unique_ptr<Child> createChild() {
|
||||||
|
return make_unique<Child>(childDestructorCallback);
|
||||||
|
}
|
||||||
|
void EXPECT_CHILD_DESTRUCTOR_CALLED() {
|
||||||
|
EXPECT_CALL(childDestructorCallback, call()).Times(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(DynamicPointerMoveDestructorTest, ChildInParentPtr) {
|
||||||
|
unique_ptr<Parent> parent = createChild();
|
||||||
|
EXPECT_CHILD_DESTRUCTOR_CALLED();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DynamicPointerMoveDestructorTest, ChildToParentCast) {
|
||||||
|
unique_ptr<Child> child = createChild();
|
||||||
|
unique_ptr<Parent> parent = dynamic_pointer_move<Parent>(child);
|
||||||
|
EXPECT_CHILD_DESTRUCTOR_CALLED();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DynamicPointerMoveDestructorTest, ParentToChildCast) {
|
||||||
|
unique_ptr<Parent> parent = createChild();
|
||||||
|
unique_ptr<Child> child = dynamic_pointer_move<Child>(parent);
|
||||||
|
EXPECT_CHILD_DESTRUCTOR_CALLED();
|
||||||
|
}
|
@ -5,14 +5,20 @@ using namespace cpputils;
|
|||||||
|
|
||||||
using std::unique_ptr;
|
using std::unique_ptr;
|
||||||
|
|
||||||
class Parent {
|
// Putting them in an own namespace is needed, so they don't clash with globally defined Parent/Child classes
|
||||||
public:
|
namespace testobjs {
|
||||||
virtual ~Parent() {}
|
class Parent {
|
||||||
};
|
public:
|
||||||
class Child: public Parent {};
|
virtual ~Parent() { }
|
||||||
class Child2: public Parent {};
|
};
|
||||||
|
|
||||||
//TODO Add test cases that the correct (virtual) destructor is called
|
class Child : public Parent {
|
||||||
|
};
|
||||||
|
|
||||||
|
class Child2 : public Parent {
|
||||||
|
};
|
||||||
|
}
|
||||||
|
using namespace testobjs;
|
||||||
|
|
||||||
TEST(DynamicPointerMoveTest, NullPtrParentToChildCast) {
|
TEST(DynamicPointerMoveTest, NullPtrParentToChildCast) {
|
||||||
unique_ptr<Parent> source(nullptr);
|
unique_ptr<Parent> source(nullptr);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user