Added some more test cases for dynamic_pointer_move

This commit is contained in:
Sebastian Messmer 2015-06-21 17:14:35 +02:00
parent 667151e6ed
commit f30bec26ee
3 changed files with 77 additions and 7 deletions

View File

@ -2,6 +2,7 @@
[requirements]
cryptopp/cryptopp: 9
google/gmock: 2
google/gtest: 11
messmer/cmake: 3

View 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();
}

View File

@ -5,14 +5,20 @@ using namespace cpputils;
using std::unique_ptr;
class Parent {
public:
virtual ~Parent() {}
};
class Child: public Parent {};
class Child2: public Parent {};
// Putting them in an own namespace is needed, so they don't clash with globally defined Parent/Child classes
namespace testobjs {
class Parent {
public:
virtual ~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) {
unique_ptr<Parent> source(nullptr);