Skip to content

Instantly share code, notes, and snippets.

@biot023
Created September 2, 2012 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biot023/3597237 to your computer and use it in GitHub Desktop.
Save biot023/3597237 to your computer and use it in GitHub Desktop.
#include "gmock/gmock.h"
#include "poc.h"
#include <string>
namespace {
using namespace std;
using poc::Poc;
using ::testing::Test;
using ::testing::Return;
using ::testing::Invoke;
using ::testing::NiceMock;
template <typename T>
struct MockPoc : public Poc<T>
{
MOCK_METHOD0_T( __function_operator, const bool() );
virtual const bool operator() () { return __function_operator(); }
MOCK_CONST_METHOD0_T( abstract_method, void() );
MOCK_METHOD0_T( _do_subtask1, const bool() );
MOCK_METHOD0_T( _do_subtask2, const bool() );
};
template <typename T>
struct PartMockPoc : public MockPoc<T>
{
PartMockPoc() {
ON_CALL( *this, __function_operator() ).
WillByDefault( Invoke( this, &PartMockPoc::__super_function_operator ) );
}
private:
const bool __super_function_operator() { return Poc<T>::operator()(); }
};
struct PocTest : public Test
{
protected:
NiceMock<PartMockPoc<string>> _poc;
PocTest()
{
ON_CALL( _poc, _do_subtask1() ).WillByDefault( Return( true ) );
ON_CALL( _poc, _do_subtask2() ).WillByDefault( Return( true ) );
}
const bool operator() () {
return _poc();
}
};
// Should call subtask 1
TEST_F( PocTest,
ShouldCallSubtask1 ) {
EXPECT_CALL( _poc, _do_subtask1() );
( *this )();
}
// Should call subtask 2
TEST_F( PocTest,
ShouldCallSubtask2 ) {
EXPECT_CALL( _poc, _do_subtask2() );
( *this )();
}
// Should return true if both subtasks return true
TEST_F( PocTest,
ShouldReturnTrueIfBothSubtasksReturnTrue ) {
EXPECT_TRUE( ( *this )() );
}
// Should return false if first subtask returns false
TEST_F( PocTest,
ShouldReturnFalseIfFirstSubtaskReturnsFalse ) {
ON_CALL( _poc, _do_subtask1() ).WillByDefault( Return( false ) );
EXPECT_FALSE( ( *this )() );
}
// Should return false if second subtask returns false
TEST_F( PocTest,
ShouldReturnFalseIfSecondSubtaskReturnsFalse ) {
ON_CALL( _poc, _do_subtask2() ).WillByDefault( Return( false ) );
EXPECT_FALSE( ( *this )() );
}
// Should not call second subtask if first returns false
TEST_F( PocTest,
ShouldNotCallSecondSubtaskIfFirstReturnsFalse ) {
ON_CALL( _poc, _do_subtask1() ).WillByDefault( Return( false ) );
EXPECT_CALL( _poc, _do_subtask2() ).Times( 0 );
( *this )();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment