void can_stub_method_after_reset() { Mock<foo_bar> mock; mock.Reset(); When(Method(mock, bar)).Return(42); ASSERT_EQUAL(mock.get().bar(), 42); Verify(Method(mock, bar)).Once(); }
int main () { using namespace fakeit; const mqmx::queue_id_type defQID = 10; const mqmx::message_id_type defMID = 10; Mock<mqmx::message_queue::listener> mock; Fake (Method (mock, notify)); { mqmx::message_queue queue (defQID); mqmx::message::upointer_type msg; mqmx::status_code retCode = queue.set_listener (mock.get ()); assert (mqmx::ExitStatus::Success == retCode); /* test for double insert */ retCode = queue.set_listener (mock.get ()); assert (mqmx::ExitStatus::AlreadyExist == retCode); retCode = queue.enqueue<mqmx::message> (defMID); assert (mqmx::ExitStatus::Success == retCode); Verify (Method (mock, notify).Using ( defQID, &queue, mqmx::message_queue::notification_flag::data)) .Once (); } Verify (Method (mock, notify).Using ( defQID, nullptr, mqmx::message_queue::notification_flag::closed)) .Once (); VerifyNoOtherInvocations (mock); return 0; }
/** * The called method accepts lambda arguments, and invokes these arguments to * decide whether the call matches. */ TEST(MockValidator, validationBuilder_called_ignore_lambda) { const int INT_VALUE = 17; const bool BOOL_VALUE = true; struct DummyType { }; Mock<DummyType> dummyMock; MockValidator<void, int, bool> dummyFunc; //no invocations have occurred ASSERT_EQ(0, dummyMock.invocations().size()); //the validator DOES NOT match this invocation ASSERT_FALSE( dummyFunc.validationBuilder(dummyMock).called(INT_VALUE, BOOL_VALUE)); //add an invocation dummyFunc.addInvocation(dummyMock, INT_VALUE, BOOL_VALUE); //there is now an invocation in the mock ASSERT_EQ(1, dummyMock.invocations().size()); //the validator matches these arguments ASSERT_TRUE( dummyFunc.validationBuilder(dummyMock).called([](int x) -> bool { return x > 5; }, Ignore())); }
void TestConstParameters() { Mock<ConstVolatileParameters> mock; Fake(Method(mock,func1)); ConstVolatileParameters& i = mock.get(); ASSERT_EQUAL(0, i.func1(1,std::string())); }
/** * The called method accepts Ignore as a single argument. */ TEST(MockValidator, validationBuilder_called_single_ignore) { const int INT_VALUE = 17; struct DummyType { }; Mock<DummyType> dummyMock; MockValidator<void, int> dummyFunc; //no invocations have occurred ASSERT_EQ(0, dummyMock.invocations().size()); //the validator DOES NOT match this invocation ASSERT_FALSE( dummyFunc.validationBuilder(dummyMock).called(Ignore())); //add an invocation dummyFunc.addInvocation(dummyMock, INT_VALUE); //there is now an invocation in the mock ASSERT_EQ(1, dummyMock.invocations().size()); //the validator matches these arguments because they are ignored. ASSERT_TRUE( dummyFunc.validationBuilder(dummyMock).called(Ignore())); }
/** * The called method returns false when a different method was called, even when * arguments are ignored. */ TEST(MockValidator, validationBuilder_called_ignore_different_function) { const int INT_VALUE = 17; const bool BOOL_VALUE = true; struct DummyType { }; Mock<DummyType> dummyMock; MockValidator<void, int, bool> dummyFunc; MockValidator<void, int> differentFunc; //no invocations have occurred ASSERT_EQ(0, dummyMock.invocations().size()); //the validator DOES NOT match this invocation ASSERT_FALSE( dummyFunc.validationBuilder(dummyMock).called(INT_VALUE, BOOL_VALUE)); //add an invocation of the other function differentFunc.addInvocation(dummyMock, INT_VALUE); //there is now an invocation in the mock ASSERT_EQ(1, dummyMock.invocations().size()); //the validator returns false because a different function was invoked ASSERT_FALSE( dummyFunc.validationBuilder(dummyMock).called(Ignore(), Ignore())); }
void dynamic_cast_to_same_type__with_concrete_type() { Mock<ConcreteType> mockDemo; ConcreteType* mockDemoPtr = &mockDemo.get(); ConcreteType* mockDemoPtr2 = dynamic_cast<ConcreteType*>(mockDemoPtr); ASSERT_EQUAL(mockDemoPtr, mockDemoPtr2); }
/** * ValidationBuilder.called returns false when a different invocation was made. */ TEST(MockValidator, validationBuilder_called_different_function) { const int INT_VALUE = 17; const bool BOOL_VALUE = true; struct DummyType { }; Mock<DummyType> dummyMock; MockValidator<void, int, bool> dummyFunc; MockValidator<bool> otherFunc; //no invocations have occurred ASSERT_EQ(0, dummyMock.invocations().size()); //the validator DOES NOT match this invocation ASSERT_FALSE( otherFunc.validationBuilder(dummyMock).called()); //add an invocation dummyFunc.addInvocation(dummyMock, INT_VALUE, BOOL_VALUE); //there is now an invocation in the mock ASSERT_EQ(1, dummyMock.invocations().size()); //the validator DOES NOT match this invocation; it was a dummyFunc //invocation. ASSERT_FALSE( otherFunc.validationBuilder(dummyMock).called()); }
void dynamic_cast_to_same_type__with_abstract_type() { Mock<TopLeft> mock; TopLeft* ptr = &mock.get(); TopLeft* ptr2 = dynamic_cast<TopLeft*>(ptr); ASSERT_EQUAL(ptr, ptr2); }
void foo() { Mock<Derived> mock; When(Method(mock, methodA)).AlwaysReturn(); Derived& derived = mock.get(); struct DoNotDelete { void operator()(Derived* p) const { } }; std::shared_ptr<Derived> derivedSPtr = std::shared_ptr<Derived>(&derived, DoNotDelete()); //This version works #1 //shared_ptr<Derived> derivedSPtr(new Derived()); std::set<std::shared_ptr<Base>> set; set.insert(derivedSPtr); for (auto item : set) { std::shared_ptr<Base> b1 = item; b1->methodA(); std::shared_ptr<Derived> d1 = std::dynamic_pointer_cast<Derived>(b1); //Next line fails. //d1 is always null for the mocked version. If a real Derived object is used like the line commented out above #1 it works fine. d1->methodA(); } }
int main(int argc, char** argv) { Test test("DUNE::Utils::StateMachine"); Mock o; struct { const char* string; int count; } testcases[] = { {"LST_STLS_STSLLLLLL", 0}, {"STS_STLS_LSTS_LSTL", 1}, {"LSTS_STLS_LSTS_LST", 2}, {"SLSTSSLLSTSLSTSLLL", 3} }; for (unsigned i = 0; i < sizeof(testcases) / sizeof(testcases[0]); i++) { test.boolean(testcases[i].string, o.count(testcases[i].string) == testcases[i].count); } return 0; }
void pass_mock_by_ref() { Mock<Change> mock; Change* change = &mock.get(); When(Method(mock, change)).AlwaysReturn(); change->change(1, 2, 3); assertChanged(mock, 1, 2, 3); }
void DefaultBeaviorOfVoidFunctionsIsToDoNothing() { Mock<VoidFunctions> mock; Fake(Method(mock,proc1)); Fake(Method(mock,proc2)); VoidFunctions& i = mock.get(); i.proc1(); i.proc2(1); }
void production_shared_ptr_mock_used_in_invocation() { Mock<ISomeInterface3> mock; std::shared_ptr<ISomeInterface3> pMockInstance(&mock.get()); fakeit::Fake(Dtor(mock)); fakeit::Fake(Method(mock, methodWithSomeSharedPointer)); pMockInstance->methodWithSomeSharedPointer(pMockInstance); pMockInstance = nullptr; }
void ReturnByReference_ReturnReferenceToDefaultConstructedObject() { Mock<ReferenceFunctions> mock; Fake(Method(mock,scalarFunc)); Fake(Method(mock,stringFunc)); ReferenceFunctions& i = mock.get(); ASSERT_EQUAL(0, i.scalarFunc()); ASSERT_EQUAL(std::string(), i.stringFunc()); }
TEST(ThreadTest, Run) { Mock mock; ASSERT_EQ(0, mock.timesCalled); mock.Start(); mock.mutex.Lock(); ASSERT_EQ(0, mock.timesCalled); mock.mutex.Unlock(); mock.Join(); ASSERT_EQ(1, mock.timesCalled); }
int main () { using namespace fakeit; const mqmx::queue_id_type defQIDa = 10; const mqmx::queue_id_type defQIDb = 20; const mqmx::message_id_type defMIDa = 10; const mqmx::message_id_type defMIDb = 20; Mock<mqmx::message_queue::listener> mock; Fake (Method (mock, notify)); mqmx::message_queue aqueue (defQIDa); mqmx::message_queue bqueue (defQIDb); mqmx::message::upointer_type msg; mqmx::status_code retCode = mqmx::ExitStatus::Success; retCode = aqueue.set_listener (mock.get ()); assert (mqmx::ExitStatus::Success == retCode); retCode = bqueue.set_listener (mock.get ()); assert (mqmx::ExitStatus::Success == retCode); retCode = aqueue.enqueue<mqmx::message> (defMIDa); retCode = bqueue.enqueue<mqmx::message> (defMIDb); Verify (Method (mock, notify).Using ( defQIDa, &aqueue, mqmx::message_queue::notification_flag::data)) .Once (); Verify (Method (mock, notify).Using ( defQIDb, &bqueue, mqmx::message_queue::notification_flag::data)) .Once (); bqueue = std::move (aqueue); Verify (Method (mock, notify).Using ( defQIDa, &aqueue, mqmx::message_queue::notification_flag::detached)) .Once (); Verify (Method (mock, notify).Using ( defQIDb, &bqueue, mqmx::message_queue::notification_flag::detached)) .Once (); msg = aqueue.pop (); /* message should be moved to the new queue */ assert (nullptr == msg.get ()); msg = bqueue.pop (); /* message should be moved from the original queue */ assert (nullptr != msg.get ()); assert (defQIDa == msg->get_qid ()); assert (defMIDa == msg->get_mid ()); VerifyNoOtherInvocations (mock); return 0; }
void check_no_more_invocations_exception() { Mock<SomeInterface> mock; try { Fake(Method(mock,foo)); mock.get().foo(1); mock.get().foo(2); Verify(Method(mock,foo).Using(1)); VerifyNoOtherInvocations(Method(mock,foo)); } catch (NoMoreInvocationsVerificationException&) { // ASSERT_EQUAL(VerificationType::NoMoreInvocations, e.verificationType()); // ASSERT_EQUAL(2, e.allIvocations().size()); // ASSERT_EQUAL(1, e.unverifedIvocations().size()); } }
void format_UnmockedMethodCallEvent() { Mock<SomeInterface> mock; SomeInterface &i = mock.get(); try { i.func(1); FAIL(); } catch (UnexpectedMethodCallException& e) { std::string expectedMsg{"Unexpected method invocation: unknown()\n"}; expectedMsg += " An unmocked method was invoked. All used virtual methods must be stubbed!"; std::string actual{to_string(e)}; ASSERT_EQUAL(expectedMsg, actual); } }
void ReturnByValue_ThrowExceptionIfNotDefaultConstructible() { Mock<NonDefaultConstructibleFunctions> mock; Fake(Method(mock,notDefaultConstructibleFunc)); NonDefaultConstructibleFunctions& i = mock.get(); try { i.notDefaultConstructibleFunc(); FAIL() ; } catch (fakeit::DefaultValueInstatiationException& e) { auto expected = std::string("Type ") + std::string(typeid(NotDefaultConstructible).name()) + std::string(" is not default constructible. Could not instantiate a default return value"); std::string actual(e.what()); ASSERT_EQUAL(expected, actual); } }
void format_UnmatchedMethodCallEvent() { Mock<SomeInterface> mock; Fake(Method(mock,func).Using(3)); SomeInterface &i = mock.get(); try { i.func(1); FAIL(); } catch (UnexpectedMethodCallException& e) { std::string expectedMsg{"Unexpected method invocation: mock.func(1)\n Could not find Any recorded behavior to support this method call."}; std::string actual{to_string(e)}; ASSERT_EQUAL(expectedMsg, actual); } }
void simple_inheritance_dynamic_down_cast() { Mock<A, Left, TopLeft> aMock; Fake(Method(aMock,l)); A& a = aMock.get(); Left* left = &a; TopLeft* topLeft = &a; A* aPtr = dynamic_cast<A*>(left); ASSERT_EQUAL(0, aPtr->l()); aPtr = dynamic_cast<A*>(topLeft); ASSERT_EQUAL(0, aPtr->l()); left = dynamic_cast<Left*>(topLeft); ASSERT_EQUAL(0, left->l()); }
GroupTestFixture() { Mock<NodeBase> mockNode; NodeBase& node1 = mockNode.get(); NodeBase& node2 = mockNode.get(); NodeBase& node3 = mockNode.get(); NodeBase& node4 = mockNode.get(); nodeGroupOne.AddMember(1, &node1); nodeGroupOne.AddMember(2, &node2); nodeGroupOne.AddMember(3, &node3); nodeGroupTwo.AddMember(3, &node3); nodeGroupTwo.AddMember(4, &node4); }
TEST( TestHeaderOnlyVTableCOWFooable_HeapAllocations, MoveConstruction ) { auto expected_heap_allocations = 0u; Fooable fooable = MockFooable(); CHECK_HEAP_ALLOC( Fooable other( std::move(fooable) ), expected_heap_allocations ); }
TEST( TestSBOFooable_HeapAllocations, MoveConstruction_SmallObject ) { auto expected_heap_allocations = 0u; Fooable fooable = MockFooable(); CHECK_HEAP_ALLOC( Fooable other( std::move(fooable) ), expected_heap_allocations ); }
TEST( TestSBOFooable_HeapAllocations, CopyConstruction_LargeObject ) { auto expected_heap_allocations = 1u; Fooable fooable = MockLargeFooable(); CHECK_HEAP_ALLOC( Fooable other( fooable ), expected_heap_allocations ); }
TEST( TestHeaderOnlyVTableCOWFooable_HeapAllocations, CopyAssignment ) { auto expected_heap_allocations = 0u; Fooable fooable = MockFooable(); CHECK_HEAP_ALLOC( Fooable other; other = fooable, expected_heap_allocations ); }
TEST( TestSBOFooable_HeapAllocations, CopyAssignment_SmallObject ) { auto expected_heap_allocations = 0u; Fooable fooable = MockFooable(); CHECK_HEAP_ALLOC( Fooable other; other = fooable, expected_heap_allocations ); }
TEST( TestSBOFooable_HeapAllocations, MoveAssignment_LargeObject ) { auto expected_heap_allocations = 0u; Fooable fooable = MockLargeFooable(); CHECK_HEAP_ALLOC( Fooable other; other = std::move(fooable), expected_heap_allocations ); }
void scalar_types_should_return_zero() { Mock<ScalarFunctions> mock; Fake(Method(mock,boolFunc)); Fake(Method(mock,charFunc)); Fake(Method(mock,char16Func)); Fake(Method(mock,char32Func)); Fake(Method(mock,wcharFunc)); Fake(Method(mock,shortFunc)); Fake(Method(mock,intFunc)); Fake(Method(mock,longFunc)); Fake(Method(mock,longLongFunc)); Fake(Method(mock,floatFunc)); Fake(Method(mock,doubleFunc)); Fake(Method(mock,longDoubleFunc)); Fake(Method(mock,enumFunc)); Fake(Method(mock,pIntFunc)); Fake(Method(mock,pScalarFuctionsfunc)); Fake(Method(mock,nullptrFunc)); Fake(Method(mock,pMemberFunc)); ScalarFunctions &i = mock.get(); //Default behavior of a scalar function is to return 0/false/null ASSERT_EQUAL(false, i.boolFunc()); ASSERT_EQUAL((char ) 0, i.charFunc()); ASSERT_EQUAL((int )(char16_t ) 0, (int )i.char16Func()); ASSERT_EQUAL((char32_t ) 0, i.char32Func()); ASSERT_EQUAL((wchar_t ) 0, i.wcharFunc()); ASSERT_EQUAL((short ) 0, i.shortFunc()); ASSERT_EQUAL((int ) 0, i.intFunc()); ASSERT_EQUAL((long ) 0, i.longFunc()); ASSERT_EQUAL((long )0, (long )i.longLongFunc()); ASSERT_EQUAL((float ) 0, i.floatFunc()); ASSERT_EQUAL((double ) 0, i.doubleFunc()); ASSERT_EQUAL((double ) 0, (double ) i.longDoubleFunc()); ASSERT_EQUAL(0, (int ) i.enumFunc()); ASSERT_EQUAL(nullptr, i.pIntFunc()); ASSERT_EQUAL(nullptr, i.pScalarFuctionsfunc()); ASSERT_EQUAL(nullptr, i.nullptrFunc()); ASSERT_EQUAL(0, union_cast<int>(i.pMemberFunc())); }