git subrepo clone git@github.com:compihu/platform-test-f4-ll.git platforms/platform-test-f4-ll

subrepo:
  subdir:   "platforms/platform-test-f4-ll"
  merged:   "bc41134"
upstream:
  origin:   "git@github.com:compihu/platform-test-f4-ll.git"
  branch:   "master"
  commit:   "bc41134"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "87ee373"
This commit is contained in:
Attila Body 2021-02-24 21:46:35 +01:00
parent 46ce55c6a0
commit 95af4ce0d8
460 changed files with 84105 additions and 0 deletions

View file

@ -0,0 +1,69 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTest/SimpleStringInternalCache.h"
#include "CppUTestExt/MemoryReporterPlugin.h"
#include "CppUTestExt/MockSupportPlugin.h"
#ifdef CPPUTEST_INCLUDE_GTEST_TESTS
#include "CppUTestExt/GTestConvertor.h"
#endif
int main(int ac, const char *const *av)
{
int result = 0;
GlobalSimpleStringCache simpleStringCache;
{
#ifdef CPPUTEST_INCLUDE_GTEST_TESTS
GTestConvertor convertor;
convertor.addAllGTestToTestRegistry();
#endif
MemoryReporterPlugin plugin;
MockSupportPlugin mockPlugin;
TestRegistry::getCurrentRegistry()->installPlugin(&plugin);
TestRegistry::getCurrentRegistry()->installPlugin(&mockPlugin);
#ifndef GMOCK_RENAME_MAIN
result = CommandLineTestRunner::RunAllTests(ac, av);
#else
/* Don't have any memory leak detector when running the Google Test tests */
testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity;
ConsoleTestOutput output;
CommandLineTestRunner runner(ac, av, TestRegistry::getCurrentRegistry());
result = runner.runAllTestsMain();
#endif
}
return result;
}

View file

@ -0,0 +1,43 @@
set(CppUTestExtTests_src
AllTests.cpp
CodeMemoryReporterTest.cpp
GMockTest.cpp
GTest1Test.cpp
IEEE754PluginTest.cpp
IEEE754PluginTest_c.c
MemoryReportAllocatorTest.cpp
MemoryReporterPluginTest.cpp
MemoryReportFormatterTest.cpp
MockActualCallTest.cpp
MockCheatSheetTest.cpp
MockCallTest.cpp
MockComparatorCopierTest.cpp
MockExpectedCallTest.cpp
ExpectedFunctionsListTest.cpp
MockFailureReporterForTest.cpp
MockFailureTest.cpp
MockHierarchyTest.cpp
MockNamedValueTest.cpp
MockParameterTest.cpp
MockPluginTest.cpp
MockSupportTest.cpp
MockSupport_cTestCFile.c
MockSupport_cTest.cpp
MockStrictOrderTest.cpp
MockReturnValueTest.cpp
OrderedTestTest_c.c
OrderedTestTest.cpp
)
if (MINGW)
find_package (Threads REQUIRED)
set(THREAD_LIB "pthread")
endif (MINGW)
add_executable(CppUTestExtTests ${CppUTestExtTests_src})
cpputest_normalize_test_output_location(CppUTestExtTests)
target_link_libraries(CppUTestExtTests CppUTest CppUTestExt ${THREAD_LIB} ${CPPUNIT_EXTERNAL_LIBRARIES})
if (TESTS_BUILD_DISCOVER)
cpputest_buildtime_discover_tests(CppUTestExtTests)
endif()

View file

@ -0,0 +1,199 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTestExt/MemoryReportAllocator.h"
#include "CppUTestExt/CodeMemoryReportFormatter.h"
#define TESTOUTPUT_EQUAL(a) STRCMP_EQUAL_LOCATION(a, testOutput.getOutput().asCharString(), "", __FILE__, __LINE__)
#define TESTOUTPUT_CONTAINS(a) STRCMP_CONTAINS_LOCATION(a, testOutput.getOutput().asCharString(), "", __FILE__, __LINE__)
TEST_GROUP(CodeMemoryReportFormatter)
{
TestMemoryAllocator* cAllocator;
TestMemoryAllocator* newAllocator;
TestMemoryAllocator* newArrayAllocator;
char* memory01;
char* memory02;
StringBufferTestOutput testOutput;
TestResult* testResult;
CodeMemoryReportFormatter* formatter;
void setup() _override
{
cAllocator = defaultMallocAllocator();
newAllocator = defaultNewAllocator();
newArrayAllocator= defaultNewArrayAllocator();
memory01 = (char*) 0x01;
memory02 = (char*) 0x02;
formatter = new CodeMemoryReportFormatter(cAllocator);
testResult = new TestResult(testOutput);
}
void teardown() _override
{
delete testResult;
delete formatter;
}
};
TEST(CodeMemoryReportFormatter, mallocCreatesAnMallocCall)
{
formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 9);
TESTOUTPUT_EQUAL("\tvoid* file_9_1 = malloc(10);\n");
}
TEST(CodeMemoryReportFormatter, freeCreatesAnFreeCall)
{
formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 9);
testOutput.flush();
formatter->report_free_memory(testResult, cAllocator, memory01, "boo", 6);
TESTOUTPUT_EQUAL("\tfree(file_9_1); /* at boo:6 */\n");
}
TEST(CodeMemoryReportFormatter, twoMallocAndTwoFree)
{
formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 2);
formatter->report_alloc_memory(testResult, cAllocator, 10, memory02, "boo", 4);
testOutput.flush();
formatter->report_free_memory(testResult, cAllocator, memory01, "foo", 6);
formatter->report_free_memory(testResult, cAllocator, memory02, "bar", 8);
TESTOUTPUT_CONTAINS("\tfree(file_2_1); /* at foo:6 */\n");
TESTOUTPUT_CONTAINS("\tfree(boo_4_1); /* at bar:8 */\n");
}
TEST(CodeMemoryReportFormatter, variableNamesShouldNotContainSlahses)
{
formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "dir/file", 2);
TESTOUTPUT_CONTAINS("\tvoid* file_2");
}
TEST(CodeMemoryReportFormatter, variableNamesShouldNotContainDotButUseUnderscore)
{
formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "foo.cpp", 2);
TESTOUTPUT_CONTAINS("foo_cpp");
}
TEST(CodeMemoryReportFormatter, newArrayAllocatorGeneratesNewArrayCode)
{
formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
TESTOUTPUT_CONTAINS("char* file_8_1 = new char[10]; /* using new [] */");
}
TEST(CodeMemoryReportFormatter, newArrayGeneratesNewCode)
{
formatter->report_alloc_memory(testResult, newAllocator, 6, memory01, "file", 4);
TESTOUTPUT_CONTAINS("new char[6]; /* using new */");
}
TEST(CodeMemoryReportFormatter, NewAllocatorGeneratesDeleteCode)
{
formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
testOutput.flush();
formatter->report_free_memory(testResult, newAllocator, memory01, "boo", 4);
TESTOUTPUT_CONTAINS("delete [] file_8_1; /* using delete at boo:4 */");
}
TEST(CodeMemoryReportFormatter, DeleteNullWorksFine)
{
formatter->report_free_memory(testResult, newAllocator, NULLPTR, "boo", 4);
TESTOUTPUT_CONTAINS("delete [] NULL; /* using delete at boo:4 */");
}
TEST(CodeMemoryReportFormatter, NewArrayAllocatorGeneratesDeleteArrayCode)
{
formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
testOutput.flush();
formatter->report_free_memory(testResult, newArrayAllocator, memory01, "boo", 4);
TESTOUTPUT_CONTAINS("delete [] file_8_1; /* using delete [] at boo:4 */");
}
TEST(CodeMemoryReportFormatter, allocationUsingMallocOnTheSameLineDoesntGenerateTheSameVariableTwice)
{
formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 8);
testOutput.flush();
formatter->report_alloc_memory(testResult, cAllocator, 10, memory02, "file", 8);
CHECK(testOutput.getOutput().contains("2"));
}
TEST(CodeMemoryReportFormatter, allocationUsingNewcOnTheSameLineDoesntGenerateTheSameVariableTwice)
{
formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
testOutput.flush();
formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
CHECK(testOutput.getOutput().contains("2"));
}
TEST(CodeMemoryReportFormatter, allocationUsingNewcOnTheSameLineDoesntGenerateVariableTwiceExceptWhenInANewTest)
{
formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
formatter->report_test_start(testResult, *UtestShell::getCurrent());
testOutput.flush();
formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
CHECK(testOutput.getOutput().contains("char*"));
}
TEST(CodeMemoryReportFormatter, testStartGeneratesTESTcode)
{
UtestShell test("groupName", "testName", "fileName", 1);
formatter->report_test_start(testResult, test);
TESTOUTPUT_EQUAL("*/\nTEST(groupName_memoryReport, testName)\n{ /* at fileName:1 */\n");
}
TEST(CodeMemoryReportFormatter, testEndGeneratesTESTcode)
{
UtestShell test("groupName", "testName", "fileName", 1);
formatter->report_test_end(testResult, test);
TESTOUTPUT_EQUAL("}/*");
}
TEST(CodeMemoryReportFormatter, TestGroupGeneratesTestGroupCode)
{
UtestShell test("groupName", "testName", "fileName", 1);
formatter->report_testgroup_start(testResult, test);
TESTOUTPUT_EQUAL("*/TEST_GROUP(groupName_memoryReport)\n{\n};\n/*");
}
TEST(CodeMemoryReportFormatter, VariableFromFileLineInfoAlreadyExists)
{
for(int i = 1; i < 100; i++) {
formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
}
formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
testOutput.flush();
formatter->report_free_memory(testResult, newArrayAllocator, memory01, "boo", 8);
TESTOUTPUT_CONTAINS("delete [] ; /* using delete [] at boo:8 */");
}
// TODO: do!
/* Dealloc without alloc */
/* Remove the ugly comments by controlling the output! */
/* Write tests for the variable name lengths */

View file

@ -0,0 +1,336 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockExpectedCallsList.h"
#include "CppUTestExt/MockCheckedExpectedCall.h"
#include "CppUTestExt/MockFailure.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockExpectedCallsList)
{
MockExpectedCallsList * list;
MockCheckedExpectedCall* call1;
MockCheckedExpectedCall* call2;
MockCheckedExpectedCall* call3;
MockCheckedExpectedCall* call4;
void setup() _override
{
list = new MockExpectedCallsList;
call1 = new MockCheckedExpectedCall;
call2 = new MockCheckedExpectedCall;
call3 = new MockCheckedExpectedCall;
call4 = new MockCheckedExpectedCall;
call1->withName("foo");
call2->withName("bar");
call3->withName("boo");
}
void teardown() _override
{
delete call1;
delete call2;
delete call3;
delete call4;
delete list;
CHECK_NO_MOCK_FAILURE();
MockFailureReporterForTest::clearReporter();
}
};
TEST(MockExpectedCallsList, emptyList)
{
CHECK(! list->hasUnfulfilledExpectations());
CHECK(! list->hasFinalizedMatchingExpectations());
LONGS_EQUAL(0, list->size());
}
TEST(MockExpectedCallsList, addingCalls)
{
list->addExpectedCall(call1);
list->addExpectedCall(call2);
LONGS_EQUAL(2, list->size());
}
TEST(MockExpectedCallsList, listWithFulfilledExpectationHasNoUnfulfilledOnes)
{
call1->callWasMade(1);
call2->callWasMade(2);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
CHECK(! list->hasUnfulfilledExpectations());
}
TEST(MockExpectedCallsList, listWithFulfilledExpectationButOutOfOrder)
{
call1->withCallOrder(1);
call2->withCallOrder(2);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
call2->callWasMade(1);
call1->callWasMade(2);
CHECK(! list->hasUnfulfilledExpectations());
CHECK(list->hasCallsOutOfOrder());
}
TEST(MockExpectedCallsList, listWithUnFulfilledExpectationHasNoUnfillfilledOnes)
{
call1->callWasMade(1);
call3->callWasMade(2);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
CHECK(list->hasUnfulfilledExpectations());
}
TEST(MockExpectedCallsList, deleteAllExpectationsAndClearList)
{
list->addExpectedCall(new MockCheckedExpectedCall);
list->addExpectedCall(new MockCheckedExpectedCall);
list->deleteAllExpectationsAndClearList();
}
TEST(MockExpectedCallsList, onlyKeepUnmatchingExpectations)
{
call1->withName("relate");
call2->withName("unrelate");
call3->withName("relate").withParameter("param",1);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
list->onlyKeepUnmatchingExpectations();
LONGS_EQUAL(1, list->size());
}
TEST(MockExpectedCallsList, onlyKeepExpectationsRelatedTo)
{
call1->withName("relate");
call2->withName("unrelate");
call3->withName("unrelate");
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
list->onlyKeepExpectationsRelatedTo("relate");
LONGS_EQUAL(1, list->size());
}
TEST(MockExpectedCallsList, removeAllExpectationsExceptThisThatRelateToTheWoleList)
{
call1->withName("relate");
call2->withName("relate");
call3->withName("relate");
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
list->onlyKeepExpectationsRelatedTo("unrelate");
LONGS_EQUAL(0, list->size());
}
TEST(MockExpectedCallsList, removeAllExpectationsExceptThisThatRelateToFirstOne)
{
call1->withName("relate");
call2->withName("unrelate");
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->onlyKeepExpectationsRelatedTo("unrelate");
LONGS_EQUAL(1, list->size());
}
TEST(MockExpectedCallsList, removeAllExpectationsExceptThisThatRelateToLastOne)
{
call1->withName("unrelate");
call2->withName("relate");
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->onlyKeepExpectationsRelatedTo("unrelate");
LONGS_EQUAL(1, list->size());
}
TEST(MockExpectedCallsList, onlyKeepExpectationsWithInputParameterName)
{
call1->withName("func").withParameter("param", 1);
call2->withName("func").withParameter("diffname", 1);
call3->withName("func").withParameter("diffname", 1);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
list->onlyKeepExpectationsWithInputParameterName("diffname");
LONGS_EQUAL(2, list->size());
}
TEST(MockExpectedCallsList, onlyKeepExpectationsWithInputParameter)
{
MockNamedValue parameter("diffname");
parameter.setValue(1);
call1->withName("func").withParameter("param", 1);
call2->withName("func").withParameter("diffname", 1);
call3->withName("func").withParameter("diffname", 1);
call4->withName("func").withParameter("diffname", 2);
call3->callWasMade(1);
call3->inputParameterWasPassed("diffname");
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
list->addExpectedCall(call4);
list->onlyKeepExpectationsWithInputParameter(parameter);
LONGS_EQUAL(2, list->size());
}
TEST(MockExpectedCallsList, addPotentiallyMatchingExpectationsWithEmptyList)
{
MockExpectedCallsList newList;
newList.addPotentiallyMatchingExpectations(*list);
LONGS_EQUAL(0, newList.size());
}
TEST(MockExpectedCallsList, addPotentiallyMatchingExpectationsMultipleUnmatchedExpectations)
{
call2->callWasMade(1);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
MockExpectedCallsList newList;
newList.addPotentiallyMatchingExpectations(*list);
LONGS_EQUAL(2, newList.size());
}
TEST(MockExpectedCallsList, amountOfActualCallsFulfilledFor_HasOneRelated)
{
call1->withName("foo");
call1->callWasMade(1);
call2->withName("bar");
call2->callWasMade(2);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
LONGS_EQUAL(1, list->amountOfActualCallsFulfilledFor("bar"));
}
TEST(MockExpectedCallsList, amountOfActualCallsFulfilledFor_HasNone)
{
call1->withName("foo");
call1->callWasMade(1);
call2->withName("bar");
list->addExpectedCall(call1);
LONGS_EQUAL(0, list->amountOfActualCallsFulfilledFor("bar"));
}
TEST(MockExpectedCallsList, callToStringForUnfulfilledFunctions)
{
call1->withName("foo");
call2->withName("bar");
call3->withName("blah");
call3->callWasMade(1);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
SimpleString expectedString;
expectedString = StringFromFormat("%s\n%s", call1->callToString().asCharString(), call2->callToString().asCharString());
STRCMP_EQUAL(expectedString.asCharString(), list->unfulfilledCallsToString().asCharString());
}
TEST(MockExpectedCallsList, callToStringForFulfilledFunctions)
{
call1->withName("foo");
call2->withName("bar");
call2->callWasMade(1);
call1->callWasMade(2);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
SimpleString expectedString;
expectedString = StringFromFormat("%s\n%s", call1->callToString().asCharString(), call2->callToString().asCharString());
STRCMP_EQUAL(expectedString.asCharString(), list->fulfilledCallsToString().asCharString());
}
TEST(MockExpectedCallsList, removeOneFinalizedMatchingExpectationFromEmptyList)
{
POINTERS_EQUAL(NULLPTR, list->removeFirstFinalizedMatchingExpectation());
}
TEST(MockExpectedCallsList, getOneMatchingExpectationFromEmptyList)
{
POINTERS_EQUAL(NULLPTR, list->getFirstMatchingExpectation());
}
TEST(MockExpectedCallsList, toStringOnEmptyList)
{
STRCMP_EQUAL("<none>", list->unfulfilledCallsToString().asCharString());
}
TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_emptyList)
{
CHECK(! list->hasFinalizedMatchingExpectations());
}
TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasNonMatchingCalls)
{
call1->withParameter("param", 0);
call2->withParameter("param", 0);
call3->withParameter("param", 0);
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
CHECK(! list->hasFinalizedMatchingExpectations());
}
TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasMatchingButNotFinalizedCall)
{
list->addExpectedCall(call1);
list->addExpectedCall(call2);
call1->ignoreOtherParameters();
call2->withParameter("param", 0);
CHECK(! list->hasFinalizedMatchingExpectations());
}
TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasFinalizedCallThatIgnoresParameters)
{
list->addExpectedCall(call1);
list->addExpectedCall(call2);
call1->ignoreOtherParameters();
call2->withParameter("param", 0);
call1->finalizeActualCallMatch();
CHECK(list->hasFinalizedMatchingExpectations());
}
TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasFinalizedCallThatDoesntIgnoreParameters)
{
list->addExpectedCall(call1);
list->addExpectedCall(call2);
call1->withParameter("param", 1);
call2->withParameter("param", 0);
call1->inputParameterWasPassed("param");
CHECK(list->hasFinalizedMatchingExpectations());
}

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2011, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef CPPUTEST_INCLUDE_GTEST_TESTS
#include "CppUTestExt/GMock.h"
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTest/TestTestingFixture.h"
TEST_GROUP(GMock)
{
TestTestingFixture *fixture;
void setup() _override
{
fixture = new TestTestingFixture;
}
void teardown() _override
{
delete fixture;
}
};
class myMock
{
public:
MOCK_METHOD0(methodName, int());
};
static void failedMockCall()
{
myMock mock;
EXPECT_CALL(mock, methodName()).WillOnce(Return(1));
}
TEST(GMock, GMockFailuresWorkAsExpected)
{
fixture->setTestFunction(failedMockCall);
fixture->runAllTests();
LONGS_EQUAL(1, fixture->getFailureCount());
}
static void failedMockCallAfterOneSuccess()
{
myMock mock;
EXPECT_CALL(mock, methodName()).Times(2).WillRepeatedly(Return(1));
mock.methodName();
}
TEST(GMock, GMockFailuresWorkAsExpectedWithTwoExpectedCallButJustOneActual)
{
fixture->setTestFunction(failedMockCallAfterOneSuccess);
fixture->runAllTests();
LONGS_EQUAL(1, fixture->getFailureCount());
}
TEST(GMock, GMockNiceMocksWorkFine)
{
NiceMock<myMock> mock;
mock.methodName();
}
#endif

View file

@ -0,0 +1,209 @@
/*
* Copyright (c) 2011, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef CPPUTEST_INCLUDE_GTEST_TESTS
#undef new
#include "CppUTestExt/GTest.h"
#include "CppUTestExt/GMock.h"
static bool g_GTestEqual_has_been_called = false;
TEST(GTestSimpleTest, GTestEqual)
{
EXPECT_EQ(1, 1);
g_GTestEqual_has_been_called = true;
}
TEST(GTestSimpleTest, GTestAssertEq)
{
ASSERT_EQ(1, 1);
}
TEST(GTestSimpleTest, GTestExpectTrue)
{
EXPECT_TRUE(true);
}
TEST(GTestSimpleTest, GTestAssertTrue)
{
ASSERT_TRUE(true);
}
TEST(GTestSimpleTest, GTestExpectFalse)
{
EXPECT_FALSE(false);
}
TEST(GTestSimpleTest, GTestExpectStreq)
{
EXPECT_STREQ("hello world", "hello world");
}
/* Death tests are IMHO not a good idea at all. But for compatibility reason, we'll support it */
static void crashMe ()
{
fprintf(stderr, "Crash me!");
*((int*) 0) = 10;
}
TEST(GTestSimpleTest, GTestDeathTest)
{
#if defined(GTEST_VERSION_GTEST_1_7)
CppuTestGTestIgnoreLeaksInTest();
#endif
ASSERT_DEATH(crashMe(), "Crash me!");
}
#undef TEST
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTest/TestTestingFixture.h"
TEST_GROUP(gtest)
{
};
TEST(gtest, SimpleGoogleTestExists)
{
TestRegistry* registry = TestRegistry::getCurrentRegistry();
CHECK(registry->findTestWithName("GTestEqual"));
}
TEST(gtest, SimpleGoogleTestGroupExists)
{
TestRegistry* registry = TestRegistry::getCurrentRegistry();
CHECK(registry->findTestWithGroup("GTestSimpleTest"));
}
TEST(gtest, SimpleGoogleTestGetCalled)
{
StringBufferTestOutput output;
TestResult result(output);
TestPlugin plugin("dummy");
TestRegistry* registry = TestRegistry::getCurrentRegistry();
UtestShell * shell = registry->findTestWithName("GTestEqual");
g_GTestEqual_has_been_called = false;
shell->runOneTest(&plugin, result);
CHECK(g_GTestEqual_has_been_called);
}
static bool afterCheck;
static void _failMethodEXPECT_EQ()
{
EXPECT_EQ(1, 2);
afterCheck = true;
}
static void _failMethodASSERT_EQ()
{
ASSERT_EQ(1, 2);
afterCheck = true;
}
static void _failMethodEXPECT_TRUE()
{
EXPECT_TRUE(false);
afterCheck = true;
}
static void _failMethodASSERT_TRUE()
{
ASSERT_TRUE(false);
afterCheck = true;
}
static void _failMethodEXPECT_FALSE()
{
EXPECT_FALSE(true);
afterCheck = true;
}
static void _failMethodEXPECT_STREQ()
{
EXPECT_STREQ("hello", "world");
afterCheck = true;
}
TEST_GROUP(gtestMacros)
{
TestTestingFixture* fixture;
void setup() _override
{
fixture = new TestTestingFixture();
afterCheck = false;
}
void teardown() _override
{
delete fixture;
}
void testFailureWith(void(*method)())
{
fixture->setTestFunction(method);
fixture->runAllTests();
LONGS_EQUAL(1, fixture->getFailureCount());
CHECK(!afterCheck);
}
};
TEST(gtestMacros, EXPECT_EQFails)
{
testFailureWith(_failMethodEXPECT_EQ);
}
TEST(gtestMacros, EXPECT_TRUEFails)
{
testFailureWith(_failMethodEXPECT_TRUE);
}
TEST(gtestMacros, EXPECT_FALSEFails)
{
testFailureWith(_failMethodEXPECT_FALSE);
}
TEST(gtestMacros, EXPECT_STREQFails)
{
testFailureWith(_failMethodEXPECT_STREQ);
}
TEST(gtestMacros, ASSERT_EQFails)
{
testFailureWith(_failMethodASSERT_EQ);
}
TEST(gtestMacros, ASSERT_TRUEFails)
{
testFailureWith(_failMethodASSERT_TRUE);
}
#endif

View file

@ -0,0 +1,109 @@
/*
* Copyright (c) 2011, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef CPPUTEST_INCLUDE_GTEST_TESTS
#include "CppUTestExt/GTestConvertor.h"
class GTestTestingFixtureTest : public testing::Test {
protected:
bool setup_was_called;
char* freed_during_teardown;
void SetUp() _override
{
setup_was_called = true;
freed_during_teardown = NULL;
}
void TearDown() _override
{
delete [] freed_during_teardown;
}
};
TEST_F(GTestTestingFixtureTest, setupBeenCalled)
{
EXPECT_TRUE(setup_was_called);
}
TEST_F(GTestTestingFixtureTest, teardownMustBeCalledOrElseThisWillLeak)
{
freed_during_teardown = new char[100];
}
#endif
#undef TEST
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTest/TestTestingFixture.h"
TEST_GROUP(GTestConvertor)
{
};
#ifdef CPPUTEST_INCLUDE_GTEST_TESTS
/*
* These tests depend on the amount of GTests that are actually added (also in other files).
* This is due to the singleton in gtest.
*
*/
TEST(GTestConvertor, correctNumberOfTestCases)
{
LONGS_EQUAL(2, ::testing::UnitTest::GetInstance()->total_test_case_count());
CHECK(::testing::UnitTest::GetInstance()->GetTestCase(0));
CHECK(::testing::UnitTest::GetInstance()->GetTestCase(1));
CHECK(::testing::UnitTest::GetInstance()->GetTestCase(2) == NULL);
}
TEST(GTestConvertor, correctNumberOfTestsInTheTestCases)
{
const ::testing::TestCase* firstTestCase = ::testing::UnitTest::GetInstance()->GetTestCase(0);
const ::testing::TestCase* secondTestCase = ::testing::UnitTest::GetInstance()->GetTestCase(1);
STRCMP_EQUAL("GTestSimpleTest", firstTestCase->name());
STRCMP_EQUAL("GTestTestingFixtureTest", secondTestCase->name());
LONGS_EQUAL(7, firstTestCase->total_test_count());
LONGS_EQUAL(2, secondTestCase->total_test_count());
}
TEST(GTestConvertor, testsGetAddedToCurrentTestRegistry)
{
TestTestingFixture fixture;
TestRegistry::getCurrentRegistry()->unDoLastAddTest();
GTestConvertor convertor(false);
convertor.addAllGTestToTestRegistry();
LONGS_EQUAL(9, TestRegistry::getCurrentRegistry()->countTests());
}
#endif

View file

@ -0,0 +1,147 @@
/*
* Copyright (c) 2015, Michael Feathers, James Grenning, Bas Vodde
* and Arnd R. Strube. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTest/TestTestingFixture.h"
#include "CppUTestExt/IEEE754ExceptionsPlugin.h"
#ifdef CPPUTEST_HAVE_FENV
#if CPPUTEST_FENV_IS_WORKING_PROPERLY
extern "C"
{
#include "IEEE754PluginTest_c.h"
}
TEST_GROUP(FE__with_Plugin)
{
TestTestingFixture fixture;
IEEE754ExceptionsPlugin ieee754Plugin;
void setup(void) _override
{
fixture.installPlugin(&ieee754Plugin);
}
};
TEST(FE__with_Plugin, should_fail____when__FE_DIVBYZERO__is_set)
{
fixture.setTestFunction(set_divisionbyzero_c);
fixture.runAllTests();
fixture.assertPrintContains("IEEE754_CHECK_CLEAR(FE_DIVBYZERO) failed");
}
TEST(FE__with_Plugin, should_fail____when__FE_OVERFLOW___is_set)
{
fixture.setTestFunction(set_overflow_c);
fixture.runAllTests();
fixture.assertPrintContains("IEEE754_CHECK_CLEAR(FE_OVERFLOW) failed");
}
TEST(FE__with_Plugin, should_fail____when__FE_UNDERFLOW__is_set)
{
fixture.setTestFunction(set_underflow_c);
fixture.runAllTests();
fixture.assertPrintContains("IEEE754_CHECK_CLEAR(FE_UNDERFLOW) failed");
}
TEST(FE__with_Plugin, should_fail____when__FE_INEXACT____is_set_and_enabled)
{
IEEE754ExceptionsPlugin::enableInexact();
fixture.setTestFunction(set_inexact_c);
fixture.runAllTests();
fixture.assertPrintContains("IEEE754_CHECK_CLEAR(FE_INEXACT) failed");
}
TEST(FE__with_Plugin, should_succeed_when__FE_INEXACT____is_set_and_disabled)
{
IEEE754ExceptionsPlugin::enableInexact();
IEEE754ExceptionsPlugin::disableInexact();
fixture.setTestFunction(set_inexact_c);
fixture.runAllTests();
fixture.assertPrintContains("OK");
}
TEST(FE__with_Plugin, should_succeed_with_5_checks_when_no_flags_are_set)
{
IEEE754ExceptionsPlugin::enableInexact();
fixture.setTestFunction(set_nothing_c);
fixture.runAllTests();
fixture.assertPrintContains("OK (1 tests, 1 ran, 5 checks, 0 ignored, 0 filtered out");
IEEE754ExceptionsPlugin::disableInexact();
}
TEST(FE__with_Plugin, should_check_five_times_when_all_flags_are_set)
{
fixture.setTestFunction(set_everything_c);
fixture.runAllTests();
LONGS_EQUAL(5, fixture.getCheckCount());
}
TEST(FE__with_Plugin, should_fail_only_once_when_all_flags_are_set)
{
fixture.setTestFunction(set_everything_c);
fixture.runAllTests();
LONGS_EQUAL(1, fixture.getFailureCount());
}
static void set_everything_but_already_failed(void)
{
set_everything_c();
CHECK(1 == 2);
}
TEST(FE__with_Plugin, should_not_fail_again_when_test_has_already_failed)
{
fixture.setTestFunction(set_everything_but_already_failed);
fixture.runAllTests();
CHECK(IEEE754ExceptionsPlugin::checkIeee754OverflowExceptionFlag());
CHECK(IEEE754ExceptionsPlugin::checkIeee754UnderflowExceptionFlag());
CHECK(IEEE754ExceptionsPlugin::checkIeee754InexactExceptionFlag());
CHECK(IEEE754ExceptionsPlugin::checkIeee754DivByZeroExceptionFlag());
LONGS_EQUAL(1, fixture.getCheckCount());
LONGS_EQUAL(1, fixture.getFailureCount());
}
static IEEE754ExceptionsPlugin ip;
TEST_GROUP(IEEE754ExceptionsPlugin2)
{
void setup(void) _override
{
TestRegistry::getCurrentRegistry()->installPlugin(&ip);
}
};
IGNORE_TEST(IEEE754ExceptionsPlugin2, should_not_fail_in_ignored_test)
{
set_everything_c();
}
#endif
#endif

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 2015, Michael Feathers, James Grenning, Bas Vodde
* and Arnd R. Strube. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "IEEE754PluginTest_c.h"
static volatile float f;
static volatile float zero_float = 0.0f;
void set_divisionbyzero_c(void)
{
f = 1.0f;
f /= zero_float;
}
void set_overflow_c(void)
{
f = 1e38f;
f *= f;
}
void set_underflow_c(void)
{
f = 1e-38f;
f *= f;
}
void set_inexact_c(void)
{
f = 10.0f;
f /= 3.0f;
}
void set_nothing_c(void)
{
}
void set_everything_c()
{
set_divisionbyzero_c();
set_overflow_c();
set_underflow_c();
set_inexact_c();
}

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2015, Michael Feathers, James Grenning, Bas Vodde
* and Arnd R. Strube. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
void set_divisionbyzero_c(void);
void set_overflow_c(void);
void set_underflow_c(void);
void set_inexact_c(void);
void set_nothing_c(void);
void set_everything_c(void);

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTestExt/MemoryReportAllocator.h"
#include "CppUTestExt/MemoryReportFormatter.h"
TEST_GROUP(MemoryReportAllocator)
{
MemoryReportAllocator allocator;
};
TEST(MemoryReportAllocator, FunctionsAreForwardedForMallocAllocator)
{
allocator.setRealAllocator(getCurrentMallocAllocator());
STRCMP_EQUAL("malloc", allocator.alloc_name());
STRCMP_EQUAL("free", allocator.free_name());
}
TEST(MemoryReportAllocator, keepingTrackOfActualAllocator)
{
TestMemoryAllocator* originalAllocator = getCurrentMallocAllocator();
allocator.setRealAllocator(getCurrentMallocAllocator());
POINTERS_EQUAL(originalAllocator->actualAllocator(), allocator.actualAllocator());
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTestExt/MemoryReportAllocator.h"
#include "CppUTestExt/MemoryReportFormatter.h"
#define TESTOUTPUT_EQUAL(a) STRCMP_EQUAL_LOCATION(a, testOutput.getOutput().asCharString(), "", __FILE__, __LINE__)
TEST_GROUP(NormalMemoryReportFormatter)
{
char* memory01;
StringBufferTestOutput testOutput;
TestResult* testResult;
NormalMemoryReportFormatter formatter;
void setup() _override
{
memory01 = (char*) 0x01;
testResult = new TestResult(testOutput);
}
void teardown() _override
{
delete testResult;
}
};
TEST(NormalMemoryReportFormatter, mallocCreatesAnMallocCall)
{
formatter.report_alloc_memory(testResult, defaultMallocAllocator(), 10, memory01, "file", 9);
TESTOUTPUT_EQUAL(StringFromFormat("\tAllocation using malloc of size: 10 pointer: %p at file:9\n", (void*) memory01).asCharString());
}
TEST(NormalMemoryReportFormatter, freeCreatesAnFreeCall)
{
formatter.report_free_memory(testResult, defaultMallocAllocator(), memory01, "boo", 6);
TESTOUTPUT_EQUAL(StringFromFormat("\tDeallocation using free of pointer: %p at boo:6\n", (void*) memory01).asCharString());
}
TEST(NormalMemoryReportFormatter, testStarts)
{
UtestShell test("groupName", "TestName", "file", 1);
formatter.report_test_start(testResult, test);
TESTOUTPUT_EQUAL("TEST(groupName, TestName)\n");
}
TEST(NormalMemoryReportFormatter, testEnds)
{
UtestShell test("groupName", "TestName", "file", 1);
formatter.report_test_end(testResult, test);
TESTOUTPUT_EQUAL("ENDTEST(groupName, TestName)\n");
}
TEST(NormalMemoryReportFormatter, testGroupStarts)
{
UtestShell test("groupName", "TestName", "file", 1);
formatter.report_testgroup_start(testResult, test);
TESTOUTPUT_EQUAL("------------------------------TEST GROUP(groupName)-----------------------------\n");
}

View file

@ -0,0 +1,316 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTestExt/MemoryReporterPlugin.h"
#include "CppUTestExt/MemoryReportFormatter.h"
#include "CppUTestExt/MockSupport.h"
#include "CppUTestExt/MockNamedValue.h"
static TestMemoryAllocator* previousNewAllocator;
class TemporaryDefaultNewAllocator
{
TestMemoryAllocator* newAllocator;
public:
TemporaryDefaultNewAllocator(TestMemoryAllocator* oldAllocator)
{
newAllocator = getCurrentNewAllocator();
setCurrentNewAllocator(oldAllocator);
}
~TemporaryDefaultNewAllocator()
{
setCurrentNewAllocator(newAllocator);
}
};
class MockMemoryReportFormatter : public MemoryReportFormatter
{
public:
virtual void report_testgroup_start(TestResult* result, UtestShell& test) _override
{
TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator);
mock("formatter").actualCall("report_testgroup_start").withParameter("result", result).withParameter("test", &test);
}
virtual void report_testgroup_end(TestResult* result, UtestShell& test) _override
{
TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator);
mock("formatter").actualCall("report_testgroup_end").withParameter("result", result).withParameter("test", &test);
}
virtual void report_test_start(TestResult* result, UtestShell& test) _override
{
TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator);
mock("formatter").actualCall("report_test_start").withParameter("result", result).withParameter("test", &test);
}
virtual void report_test_end(TestResult* result, UtestShell& test) _override
{
TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator);
mock("formatter").actualCall("report_test_end").withParameter("result", result).withParameter("test", &test);
}
virtual void report_alloc_memory(TestResult* result, TestMemoryAllocator* allocator, size_t, char* , const char* , size_t ) _override
{
TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator);
mock("formatter").actualCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", allocator);
}
virtual void report_free_memory(TestResult* result, TestMemoryAllocator* allocator, char* , const char* , size_t ) _override
{
TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator);
mock("formatter").actualCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", allocator);
}
};
static MockMemoryReportFormatter formatterForPluginTest;
class MemoryReporterPluginUnderTest : public MemoryReporterPlugin
{
public:
MemoryReportFormatter* createMemoryFormatter(const SimpleString& type) _override
{
mock("reporter").actualCall("createMemoryFormatter").onObject(this).withParameter("type", type.asCharString());
return new MockMemoryReportFormatter;
}
};
class TestMemoryAllocatorComparator : public MockNamedValueComparator
{
public:
bool isEqual(const void* object1, const void* object2) _override
{
return ((const TestMemoryAllocator*)object1)->name() == ((const TestMemoryAllocator*)object2)->name();
}
SimpleString valueToString(const void* object) _override
{
return ((const TestMemoryAllocator*)object)->name();
}
};
TEST_GROUP(MemoryReporterPlugin)
{
MemoryReporterPluginUnderTest* reporter;
StringBufferTestOutput output;
TestMemoryAllocatorComparator memLeakAllocatorComparator;
TestResult* result;
UtestShell* test;
void setup() _override
{
previousNewAllocator = getCurrentNewAllocator();
result = new TestResult(output);
test = new UtestShell("groupname", "testname", "filename", 1);
reporter = new MemoryReporterPluginUnderTest;
mock("formatter").installComparator("TestMemoryAllocator", memLeakAllocatorComparator);
mock("reporter").disable();
const char *cmd_line[] = {"-pmemoryreport=normal"};
reporter->parseArguments(1, cmd_line, 0);
mock("reporter").enable();
}
void teardown() _override
{
setCurrentNewAllocator(previousNewAllocator);
mock().clear();
delete reporter;
delete test;
delete result;
}
};
TEST(MemoryReporterPlugin, offReportsNothing)
{
MemoryReporterPluginUnderTest freshReporter;
freshReporter.preTestAction(*test, *result);
char* memory = new char;
delete memory;
freshReporter.postTestAction(*test, *result);
}
TEST(MemoryReporterPlugin, meaninglessArgumentsAreIgnored)
{
const char *cmd_line[] = {"-nothing", "-pnotmemoryreport=normal", "alsomeaningless", "-pmemoryreportnonsensebutnotus"};
CHECK(reporter->parseArguments(3, cmd_line, 1) == false);
}
TEST(MemoryReporterPlugin, commandLineParameterTurnsOnNormalLogging)
{
mock("reporter").expectOneCall("createMemoryFormatter").onObject(reporter).withParameter("type", "normal");
const char *cmd_line[] = {"-nothing", "-pmemoryreport=normal", "alsomeaningless" };
CHECK(reporter->parseArguments(3, cmd_line, 1));
}
TEST(MemoryReporterPlugin, preTestActionReportsTest)
{
mock("formatter").expectOneCall("report_testgroup_start").withParameter("result", result).withParameter("test", test);
mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test);
reporter->preTestAction(*test, *result);
}
TEST(MemoryReporterPlugin, postTestActionReportsTest)
{
mock("formatter").expectOneCall("report_test_end").withParameter("result", result).withParameter("test", test);
mock("formatter").expectOneCall("report_testgroup_end").withParameter("result", result).withParameter("test", test);
reporter->postTestAction(*test, *result);
}
TEST(MemoryReporterPlugin, newAllocationsAreReportedTest)
{
mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewAllocator());
mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewAllocator());
mock("formatter").ignoreOtherCalls();
reporter->preTestAction(*test, *result);
char *memory = getCurrentNewAllocator()->allocMemoryLeakNode(100);
getCurrentNewAllocator()->free_memory(memory, 100, "unknown", 1);
}
TEST(MemoryReporterPlugin, whenUsingOnlyMallocAllocatorNoOtherOfTheAllocatorsAreUsed)
{
mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test);
mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator());
mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator());
mock("formatter").ignoreOtherCalls();
reporter->preTestAction(*test, *result);
char *memory = getCurrentMallocAllocator()->allocMemoryLeakNode(100);
getCurrentMallocAllocator()->free_memory(memory, 100, "unknown", 1);
}
TEST(MemoryReporterPlugin, newArrayAllocationsAreReportedTest)
{
mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewArrayAllocator());
mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewArrayAllocator());
mock("formatter").ignoreOtherCalls();
reporter->preTestAction(*test, *result);
char *memory = getCurrentNewArrayAllocator()->allocMemoryLeakNode(100);
getCurrentNewArrayAllocator()->free_memory(memory, 100, "unknown", 1);
}
TEST(MemoryReporterPlugin, mallocAllocationsAreReportedTest)
{
mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator());
mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator());
mock("formatter").ignoreOtherCalls();
reporter->preTestAction(*test, *result);
char *memory = getCurrentMallocAllocator()->allocMemoryLeakNode(100);
getCurrentMallocAllocator()->free_memory(memory, 100, "unknown", 1);
}
TEST(MemoryReporterPlugin, startOfANewTestWillReportTheTestGroupStart)
{
mock("formatter").expectOneCall("report_testgroup_start").withParameter("result", result).withParameter("test", test);
mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test);
mock("formatter").expectOneCall("report_test_end").withParameter("result", result).withParameter("test", test);
mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test);
mock("formatter").expectOneCall("report_test_end").withParameter("result", result).withParameter("test", test);
mock("formatter").ignoreOtherCalls();
reporter->preTestAction(*test, *result);
reporter->postTestAction(*test, *result);
reporter->preTestAction(*test, *result);
reporter->postTestAction(*test, *result);
}
class UtestForMemoryReportingPlugingTest : public UtestShell
{
public:
UtestForMemoryReportingPlugingTest(const char* groupname, UtestShell* test) : UtestShell(groupname, "testname", "filename", 1, test)
{
}
};
TEST(MemoryReporterPlugin, endOfaTestGroupWillReportSo)
{
UtestForMemoryReportingPlugingTest fourthTest("differentGroupName", NULLPTR);
UtestForMemoryReportingPlugingTest thirdTest("differentGroupName", &fourthTest);
UtestForMemoryReportingPlugingTest secondTest("groupname", &thirdTest);
UtestForMemoryReportingPlugingTest firstTest("groupname", &secondTest);
mock("formatter").expectOneCall("report_testgroup_end").withParameter("result", result).withParameter("test", &secondTest);
mock("formatter").ignoreOtherCalls();
reporter->preTestAction(firstTest, *result);
reporter->postTestAction(firstTest, *result);
reporter->preTestAction(secondTest, *result);
reporter->postTestAction(secondTest, *result);
reporter->preTestAction(thirdTest, *result);
reporter->postTestAction(thirdTest, *result);
}
TEST(MemoryReporterPlugin, preActionReplacesAllocators)
{
mock("formatter").ignoreOtherCalls();
TestMemoryAllocator* allocator = getCurrentMallocAllocator();
reporter->preTestAction(*test, *result);
CHECK(allocator != getCurrentMallocAllocator());
}
TEST(MemoryReporterPlugin, postActionRestoresAllocators)
{
mock("formatter").ignoreOtherCalls();
TestMemoryAllocator* allocator = getCurrentMallocAllocator();
reporter->preTestAction(*test, *result);
reporter->postTestAction(*test, *result);
CHECK(allocator == getCurrentMallocAllocator());
}
TEST(MemoryReporterPlugin, shouldCreateNormalMemoryReportFormatterWithoutMock)
{
MemoryReporterPlugin realReporter;
const char *cmd_line[] = {"-pmemoryreport=normal"};
CHECK(realReporter.parseArguments(1, cmd_line, 0));
}
TEST(MemoryReporterPlugin, shouldCreateCodeMemoryReportFormatterWithoutMock)
{
MemoryReporterPlugin realReporter;
const char *cmd_line[] = {"-pmemoryreport=code"};
CHECK(realReporter.parseArguments(1, cmd_line, 0));
}
TEST(MemoryReporterPlugin, shouldntCrashCreateInvalidMemoryReportFormatterWithoutMock)
{
MemoryReporterPlugin realReporter;
const char *cmd_line[] = {"-pmemoryreport=foo"};
CHECK(realReporter.parseArguments(1, cmd_line, 0));
realReporter.preTestAction(*test, *result);
realReporter.postTestAction(*test, *result);
}

View file

@ -0,0 +1,272 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockCheckedActualCall.h"
#include "CppUTestExt/MockCheckedExpectedCall.h"
#include "CppUTestExt/MockExpectedCallsList.h"
#include "CppUTestExt/MockFailure.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockCheckedActualCall)
{
MockExpectedCallsList* emptyList;
MockExpectedCallsList* list;
MockFailureReporter* reporter;
void setup() _override
{
emptyList = new MockExpectedCallsList;
list = new MockExpectedCallsList;
reporter = MockFailureReporterForTest::getReporter();
}
void teardown() _override
{
CHECK_NO_MOCK_FAILURE();
MockFailureReporterForTest::clearReporter();
delete emptyList;
delete list;
}
};
TEST(MockCheckedActualCall, unExpectedCall)
{
MockCheckedActualCall actualCall(1, reporter, *emptyList);
actualCall.withName("unexpected");
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCheckedActualCall, unExpectedCallWithAParameter)
{
MockCheckedActualCall actualCall(1, reporter, *emptyList);
actualCall.withName("unexpected").withParameter("bar", 0);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCheckedActualCall, unExpectedCallWithAnOutputParameter)
{
MockCheckedActualCall actualCall(1, reporter, *emptyList);
actualCall.withName("unexpected").withOutputParameter("bar", NULLPTR);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCheckedActualCall, unExpectedCallOnObject)
{
int object;
MockCheckedActualCall actualCall(1, reporter, *emptyList);
actualCall.withName("unexpected").onObject(&object);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
CHECK(actualCall.hasFailed()); // Checks that onObject() doesn't "reset" call state
}
TEST(MockCheckedActualCall, actualCallWithNoReturnValueAndMeaninglessCallOrderForCoverage)
{
MockCheckedActualCall actualCall(1, reporter, *emptyList);
actualCall.withName("noreturn").withCallOrder(0).returnValue();
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "noreturn", *list);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCheckedActualCall, unExpectedParameterName)
{
MockCheckedExpectedCall call1;
call1.withName("func");
list->addExpectedCall(&call1);
MockCheckedActualCall actualCall(1, reporter, *list);
actualCall.withName("func").withParameter("integer", 1);
MockNamedValue parameter("integer");
parameter.setValue(1);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "func", parameter, *list);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCheckedActualCall, multipleSameFunctionsExpectingAndHappenGradually)
{
MockCheckedExpectedCall* call1 = new MockCheckedExpectedCall();
MockCheckedExpectedCall* call2 = new MockCheckedExpectedCall();
call1->withName("func");
call2->withName("func");
list->addExpectedCall(call1);
list->addExpectedCall(call2);
LONGS_EQUAL(2, list->amountOfUnfulfilledExpectations());
MockCheckedActualCall actualCall1(1, reporter, *list);
actualCall1.withName("func");
actualCall1.checkExpectations();
LONGS_EQUAL(1, list->amountOfUnfulfilledExpectations());
MockCheckedActualCall actualCall2(2, reporter, *list);
actualCall2.withName("func");
actualCall2.checkExpectations();
LONGS_EQUAL(0, list->amountOfUnfulfilledExpectations());
list->deleteAllExpectationsAndClearList();
}
TEST(MockCheckedActualCall, MockIgnoredActualCallWorksAsItShould)
{
MockIgnoredActualCall actual;
actual.withName("func");
actual.withCallOrder(1);
CHECK(false == actual.returnBoolValue());
CHECK(true == actual.returnBoolValueOrDefault(true));
CHECK(false == actual.returnBoolValueOrDefault(false));
CHECK(0 == actual.returnUnsignedLongIntValue());
CHECK(0 == actual.returnIntValue());
CHECK(1ul == actual.returnUnsignedLongIntValueOrDefault(1ul));
CHECK(1 == actual.returnIntValueOrDefault(1));
CHECK(0 == actual.returnLongIntValue());
CHECK(1l == actual.returnLongIntValueOrDefault(1l));
CHECK(0 == actual.returnUnsignedIntValue());
CHECK(1u == actual.returnUnsignedIntValueOrDefault(1u));
#ifdef CPPUTEST_USE_LONG_LONG
CHECK(0 == actual.returnLongLongIntValue());
CHECK(1ll == actual.returnLongLongIntValueOrDefault(1ll));
CHECK(0 == actual.returnUnsignedLongLongIntValue());
CHECK(1ull == actual.returnUnsignedLongLongIntValueOrDefault(1ull));
#endif
DOUBLES_EQUAL(0.0, actual.returnDoubleValue(), 0.0);
DOUBLES_EQUAL(1.5, actual.returnDoubleValueOrDefault(1.5), 0.0);
STRCMP_EQUAL("bla", actual.returnStringValueOrDefault("bla"));
STRCMP_EQUAL("", actual.returnStringValue());
CHECK(NULLPTR == actual.returnPointerValue());
CHECK((void*) 0x2 == actual.returnPointerValueOrDefault((void*) 0x2));
CHECK(NULLPTR == actual.returnConstPointerValue());
CHECK((const void*) 0x2 == actual.returnConstPointerValueOrDefault((const void*) 0x2));
CHECK(NULLPTR == actual.returnFunctionPointerValue());
CHECK((void(*)()) 1 == actual.returnFunctionPointerValueOrDefault((void(*)()) 0x1));
CHECK_FALSE(actual.hasReturnValue());
CHECK(actual.returnValue().equals(MockNamedValue("")));
}
TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould)
{
int value;
const int const_value = 1;
const unsigned char mem_buffer[] = { 0xFE, 0x15 };
void (*function_value)() = (void (*)())0xDEAD;
MockActualCallTrace actual;
actual.withName("func");
actual.withCallOrder(1);
actual.onObject(&value);
actual.withBoolParameter("bool", true);
actual.withUnsignedIntParameter("unsigned_int", (unsigned int) 1);
actual.withUnsignedLongIntParameter("unsigned_long", (unsigned long)1);
actual.withLongIntParameter("long_int", (long int) 1);
#ifdef CPPUTEST_USE_LONG_LONG
actual.withLongLongIntParameter("long_long_int", (long long int) 1);
actual.withUnsignedLongLongIntParameter("unsigned_long_long_int", (unsigned long long int) 1);
#endif
actual.withPointerParameter("pointer", &value);
actual.withConstPointerParameter("const_pointer", &const_value);
actual.withFunctionPointerParameter("function_pointer", function_value);
actual.withMemoryBufferParameter("mem_buffer", mem_buffer, sizeof(mem_buffer));
actual.withParameterOfType("int", "named_type", &const_value);
SimpleString expectedString("\nFunction name:func");
expectedString += " withCallOrder:1";
expectedString += " onObject:0x";
expectedString += HexStringFrom(&value);
expectedString += " bool:true";
expectedString += " unsigned_int:1 (0x1)";
expectedString += " unsigned_long:1 (0x1)";
expectedString += " long_int:1 (0x1)";
#ifdef CPPUTEST_USE_LONG_LONG
expectedString += " long_long_int:1 (0x1)";
expectedString += " unsigned_long_long_int:1 (0x1)";
#endif
expectedString += " pointer:0x";
expectedString += HexStringFrom(&value);
expectedString += " const_pointer:0x";
expectedString += HexStringFrom(&const_value);
expectedString += " function_pointer:0x";
expectedString += HexStringFrom(function_value);
expectedString += " mem_buffer:Size = 2 | HexContents = FE 15";
expectedString += " int named_type:0x";
expectedString += HexStringFrom(&const_value);
STRCMP_EQUAL(expectedString.asCharString(), actual.getTraceOutput());
CHECK_FALSE(actual.hasReturnValue());
CHECK(actual.returnValue().equals(MockNamedValue("")));
CHECK(false == actual.returnBoolValue());
CHECK(false == actual.returnBoolValueOrDefault(true));
CHECK(0 == actual.returnLongIntValue());
CHECK(0 == actual.returnUnsignedLongIntValue());
CHECK(0 == actual.returnIntValue());
CHECK(0 == actual.returnUnsignedLongIntValueOrDefault(1ul));
CHECK(0 == actual.returnIntValueOrDefault(1));
CHECK(0 == actual.returnLongIntValue());
CHECK(0 == actual.returnLongIntValueOrDefault(1l));
#ifdef CPPUTEST_USE_LONG_LONG
CHECK(0 == actual.returnLongLongIntValue());
CHECK(0 == actual.returnLongLongIntValueOrDefault(1ll));
CHECK(0 == actual.returnUnsignedLongLongIntValue());
CHECK(0 == actual.returnUnsignedLongLongIntValueOrDefault(1ull));
#endif
CHECK(0 == actual.returnUnsignedIntValue());
CHECK(0 == actual.returnUnsignedIntValueOrDefault(1u));
DOUBLES_EQUAL(0.0, actual.returnDoubleValue(), 0.0);
DOUBLES_EQUAL(0.0, actual.returnDoubleValueOrDefault(1.0), 0.0);
STRCMP_EQUAL("", actual.returnStringValueOrDefault("bla"));
STRCMP_EQUAL("", actual.returnStringValue());
CHECK(NULLPTR == actual.returnPointerValue());
CHECK(NULLPTR == actual.returnPointerValueOrDefault((void*) NULLPTR));
CHECK(NULLPTR == actual.returnConstPointerValue());
CHECK(NULLPTR == actual.returnConstPointerValueOrDefault((const void*) NULLPTR));
CHECK(NULLPTR == actual.returnFunctionPointerValue());
CHECK(NULLPTR == actual.returnFunctionPointerValueOrDefault((void (*)()) NULLPTR));
}
TEST(MockCheckedActualCall, MockActualCallTraceClear)
{
MockActualCallTrace actual;
actual.withName("func");
actual.clear();
STRCMP_EQUAL("", actual.getTraceOutput());
}

View file

@ -0,0 +1,502 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestTestingFixture.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockCallTest)
{
void teardown() _override
{
mock().checkExpectations();
mock().clear();
}
};
TEST(MockCallTest, clear)
{
mock().expectOneCall("func");
mock().clear();
CHECK(! mock().expectedCallsLeft());
}
TEST(MockCallTest, checkExpectationsDoesntFail)
{
mock().checkExpectations();
}
TEST(MockCallTest, expectASingleCallThatHappens)
{
mock().expectOneCall("func");
MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func");
actualCall.checkExpectations();
CHECK(! mock().expectedCallsLeft());
}
TEST(MockCallTest, expectASingleCallThatDoesntHappen)
{
mock().expectOneCall("func");
CHECK(mock().expectedCallsLeft());
mock().clear();
}
TEST(MockCallTest, expectAMultiCallThatHappensTheExpectedTimes)
{
mock().expectNCalls(2, "func");
mock().actualCall("func");
MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func");
actualCall.checkExpectations();
CHECK(! mock().expectedCallsLeft());
}
TEST(MockCallTest, expectAMultiCallThatDoesntHappenTheExpectedTimes)
{
mock().expectNCalls(2, "func");
MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func");
actualCall.checkExpectations();
CHECK(mock().expectedCallsLeft());
mock().clear();
}
TEST(MockCallTest, checkExpectationsClearsTheExpectations)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foobar");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock().expectOneCall("foobar");
mock().checkExpectations();
CHECK(! mock().expectedCallsLeft());
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectOneCallInScopeButNotHappen)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("scope::foobar");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock("scope").expectOneCall("foobar");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, unexpectedCallHappened)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest emptyExpectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "func", emptyExpectations);
mock().actualCall("func");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, unexpectedScopeCallHappened)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest emptyExpectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::func", emptyExpectations);
mock("scope").actualCall("func");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectOneCallInOneScopeButActualCallInAnotherScope)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest emptyExpectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "class::foo", emptyExpectations);
mock("scope").expectOneCall("foo");
mock("class").actualCall("foo");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().clear();
}
TEST(MockCallTest, expectOneCallInScopeButActualCallInGlobal)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest emptyExpectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", emptyExpectations);
mock("scope").expectOneCall("foo");
mock().actualCall("foo");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().clear();
}
TEST(MockCallTest, expectMultipleSingleCallsThatHappen)
{
mock().expectOneCall("foo");
mock().expectOneCall("foo");
mock().actualCall("foo");
mock().actualCall("foo");
mock().checkExpectations();
}
TEST(MockCallTest, expectOneCallHoweverMultipleHappened)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->callWasMade(1);
expectations.addFunction("foo")->callWasMade(2);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations);
mock().expectOneCall("foo");
mock().expectOneCall("foo");
mock().actualCall("foo");
mock().actualCall("foo");
mock().actualCall("foo");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectNoCallThatHappened)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction(0, "lazy");
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
mock().expectNoCall("lazy");
mock().actualCall("lazy");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectNoCallDoesntInfluenceExpectOneCall)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction(0, "lazy");
expectations.addFunction("influence")->callWasMade(1);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
mock().expectNoCall("lazy");
mock().expectOneCall("influence");
mock().actualCall("influence");
mock().actualCall("lazy");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectNoCallOnlyFailureOnceWhenMultipleHappened)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction(0, "lazy");
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
mock().expectNoCall("lazy");
mock().actualCall("lazy");
mock().actualCall("lazy");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, ignoreOtherCallsExceptForTheUnExpectedOne)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction(0, "lazy");
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
mock().expectNoCall("lazy");
mock().ignoreOtherCalls();
mock().actualCall("bar").withParameter("foo", 1);
mock().actualCall("bar1").withParameter("foo", 1);
mock().actualCall("bar2").withParameter("foo", 1);
mock().actualCall("lazy");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectNoCallInScopeThatHappened)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction(0, "scope::lazy");
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::lazy", expectations);
mock("scope").expectNoCall("lazy");
mock("scope").actualCall("lazy");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectNoCallInScopeButActualCallInAnotherScope)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope2::lazy", expectations);
mock("scope1").expectNoCall("lazy");
mock("scope2").actualCall("lazy");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectNoCallInScopeButActualCallInGlobal)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
mock("scope1").expectNoCall("lazy");
mock().actualCall("lazy");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, ignoreOtherCallsExceptForTheExpectedOne)
{
mock().expectOneCall("foo");
mock().ignoreOtherCalls();
mock().actualCall("bar").withParameter("foo", 1);
mock().clear();
}
TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->callWasMade(1);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations);
mock().expectOneCall("foo");
mock().ignoreOtherCalls();
mock().actualCall("bar");
mock().actualCall("foo");
mock().actualCall("foo");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock().expectOneCall("foo");
mock().ignoreOtherCalls();
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, threeExpectedAndActual)
{
mock().expectOneCall("function1");
mock().expectOneCall("function2");
mock().expectOneCall("function3");
mock().actualCall("function1");
mock().actualCall("function2");
mock().actualCall("function3");
mock().checkExpectations();
}
TEST(MockCallTest, disableEnable)
{
mock().disable();
mock().expectOneCall("function");
mock().actualCall("differenFunction");
CHECK(! mock().expectedCallsLeft());
mock().enable();
mock().expectOneCall("function");
CHECK(mock().expectedCallsLeft());
mock().actualCall("function");
mock().checkExpectations();
}
TEST(MockCallTest, OnObject)
{
void* objectPtr = (void*) 0x001;
mock().expectOneCall("boo").onObject(objectPtr);
mock().actualCall("boo").onObject(objectPtr);
}
TEST(MockCallTest, OnObjectIgnored_MatchingAlreadyWhenObjectPassed)
{
void* objectPtr = (void*) 0x001;
mock().expectOneCall("boo");
mock().actualCall("boo").onObject(objectPtr);
}
TEST(MockCallTest, OnObjectIgnored_NotMatchingYetWhenObjectPassed)
{
void* objectPtr = (void*) 0x001;
mock().expectOneCall("boo").withBoolParameter("p", true);
mock().actualCall("boo").onObject(objectPtr).withBoolParameter("p", true);
}
TEST(MockCallTest, OnObjectIgnored_InitialMatchDiscarded)
{
void* objectPtr1 = (void*) 0x001;
void* objectPtr2 = (void*) 0x002;
mock().expectOneCall("boo");
mock().expectOneCall("boo").withBoolParameter("p", true);
mock().actualCall("boo").onObject(objectPtr2).withBoolParameter("p", true);
mock().actualCall("boo").onObject(objectPtr1);
}
TEST(MockCallTest, OnObjectFails)
{
MockFailureReporterInstaller failureReporterInstaller;
void* objectPtr = (void*) 0x001;
void* objectPtr2 = (void*) 0x002;
MockExpectedCallsListForTest expectations;
expectations.addFunction("boo")->onObject(objectPtr);
mock().expectOneCall("boo").onObject(objectPtr);
mock().actualCall("boo").onObject(objectPtr2);
MockUnexpectedObjectFailure expectedFailure(mockFailureTest(), "boo", objectPtr2, expectations);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, OnObjectExpectedButNotCalled)
{
MockFailureReporterInstaller failureReporterInstaller;
void* objectPtr = (void*) 0x001;
MockExpectedCallsListForTest expectations;
expectations.addFunction("boo")->onObject(objectPtr);
expectations.addFunction("boo")->onObject(objectPtr);
mock().expectOneCall("boo").onObject(objectPtr);
mock().expectOneCall("boo").onObject(objectPtr);
mock().actualCall("boo");
mock().actualCall("boo");
MockExpectedObjectDidntHappenFailure expectedFailure(mockFailureTest(), "boo", expectations);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, expectNCalls_Fulfilled)
{
mock().expectNCalls(2, "boo");
mock().actualCall("boo");
mock().actualCall("boo");
mock().checkExpectations();
}
TEST(MockCallTest, expectNCalls_NotFulfilled)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction(2, "boo")->callWasMade(1);
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock().expectNCalls(2, "boo");
mock().actualCall("boo");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockCallTest, shouldntFailTwice)
{
MockFailureReporterInstaller failureReporterInstaller;
mock().strictOrder();
mock().expectOneCall("foo");
mock().expectOneCall("boo");
mock().actualCall("boo");
mock().actualCall("bar");
mock().checkExpectations();
CHECK(!MockFailureReporterForTest::getReporter()->mockFailureString.contains("bar"));
CHECK(MockFailureReporterForTest::getReporter()->mockFailureString.contains("boo"));
}
TEST(MockCallTest, shouldReturnDefaultWhenThereIsntAnythingToReturn)
{
CHECK(mock().returnValue().equals(MockNamedValue("")));
}
IGNORE_TEST(MockCallTest, testForPerformanceProfiling)
{
/* TO fix! */
mock().expectNCalls(2000, "SimpleFunction");
for (int i = 0; i < 2000; i++) {
mock().actualCall("SimpleFunction");
}
}
static void mocksAreCountedAsChecksTestFunction_()
{
mock().expectOneCall("foo");
mock().expectNCalls(3, "bar");
mock().expectNoCall("lazy");
mock().clear();
}
TEST(MockCallTest, mockExpectationShouldIncreaseNumberOfChecks)
{
TestTestingFixture fixture;
fixture.setTestFunction(mocksAreCountedAsChecksTestFunction_);
fixture.runAllTests();
LONGS_EQUAL(3, fixture.getCheckCount());
}

View file

@ -0,0 +1,75 @@
/* Additional include from CppUTestExt */
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
/* Stubbed out product code using linker, function pointer, or overriding */
static int foo(const char* param_string, int param_int)
{
/* Tell CppUTest Mocking what we mock. Also return recorded value */
return mock().actualCall("Foo")
.withParameter("param_string", param_string)
.withParameter("param_int", param_int)
.returnValue().getIntValue();
}
static void bar(double param_double, const char* param_string)
{
mock().actualCall("Bar")
.withParameter("param_double", param_double)
.withParameter("param_string", param_string);
}
/* Production code calls to the methods we stubbed */
static int productionCodeFooCalls()
{
int return_value;
return_value = foo("value_string", 10);
(void)return_value;
return_value = foo("value_string", 10);
return return_value;
}
static void productionCodeBarCalls()
{
bar(1.5, "more");
bar(1.5, "more");
}
/* Actual test */
TEST_GROUP(MockCheatSheet)
{
void teardown() _override
{
/* Check expectations. Alternatively use MockSupportPlugin */
mock().checkExpectations();
mock().clear();
}
};
TEST(MockCheatSheet, foo)
{
/* Record 2 calls to Foo. Return different values on each call */
mock().expectOneCall("Foo")
.withParameter("param_string", "value_string")
.withParameter("param_int", 10)
.andReturnValue(30);
mock().expectOneCall("Foo")
.ignoreOtherParameters()
.andReturnValue(50);
/* Call production code */
productionCodeFooCalls();
}
TEST(MockCheatSheet, bar)
{
/* Expect 2 calls on Bar. Check only one parameter */
mock().expectNCalls(2, "Bar")
.withParameter("param_double", 1.5)
.ignoreOtherParameters();
/* And the production code call */
productionCodeBarCalls();
}

View file

@ -0,0 +1,600 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockComparatorCopierTest)
{
void teardown() _override
{
mock().checkExpectations();
mock().clear();
mock().removeAllComparatorsAndCopiers();
}
};
class MyTypeForTesting
{
public:
MyTypeForTesting(long val)
{
value = new long(val);
}
virtual ~MyTypeForTesting()
{
delete value;
}
long *value;
};
class MyTypeForTestingComparator : public MockNamedValueComparator
{
public:
virtual bool isEqual(const void* object1, const void* object2) _override
{
const MyTypeForTesting* obj1 = (const MyTypeForTesting*) object1;
const MyTypeForTesting* obj2 = (const MyTypeForTesting*) object2;
return *(obj1->value) == *(obj2->value);
}
virtual SimpleString valueToString(const void* object) _override
{
const MyTypeForTesting* obj = (const MyTypeForTesting*) object;
return StringFrom(*(obj->value));
}
};
class MyTypeForTestingCopier : public MockNamedValueCopier
{
public:
virtual void copy(void* dst_, const void* src_) _override
{
MyTypeForTesting* dst = (MyTypeForTesting*) dst_;
const MyTypeForTesting* src = (const MyTypeForTesting*) src_;
*(dst->value) = *(src->value);
}
};
TEST(MockComparatorCopierTest, customObjectParameterFailsWhenNotHavingAComparisonRepository)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting object(1);
mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
MockNoWayToCompareCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockComparatorCopierTest, customObjectParameterFailsWhenNotHavingACopierRepository)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting object(1);
mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &object);
mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &object);
MockNoWayToCopyCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockComparatorCopierTest, customObjectParameterSucceeds)
{
MyTypeForTesting object(1);
MyTypeForTestingComparator comparator;
mock().installComparator("MyTypeForTesting", comparator);
mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().checkExpectations();
mock().removeAllComparatorsAndCopiers();
}
static bool myTypeIsEqual(const void* object1, const void* object2)
{
return ((const MyTypeForTesting*)object1)->value == ((const MyTypeForTesting*)object2)->value;
}
static SimpleString myTypeValueToString(const void* object)
{
return StringFrom(((const MyTypeForTesting*)object)->value);
}
TEST(MockComparatorCopierTest, customObjectWithFunctionComparator)
{
MyTypeForTesting object(1);
MockFunctionComparator comparator(myTypeIsEqual, myTypeValueToString);
mock().installComparator("MyTypeForTesting", comparator);
mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().checkExpectations();
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, customObjectWithFunctionComparatorThatFailsCoversValueToString)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting object(5);
MockFunctionComparator comparator(myTypeIsEqual, myTypeValueToString);
mock().installComparator("MyTypeForTesting", comparator);
MockExpectedCallsListForTest expectations;
expectations.addFunction("function")->withParameterOfType("MyTypeForTesting", "parameterName", &object);
MockExpectedCallsDidntHappenFailure failure(UtestShell::getCurrent(), expectations);
mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE_LOCATION(failure, __FILE__, __LINE__);
}
TEST(MockComparatorCopierTest, customTypeOutputParameterSucceeds)
{
MyTypeForTesting expectedObject(55);
MyTypeForTesting actualObject(99);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject);
mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject);
mock().checkExpectations();
CHECK_EQUAL(55, *(expectedObject.value));
CHECK_EQUAL(55, *(actualObject.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, noActualCallForCustomTypeOutputParameter)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting expectedObject(1);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, unexpectedCustomTypeOutputParameter)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting actualObject(8834);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo");
MockNamedValue parameter("parameterName");
parameter.setConstObjectPointer("MyTypeForTesting", &actualObject);
MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo");
mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, customTypeOutputParameterMissing)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting expectedObject(123464);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations);
mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
mock().actualCall("foo");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, customTypeOutputParameterOfWrongType)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting expectedObject(123464);
MyTypeForTesting actualObject(75646);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
MockNamedValue parameter("output");
parameter.setConstObjectPointer("OtherTypeForTesting", &actualObject);
MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
mock().actualCall("foo").withOutputParameterOfType("OtherTypeForTesting", "output", &actualObject);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, noCopierForCustomTypeOutputParameter)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting expectedObject(123464);
MyTypeForTesting actualObject(8834);
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
MockNoWayToCopyCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting");
mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject);
mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "output", &actualObject);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockComparatorCopierTest, twoCustomTypeOutputParameters)
{
MyTypeForTesting expectedObject1(545);
MyTypeForTesting actualObject1(979);
MyTypeForTesting expectedObject2(123);
MyTypeForTesting actualObject2(4567);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject1).withParameter("id", 1);
mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject2).withParameter("id", 2);
mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject1).withParameter("id", 1);
mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject2).withParameter("id", 2);
mock().checkExpectations();
CHECK_EQUAL(545, *(expectedObject1.value));
CHECK_EQUAL(545, *(actualObject1.value));
CHECK_EQUAL(123, *(expectedObject2.value));
CHECK_EQUAL(123, *(actualObject2.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, twoInterleavedCustomTypeOutputParameters)
{
MyTypeForTesting expectedObject1(9545);
MyTypeForTesting actualObject1(79);
MyTypeForTesting expectedObject2(132);
MyTypeForTesting actualObject2(743);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject1).withParameter("id", 1);
mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject2).withParameter("id", 2);
mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject2).withParameter("id", 2);
mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject1).withParameter("id", 1);
mock().checkExpectations();
CHECK_EQUAL(9545, *(expectedObject1.value));
CHECK_EQUAL(9545, *(actualObject1.value));
CHECK_EQUAL(132, *(expectedObject2.value));
CHECK_EQUAL(132, *(actualObject2.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, twoDifferentCustomTypeOutputParametersInSameFunctionCallSucceeds)
{
MyTypeForTesting expectedObject1(11);
MyTypeForTesting actualObject1(22);
MyTypeForTesting expectedObject2(33);
MyTypeForTesting actualObject2(44);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
mock().expectOneCall("foo")
.withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject1)
.withOutputParameterOfTypeReturning("MyTypeForTesting", "foobar", &expectedObject2);
mock().actualCall("foo")
.withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject1)
.withOutputParameterOfType("MyTypeForTesting", "foobar", &actualObject2);
mock().checkExpectations();
CHECK_EQUAL(11, *(expectedObject1.value));
CHECK_EQUAL(11, *(actualObject1.value));
CHECK_EQUAL(33, *(expectedObject2.value));
CHECK_EQUAL(33, *(actualObject2.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, customTypeOutputAndInputParametersOfSameNameInDifferentFunctionCallsOfSameFunctionSucceeds)
{
MyTypeForTesting expectedObject1(911);
MyTypeForTesting actualObject1(6576878);
MyTypeForTesting expectedObject2(123);
MyTypeForTesting actualObject2(123);
MyTypeForTestingCopier copier;
MyTypeForTestingComparator comparator;
mock().installCopier("MyTypeForTesting", copier);
mock().installComparator("MyTypeForTesting", comparator);
mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject1);
mock().expectOneCall("foo").withParameterOfType("MyTypeForTesting", "bar", &expectedObject2);
mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject1);
mock().actualCall("foo").withParameterOfType("MyTypeForTesting", "bar", &actualObject2);
mock().checkExpectations();
CHECK_EQUAL(911, *(expectedObject1.value));
CHECK_EQUAL(911, *(actualObject1.value));
CHECK_EQUAL(123, *(expectedObject2.value));
CHECK_EQUAL(123, *(actualObject2.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, twoCustomTypeOutputParametersOfSameNameInDifferentFunctionsSucceeds)
{
MyTypeForTesting expectedObject1(657);
MyTypeForTesting actualObject1(984465);
MyTypeForTesting expectedObject2(987);
MyTypeForTesting actualObject2(987);
MyTypeForTestingCopier copier;
MyTypeForTestingComparator comparator;
mock().installCopier("MyTypeForTesting", copier);
mock().installComparator("MyTypeForTesting", comparator);
mock().expectOneCall("foo1").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject1);
mock().expectOneCall("foo2").withParameterOfType("MyTypeForTesting", "bar", &expectedObject2);
mock().actualCall("foo1").withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject1);
mock().actualCall("foo2").withParameterOfType("MyTypeForTesting", "bar", &actualObject2);
mock().checkExpectations();
CHECK_EQUAL(657, *(expectedObject1.value));
CHECK_EQUAL(657, *(actualObject1.value));
CHECK_EQUAL(987, *(expectedObject2.value));
CHECK_EQUAL(987, *(actualObject2.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, customTypeOutputAndInputParameterOfSameTypeInSameFunctionCall)
{
MyTypeForTesting expectedObject1(45);
MyTypeForTesting actualObject1(45);
MyTypeForTesting expectedObject2(987765443);
MyTypeForTesting actualObject2(0);
MyTypeForTestingCopier copier;
MyTypeForTestingComparator comparator;
mock().installCopier("MyTypeForTesting", copier);
mock().installComparator("MyTypeForTesting", comparator);
mock().expectOneCall("foo")
.withParameterOfType("MyTypeForTesting", "bar", &expectedObject1)
.withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject2);
mock().actualCall("foo")
.withParameterOfType("MyTypeForTesting", "bar", &actualObject1)
.withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject2);
mock().checkExpectations();
CHECK_EQUAL(45, *(expectedObject1.value));
CHECK_EQUAL(45, *(actualObject1.value));
CHECK_EQUAL(987765443, *(expectedObject2.value));
CHECK_EQUAL(987765443, *(actualObject2.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, customTypeOutputParameterTraced)
{
MyTypeForTesting actualObject(676789);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
mock().tracing(true);
mock().actualCall("someFunc").withOutputParameterOfType("MyTypeForTesting", "someParameter", &actualObject);
mock().checkExpectations();
STRCMP_CONTAINS("Function name:someFunc MyTypeForTesting someParameter:", mock().getTraceOutput());
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, customTypeOutputParameterWithIgnoredParameters)
{
MyTypeForTesting expectedObject(444537909);
MyTypeForTesting actualObject(98765);
MyTypeForTestingCopier copier;
mock().installCopier("MyTypeForTesting", copier);
mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject).ignoreOtherParameters();
mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject).withParameter("other", 1);
mock().checkExpectations();
CHECK_EQUAL(444537909, *(expectedObject.value));
CHECK_EQUAL(444537909, *(actualObject.value));
mock().removeAllComparatorsAndCopiers();
}
static void myTypeCopy(void* dst_, const void* src_)
{
MyTypeForTesting* dst = (MyTypeForTesting*) dst_;
const MyTypeForTesting* src = (const MyTypeForTesting*) src_;
*(dst->value) = *(src->value);
}
TEST(MockComparatorCopierTest, customObjectWithFunctionCopier)
{
MyTypeForTesting expectedObject(9874452);
MyTypeForTesting actualObject(2034);
MockFunctionCopier copier(myTypeCopy);
mock().installCopier("MyTypeForTesting", copier);
mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject);
mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject);
mock().checkExpectations();
CHECK_EQUAL(9874452, *(expectedObject.value));
CHECK_EQUAL(9874452, *(actualObject.value));
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, removingComparatorsWorksHierachically)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting object(1);
MyTypeForTestingComparator comparator;
mock("scope").installComparator("MyTypeForTesting", comparator);
mock().removeAllComparatorsAndCopiers();
mock("scope").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock("scope").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
MockNoWayToCompareCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockComparatorCopierTest, removingCopiersWorksHierachically)
{
MockFailureReporterInstaller failureReporterInstaller;
MyTypeForTesting object(1);
MyTypeForTestingCopier copier;
mock("scope").installCopier("MyTypeForTesting", copier);
mock().removeAllComparatorsAndCopiers();
mock("scope").expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &object);
mock("scope").actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "bar", &object);
MockNoWayToCopyCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockComparatorCopierTest, installComparatorWorksHierarchicalOnBothExistingAndDynamicallyCreatedMockSupports)
{
MyTypeForTesting object(1);
MyTypeForTestingComparator comparator;
mock("existing");
mock().installComparator("MyTypeForTesting", comparator);
mock("existing").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock("existing").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock("dynamic").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock("dynamic").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().checkExpectations();
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, installComparatorsWorksHierarchical)
{
MyTypeForTesting object(1);
MyTypeForTestingComparator comparator;
MockNamedValueComparatorsAndCopiersRepository repos;
repos.installComparator("MyTypeForTesting", comparator);
mock("existing");
mock().installComparatorsAndCopiers(repos);
mock("existing").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock("existing").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().checkExpectations();
mock().removeAllComparatorsAndCopiers();
}
TEST(MockComparatorCopierTest, installCopiersWorksHierarchically)
{
MyTypeForTesting object(1);
MyTypeForTestingCopier copier;
mock("existing");
mock().installCopier("MyTypeForTesting", copier);
mock("existing").expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &object);
mock("existing").actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &object);
mock().checkExpectations();
mock().removeAllComparatorsAndCopiers();
}
class StubComparator : public MockNamedValueComparator
{
public:
virtual bool isEqual(const void*, const void*) _override
{
return true;
}
virtual SimpleString valueToString(const void*) _override
{
return "";
}
};
struct SomeClass
{
int someDummy_;
};
static void functionWithConstParam(const SomeClass param)
{
mock().actualCall("functionWithConstParam").withParameterOfType("SomeClass", "param", &param);
}
TEST(MockComparatorCopierTest, shouldSupportConstParameters)
{
StubComparator comparator;
mock().installComparator("SomeClass", comparator);
SomeClass param;
mock().expectOneCall("functionWithConstParam").withParameterOfType("SomeClass", "param", &param);
functionWithConstParam(param);
mock().checkExpectations();
}

View file

@ -0,0 +1,813 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockCheckedExpectedCall.h"
#include "CppUTestExt/MockFailure.h"
#include "MockFailureReporterForTest.h"
class TypeForTestingExpectedFunctionCall
{
public:
TypeForTestingExpectedFunctionCall(int val)
{
value = new int(val);
}
virtual ~TypeForTestingExpectedFunctionCall()
{
delete value;
}
int *value;
};
class TypeForTestingExpectedFunctionCallComparator : public MockNamedValueComparator
{
public:
virtual bool isEqual(const void* object1, const void* object2) _override
{
const TypeForTestingExpectedFunctionCall* obj1 = (const TypeForTestingExpectedFunctionCall*) object1;
const TypeForTestingExpectedFunctionCall* obj2 = (const TypeForTestingExpectedFunctionCall*) object2;
return *(obj1->value) == *(obj2->value);
}
virtual SimpleString valueToString(const void* object) _override
{
const TypeForTestingExpectedFunctionCall* obj = (const TypeForTestingExpectedFunctionCall*) object;
return StringFrom(*(obj->value));
}
};
class TypeForTestingExpectedFunctionCallCopier : public MockNamedValueCopier
{
public:
virtual void copy(void* dst_, const void* src_) _override
{
TypeForTestingExpectedFunctionCall* dst = (TypeForTestingExpectedFunctionCall*) dst_;
const TypeForTestingExpectedFunctionCall* src = (const TypeForTestingExpectedFunctionCall*) src_;
*(dst->value) = *(src->value);
}
};
TEST_GROUP(MockNamedValueHandlerRepository)
{
void teardown() _override
{
CHECK_NO_MOCK_FAILURE();
MockFailureReporterForTest::clearReporter();
}
};
TEST(MockNamedValueHandlerRepository, getComparatorForNonExistingName)
{
MockNamedValueComparatorsAndCopiersRepository repository;
POINTERS_EQUAL(NULLPTR, repository.getComparatorForType("typeName"));
}
TEST(MockNamedValueHandlerRepository, installComparator)
{
TypeForTestingExpectedFunctionCallComparator comparator;
MockNamedValueComparatorsAndCopiersRepository repository;
repository.installComparator("typeName", comparator);
POINTERS_EQUAL(&comparator, repository.getComparatorForType("typeName"));
}
TEST(MockNamedValueHandlerRepository, installMultipleComparators)
{
TypeForTestingExpectedFunctionCallComparator comparator1, comparator2, comparator3;
MockNamedValueComparatorsAndCopiersRepository repository;
repository.installComparator("type1", comparator1);
repository.installComparator("type2", comparator2);
repository.installComparator("type3", comparator3);
POINTERS_EQUAL(&comparator3, repository.getComparatorForType("type3"));
POINTERS_EQUAL(&comparator2, repository.getComparatorForType("type2"));
POINTERS_EQUAL(&comparator1, repository.getComparatorForType("type1"));
}
TEST(MockNamedValueHandlerRepository, getCopierForNonExistingName)
{
MockNamedValueComparatorsAndCopiersRepository repository;
POINTERS_EQUAL(NULLPTR, repository.getCopierForType("typeName"));
}
TEST(MockNamedValueHandlerRepository, installCopier)
{
TypeForTestingExpectedFunctionCallCopier copier;
MockNamedValueComparatorsAndCopiersRepository repository;
repository.installCopier("typeName", copier);
POINTERS_EQUAL(&copier, repository.getCopierForType("typeName"));
}
TEST(MockNamedValueHandlerRepository, installMultipleCopiers)
{
TypeForTestingExpectedFunctionCallCopier copier1, copier2, copier3;
MockNamedValueComparatorsAndCopiersRepository repository;
repository.installCopier("type1", copier1);
repository.installCopier("type2", copier2);
repository.installCopier("type3", copier3);
POINTERS_EQUAL(&copier3, repository.getCopierForType("type3"));
POINTERS_EQUAL(&copier2, repository.getCopierForType("type2"));
POINTERS_EQUAL(&copier1, repository.getCopierForType("type1"));
}
TEST(MockNamedValueHandlerRepository, installMultipleHandlers)
{
TypeForTestingExpectedFunctionCallCopier copier1, copier2, copier3;
TypeForTestingExpectedFunctionCallComparator comparator1, comparator2, comparator3;
MockNamedValueComparatorsAndCopiersRepository repository;
repository.installCopier("type1", copier1);
repository.installComparator("type1", comparator1);
repository.installCopier("type2", copier2);
repository.installCopier("type3", copier3);
repository.installComparator("type2", comparator2);
repository.installComparator("type3", comparator3);
POINTERS_EQUAL(&comparator3, repository.getComparatorForType("type3"));
POINTERS_EQUAL(&comparator2, repository.getComparatorForType("type2"));
POINTERS_EQUAL(&comparator1, repository.getComparatorForType("type1"));
POINTERS_EQUAL(&copier3, repository.getCopierForType("type3"));
POINTERS_EQUAL(&copier2, repository.getCopierForType("type2"));
POINTERS_EQUAL(&copier1, repository.getCopierForType("type1"));
}
TEST_GROUP(MockExpectedCall)
{
MockCheckedExpectedCall* call;
MockNamedValueComparatorsAndCopiersRepository* originalComparatorRepository;
void setup() _override
{
originalComparatorRepository = MockNamedValue::getDefaultComparatorsAndCopiersRepository();
call = new MockCheckedExpectedCall(1);
call->withName("funcName");
}
void teardown() _override
{
MockNamedValue::setDefaultComparatorsAndCopiersRepository(originalComparatorRepository);
delete call;
CHECK_NO_MOCK_FAILURE();
MockFailureReporterForTest::clearReporter();
}
};
TEST(MockExpectedCall, callWithoutParameterSetOrNotFound)
{
STRCMP_EQUAL("", call->getInputParameterType("nonexisting").asCharString());
LONGS_EQUAL(0, call->getInputParameter("nonexisting").getIntValue());
CHECK(!call->hasInputParameterWithName("nonexisting"));
}
TEST(MockExpectedCall, callWithUnsignedIntegerParameter)
{
const SimpleString paramName = "paramName";
unsigned int value = 356;
call->withParameter(paramName, value);
STRCMP_EQUAL("unsigned int", call->getInputParameterType(paramName).asCharString());
LONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedIntValue());
CHECK(call->hasInputParameterWithName(paramName));
STRCMP_CONTAINS("funcName -> unsigned int paramName: <356 (0x164)>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithIntegerParameter)
{
const SimpleString paramName = "paramName";
int value = 2;
call->withParameter(paramName, value);
STRCMP_EQUAL("int", call->getInputParameterType(paramName).asCharString());
LONGS_EQUAL(value, call->getInputParameter(paramName).getIntValue());
CHECK(call->hasInputParameterWithName(paramName));
STRCMP_CONTAINS("funcName -> int paramName: <2 (0x2)>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithBooleanParameter)
{
const SimpleString paramName = "paramName";
bool value = true;
call->withParameter(paramName, value);
STRCMP_EQUAL("bool", call->getInputParameterType(paramName).asCharString());
CHECK_EQUAL(value, call->getInputParameter(paramName).getBoolValue());
CHECK(call->hasInputParameterWithName(paramName));
STRCMP_CONTAINS("funcName -> bool paramName: <true>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithUnsignedLongIntegerParameter)
{
const SimpleString paramName = "paramName";
unsigned long value = 888;
call->withParameter(paramName, value);
STRCMP_EQUAL("unsigned long int", call->getInputParameterType(paramName).asCharString());
LONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedLongIntValue());
CHECK(call->hasInputParameterWithName(paramName));
STRCMP_CONTAINS("funcName -> unsigned long int paramName: <888 (0x378)>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithLongIntegerParameter)
{
const SimpleString paramName = "paramName";
long value = 777;
call->withParameter(paramName, value);
STRCMP_EQUAL("long int", call->getInputParameterType(paramName).asCharString());
LONGS_EQUAL(value, call->getInputParameter(paramName).getLongIntValue());
CHECK(call->hasInputParameterWithName(paramName));
STRCMP_CONTAINS("funcName -> long int paramName: <777 (0x309)>", call->callToString().asCharString());
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockExpectedCall, callWithUnsignedLongLongIntegerParameter)
{
const SimpleString paramName = "paramName";
unsigned long long value = 888;
call->withParameter(paramName, value);
STRCMP_EQUAL("unsigned long long int", call->getInputParameterType(paramName).asCharString());
UNSIGNED_LONGLONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedLongLongIntValue());
CHECK(call->hasInputParameterWithName(paramName));
STRCMP_CONTAINS("funcName -> unsigned long long int paramName: <888 (0x378)>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithLongLongIntegerParameter)
{
const SimpleString paramName = "paramName";
long long value = 777;
call->withParameter(paramName, value);
STRCMP_EQUAL("long long int", call->getInputParameterType(paramName).asCharString());
LONGLONGS_EQUAL(value, call->getInputParameter(paramName).getLongLongIntValue());
CHECK(call->hasInputParameterWithName(paramName));
STRCMP_CONTAINS("funcName -> long long int paramName: <777 (0x309)>", call->callToString().asCharString());
}
#endif
TEST(MockExpectedCall, callWithDoubleParameter)
{
const SimpleString paramName = "paramName";
double value = 1.2;
call->withParameter(paramName, value);
STRCMP_EQUAL("double", call->getInputParameterType(paramName).asCharString());
DOUBLES_EQUAL(value, call->getInputParameter(paramName).getDoubleValue(), 0.0);
STRCMP_CONTAINS("funcName -> double paramName: <1.2>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithDoubleParameterAndTolerance)
{
const SimpleString paramName = "paramName";
double value = 1.2;
double tolerance = 0.2;
call->withParameter(paramName, value, tolerance);
STRCMP_EQUAL("double", call->getInputParameterType(paramName).asCharString());
DOUBLES_EQUAL(value, call->getInputParameter(paramName).getDoubleValue(), 0.0);
DOUBLES_EQUAL(tolerance, call->getInputParameter(paramName).getDoubleTolerance(), 0.0);
STRCMP_CONTAINS("funcName -> double paramName: <1.2>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithStringParameter)
{
const SimpleString paramName = "paramName";
const char* value = "hello world";
call->withParameter(paramName, value);
STRCMP_EQUAL("const char*", call->getInputParameterType(paramName).asCharString());
STRCMP_EQUAL(value, call->getInputParameter(paramName).getStringValue());
STRCMP_CONTAINS("funcName -> const char* paramName: <hello world>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithPointerParameter)
{
const SimpleString paramName = "paramName";
void* value = (void*) 0x123;
call->withParameter(paramName, value);
STRCMP_EQUAL("void*", call->getInputParameterType(paramName).asCharString());
POINTERS_EQUAL(value, call->getInputParameter(paramName).getPointerValue());
STRCMP_CONTAINS("funcName -> void* paramName: <0x123>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithConstPointerParameter)
{
const SimpleString paramName = "paramName";
const void* value = (const void*) 0x345;
call->withParameter(paramName, value);
STRCMP_EQUAL("const void*", call->getInputParameterType(paramName).asCharString());
POINTERS_EQUAL(value, call->getInputParameter(paramName).getConstPointerValue());
STRCMP_CONTAINS("funcName -> const void* paramName: <0x345>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithFunctionPointerParameter)
{
const SimpleString paramName = "paramName";
void (*value)() = (void (*)()) 0xdead;
call->withParameter(paramName, value);
STRCMP_EQUAL("void (*)()", call->getInputParameterType(paramName).asCharString());
FUNCTIONPOINTERS_EQUAL(value, call->getInputParameter(paramName).getFunctionPointerValue());
STRCMP_CONTAINS("funcName -> void (*)() paramName: <0xdead>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithMemoryBuffer)
{
const SimpleString paramName = "paramName";
const unsigned char value[] = { 0x12, 0xFE, 0xA1 };
call->withParameter(paramName, value, sizeof(value));
STRCMP_EQUAL("const unsigned char*", call->getInputParameterType(paramName).asCharString());
POINTERS_EQUAL( (void*) value, (void*) call->getInputParameter(paramName).getMemoryBuffer() );
LONGS_EQUAL(sizeof(value), call->getInputParameter(paramName).getSize());
STRCMP_CONTAINS("funcName -> const unsigned char* paramName: <Size = 3 | HexContents = 12 FE A1>", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithObjectParameter)
{
const SimpleString paramName = "paramName";
void* value = (void*) 0x123;
call->withParameterOfType("ClassName", paramName, value);
POINTERS_EQUAL(value, call->getInputParameter(paramName).getConstObjectPointer());
STRCMP_EQUAL("ClassName", call->getInputParameterType(paramName).asCharString());
STRCMP_CONTAINS("funcName -> ClassName paramName: <No comparator found for type: \"ClassName\">", call->callToString().asCharString());
}
TEST(MockExpectedCall, callWithObjectParameterUnequalComparison)
{
TypeForTestingExpectedFunctionCall type(1), unequalType(2);
MockNamedValue parameter("name");
parameter.setConstObjectPointer("type", &unequalType);
call->withParameterOfType("type", "name", &type);
CHECK(!call->hasInputParameter(parameter));
}
TEST(MockExpectedCall, callWithObjectParameterEqualComparisonButFailsWithoutRepository)
{
TypeForTestingExpectedFunctionCall type(1), equalType(1);
MockNamedValue parameter("name");
parameter.setConstObjectPointer("type", &equalType);
call->withParameterOfType("type", "name", &type);
CHECK(!call->hasInputParameter(parameter));
}
TEST(MockExpectedCall, callWithObjectParameterEqualComparisonButFailsWithoutComparator)
{
MockNamedValueComparatorsAndCopiersRepository repository;
MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository);
TypeForTestingExpectedFunctionCall type(1), equalType(1);
MockNamedValue parameter("name");
parameter.setConstObjectPointer("type", &equalType);
call->withParameterOfType("type", "name", &type);
CHECK(!call->hasInputParameter(parameter));
}
TEST(MockExpectedCall, callWithObjectParameterEqualComparison)
{
TypeForTestingExpectedFunctionCallComparator comparator;
MockNamedValueComparatorsAndCopiersRepository repository;
MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository);
repository.installComparator("type", comparator);
TypeForTestingExpectedFunctionCall type(1), equalType(1);
MockNamedValue parameter("name");
parameter.setConstObjectPointer("type", &equalType);
call->withParameterOfType("type", "name", &type);
CHECK(call->hasInputParameter(parameter));
}
TEST(MockExpectedCall, getParameterValueOfObjectType)
{
TypeForTestingExpectedFunctionCallComparator comparator;
MockNamedValueComparatorsAndCopiersRepository repository;
MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository);
repository.installComparator("type", comparator);
TypeForTestingExpectedFunctionCall type(1);
call->withParameterOfType("type", "name", &type);
POINTERS_EQUAL(&type, call->getInputParameter("name").getConstObjectPointer());
STRCMP_EQUAL("1", call->getInputParameterValueString("name").asCharString());
}
TEST(MockExpectedCall, getParameterValueOfObjectTypeWithoutRepository)
{
TypeForTestingExpectedFunctionCall type(1);
call->withParameterOfType("type", "name", &type);
STRCMP_EQUAL("No comparator found for type: \"type\"", call->getInputParameterValueString("name").asCharString());
}
TEST(MockExpectedCall, getParameterValueOfObjectTypeWithoutComparator)
{
TypeForTestingExpectedFunctionCall type(1);
MockNamedValueComparatorsAndCopiersRepository repository;
MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository);
call->withParameterOfType("type", "name", &type);
STRCMP_EQUAL("No comparator found for type: \"type\"", call->getInputParameterValueString("name").asCharString());
}
TEST(MockExpectedCall, callWithTwoUnsignedIntegerParameter)
{
unsigned int expected_value = 1;
unsigned int another_expected_value = 2;
call->withParameter("unsigned-integer1", expected_value);
call->withParameter("unsigned-integer2", another_expected_value);
STRCMP_EQUAL("unsigned int", call->getInputParameterType("unsigned-integer1").asCharString());
STRCMP_EQUAL("unsigned int", call->getInputParameterType("unsigned-integer2").asCharString());
LONGS_EQUAL(expected_value, call->getInputParameter("unsigned-integer1").getUnsignedIntValue());
LONGS_EQUAL(another_expected_value, call->getInputParameter("unsigned-integer2").getUnsignedIntValue());
}
TEST(MockExpectedCall, callWithTwoIntegerParameter)
{
int expected_value = 1;
int another_expected_value = -1;
call->withParameter("integer1", expected_value);
call->withParameter("integer2", another_expected_value);
STRCMP_EQUAL("int", call->getInputParameterType("integer1").asCharString());
STRCMP_EQUAL("int", call->getInputParameterType("integer2").asCharString());
LONGS_EQUAL(expected_value, call->getInputParameter("integer1").getIntValue());
LONGS_EQUAL(another_expected_value, call->getInputParameter("integer2").getIntValue());
}
TEST(MockExpectedCall, callWithThreeDifferentParameter)
{
call->withParameter("integer", 1);
call->withParameter("string", "hello world");
call->withParameter("double", 0.12);
STRCMP_EQUAL("int", call->getInputParameterType("integer").asCharString());
STRCMP_EQUAL("const char*", call->getInputParameterType("string").asCharString());
STRCMP_EQUAL("double", call->getInputParameterType("double").asCharString());
LONGS_EQUAL(1, call->getInputParameter("integer").getIntValue());
STRCMP_EQUAL("hello world", call->getInputParameter("string").getStringValue());
DOUBLES_EQUAL(0.12, call->getInputParameter("double").getDoubleValue(), 0.05);
}
TEST(MockExpectedCall, singleCallNotMadeIsNotFulfilledButCanMatchActualCalls)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
CHECK(!expectedCall.isFulfilled());
CHECK(expectedCall.canMatchActualCalls());
}
TEST(MockExpectedCall, singleCallMadeIsFulFilledAndCannotMatchActualCalls)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.callWasMade(1);
CHECK(expectedCall.isFulfilled());
CHECK(!expectedCall.canMatchActualCalls());
}
TEST(MockExpectedCall, multiCallNotMadeIsNotFulfilledButCanMatchActualCalls)
{
MockCheckedExpectedCall expectedCall(2);
expectedCall.withName("name");
CHECK(!expectedCall.isFulfilled());
CHECK(expectedCall.canMatchActualCalls());
}
TEST(MockExpectedCall, multiCallNotMadeExpectedTimesIsNotFulfilledButCanMatchActualCalls)
{
MockCheckedExpectedCall expectedCall(2);
expectedCall.withName("name");
expectedCall.callWasMade(1);
CHECK(!expectedCall.isFulfilled());
CHECK(expectedCall.canMatchActualCalls());
}
TEST(MockExpectedCall, multiCallsMadeExpectedTimesIsFulfilledAndCannotMatchActualCalls)
{
MockCheckedExpectedCall expectedCall(3);
expectedCall.withName("name");
expectedCall.callWasMade(1);
expectedCall.callWasMade(2);
expectedCall.callWasMade(3);
CHECK(expectedCall.isFulfilled());
CHECK(!expectedCall.canMatchActualCalls());
}
TEST(MockExpectedCall, multiCallsMadeMoreThanExpectedTimesIsNotFulfilledAndCannotMatchActualCalls)
{
MockCheckedExpectedCall expectedCall(3);
expectedCall.withName("name");
expectedCall.callWasMade(1);
expectedCall.callWasMade(2);
expectedCall.callWasMade(3);
expectedCall.callWasMade(4);
CHECK(!expectedCall.isFulfilled());
CHECK(!expectedCall.canMatchActualCalls());
}
TEST(MockExpectedCall, callsWithoutParameterAlwaysMatch)
{
MockCheckedExpectedCall expectedCall(1);
CHECK(expectedCall.isMatchingActualCall());
}
TEST(MockExpectedCall, callsWithParameterNotFulfilledDontMatch)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withParameter("para", 1);
CHECK(!expectedCall.isMatchingActualCall());
}
TEST(MockExpectedCall, callsWithParameterFulfilledDoMatch)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withParameter("para", 1);
expectedCall.inputParameterWasPassed("para");
CHECK(expectedCall.isMatchingActualCall());
}
TEST(MockExpectedCall, callsWithSomeParametersNotFulfilledDontMatch)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withParameter("para", 1).withParameter("two", 2);
expectedCall.inputParameterWasPassed("para");
CHECK(!expectedCall.isMatchingActualCall());
}
TEST(MockExpectedCall, toStringForNoParametersSingleCallNotCalled)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
STRCMP_EQUAL("name -> no parameters (expected 1 call, called 0 times)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledLessThanExpectedTimes)
{
MockCheckedExpectedCall expectedCall(2);
expectedCall.withName("name");
expectedCall.callWasMade(1);
STRCMP_EQUAL("name -> no parameters (expected 2 calls, called 1 time)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledExpectedTimes)
{
MockCheckedExpectedCall expectedCall(2);
expectedCall.withName("name");
expectedCall.callWasMade(1);
expectedCall.callWasMade(2);
STRCMP_EQUAL("name -> no parameters (expected 2 calls, called 2 times)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForIgnoredParameters)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
expectedCall.ignoreOtherParameters();
STRCMP_EQUAL("name -> all parameters ignored (expected 1 call, called 0 times)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForMultipleInputParameters)
{
int int_value = 10;
unsigned int uint_value = 7;
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
expectedCall.withParameter("string", "value");
expectedCall.withParameter("integer", int_value);
expectedCall.withParameter("unsigned-integer", uint_value);
expectedCall.callWasMade(1);
STRCMP_EQUAL("name -> const char* string: <value>, int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)> "
"(expected 1 call, called 1 time)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForMultipleInputAndOutputParameters)
{
int int_value = 10;
unsigned int uint_value = 7;
unsigned char buffer_value[3];
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
expectedCall.withParameter("string", "value");
expectedCall.withParameter("integer", int_value);
expectedCall.withParameter("unsigned-integer", uint_value);
expectedCall.withOutputParameterReturning("buffer", buffer_value, sizeof(buffer_value));
expectedCall.callWasMade(1);
STRCMP_EQUAL("name -> const char* string: <value>, int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)>, "
"const void* buffer: <output> (expected 1 call, called 1 time)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForMultipleOutputParameters)
{
unsigned char buffer_value[3];
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
expectedCall.withOutputParameterReturning("buffer1", buffer_value, sizeof(buffer_value));
expectedCall.withOutputParameterReturning("buffer2", buffer_value, sizeof(buffer_value));
expectedCall.callWasMade(1);
STRCMP_EQUAL("name -> const void* buffer1: <output>, const void* buffer2: <output> (expected 1 call, called 1 time)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForUnmodifiedOutputParameter)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
expectedCall.withUnmodifiedOutputParameter("buffer1");
expectedCall.callWasMade(1);
STRCMP_EQUAL("name -> const void* buffer1: <output> (expected 1 call, called 1 time)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForParameterAndIgnored)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
expectedCall.withParameter("string", "value");
expectedCall.ignoreOtherParameters();
expectedCall.callWasMade(1);
STRCMP_EQUAL("name -> const char* string: <value>, other parameters are ignored (expected 1 call, called 1 time)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForCallOrderSingle)
{
MockCheckedExpectedCall expectedCall(1);
expectedCall.withName("name");
expectedCall.withCallOrder(2);
expectedCall.callWasMade(1);
STRCMP_EQUAL("name -> expected call order: <2> -> no parameters (expected 1 call, called 1 time)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, toStringForCallOrderMultiple)
{
MockCheckedExpectedCall expectedCall(5);
expectedCall.withName("name");
expectedCall.withCallOrder(5, 9);
expectedCall.callWasMade(5);
expectedCall.callWasMade(6);
expectedCall.callWasMade(7);
expectedCall.callWasMade(8);
expectedCall.callWasMade(9);
STRCMP_EQUAL("name -> expected calls order: <5..9> -> no parameters (expected 5 calls, called 5 times)", expectedCall.callToString().asCharString());
}
TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderSingle)
{
call->withName("name");
call->withCallOrder(2);
call->callWasMade(1);
CHECK(call->isFulfilled());
CHECK(call->isOutOfOrder());
}
TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderMultipleTooEarly)
{
MockCheckedExpectedCall expectedCall(3);
expectedCall.withName("name");
expectedCall.withCallOrder(10, 12);
expectedCall.callWasMade(9);
expectedCall.callWasMade(10);
expectedCall.callWasMade(11);
CHECK(expectedCall.isFulfilled());
CHECK(expectedCall.isOutOfOrder());
}
TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderMultipleTooLate)
{
MockCheckedExpectedCall expectedCall(3);
expectedCall.withName("name");
expectedCall.withCallOrder(10, 12);
expectedCall.callWasMade(11);
expectedCall.callWasMade(12);
expectedCall.callWasMade(13);
CHECK(expectedCall.isFulfilled());
CHECK(expectedCall.isOutOfOrder());
}
TEST(MockExpectedCall, callOrderIsFulfilledSingle)
{
call->withName("name");
call->withCallOrder(1);
call->callWasMade(1);
CHECK(call->isFulfilled());
CHECK_FALSE(call->isOutOfOrder());
}
TEST(MockExpectedCall, callOrderIsFulfilledMultiple)
{
MockCheckedExpectedCall expectedCall(4);
expectedCall.withName("name");
expectedCall.withCallOrder(150, 153);
expectedCall.callWasMade(150);
expectedCall.callWasMade(151);
expectedCall.callWasMade(152);
expectedCall.callWasMade(153);
CHECK(expectedCall.isFulfilled());
CHECK_FALSE(expectedCall.isOutOfOrder());
}
TEST(MockExpectedCall, hasOutputParameter)
{
const int value = 1;
call->withOutputParameterReturning("foo", &value, sizeof(value));
MockNamedValue foo("foo");
foo.setValue(&value);
CHECK(call->hasOutputParameter(foo));
}
TEST(MockExpectedCall, hasUnmodifiedOutputParameter)
{
call->withUnmodifiedOutputParameter("foo");
MockNamedValue foo("foo");
foo.setValue((const void *)NULLPTR);
foo.setSize(0);
CHECK(call->hasOutputParameter(foo));
}
TEST(MockExpectedCall, hasNoOutputParameter)
{
call->withIntParameter("foo", (int)1);
MockNamedValue foo("foo");
foo.setValue((int)1);
CHECK_FALSE(call->hasOutputParameter(foo));
}
TEST(MockExpectedCall, hasOutputParameterOfType)
{
TypeForTestingExpectedFunctionCall object(6789);
call->withOutputParameterOfTypeReturning("TypeForTestingExpectedFunctionCall", "foo", &object);
MockNamedValue foo("foo");
foo.setConstObjectPointer("TypeForTestingExpectedFunctionCall", &object);
CHECK(call->hasOutputParameter(foo));
}
TEST(MockExpectedCall, hasNoOutputParameterOfTypeSameTypeButInput)
{
TypeForTestingExpectedFunctionCall object(543);
call->withParameterOfType("TypeForTestingExpectedFunctionCall", "foo", &object);
MockNamedValue foo("foo");
foo.setConstObjectPointer("TypeForTestingExpectedFunctionCall", &object);
CHECK_FALSE(call->hasOutputParameter(foo));
}
TEST(MockExpectedCall, hasNoOutputParameterOfTypeDifferentType)
{
TypeForTestingExpectedFunctionCall object(543);
call->withOutputParameterOfTypeReturning("TypeForTestingExpectedFunctionCall", "foo", &object);
MockNamedValue foo("foo");
foo.setConstObjectPointer("OtherTypeForTestingExpectedFunctionCall", &object);
CHECK_FALSE(call->hasOutputParameter(foo));
}
TEST_GROUP(MockIgnoredExpectedCall)
{
MockIgnoredExpectedCall ignored;
};
TEST(MockIgnoredExpectedCall, worksAsItShould)
{
ignored.withName("func");
ignored.withCallOrder(1);
ignored.withCallOrder(1, 1);
ignored.onObject(NULLPTR);
ignored.withBoolParameter("umm", true);
ignored.withIntParameter("bla", (int) 1);
ignored.withUnsignedIntParameter("foo", (unsigned int) 1);
ignored.withLongIntParameter("hey", (long int) 1);
ignored.withUnsignedLongIntParameter("bah", (unsigned long int) 1);
#ifdef CPPUTEST_USE_LONG_LONG
ignored.withLongLongIntParameter("yo", (long long int) 1);
ignored.withUnsignedLongLongIntParameter("grr", (unsigned long long int) 1);
#endif
ignored.withDoubleParameter("hah", (double) 1.1f);
ignored.withDoubleParameter("gah", 2.1, 0.3);
ignored.withStringParameter("goo", "hello");
ignored.withPointerParameter("pie", (void*) NULLPTR);
ignored.withConstPointerParameter("woo", (const void*) NULLPTR);
ignored.withFunctionPointerParameter("fop", (void(*)()) NULLPTR);
ignored.withMemoryBufferParameter("waa", (const unsigned char*) NULLPTR, 0);
ignored.withParameterOfType( "mytype", "top", (const void*) NULLPTR);
ignored.withOutputParameterReturning("bar", (void*) NULLPTR, 1);
ignored.withOutputParameterOfTypeReturning("mytype", "bar", (const void*) NULLPTR);
ignored.withUnmodifiedOutputParameter("unmod");
ignored.ignoreOtherParameters();
ignored.andReturnValue(true);
ignored.andReturnValue((double) 1.0f);
ignored.andReturnValue((unsigned int) 1);
ignored.andReturnValue((int) 1);
ignored.andReturnValue((unsigned long int) 1);
ignored.andReturnValue((long int) 1);
#ifdef CPPUTEST_USE_LONG_LONG
ignored.andReturnValue((unsigned long long int) 1);
ignored.andReturnValue((long long int) 1);
#endif
ignored.andReturnValue("boo");
ignored.andReturnValue((void*) NULLPTR);
ignored.andReturnValue((const void*) NULLPTR);
ignored.andReturnValue((void(*)()) NULLPTR);
}

View file

@ -0,0 +1,133 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "MockFailureReporterForTest.h"
void MockFailureReporterForTest::failTest(const MockFailure& failure)
{
mockFailureString = failure.getMessage();
}
MockFailureReporterForTest* MockFailureReporterForTest::instance_ = NULLPTR;
MockFailureReporterForTest* MockFailureReporterForTest::getReporter()
{
if (instance_ == NULLPTR)
instance_ = new MockFailureReporterForTest;
return instance_;
}
void MockFailureReporterForTest::clearReporter()
{
delete instance_;
instance_ = NULLPTR;
}
MockFailureReporterInstaller::MockFailureReporterInstaller()
{
mock().setMockFailureStandardReporter(MockFailureReporterForTest::getReporter());
}
MockFailureReporterInstaller::~MockFailureReporterInstaller()
{
mock().setMockFailureStandardReporter(NULLPTR);
MockFailureReporterForTest::clearReporter();
}
UtestShell* mockFailureTest()
{
return MockFailureReporterForTest::getReporter()->getTestToFail();
}
SimpleString mockFailureString()
{
return MockFailureReporterForTest::getReporter()->mockFailureString;
}
void CLEAR_MOCK_FAILURE()
{
MockFailureReporterForTest::getReporter()->mockFailureString = "";
}
void CHECK_EXPECTED_MOCK_FAILURE_LOCATION(const MockFailure& expectedFailure, const char* file, size_t line)
{
SimpleString expectedFailureString = expectedFailure.getMessage();
SimpleString actualFailureString = mockFailureString();
CLEAR_MOCK_FAILURE();
if (expectedFailureString != actualFailureString)
{
SimpleString error = "MockFailures are different.\n";
error += "Expected MockFailure:\n\t";
error += expectedFailureString;
error += "\nActual MockFailure:\n\t";
error += actualFailureString;
FAIL_LOCATION(error.asCharString(), file, line);
}
}
void CHECK_NO_MOCK_FAILURE_LOCATION(const char* file, size_t line)
{
if (mockFailureString() != "") {
SimpleString error = "Unexpected mock failure:\n";
error += mockFailureString();
CLEAR_MOCK_FAILURE();
FAIL_LOCATION(error.asCharString(), file, line);
}
CLEAR_MOCK_FAILURE();
}
MockExpectedCallsListForTest::~MockExpectedCallsListForTest()
{
deleteAllExpectationsAndClearList();
}
MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleString& name)
{
MockCheckedExpectedCall* newCall = new MockCheckedExpectedCall;
newCall->withName(name);
addExpectedCall(newCall);
return newCall;
}
MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(unsigned int numCalls, const SimpleString& name)
{
MockCheckedExpectedCall* newCall = new MockCheckedExpectedCall(numCalls);
newCall->withName(name);
addExpectedCall(newCall);
return newCall;
}
MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunctionOrdered(const SimpleString& name, unsigned int order)
{
MockCheckedExpectedCall* newCall = addFunction(name);
newCall->withCallOrder(order);
return newCall;
}

View file

@ -0,0 +1,70 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef D_MockFailureReporterForTest_h
#define D_MockFailureReporterForTest_h
#include "CppUTestExt/MockSupport.h"
#define CHECK_EXPECTED_MOCK_FAILURE(expectedFailure) CHECK_EXPECTED_MOCK_FAILURE_LOCATION(expectedFailure, __FILE__, __LINE__)
#define CHECK_NO_MOCK_FAILURE() CHECK_NO_MOCK_FAILURE_LOCATION(__FILE__, __LINE__)
class MockFailureReporterForTest : public MockFailureReporter
{
public:
SimpleString mockFailureString;
virtual void failTest(const MockFailure& failure) _override;
static MockFailureReporterForTest* getReporter();
static void clearReporter();
private:
static MockFailureReporterForTest* instance_;
};
class MockFailureReporterInstaller
{
public:
MockFailureReporterInstaller();
~MockFailureReporterInstaller();
};
UtestShell* mockFailureTest();
SimpleString mockFailureString();
void CLEAR_MOCK_FAILURE();
void CHECK_EXPECTED_MOCK_FAILURE_LOCATION(const MockFailure& expectedFailure, const char* file, size_t line);
void CHECK_NO_MOCK_FAILURE_LOCATION(const char* file, size_t line);
class MockExpectedCallsListForTest : public MockExpectedCallsList
{
public:
~MockExpectedCallsListForTest() _destructor_override;
MockCheckedExpectedCall* addFunction(const SimpleString& name);
MockCheckedExpectedCall* addFunction(unsigned int numCalls, const SimpleString& name);
MockCheckedExpectedCall* addFunctionOrdered(const SimpleString& name, unsigned int order);
};
#endif

View file

@ -0,0 +1,292 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockFailure.h"
#include "CppUTestExt/MockCheckedExpectedCall.h"
#include "CppUTestExt/MockExpectedCallsList.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockFailureTest)
{
MockFailureReporter reporter;
MockExpectedCallsList *list;
MockCheckedExpectedCall* call1;
MockCheckedExpectedCall* call2;
MockCheckedExpectedCall* call3;
void setup () _override
{
list = new MockExpectedCallsList;
call1 = new MockCheckedExpectedCall;
call2 = new MockCheckedExpectedCall;
call3 = new MockCheckedExpectedCall;
}
void teardown () _override
{
delete list;
delete call1;
delete call2;
delete call3;
CHECK_NO_MOCK_FAILURE();
MockFailureReporterForTest::clearReporter();
}
void addAllToList()
{
list->addExpectedCall(call1);
list->addExpectedCall(call2);
list->addExpectedCall(call3);
}
void checkUnexpectedNthCallMessage(unsigned int count, const char* expectedOrdinal)
{
MockExpectedCallsList callList;
MockCheckedExpectedCall expectedCallSingle(1);
MockCheckedExpectedCall expectedCallMulti(count-1);
expectedCallSingle.withName("bar");
expectedCallMulti.withName("bar");
if (count > 1) {
callList.addExpectedCall(&expectedCallSingle);
expectedCallSingle.callWasMade(1);
}
if (count > 2) {
callList.addExpectedCall(&expectedCallMulti);
for (unsigned int i = 1; i < (count - 1); i++) {
expectedCallMulti.callWasMade(i+1);
}
}
MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "bar", callList);
SimpleString expectedMessage = StringFromFormat("Mock Failure: Unexpected additional (%s) call to function: bar\n\tEXPECTED", expectedOrdinal);
STRCMP_CONTAINS(expectedMessage.asCharString(), failure.getMessage().asCharString());
}
};
TEST(MockFailureTest, noErrorFailureSomethingGoneWrong)
{
MockFailure failure(UtestShell::getCurrent());
STRCMP_EQUAL("Test failed with MockFailure without an error! Something went seriously wrong.", failure.getMessage().asCharString());
}
TEST(MockFailureTest, unexpectedCallHappened)
{
MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "foobar", *list);
STRCMP_EQUAL("Mock Failure: Unexpected call to function: foobar\n"
"\tEXPECTED calls that WERE NOT fulfilled:\n"
"\t\t<none>\n"
"\tEXPECTED calls that WERE fulfilled:\n"
"\t\t<none>", failure.getMessage().asCharString());
}
TEST(MockFailureTest, expectedCallDidNotHappen)
{
call1->withName("foobar");
call2->withName("world").withParameter("boo", 2).withParameter("hello", "world");
call3->withName("haphaphap");
call3->callWasMade(1);
addAllToList();
MockExpectedCallsDidntHappenFailure failure(UtestShell::getCurrent(), *list);
STRCMP_EQUAL("Mock Failure: Expected call WAS NOT fulfilled.\n"
"\tEXPECTED calls that WERE NOT fulfilled:\n"
"\t\tfoobar -> no parameters (expected 1 call, called 0 times)\n"
"\t\tworld -> int boo: <2 (0x2)>, const char* hello: <world> (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled:\n"
"\t\thaphaphap -> no parameters (expected 1 call, called 1 time)", failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockUnexpectedNthAdditionalCallFailure)
{
checkUnexpectedNthCallMessage(2, "2nd");
checkUnexpectedNthCallMessage(3, "3rd");
checkUnexpectedNthCallMessage(4, "4th");
checkUnexpectedNthCallMessage(11, "11th");
checkUnexpectedNthCallMessage(12, "12th");
checkUnexpectedNthCallMessage(13, "13th");
checkUnexpectedNthCallMessage(14, "14th");
checkUnexpectedNthCallMessage(21, "21st");
checkUnexpectedNthCallMessage(22, "22nd");
checkUnexpectedNthCallMessage(23, "23rd");
}
TEST(MockFailureTest, MockUnexpectedInputParameterFailure)
{
call1->withName("foo").withParameter("boo", 2);
call2->withName("foo").withParameter("boo", 3.3);
call3->withName("unrelated");
addAllToList();
MockNamedValue actualParameter("bar");
actualParameter.setValue(2);
MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
STRCMP_EQUAL("Mock Failure: Unexpected parameter name to function \"foo\": bar\n"
"\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
"\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, called 0 times)\n"
"\t\tfoo -> double boo: <3.3> (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled related to function: foo\n"
"\t\t<none>\n"
"\tACTUAL unexpected parameter passed to function: foo\n"
"\t\tint bar: <2 (0x2)>", failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockUnexpectedOutputParameterFailure)
{
int out1;
int out2;
call1->withName("foo").withOutputParameterReturning("boo", &out1, sizeof(out1));
call2->withName("foo").withOutputParameterReturning("boo", &out2, sizeof(out2));
call3->withName("unrelated");
addAllToList();
MockNamedValue actualParameter("bar");
actualParameter.setValue((void *)0x123);
MockUnexpectedOutputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
STRCMP_EQUAL("Mock Failure: Unexpected output parameter name to function \"foo\": bar\n"
"\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
"\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
"\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled related to function: foo\n"
"\t\t<none>\n"
"\tACTUAL unexpected output parameter passed to function: foo\n"
"\t\tvoid* bar", failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockUnexpectedUnmodifiedOutputParameterFailure)
{
int out1;
call1->withName("foo").withOutputParameterReturning("boo", &out1, sizeof(out1));
call2->withName("foo").withUnmodifiedOutputParameter("boo");
call3->withName("unrelated");
addAllToList();
MockNamedValue actualParameter("bar");
actualParameter.setValue((void *)0x123);
MockUnexpectedOutputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
STRCMP_EQUAL("Mock Failure: Unexpected output parameter name to function \"foo\": bar\n"
"\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
"\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
"\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled related to function: foo\n"
"\t\t<none>\n"
"\tACTUAL unexpected output parameter passed to function: foo\n"
"\t\tvoid* bar", failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockUnexpectedParameterValueFailure)
{
call1->withName("foo").withParameter("boo", 2);
call2->withName("foo").withParameter("boo", 10);
call3->withName("unrelated");
addAllToList();
MockNamedValue actualParameter("boo");
actualParameter.setValue(20);
MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
STRCMP_EQUAL("Mock Failure: Unexpected parameter value to parameter \"boo\" to function \"foo\": <20 (0x14)>\n"
"\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
"\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, called 0 times)\n"
"\t\tfoo -> int boo: <10 (0xa)> (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled related to function: foo\n"
"\t\t<none>\n"
"\tACTUAL unexpected parameter passed to function: foo\n"
"\t\tint boo: <20 (0x14)>", failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockExpectedParameterDidntHappenFailure)
{
call1->withName("foo").withParameter("bar", 2).withParameter("boo", "str");
call2->withName("foo").withParameter("bar", 10).withParameter("boo", "bleh");
call2->callWasMade(1);
call2->inputParameterWasPassed("bar");
call2->inputParameterWasPassed("boo");
call3->withName("unrelated");
addAllToList();
MockExpectedParameterDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list);
STRCMP_EQUAL("Mock Failure: Expected parameter for function \"foo\" did not happen.\n"
"\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
"\t\tfoo -> int bar: <2 (0x2)>, const char* boo: <str> (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled related to function: foo\n"
"\t\tfoo -> int bar: <10 (0xa)>, const char* boo: <bleh> (expected 1 call, called 1 time)\n"
"\tMISSING parameters that didn't happen:\n"
"\t\tint bar, const char* boo", failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockNoWayToCompareCustomTypeFailure)
{
MockNoWayToCompareCustomTypeFailure failure(UtestShell::getCurrent(), "myType");
STRCMP_EQUAL("MockFailure: No way to compare type <myType>. Please install a MockNamedValueComparator.", failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockUnexpectedObjectFailure)
{
call1->withName("foo").onObject((void*) 0x02);
call2->withName("foo").onObject((void*) 0x03);
call2->callWasMade(1);
call2->wasPassedToObject();
call3->withName("unrelated");
addAllToList();
MockUnexpectedObjectFailure failure(UtestShell::getCurrent(), "foo", (void*)0x1, *list);
STRCMP_EQUAL(StringFromFormat (
"MockFailure: Function called on an unexpected object: foo\n"
"\tActual object for call has address: <%p>\n"
"\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
"\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled related to function: foo\n"
"\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 1 time)",
(void*) 0x01, (void*) 0x02, (void*) 0x03).asCharString(), failure.getMessage().asCharString());
}
TEST(MockFailureTest, MockExpectedObjectDidntHappenFailure)
{
call1->withName("foo").onObject((void*) 0x02);
call2->withName("foo").onObject((void*) 0x03);
call2->callWasMade(1);
call2->wasPassedToObject();
call3->withName("unrelated");
addAllToList();
MockExpectedObjectDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list);
STRCMP_EQUAL(StringFromFormat(
"Mock Failure: Expected call on object for function \"foo\" but it did not happen.\n"
"\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
"\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 0 times)\n"
"\tEXPECTED calls that WERE fulfilled related to function: foo\n"
"\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 1 time)",
(void*) 0x2, (void*) 0x3).asCharString(), failure.getMessage().asCharString());
}

View file

@ -0,0 +1,171 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTest/TestTestingFixture.h"
#include "CppUTestExt/MockCheckedActualCall.h"
#include "CppUTestExt/MockCheckedExpectedCall.h"
#include "CppUTestExt/MockExpectedCallsList.h"
#include "CppUTestExt/MockFailure.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(FakeLongLongs)
{
TestTestingFixture fixture;
};
#ifndef CPPUTEST_USE_LONG_LONG
#define CHECK_TEST_FAILS_PROPER_WITH_TEXT(text) fixture.checkTestFailsWithProperTestLocation(text, __FILE__, __LINE__)
static void _actualCallWithFakeLongLongParameter()
{
cpputest_longlong value = {0};
mock().expectOneCall("foo").withParameter("bar", 0);
mock().actualCall("foo").withParameter("bar", value);
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ActualCallWithFakeLongLongParameterFAILS)
{
fixture.runTestWithMethod(_actualCallWithFakeLongLongParameter);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Long Long type is not supported");
}
static void _actualCallWithFakeUnsignedLongLongParameter()
{
cpputest_ulonglong value = {0};
mock().expectOneCall("foo").withParameter("bar", 0);
mock().actualCall("foo").withParameter("bar", value);
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ActualCallWithFakeUnsignedLongLongParameterFAILS)
{
fixture.runTestWithMethod(_actualCallWithFakeUnsignedLongLongParameter);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Unsigned Long Long type is not supported");
}
static void _actualCallWithFakeLongLongReturn()
{
mock().expectOneCall("foo").andReturnValue(0);
mock().actualCall("foo").returnLongLongIntValue();
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ActualCallWithFakeLongLongReturnFAILS)
{
fixture.runTestWithMethod(_actualCallWithFakeLongLongReturn);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Long Long type is not supported");
}
static void _actualCallWithFakeUnsignedLongLongReturn()
{
mock().expectOneCall("foo").andReturnValue(0);
mock().actualCall("foo").returnUnsignedLongLongIntValue();
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ActualCallWithFakeUnsignedLongLongReturnFAILS)
{
fixture.runTestWithMethod(_actualCallWithFakeUnsignedLongLongReturn);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Unsigned Long Long type is not supported");
}
static void _expectOneCallWithFakeLongLongParameter()
{
cpputest_longlong value = {0};
mock().expectOneCall("foo").withParameter("bar", value);
mock().actualCall("foo").withParameter("bar", 0);
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ExpectedCallWithFakeLongLongParameterFAILS)
{
fixture.runTestWithMethod(_expectOneCallWithFakeLongLongParameter);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Long Long type is not supported");
}
static void _expectOneCallWithFakeUnsignedLongLongParameter()
{
cpputest_ulonglong value = {0};
mock().expectOneCall("foo").withParameter("bar", value);
mock().actualCall("foo").withParameter("bar", 0);
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ExpectedCallWithFakeUnsignedLongLongParameterFAILS)
{
fixture.runTestWithMethod(_expectOneCallWithFakeUnsignedLongLongParameter);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Unsigned Long Long type is not supported");
}
static void _expectOneCallWithFakeLongLongReturn()
{
cpputest_longlong value = {0};
mock().expectOneCall("foo").andReturnValue(value);
mock().actualCall("foo").returnIntValue();
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ExpectedCallWithFakeLongLongReturnFAILS)
{
fixture.runTestWithMethod(_expectOneCallWithFakeLongLongReturn);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Long Long type is not supported");
}
static void _expectOneCallWithFakeUnsignedLongLongReturn()
{
cpputest_ulonglong value = {0};
mock().expectOneCall("foo").andReturnValue(value);
mock().actualCall("foo").returnIntValue();
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
TEST(FakeLongLongs, ExpectedCallWithFakeUnsignedLongLongReturnFAILS)
{
fixture.runTestWithMethod(_expectOneCallWithFakeUnsignedLongLongReturn);
mock().clear();
CHECK_TEST_FAILS_PROPER_WITH_TEXT("Unsigned Long Long type is not supported");
}
#endif

View file

@ -0,0 +1,153 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockHierarchyTest)
{
void teardown() _override
{
mock().clear();
}
};
TEST(MockHierarchyTest, getMockSupportScope)
{
MockSupport* mock1 = mock().getMockSupportScope("name");
MockSupport* mock2 = mock().getMockSupportScope("differentName");
CHECK(!mock().hasData("name"));
CHECK(mock1 != mock2);
POINTERS_EQUAL(mock1, mock().getMockSupportScope("name"));
CHECK(mock1 != &mock());
}
TEST(MockHierarchyTest, usingTwoMockSupportsByName)
{
mock("first").expectOneCall("boo");
LONGS_EQUAL(0, mock("other").expectedCallsLeft());
LONGS_EQUAL(1, mock("first").expectedCallsLeft());
mock("first").clear();
}
TEST(MockHierarchyTest, EnableDisableWorkHierarchically)
{
mock("first");
mock().disable();
mock("first").expectOneCall("boo");
LONGS_EQUAL(0, mock("first").expectedCallsLeft());
mock().enable();
mock("first").expectOneCall("boo");
LONGS_EQUAL(1, mock("first").expectedCallsLeft());
mock("first").clear();
}
TEST(MockHierarchyTest, EnableDisableWorkHierarchicallyWhenSupportIsDynamicallyCreated)
{
mock().disable();
mock("first").expectOneCall("boo");
LONGS_EQUAL(0, mock("first").expectedCallsLeft());
mock().enable();
mock("second").expectOneCall("boo");
LONGS_EQUAL(1, mock("second").expectedCallsLeft());
mock().clear();
}
TEST(MockHierarchyTest, ExpectedCallsLeftWorksHierarchically)
{
mock("first").expectOneCall("foobar");
LONGS_EQUAL(1, mock().expectedCallsLeft());
mock().clear();
}
TEST(MockHierarchyTest, checkExpectationsWorksHierarchically)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("first::foobar");
expectations.addFunction("second::helloworld");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock("first").expectOneCall("foobar");
mock("second").expectOneCall("helloworld");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockHierarchyTest, ignoreOtherCallsWorksHierarchically)
{
mock("first");
mock().ignoreOtherCalls();
mock("first").actualCall("boo");
mock().checkExpectations();
}
TEST(MockHierarchyTest, ignoreOtherCallsWorksHierarchicallyWhenDynamicallyCreated)
{
mock().ignoreOtherCalls();
mock("first").actualCall("boo");
mock().checkExpectations();
}
TEST(MockHierarchyTest, checkExpectationsWorksHierarchicallyForLastCallNotFinished)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("first::foobar")->withParameter("boo", 1);
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "first::foobar", expectations);
mock("first").expectOneCall("foobar").withParameter("boo", 1);
mock("first").actualCall("foobar");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockHierarchyTest, reporterIsInheritedInHierarchicalMocks)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
mock("differentScope").actualCall("foobar");
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "differentScope::foobar", expectations);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

View file

@ -0,0 +1,141 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockNamedValue.h"
TEST_GROUP(ComparatorsAndCopiersRepository)
{
};
class MyComparator : public MockNamedValueComparator
{
public:
MyComparator() {}
virtual ~MyComparator() _destructor_override {}
virtual bool isEqual(const void*, const void*) _override { return false; }
virtual SimpleString valueToString(const void*) _override { return ""; }
};
class MyCopier : public MockNamedValueCopier
{
public:
MyCopier() {}
virtual ~MyCopier() _destructor_override {}
virtual void copy(void*, const void*) _override {}
};
TEST(ComparatorsAndCopiersRepository, InstallCopierAndRetrieveIt)
{
MyCopier copier;
MockNamedValueComparatorsAndCopiersRepository repository;
repository.installCopier("MyType", copier);
POINTERS_EQUAL(&copier, repository.getCopierForType("MyType"));
repository.clear();
}
TEST(ComparatorsAndCopiersRepository, ComparatorAndCopierByTheSameNameShouldBothBeFound)
{
MyComparator comparator;
MyCopier copier;
MockNamedValueComparatorsAndCopiersRepository repository;
repository.installCopier("MyType", copier);
repository.installComparator("MyType", comparator);
POINTERS_EQUAL(&comparator, repository.getComparatorForType("MyType"));
POINTERS_EQUAL(&copier, repository.getCopierForType("MyType"));
repository.clear();
}
TEST(ComparatorsAndCopiersRepository, InstallComparatorsAndCopiersFromRepository)
{
MyComparator comparator;
MyCopier copier;
MockNamedValueComparatorsAndCopiersRepository source;
MockNamedValueComparatorsAndCopiersRepository target;
source.installCopier("MyType", copier);
source.installComparator("MyType", comparator);
target.installComparatorsAndCopiers(source);
POINTERS_EQUAL(&comparator, target.getComparatorForType("MyType"));
POINTERS_EQUAL(&copier, target.getCopierForType("MyType"));
source.clear();
target.clear();
}
TEST_GROUP(MockNamedValue)
{
MockNamedValue * value;
void setup() _override
{
value = new MockNamedValue("param");
}
void teardown() _override
{
delete value;
}
};
TEST(MockNamedValue, DefaultToleranceUsedWhenNoToleranceGiven)
{
value->setValue(0.2);
DOUBLES_EQUAL(MockNamedValue::defaultDoubleTolerance, value->getDoubleTolerance(), 0.0);
}
TEST(MockNamedValue, GivenToleranceUsed)
{
value->setValue(0.2, 3.2);
STRCMP_EQUAL("double", value->getType().asCharString());
DOUBLES_EQUAL(0.2, value->getDoubleValue(), 0.0);
DOUBLES_EQUAL(3.2, value->getDoubleTolerance(), 0.0);
}
TEST(MockNamedValue, DoublesEqualIfWithinTolerance)
{
value->setValue(5.0, 0.4);
MockNamedValue other("param2");
other.setValue(5.3);
CHECK_TRUE(value->equals(other));
}
TEST(MockNamedValue, DoublesNotEqualIfOutsideTolerance)
{
value->setValue(5.0, 0.4);
MockNamedValue other("param2");
other.setValue(5.5);
CHECK_FALSE(value->equals(other));
}

View file

@ -0,0 +1,932 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockParameterTest)
{
void teardown() _override
{
mock().checkExpectations();
mock().clear();
}
};
TEST(MockParameterTest, expectOneBooleanParameterAndValue)
{
mock().expectOneCall("foo").withParameter("parameter", true);
mock().actualCall("foo").withParameter("parameter", true);
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneUnsignedIntegerParameterAndValue)
{
unsigned int value = 14400;
mock().expectOneCall("foo").withParameter("parameter", value);
mock().actualCall("foo").withParameter("parameter", value);
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneIntegerParameterAndValue)
{
mock().expectOneCall("foo").withParameter("parameter", 10);
mock().actualCall("foo").withParameter("parameter", 10);
mock().checkExpectations();
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockParameterTest, expectOneUnsignedLongLongIntegerParameterAndValue)
{
unsigned long long value = 0xFFFFAAAAFFFFAAAA;
mock().expectOneCall("foo").withParameter("parameter", value);
mock().actualCall("foo").withParameter("parameter", value);
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneLongLongIntegerParameterAndValue)
{
long long value = 0x7FFFAAAAFFFFAAAA;
mock().expectOneCall("foo").withParameter("parameter", value);
mock().actualCall("foo").withParameter("parameter", value);
mock().checkExpectations();
}
#endif
TEST(MockParameterTest, mismatchedIntegerTypesIntAndLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (int)1);
mock().actualCall("foo").withParameter("parameter", (long)1);
mock().expectOneCall("foo").withParameter("parameter", (long)1);
mock().actualCall("foo").withParameter("parameter", (int)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesIntAndUnsignedAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (int)1);
mock().actualCall("foo").withParameter("parameter", (unsigned)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned)1);
mock().actualCall("foo").withParameter("parameter", (int)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesIntAndUnsignedLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (int)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long)1);
mock().actualCall("foo").withParameter("parameter", (int)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesUnsignedAndLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (unsigned)1);
mock().actualCall("foo").withParameter("parameter", (long)1);
mock().expectOneCall("foo").withParameter("parameter", (long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesUnsignedAndUnsignedLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (unsigned)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesLongAndUnsignedLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long)1);
mock().actualCall("foo").withParameter("parameter", (long)1);
mock().checkExpectations();
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockParameterTest, mismatchedIntegerTypesIntAndLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (int)1);
mock().actualCall("foo").withParameter("parameter", (long long)1);
mock().expectOneCall("foo").withParameter("parameter", (long long)1);
mock().actualCall("foo").withParameter("parameter", (int)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesIntAndUnsignedLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (int)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long long)1);
mock().actualCall("foo").withParameter("parameter", (int)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesUnsignedAndLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (unsigned)1);
mock().actualCall("foo").withParameter("parameter", (long long)1);
mock().expectOneCall("foo").withParameter("parameter", (long long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesUnsignedAndUnsignedLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (unsigned)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesUnsignedLongAndUnsignedLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (unsigned long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesLongAndLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (long)1);
mock().actualCall("foo").withParameter("parameter", (long long)1);
mock().expectOneCall("foo").withParameter("parameter", (long long)1);
mock().actualCall("foo").withParameter("parameter", (long)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesLongAndUnsignedLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long long)1);
mock().actualCall("foo").withParameter("parameter", (long)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesUnsignedLongAndLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (unsigned long)1);
mock().actualCall("foo").withParameter("parameter", (long long)1);
mock().expectOneCall("foo").withParameter("parameter", (long long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long)1);
mock().checkExpectations();
}
TEST(MockParameterTest, mismatchedIntegerTypesLongLongAndUnsignedLongLongAreAllowed)
{
mock().expectOneCall("foo").withParameter("parameter", (long long)1);
mock().actualCall("foo").withParameter("parameter", (unsigned long long)1);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long long)1);
mock().actualCall("foo").withParameter("parameter", (long long)1);
mock().checkExpectations();
}
#endif
TEST(MockParameterTest, longAndUnsignedLongWithSameBitRepresentationShouldNotBeTreatedAsEqual)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", (long)-1);
MockNamedValue parameter("parameter");
parameter.setValue((unsigned long)-1);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", (long)-1);
mock().actualCall("foo").withParameter("parameter", (unsigned long)-1);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, unsignedLongAndLongWithSameBitRepresentationShouldnotBeTreatedAsEqual)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", (unsigned long)-1);
MockNamedValue parameter("parameter");
parameter.setValue((long)-1);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", (unsigned long)-1);
mock().actualCall("foo").withParameter("parameter", (long)-1);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneDoubleParameterAndValue)
{
mock().expectOneCall("foo").withParameter("parameter", 1.0);
mock().actualCall("foo").withParameter("parameter", 1.0);
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneDoubleParameterAndValueAndTolerance)
{
mock( ).expectOneCall("foo").withParameter("parameter", 100.0, 5.0);
mock( ).actualCall("foo").withParameter("parameter", 96.0);
mock( ).checkExpectations();
}
TEST(MockParameterTest, doubleParameterNotEqualIfOutsideTolerance)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", 100.0);
MockNamedValue parameter("parameter");
parameter.setValue(106.0);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock( ).expectOneCall("foo").withParameter("parameter", 100.0, 5.0);
mock( ).actualCall("foo").withParameter("parameter", 106.0);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneStringParameterAndValue)
{
mock().expectOneCall("foo").withParameter("parameter", "string");
mock().actualCall("foo").withParameter("parameter", "string");
mock().checkExpectations();
}
TEST(MockParameterTest, expectOnePointerParameterAndValue)
{
mock().expectOneCall("foo").withParameter("parameter", (void*) 0x01);
mock().actualCall("foo").withParameter("parameter", (void*) 0x01);
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneConstPointerParameterAndValue)
{
mock().expectOneCall("foo").withParameter("parameter", (const void*) 0x01);
mock().actualCall("foo").withParameter("parameter", (const void*) 0x01);
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneFunctionPointerParameterAndValue)
{
mock().expectOneCall("foo").withParameter("parameter", (void(*)()) 0x01);
mock().actualCall("foo").withParameter("parameter", (void(*)()) 0x01);
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneMemBufferParameterAndValue)
{
unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF };
unsigned char memBuffer2[] = { 0x12, 0x15, 0xFF };
mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1));
mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2));
mock().checkExpectations();
}
TEST(MockParameterTest, expectOneMemBufferParameterAndValueFailsDueToContents)
{
MockFailureReporterInstaller failureReporterInstaller;
unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF };
unsigned char memBuffer2[] = { 0x12, 0x05, 0xFF };
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1));
MockNamedValue parameter("parameter");
parameter.setMemoryBuffer( memBuffer2, sizeof(memBuffer2) );
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1));
mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2));
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneMemBufferParameterAndValueFailsDueToSize)
{
MockFailureReporterInstaller failureReporterInstaller;
unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF };
unsigned char memBuffer2[] = { 0x12, 0x15, 0xFF, 0x90 };
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1));
MockNamedValue parameter("parameter");
parameter.setMemoryBuffer( memBuffer2, sizeof(memBuffer2) );
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1));
mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2));
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneStringParameterAndValueFails)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", "string");
MockNamedValue parameter("parameter");
parameter.setValue("different");
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", "string");
mock().actualCall("foo").withParameter("parameter", "different");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneUnsignedIntegerParameterAndFailsDueToParameterName)
{
MockFailureReporterInstaller failureReporterInstaller;
unsigned int value = 7;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", value);
MockNamedValue parameter("different");
parameter.setValue(value);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", value);
mock().actualCall("foo").withParameter("different", value);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneIntegerParameterAndFailsDueToParameterName)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", 10);
MockNamedValue parameter("different");
parameter.setValue(10);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", 10);
mock().actualCall("foo").withParameter("different", 10);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneUnsignedIntegerParameterAndFailsDueToValue)
{
MockFailureReporterInstaller failureReporterInstaller;
unsigned int actual_value = 8;
unsigned int expected_value = actual_value + 1;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", expected_value);
MockNamedValue parameter("parameter");
parameter.setValue(actual_value);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", expected_value);
mock().actualCall("foo").withParameter("parameter", actual_value);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneIntegerParameterAndFailsDueToValue)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", 10);
MockNamedValue parameter("parameter");
parameter.setValue(8);
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", 10);
mock().actualCall("foo").withParameter("parameter", 8);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectOneIntegerParameterAndFailsDueToTypes)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("parameter", 10);
MockNamedValue parameter("parameter");
parameter.setValue("heh");
MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().expectOneCall("foo").withParameter("parameter", 10);
mock().actualCall("foo").withParameter("parameter", "heh");
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, expectMultipleCallsWithDifferentParametersThatHappenOutOfOrder)
{
mock().expectOneCall("foo").withParameter("p1", 1);
mock().expectOneCall("foo").withParameter("p1", 2);
mock().actualCall("foo").withParameter("p1", 2);
mock().actualCall("foo").withParameter("p1", 1);
mock().checkExpectations();
}
TEST(MockParameterTest, expectMultipleCallsWithMultipleDifferentParametersThatHappenOutOfOrder)
{
mock().expectOneCall("foo").withParameter("p1", 1).withParameter("p2", 2);
mock().expectOneCall("foo").withParameter("p1", 1).withParameter("p2", 20);
mock().actualCall("foo").withParameter("p1", 1).withParameter("p2", 20);
mock().actualCall("foo").withParameter("p1", 1).withParameter("p2", 2);
mock().checkExpectations();
}
TEST(MockParameterTest, twiceCalledWithSameParameters)
{
mock().expectOneCall("foo").withParameter("p1", 1).withParameter("p2", 2);
mock().expectOneCall("foo").withParameter("p1", 1).withParameter("p2", 2);
mock().actualCall("foo").withParameter("p1", 1).withParameter("p2", 2);
mock().actualCall("foo").withParameter("p1", 1).withParameter("p2", 2);
mock().checkExpectations();
}
TEST(MockParameterTest, calledWithoutParameters)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("p1", 1);
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations);
mock().expectOneCall("foo").withParameter("p1", 1);
mock().actualCall("foo");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, ignoreOtherParameters)
{
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters();
mock().actualCall("foo").withParameter("p1", 1).withParameter("p2", 2);
mock().checkExpectations();
}
TEST(MockParameterTest, ignoreOtherParametersButStillPassAll)
{
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters();
mock().actualCall("foo").withParameter("p1", 1);
mock().checkExpectations();
}
TEST(MockParameterTest, ignoreOtherParametersButExpectedParameterDidntHappen)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("p1", 1).ignoreOtherParameters();
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters();
mock().actualCall("foo").withParameter("p2", 2).withParameter("p3", 3).withParameter("p4", 4);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, ignoreOtherParametersMultipleCalls)
{
mock().expectOneCall("foo").ignoreOtherParameters();
mock().expectOneCall("foo").ignoreOtherParameters();
mock().actualCall("foo").withParameter("p2", 2).withParameter("p3", 3).withParameter("p4", 4);
LONGS_EQUAL(1, mock().expectedCallsLeft());
mock().actualCall("foo").withParameter("p2", 2).withParameter("p3", 3).withParameter("p4", 4);
mock().checkExpectations();
}
TEST(MockParameterTest, ignoreOtherParametersMultipleCallsButOneDidntHappen)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
MockCheckedExpectedCall* call = expectations.addFunction("boo");
call->ignoreOtherParameters();
call->callWasMade(1);
call->finalizeActualCallMatch();
call->ignoreOtherParameters();
expectations.addFunction("boo")->ignoreOtherParameters();
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock().expectOneCall("boo").ignoreOtherParameters();
mock().expectOneCall("boo").ignoreOtherParameters();
mock().actualCall("boo");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, newCallStartsWhileNotAllParametersWerePassed)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->withParameter("p1", 1);
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations);
mock().expectOneCall("foo").withParameter("p1", 1);
mock().actualCall("foo");
mock().actualCall("foo").withParameter("p1", 1);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, outputParameterSucceeds)
{
int param = 1;
int retval = 2;
mock().expectOneCall("function").withOutputParameterReturning("parameterName", &retval, sizeof(retval));
mock().actualCall("function").withOutputParameter("parameterName", &param);
CHECK_EQUAL(param, 2);
CHECK_EQUAL(retval, 2);
mock().checkExpectations();
}
TEST(MockParameterTest, unmodifiedOutputParameterSucceeds)
{
int param = 1;
mock().expectOneCall("function").withUnmodifiedOutputParameter("parameterName");
mock().actualCall("function").withOutputParameter("parameterName", &param);
CHECK_EQUAL(param, 1);
mock().checkExpectations();
}
TEST(MockParameterTest, noActualCallForOutputParameter)
{
MockFailureReporterInstaller failureReporterInstaller;
int output;
MockExpectedCallsListForTest expectations;
mock().expectOneCall("foo").withOutputParameterReturning("output", &output, sizeof(output));
expectations.addFunction("foo")->withOutputParameterReturning("output", &output, sizeof(output));
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, noActualCallForUnmodifiedOutputParameter)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
mock().expectOneCall("foo").withUnmodifiedOutputParameter("output");
expectations.addFunction("foo")->withUnmodifiedOutputParameter("output");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, unexpectedOutputParameter)
{
MockFailureReporterInstaller failureReporterInstaller;
int param;
MockExpectedCallsListForTest expectations;
mock().expectOneCall("foo");
mock().actualCall("foo").withOutputParameter("parameterName", &param);
expectations.addFunction("foo");
MockNamedValue parameter("parameterName");
parameter.setValue(&param);
MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, outputParameterMissing)
{
MockFailureReporterInstaller failureReporterInstaller;
int output;
MockExpectedCallsListForTest expectations;
mock().expectOneCall("foo").withOutputParameterReturning("output", &output, sizeof(output));
mock().actualCall("foo");
expectations.addFunction("foo")->withOutputParameterReturning("output", &output, sizeof(output));
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations);
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockParameterTest, twoOutputParameters)
{
int param1 = 55;
int retval1 = 1;
int param2 = 77;
int retval2 = 2;
mock().expectOneCall("function").withOutputParameterReturning("parameterName", &retval1, sizeof(retval1)).withParameter("id", 1);
mock().expectOneCall("function").withOutputParameterReturning("parameterName", &retval2, sizeof(retval2)).withParameter("id", 2);
mock().actualCall("function").withOutputParameter("parameterName", &param1).withParameter("id", 1);
mock().actualCall("function").withOutputParameter("parameterName", &param2).withParameter("id", 2);
CHECK_EQUAL(retval1, param1);
CHECK_EQUAL(retval2, param2);
mock().checkExpectations();
}
TEST(MockParameterTest, twoInterleavedOutputParameters)
{
int param1 = 55;
int retval1 = 1;
int param2 = 77;
int retval2 = 2;
mock().expectOneCall("function").withOutputParameterReturning("parameterName", &retval1, sizeof(retval1)).withParameter("id", 1);
mock().expectOneCall("function").withOutputParameterReturning("parameterName", &retval2, sizeof(retval2)).withParameter("id", 2);
mock().actualCall("function").withOutputParameter("parameterName", &param2).withParameter("id", 2);
mock().actualCall("function").withOutputParameter("parameterName", &param1).withParameter("id", 1);
CHECK_EQUAL(retval1, param1);
CHECK_EQUAL(retval2, param2);
mock().checkExpectations();
}
TEST(MockParameterTest, twoDifferentOutputParametersInSameFunctionCallSucceeds)
{
int param1 = 1;
int param2 = 1;
int retval1 = 2;
int retval2 = 3;
mock().expectOneCall("foo")
.withOutputParameterReturning("bar", &retval1, sizeof(retval1))
.withOutputParameterReturning("foobar", &retval2, sizeof(retval2));
mock().actualCall("foo")
.withOutputParameter("bar", &param1)
.withOutputParameter("foobar", &param2);
CHECK_EQUAL(2, retval1);
CHECK_EQUAL(2, param1);
CHECK_EQUAL(3, retval2);
CHECK_EQUAL(3, param2);
mock().checkExpectations();
}
TEST(MockParameterTest, outputAndIntParametersOfSameNameInDifferentFunctionCallsOfSameFunctionSucceeds)
{
int param = 1;
int retval = 2;
mock().expectOneCall("foo").withOutputParameterReturning("bar", &retval, sizeof(retval));
mock().expectOneCall("foo").withIntParameter("bar", 25);
mock().actualCall("foo").withOutputParameter("bar", &param);
mock().actualCall("foo").withIntParameter("bar", 25);
CHECK_EQUAL(2, retval);
CHECK_EQUAL(2, param);
mock().checkExpectations();
}
TEST(MockParameterTest, twoOutputParameterOfSameNameInDifferentFunctionCallsOfSameFunctionSucceeds)
{
int param1 = 1;
int param2 = 1;
int retval1 = 2;
int retval2 = 3;
mock().expectOneCall("foo").withOutputParameterReturning("bar", &retval1, sizeof(retval1));
mock().expectOneCall("foo").withOutputParameterReturning("bar", &retval2, sizeof(retval2));
mock().actualCall("foo").withOutputParameter("bar", &param1);
mock().actualCall("foo").withOutputParameter("bar", &param2);
CHECK_EQUAL(2, retval1);
CHECK_EQUAL(2, param1);
CHECK_EQUAL(3, retval2);
CHECK_EQUAL(3, param2);
mock().checkExpectations();
}
TEST(MockParameterTest, twoOutputParametersOfSameNameInDifferentFunctionsSucceeds)
{
int param = 1;
int retval = 2;
mock().expectOneCall("foo1").withOutputParameterReturning("bar", &retval, sizeof(retval));
mock().expectOneCall("foo2").withIntParameter("bar", 25);
mock().actualCall("foo1").withOutputParameter("bar", &param);
mock().actualCall("foo2").withIntParameter("bar", 25);
CHECK_EQUAL(2, retval);
CHECK_EQUAL(2, param);
mock().checkExpectations();
}
TEST(MockParameterTest, outputAndInputParameter)
{
int return_value = 5;
int returned_value = 7;
mock().expectOneCall("foo").withParameter("bar", 10).withOutputParameterReturning("bar", &return_value, sizeof(return_value));
mock().actualCall("foo").withParameter("bar", 10).withOutputParameter("bar", &returned_value);
LONGS_EQUAL(5, returned_value);
mock().checkExpectations();
}
TEST(MockParameterTest, outputParameterTraced)
{
mock().tracing(true);
int param = 1;
mock().actualCall("someFunc").withOutputParameter("someParameter", &param);
mock().checkExpectations();
STRCMP_CONTAINS("Function name:someFunc someParameter:", mock().getTraceOutput());
mock().checkExpectations();
}
TEST(MockParameterTest, outputParameterThatIsIgnoredShouldNotFail)
{
int param;
mock().expectOneCall("function").ignoreOtherParameters();
mock().actualCall("function").withOutputParameter("parameterName", &param);
mock().checkExpectations();
}
TEST(MockParameterTest, outputParameterWithIgnoredParameters)
{
int param = 1;
int retval = 2;
mock().expectOneCall("foo").withOutputParameterReturning("bar", &param, sizeof(param)).ignoreOtherParameters();
mock().actualCall("foo").withOutputParameter("bar", &retval).withParameter("other", 1);
LONGS_EQUAL(param, retval);
mock().checkExpectations();
}
/*
* This test checks that the proper output parameters are copied when multiple calls to the same
* function are expected.
*/
TEST(MockParameterTest, properOutputParametersAreCopied)
{
int expectedValue1 = 1;
int expectedValue2 = 2;
mock().expectOneCall("foo").withOutputParameterReturning("param", &expectedValue1, sizeof(expectedValue1)).ignoreOtherParameters();
mock().expectOneCall("foo").withOutputParameterReturning("param", &expectedValue2, sizeof(expectedValue2));
int returnedValue1 = 0;
int returnedValue2 = 0;
mock().actualCall("foo").withOutputParameter("param", &returnedValue1);
mock().actualCall("foo").withOutputParameter("param", &returnedValue2).withParameter("optional", 50);
CHECK_EQUAL_TEXT(expectedValue2, returnedValue1, "Wrong output value in 1st call");
CHECK_EQUAL_TEXT(expectedValue1, returnedValue2, "Wrong output value in 2nd call");
mock().checkExpectations();
}
TEST(MockParameterTest, ignoreOtherCallsIgnoresWithAllKindsOfParameters)
{
mock().ignoreOtherCalls();
mock().actualCall("boo")
.withParameter("umm", true)
.withParameter("bar", 1u)
.withParameter("foo", 1l)
.withParameter("hey", 1ul)
#ifdef CPPUTEST_USE_LONG_LONG
.withParameter("ick", 1ll)
.withParameter("grr", 1ull)
#endif
.withParameter("duh", 1.0)
.withParameter("yoo", (const void*) NULLPTR)
.withParameter("func", (void(*)()) NULLPTR)
.withParameter("mem", (const unsigned char*) NULLPTR, 0)
.withParameterOfType("hoo", "int", (const void*) NULLPTR)
.withOutputParameter("gah", (void*) NULLPTR)
.withOutputParameterOfType("goo", "int", (void*) NULLPTR);
mock().checkExpectations();
}
TEST(MockParameterTest, expectMultipleCallsWithParameters)
{
int expected_int = -7;
unsigned int expected_uint = 7;
mock().expectNCalls(2, "boo").withParameter("double", 1.0).withParameter("int", expected_int).
withParameter("string", "string").withParameter("uint", expected_uint);
mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", expected_int).withParameter("string", "string").
withParameter("uint", expected_uint);
mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", expected_int).withParameter("string", "string").
withParameter("uint", expected_uint);
mock().checkExpectations();
}
TEST(MockParameterTest, expectMultipleMultipleCallsWithParameters)
{
mock().expectNCalls(2, "boo").withParameter("double", 1.0).ignoreOtherParameters();
mock().expectNCalls(2, "boo").withParameter("double", 1.0).ignoreOtherParameters();
mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
mock().checkExpectations();
}

View file

@ -0,0 +1,176 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTestExt/MockSupport.h"
#include "CppUTestExt/MockSupportPlugin.h"
#include "MockFailureReporterForTest.h"
#include "CppUTest/TestTestingFixture.h"
TEST_GROUP(MockPlugin)
{
StringBufferTestOutput output;
UtestShell *test;
TestResult *result;
MockSupportPlugin plugin;
void setup() _override
{
test = new UtestShell("group", "name", "file", 1);
result = new TestResult(output);
}
void teardown() _override
{
delete test;
delete result;
mock().clear();
mock().removeAllComparatorsAndCopiers();
}
};
TEST(MockPlugin, checkExpectationsAndClearAtEnd)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("foobar");
MockExpectedCallsDidntHappenFailure expectedFailure(test, expectations);
mock().expectOneCall("foobar");
plugin.postTestAction(*test, *result);
STRCMP_CONTAINS(expectedFailure.getMessage().asCharString(), output.getOutput().asCharString());
LONGS_EQUAL(0, mock().expectedCallsLeft());
CHECK_NO_MOCK_FAILURE();
}
TEST(MockPlugin, checkExpectationsWorksAlsoWithHierachicalObjects)
{
MockFailureReporterInstaller failureReporterInstaller;
MockExpectedCallsListForTest expectations;
expectations.addFunction("differentScope::foobar")->onObject((void*) 1);
MockExpectedObjectDidntHappenFailure expectedFailure(test, "differentScope::foobar", expectations);
mock("differentScope").expectOneCall("foobar").onObject((void*) 1);
mock("differentScope").actualCall("foobar");
plugin.postTestAction(*test, *result);
STRCMP_CONTAINS(expectedFailure.getMessage().asCharString(), output.getOutput().asCharString());
CHECK_NO_MOCK_FAILURE();
}
class DummyComparator : public MockNamedValueComparator
{
public:
bool isEqual(const void* object1, const void* object2) _override
{
return object1 == object2;
}
SimpleString valueToString(const void*) _override
{
return "string";
}
};
TEST(MockPlugin, installComparatorRecordsTheComparatorButNotInstallsItYet)
{
MockFailureReporterInstaller failureReporterInstaller;
DummyComparator comparator;
plugin.installComparator("myType", comparator);
mock().expectOneCall("foo").withParameterOfType("myType", "name", NULLPTR);
mock().actualCall("foo").withParameterOfType("myType", "name", NULLPTR);
MockNoWayToCompareCustomTypeFailure failure(test, "myType");
CHECK_EXPECTED_MOCK_FAILURE(failure);
plugin.clear();
}
class DummyCopier : public MockNamedValueCopier
{
public:
void copy(void* dst, const void* src) _override
{
*(int*)dst = *(const int*)src;
}
};
TEST(MockPlugin, installCopierRecordsTheCopierButNotInstallsItYet)
{
MockFailureReporterInstaller failureReporterInstaller;
DummyCopier copier;
plugin.installCopier("myType", copier);
mock().expectOneCall("foo").withOutputParameterOfTypeReturning("myType", "name", NULLPTR);
mock().actualCall("foo").withOutputParameterOfType("myType", "name", NULLPTR);
MockNoWayToCopyCustomTypeFailure failure(test, "myType");
CHECK_EXPECTED_MOCK_FAILURE(failure);
plugin.clear();
}
TEST(MockPlugin, preTestActionWillEnableMultipleComparatorsToTheGlobalMockSupportSpace)
{
DummyComparator comparator;
DummyComparator comparator2;
plugin.installComparator("myType", comparator);
plugin.installComparator("myOtherType", comparator2);
plugin.preTestAction(*test, *result);
mock().expectOneCall("foo").withParameterOfType("myType", "name", &comparator);
mock().expectOneCall("foo").withParameterOfType("myOtherType", "name", &comparator);
mock().actualCall("foo").withParameterOfType("myType", "name", &comparator);
mock().actualCall("foo").withParameterOfType("myOtherType", "name", &comparator);
mock().checkExpectations();
LONGS_EQUAL(0, result->getFailureCount());
plugin.clear();
}
static void _failTwiceFunction()
{
mock().expectOneCall("foobar");
FAIL("This failed");
}
TEST(MockPlugin, shouldNotFailAgainWhenTestAlreadyFailed)
{
TestTestingFixture fixture;
fixture.installPlugin(&plugin);
fixture.setTestFunction(_failTwiceFunction);
fixture.runAllTests();
fixture.assertPrintContains("1 failures, 1 tests, 1 ran, 2 checks,");
}

View file

@ -0,0 +1,747 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockReturnValueTest)
{
void teardown() _override
{
mock().checkExpectations();
mock().clear();
}
};
TEST(MockReturnValueTest, hasReturnValue)
{
CHECK(!mock().hasReturnValue());
mock().expectOneCall("foo");
CHECK(!mock().actualCall("foo").hasReturnValue());
CHECK(!mock().hasReturnValue());
mock().expectOneCall("foo2").andReturnValue(1);
CHECK(mock().actualCall("foo2").hasReturnValue());
CHECK(mock().hasReturnValue());
}
TEST(MockReturnValueTest, UnsignedIntegerReturnValue)
{
unsigned int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
MockActualCall& actual_call = mock().actualCall("foo");
LONGS_EQUAL(expected_value, actual_call.returnValue().getUnsignedIntValue());
LONGS_EQUAL(expected_value, actual_call.returnUnsignedIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getUnsignedIntValue());
LONGS_EQUAL(expected_value, mock().unsignedIntReturnValue());
}
TEST(MockReturnValueTest, PositiveIntReturnValueCanBeRetrievedAsUnsignedInt)
{
int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedIntValue());
}
TEST(MockReturnValueTest, IntReturnValueCanBeRetrievedAsLongInt)
{
int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongIntValue());
}
TEST(MockReturnValueTest, UnsignedIntReturnValueCanBeRetrievedAsLongInt)
{
unsigned int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongIntValue());
}
TEST(MockReturnValueTest, PositiveIntReturnValueCanBeRetrievedAsUnsignedLongInt)
{
int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongIntValue());
}
TEST(MockReturnValueTest, PositiveLongIntReturnValueCanBeRetrievedAsUnsignedLongInt)
{
long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongIntValue());
}
TEST(MockReturnValueTest, UnsignedIntReturnValueCanBeRetrievedAsUnsignedLongInt)
{
unsigned int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongIntValue());
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockReturnValueTest, PositiveIntReturnValueCanBeRetrievedAsUnsignedLongLongInt)
{
int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
}
TEST(MockReturnValueTest, PositiveLongIntReturnValueCanBeRetrievedAsUnsignedLongLongInt)
{
long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
}
TEST(MockReturnValueTest, PositiveLongLongIntReturnValueCanBeRetrievedAsUnsignedLongLongInt)
{
long long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
}
TEST(MockReturnValueTest, UnsignedIntReturnValueCanBeRetrievedAsUnsignedLongLongInt)
{
unsigned int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
}
TEST(MockReturnValueTest, UnsignedLongIntReturnValueCanBeRetrievedAsUnsignedLongLongInt)
{
unsigned long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
}
TEST(MockReturnValueTest, UnsignedLongLongIntReturnValueCanBeRetrieved)
{
unsigned long long int expected_value = ULLONG_MAX;
mock().expectOneCall("foo").andReturnValue(expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
}
TEST(MockReturnValueTest, PositiveIntReturnValueCanBeRetrievedAsLongLongInt)
{
int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongLongIntValue());
}
TEST(MockReturnValueTest, PositiveLongIntReturnValueCanBeRetrievedAsLongLongInt)
{
long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongLongIntValue());
}
TEST(MockReturnValueTest, UnsignedIntReturnValueCanBeRetrievedAsLongLongInt)
{
unsigned int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongLongIntValue());
}
TEST(MockReturnValueTest, UnsignedLongIntReturnValueCanBeRetrievedAsLongLongInt)
{
unsigned long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongLongIntValue());
}
TEST(MockReturnValueTest, LongLongIntReturnValueCanBeRetrieved)
{
long long int expected_value = LLONG_MAX;
mock().expectOneCall("foo").andReturnValue(expected_value);
LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongLongIntValue());
}
#endif
TEST(MockReturnValueTest, UnsignedIntegerReturnValueSetsDifferentValues)
{
unsigned int expected_value = 1;
unsigned int another_expected_value = 2;
mock().expectOneCall("foo").andReturnValue(expected_value);
mock().expectOneCall("foo").andReturnValue(another_expected_value);
LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getUnsignedIntValue());
LONGS_EQUAL(another_expected_value, mock().actualCall("foo").returnValue().getUnsignedIntValue());
LONGS_EQUAL(another_expected_value, mock().returnValue().getUnsignedIntValue());
}
TEST(MockReturnValueTest, UnsignedIntegerReturnValueSetsDifferentValuesWhileParametersAreIgnored)
{
unsigned int ret_value = 1;
unsigned int another_ret_value = 2;
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(ret_value);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(another_ret_value);
LONGS_EQUAL(ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getUnsignedIntValue());
LONGS_EQUAL(ret_value, mock().returnValue().getUnsignedIntValue());
LONGS_EQUAL(another_ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getUnsignedIntValue());
LONGS_EQUAL(another_ret_value, mock().returnValue().getUnsignedIntValue());
}
TEST(MockReturnValueTest, WhenADoubleReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
double default_return_value = 10.7;
double expected_return_value = default_return_value + 1.3;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
DOUBLES_EQUAL(expected_return_value, mock().actualCall("foo").returnDoubleValueOrDefault(default_return_value), 0.05);
DOUBLES_EQUAL(expected_return_value, mock().returnDoubleValueOrDefault(default_return_value), 0.05);
}
TEST(MockReturnValueTest, WhenNoDoubleReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
double default_return_value = 7.7;
mock().expectOneCall("foo");
DOUBLES_EQUAL(default_return_value, mock().actualCall("foo").returnDoubleValueOrDefault(default_return_value), 0.05);
DOUBLES_EQUAL(default_return_value, mock().returnDoubleValueOrDefault(default_return_value), 0.05);
}
TEST(MockReturnValueTest, WhenAUnsignedIntegerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
unsigned int default_return_value = 10;
unsigned int expected_return_value = default_return_value + 1;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
LONGS_EQUAL(expected_return_value, mock().actualCall("foo").returnUnsignedIntValueOrDefault(default_return_value));
LONGS_EQUAL(expected_return_value, mock().returnUnsignedIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoUnsignedIntegerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
unsigned int default_return_value = 10;
mock().expectOneCall("foo");
LONGS_EQUAL(default_return_value, mock().actualCall("foo").returnUnsignedIntValueOrDefault(default_return_value));
LONGS_EQUAL(default_return_value, mock().returnUnsignedIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenAUnsignedLongIntegerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
unsigned long int default_return_value = 121487;
unsigned long int expected_return_value = default_return_value + 1;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
LONGS_EQUAL(expected_return_value, mock().actualCall("foo").returnUnsignedLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(expected_return_value, mock().returnUnsignedLongIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoUnsignedLongIntegerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
unsigned long int default_return_value = 7710144;
mock().expectOneCall("foo");
LONGS_EQUAL(default_return_value, mock().actualCall("foo").returnUnsignedLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(default_return_value, mock().returnUnsignedLongIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenALongIntegerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
long int default_return_value = 748797;
long int expected_return_value = default_return_value + 1;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
LONGS_EQUAL(expected_return_value, mock().actualCall("foo").returnLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(expected_return_value, mock().returnLongIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoLongIntegerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
long int default_return_value = 123659;
mock().expectOneCall("foo");
LONGS_EQUAL(default_return_value, mock().actualCall("foo").returnLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(default_return_value, mock().returnLongIntValueOrDefault(default_return_value));
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockReturnValueTest, WhenAUnsignedLongLongIntegerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
unsigned long long int default_return_value = ULLONG_MAX;
unsigned long long int expected_return_value = default_return_value - 1;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
LONGS_EQUAL(expected_return_value, mock().actualCall("foo").returnUnsignedLongLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(expected_return_value, mock().returnUnsignedLongLongIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoUnsignedLongLongIntegerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
unsigned long long int default_return_value = ULLONG_MAX;
mock().expectOneCall("foo");
LONGS_EQUAL(default_return_value, mock().actualCall("foo").returnUnsignedLongLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(default_return_value, mock().returnUnsignedLongLongIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenALongLongIntegerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
long long int default_return_value = LLONG_MAX;
long long int expected_return_value = default_return_value - 1;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
LONGS_EQUAL(expected_return_value, mock().actualCall("foo").returnLongLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(expected_return_value, mock().returnLongLongIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoLongLongIntegerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
long long int default_return_value = LLONG_MAX;
mock().expectOneCall("foo");
LONGS_EQUAL(default_return_value, mock().actualCall("foo").returnLongLongIntValueOrDefault(default_return_value));
LONGS_EQUAL(default_return_value, mock().returnLongLongIntValueOrDefault(default_return_value));
}
#endif
TEST(MockReturnValueTest, WhenABooleanReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
bool default_return_value = true;
bool expected_return_value = false;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
CHECK_EQUAL(expected_return_value, mock().actualCall("foo").returnBoolValueOrDefault(default_return_value));
CHECK_EQUAL(expected_return_value, mock().returnBoolValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoBooleanReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
bool default_return_value = true;
mock().expectOneCall("foo");
CHECK_EQUAL(default_return_value, mock().actualCall("foo").returnBoolValueOrDefault(default_return_value));
CHECK_EQUAL(default_return_value, mock().returnBoolValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenAIntegerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
int default_return_value = 777;
int expected_return_value = default_return_value + 1;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
LONGS_EQUAL(expected_return_value, mock().actualCall("foo").returnIntValueOrDefault(default_return_value));
LONGS_EQUAL(expected_return_value, mock().returnIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoIntegerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
int default_return_value = 777;
mock().expectOneCall("foo");
LONGS_EQUAL(default_return_value, mock().actualCall("foo").returnIntValueOrDefault(default_return_value));
LONGS_EQUAL(default_return_value, mock().returnIntValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, BooleanReturnValue)
{
bool expected_value = true;
mock().expectOneCall("foo").andReturnValue(true);
MockActualCall& actual_call = mock().actualCall("foo");
CHECK_EQUAL(expected_value, actual_call.returnValue().getBoolValue());
CHECK_EQUAL(expected_value, actual_call.returnBoolValue());
CHECK_EQUAL(expected_value, mock().returnValue().getBoolValue());
CHECK_EQUAL(expected_value, mock().boolReturnValue());
}
TEST(MockReturnValueTest, BooleanReturnValueSetsDifferentValues)
{
bool expected_value = true;
bool another_expected_value = false;
mock().expectOneCall("foo").andReturnValue(expected_value);
mock().expectOneCall("foo").andReturnValue(another_expected_value);
CHECK_EQUAL(expected_value, mock().actualCall("foo").returnValue().getBoolValue());
CHECK_EQUAL(expected_value, mock().returnValue().getBoolValue());
CHECK_EQUAL(another_expected_value, mock().actualCall("foo").returnValue().getBoolValue());
CHECK_EQUAL(another_expected_value, mock().returnValue().getBoolValue());
}
TEST(MockReturnValueTest, BooleanReturnValueSetsDifferentValuesWhileParametersAreIgnored)
{
bool ret_value = true;
bool another_ret_value = false;
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(ret_value);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(another_ret_value);
CHECK_EQUAL(ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getBoolValue());
CHECK_EQUAL(ret_value, mock().returnValue().getBoolValue());
CHECK_EQUAL(another_ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getBoolValue());
CHECK_EQUAL(another_ret_value, mock().returnValue().getBoolValue());
}
TEST(MockReturnValueTest, IntegerReturnValue)
{
int expected_value = 1;
mock().expectOneCall("foo").andReturnValue(1);
MockActualCall& actual_call = mock().actualCall("foo");
LONGS_EQUAL(expected_value, actual_call.returnValue().getIntValue());
LONGS_EQUAL(expected_value, actual_call.returnIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getIntValue());
LONGS_EQUAL(expected_value, mock().intReturnValue());
}
TEST(MockReturnValueTest, IntegerReturnValueSetsDifferentValues)
{
int expected_value = 1;
int another_expected_value = -1;
mock().expectOneCall("foo").andReturnValue(expected_value);
mock().expectOneCall("foo").andReturnValue(another_expected_value);
LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getIntValue());
LONGS_EQUAL(another_expected_value, mock().actualCall("foo").returnValue().getIntValue());
LONGS_EQUAL(another_expected_value, mock().returnValue().getIntValue());
}
TEST(MockReturnValueTest, IntegerReturnValueSetsDifferentValuesWhileParametersAreIgnored)
{
int ret_value = 1;
int another_ret_value = -1;
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(ret_value);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(another_ret_value);
LONGS_EQUAL(ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getIntValue());
LONGS_EQUAL(ret_value, mock().returnValue().getIntValue());
LONGS_EQUAL(another_ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getIntValue());
LONGS_EQUAL(another_ret_value, mock().returnValue().getIntValue());
}
TEST(MockReturnValueTest, LongIntegerReturnValue)
{
long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
MockActualCall& actual_call = mock().actualCall("foo");
LONGS_EQUAL(expected_value, actual_call.returnValue().getLongIntValue());
LONGS_EQUAL(expected_value, actual_call.returnLongIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getLongIntValue());
LONGS_EQUAL(expected_value, mock().longIntReturnValue());
}
TEST(MockReturnValueTest, LongIntegerReturnValueSetsDifferentValues)
{
long int expected_value = 1;
long int another_expected_value = 2;
mock().expectOneCall("foo").andReturnValue(expected_value);
mock().expectOneCall("foo").andReturnValue(another_expected_value);
LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getLongIntValue());
LONGS_EQUAL(another_expected_value, mock().actualCall("foo").returnValue().getLongIntValue());
LONGS_EQUAL(another_expected_value, mock().returnValue().getLongIntValue());
}
TEST(MockReturnValueTest, LongIntegerReturnValueSetsDifferentValuesWhileParametersAreIgnored)
{
long int ret_value = 1;
long int another_ret_value = 2;
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(ret_value);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(another_ret_value);
LONGS_EQUAL(ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getLongIntValue());
LONGS_EQUAL(ret_value, mock().returnValue().getLongIntValue());
LONGS_EQUAL(another_ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getLongIntValue());
LONGS_EQUAL(another_ret_value, mock().returnValue().getLongIntValue());
}
TEST(MockReturnValueTest, UnsignedLongIntegerReturnValue)
{
unsigned long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
MockActualCall& actual_call = mock().actualCall("foo");
LONGS_EQUAL(expected_value, actual_call.returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(expected_value, actual_call.returnUnsignedLongIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(expected_value, mock().unsignedLongIntReturnValue());
}
TEST(MockReturnValueTest, UnsignedLongIntegerReturnValueSetsDifferentValues)
{
unsigned long int expected_value = 1;
unsigned long int another_expected_value = 2;
mock().expectOneCall("foo").andReturnValue(expected_value);
mock().expectOneCall("foo").andReturnValue(another_expected_value);
LONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(expected_value, mock().returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(another_expected_value, mock().actualCall("foo").returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(another_expected_value, mock().returnValue().getUnsignedLongIntValue());
}
TEST(MockReturnValueTest, UnsignedLongIntegerReturnValueSetsDifferentValuesWhileParametersAreIgnored)
{
unsigned long int ret_value = 1;
unsigned long int another_ret_value = 2;
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(ret_value);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(another_ret_value);
LONGS_EQUAL(ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(ret_value, mock().returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(another_ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getUnsignedLongIntValue());
LONGS_EQUAL(another_ret_value, mock().returnValue().getUnsignedLongIntValue());
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockReturnValueTest, LongLongIntegerReturnValue)
{
long long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
MockActualCall& actual_call = mock().actualCall("foo");
LONGLONGS_EQUAL(expected_value, actual_call.returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(expected_value, actual_call.returnLongLongIntValue());
LONGLONGS_EQUAL(expected_value, mock().returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(expected_value, mock().longLongIntReturnValue());
}
TEST(MockReturnValueTest, LongLongIntegerReturnValueSetsDifferentValues)
{
long long int expected_value = 1;
long long int another_expected_value = 2;
mock().expectOneCall("foo").andReturnValue(expected_value);
mock().expectOneCall("foo").andReturnValue(another_expected_value);
LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(expected_value, mock().returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(another_expected_value, mock().actualCall("foo").returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(another_expected_value, mock().returnValue().getLongLongIntValue());
}
TEST(MockReturnValueTest, LongLongIntegerReturnValueSetsDifferentValuesWhileParametersAreIgnored)
{
long long int ret_value = 1;
long long int another_ret_value = 2;
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(ret_value);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(another_ret_value);
LONGLONGS_EQUAL(ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(ret_value, mock().returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(another_ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getLongLongIntValue());
LONGLONGS_EQUAL(another_ret_value, mock().returnValue().getLongLongIntValue());
}
TEST(MockReturnValueTest, UnsignedLongLongIntegerReturnValue)
{
unsigned long long int expected_value = 7;
mock().expectOneCall("foo").andReturnValue(expected_value);
MockActualCall& actual_call = mock().actualCall("foo");
UNSIGNED_LONGLONGS_EQUAL(expected_value, actual_call.returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(expected_value, actual_call.returnUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().unsignedLongLongIntReturnValue());
}
TEST(MockReturnValueTest, UnsignedLongLongIntegerReturnValueSetsDifferentValues)
{
unsigned long long int expected_value = 1;
unsigned long long int another_expected_value = 2;
mock().expectOneCall("foo").andReturnValue(expected_value);
mock().expectOneCall("foo").andReturnValue(another_expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock().returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(another_expected_value, mock().actualCall("foo").returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(another_expected_value, mock().returnValue().getUnsignedLongLongIntValue());
}
TEST(MockReturnValueTest, UnsignedLongLongIntegerReturnValueSetsDifferentValuesWhileParametersAreIgnored)
{
unsigned long long int ret_value = 1;
unsigned long long int another_ret_value = 2;
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(ret_value);
mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters().andReturnValue(another_ret_value);
UNSIGNED_LONGLONGS_EQUAL(ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(ret_value, mock().returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(another_ret_value, mock().actualCall("foo").withParameter("p1", 1).returnValue().getUnsignedLongLongIntValue());
UNSIGNED_LONGLONGS_EQUAL(another_ret_value, mock().returnValue().getUnsignedLongLongIntValue());
}
#endif
TEST(MockReturnValueTest, MatchingReturnValueOnWhileSignature)
{
mock().expectOneCall("foo").withParameter("p1", 1).andReturnValue(1);
mock().expectOneCall("foo").withParameter("p1", 2).andReturnValue(2);
mock().expectOneCall("foo").withParameter("p1", 3).andReturnValue(3);
mock().expectOneCall("foo").ignoreOtherParameters().andReturnValue(4);
LONGS_EQUAL(3, mock().actualCall("foo").withParameter("p1", 3).returnValue().getIntValue());
LONGS_EQUAL(4, mock().actualCall("foo").withParameter("p1", 4).returnValue().getIntValue());
LONGS_EQUAL(1, mock().actualCall("foo").withParameter("p1", 1).returnValue().getIntValue());
LONGS_EQUAL(2, mock().actualCall("foo").withParameter("p1", 2).returnValue().getIntValue());
}
TEST(MockReturnValueTest, WhenAStringReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
const char * default_return_value = "default";
const char * expected_return_value = "expected";
mock().expectOneCall("foo").andReturnValue(expected_return_value);
STRCMP_EQUAL(expected_return_value, mock().actualCall("foo").returnStringValueOrDefault(default_return_value));
STRCMP_EQUAL(expected_return_value, mock().returnStringValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoStringReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
const char * default_return_value = "default";
mock().expectOneCall("foo");
STRCMP_EQUAL(default_return_value, mock().actualCall("foo").returnStringValueOrDefault(default_return_value));
STRCMP_EQUAL(default_return_value, mock().returnStringValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, StringReturnValue)
{
mock().expectOneCall("foo").andReturnValue("hello world");
MockActualCall& actual_call = mock().actualCall("foo");
STRCMP_EQUAL("hello world", actual_call.returnValue().getStringValue());
STRCMP_EQUAL("hello world", actual_call.returnStringValue());
STRCMP_EQUAL("hello world", mock().stringReturnValue());
}
TEST(MockReturnValueTest, DoubleReturnValue)
{
double expected_return_value = 7.8;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
MockActualCall& actual_call = mock().actualCall("foo");
DOUBLES_EQUAL(expected_return_value, actual_call.returnValue().getDoubleValue(), 0.05);
DOUBLES_EQUAL(expected_return_value, actual_call.returnDoubleValue(), 0.05);
DOUBLES_EQUAL(expected_return_value, mock().doubleReturnValue(), 0.05);
}
TEST(MockReturnValueTest, WhenAConstPointerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
const void * default_return_value = (void*) 0x7778;
const void * expected_return_value = (void*) 0x144010;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
POINTERS_EQUAL(expected_return_value, mock().actualCall("foo").returnConstPointerValueOrDefault(default_return_value));
POINTERS_EQUAL(expected_return_value, mock().returnConstPointerValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoConstPointerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
const void * default_return_value = (void*) 0x11;
mock().expectOneCall("foo");
POINTERS_EQUAL(default_return_value, mock().actualCall("foo").returnConstPointerValueOrDefault(default_return_value));
POINTERS_EQUAL(default_return_value, mock().returnConstPointerValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenAPointerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
void * default_return_value = (void*) 0x777;
void * expected_return_value = (void*) 0x144000;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
POINTERS_EQUAL(expected_return_value, mock().actualCall("foo").returnPointerValueOrDefault(default_return_value));
POINTERS_EQUAL(expected_return_value, mock().returnPointerValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoPointerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
void * default_return_value = (void*) 0x10;
mock().expectOneCall("foo");
POINTERS_EQUAL(default_return_value, mock().actualCall("foo").returnPointerValueOrDefault(default_return_value));
POINTERS_EQUAL(default_return_value, mock().returnPointerValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenAFunctionPointerReturnValueIsExpectedAndAlsoThereIsADefaultShouldlIgnoreTheDefault)
{
void (*default_return_value)() = (void(*)()) 0x777;
void (*expected_return_value)() = (void(*)()) 0x144000;
mock().expectOneCall("foo").andReturnValue(expected_return_value);
FUNCTIONPOINTERS_EQUAL(expected_return_value, mock().actualCall("foo").returnFunctionPointerValueOrDefault(default_return_value));
FUNCTIONPOINTERS_EQUAL(expected_return_value, mock().returnFunctionPointerValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, WhenNoFunctionPointerReturnValueIsExpectedButThereIsADefaultShouldlUseTheDefaultValue)
{
void (*default_return_value)() = (void(*)()) 0x10;
mock().expectOneCall("foo");
FUNCTIONPOINTERS_EQUAL(default_return_value, mock().actualCall("foo").returnFunctionPointerValueOrDefault(default_return_value));
FUNCTIONPOINTERS_EQUAL(default_return_value, mock().returnFunctionPointerValueOrDefault(default_return_value));
}
TEST(MockReturnValueTest, PointerReturnValue)
{
void* ptr = (void*) 0x00107;
mock().expectOneCall("foo").andReturnValue(ptr);
MockActualCall& actual_call = mock().actualCall("foo");
POINTERS_EQUAL(ptr, actual_call.returnValue().getPointerValue());
POINTERS_EQUAL(ptr, actual_call.returnPointerValue());
POINTERS_EQUAL(ptr, mock().pointerReturnValue());
}
TEST(MockReturnValueTest, ConstPointerReturnValue)
{
const void* ptr = (const void*) 0x001074;
mock().expectOneCall("foo").andReturnValue(ptr);
MockActualCall& actual_call = mock().actualCall("foo");
POINTERS_EQUAL(ptr, actual_call.returnValue().getConstPointerValue());
POINTERS_EQUAL(ptr, actual_call.returnConstPointerValue());
POINTERS_EQUAL(ptr, mock().constPointerReturnValue());
}
TEST(MockReturnValueTest, FunctionPointerReturnValue)
{
void (*ptr)() = (void(*)()) 0x00107;
mock().expectOneCall("foo").andReturnValue(ptr);
MockActualCall& actual_call = mock().actualCall("foo");
FUNCTIONPOINTERS_EQUAL(ptr, actual_call.returnValue().getFunctionPointerValue());
FUNCTIONPOINTERS_EQUAL(ptr, actual_call.returnFunctionPointerValue());
FUNCTIONPOINTERS_EQUAL(ptr, mock().functionPointerReturnValue());
}
TEST(MockReturnValueTest, whenCallingDisabledOrIgnoredActualCallsThenTheyDontReturnPreviousCallsValues)
{
mock().expectOneCall("boo").ignoreOtherParameters().andReturnValue(10);
mock().ignoreOtherCalls();
mock().actualCall("boo");
mock().actualCall("An Ignored Call");
CHECK(!mock().hasReturnValue());
}

View file

@ -0,0 +1,199 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockStrictOrderTest)
{
void teardown() _override
{
mock().clear();
}
};
TEST(MockStrictOrderTest, OrderObserved)
{
mock().strictOrder();
mock().expectOneCall("foo1");
mock().expectOneCall("foo2");
mock().actualCall("foo1");
mock().actualCall("foo2");
mock().checkExpectations();
}
TEST(MockStrictOrderTest, someOrderObserved)
{
mock().expectOneCall("foo3").withCallOrder(3);
mock().expectOneCall("foo1");
mock().expectOneCall("foo2");
mock().actualCall("foo2");
mock().actualCall("foo1");
mock().actualCall("foo3");
mock().checkExpectations();
}
TEST(MockStrictOrderTest, orderViolated)
{
MockFailureReporterInstaller failureReporterInstaller;
mock().strictOrder();
MockExpectedCallsListForTest expectations;
expectations.addFunctionOrdered("foo1", 1)->callWasMade(1);
expectations.addFunctionOrdered("foo1", 2)->callWasMade(3);
expectations.addFunctionOrdered("foo2", 3)->callWasMade(2);
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);
mock().expectOneCall("foo1");
mock().expectOneCall("foo1");
mock().expectOneCall("foo2");
mock().actualCall("foo1");
mock().actualCall("foo2");
mock().actualCall("foo1");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockStrictOrderTest, orderViolatedWorksHierarchically)
{
MockFailureReporterInstaller failureReporterInstaller;
mock().strictOrder();
mock("bla").strictOrder();
MockExpectedCallsListForTest expectations;
expectations.addFunctionOrdered("foo::foo1", 1)->callWasMade(2);
expectations.addFunctionOrdered("foo::foo2", 2)->callWasMade(1);
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);
mock("bla").expectOneCall("foo1");
mock("foo").expectOneCall("foo1");
mock("foo").expectOneCall("foo2");
mock("bla").actualCall("foo1");
mock("foo").actualCall("foo2");
mock("foo").actualCall("foo1");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockStrictOrderTest, orderViolatedWorksWithExtraUnexpectedCall)
{
MockFailureReporterInstaller failureReporterInstaller;
mock().strictOrder();
mock("bla").strictOrder();
mock().ignoreOtherCalls();
MockExpectedCallsListForTest expectations;
expectations.addFunctionOrdered("foo::foo1", 1)->callWasMade(2);
expectations.addFunctionOrdered("foo::foo2", 2)->callWasMade(1);
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);
mock("bla").expectOneCall("foo1");
mock("foo").expectOneCall("foo1");
mock("foo").expectOneCall("foo2");
mock("bla").actualCall("foo1");
mock("foo").actualCall("foo2");
mock("foo").actualCall("unexpected1");
mock("foo").actualCall("foo1");
mock("foo").actualCall("unexpected2");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockStrictOrderTest, orderViolatedWithinAScope)
{
MockFailureReporterInstaller failureReporterInstaller;
mock().strictOrder();
MockExpectedCallsListForTest expectations;
expectations.addFunctionOrdered("scope::foo1", 1)->callWasMade(2);
expectations.addFunctionOrdered("scope::foo2", 2)->callWasMade(1);
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);
mock("scope").expectOneCall("foo1");
mock("scope").expectOneCall("foo2");
mock("scope").actualCall("foo2");
mock("scope").actualCall("foo1");
mock().checkExpectations();
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}
TEST(MockStrictOrderTest, orderNotViolatedAcrossScopes)
{
mock("mock1").strictOrder();
mock("mock2").strictOrder();
mock("mock1").expectOneCall("foo1");
mock("mock2").expectOneCall("foo2");
mock("mock1").actualCall("foo1");
mock("mock2").actualCall("foo2");
mock("mock1").checkExpectations();
mock("mock2").checkExpectations();
}
TEST(MockStrictOrderTest, orderViolatedAcrossScopes)
{
mock("mock1").strictOrder();
mock("mock2").strictOrder();
mock("mock1").expectOneCall("foo1");
mock("mock2").expectOneCall("foo2");
mock("mock2").actualCall("foo2");
mock("mock1").actualCall("foo1");
mock("mock1").checkExpectations();
mock("mock2").checkExpectations();
}
TEST(MockStrictOrderTest, orderUsingNCalls)
{
mock().strictOrder();
mock().expectOneCall("foo1");
mock().expectNCalls(2, "foo2");
mock().expectOneCall("foo1");
mock().expectNCalls(3, "foo2");
mock().actualCall("foo1");
mock().actualCall("foo2");
mock().actualCall("foo2");
mock().actualCall("foo1");
mock().actualCall("foo2");
mock().actualCall("foo2");
mock().actualCall("foo2");
mock().checkExpectations();
}

View file

@ -0,0 +1,286 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestTestingFixture.h"
#include "CppUTestExt/MockSupport.h"
#include "CppUTestExt/MockExpectedCall.h"
#include "CppUTestExt/MockFailure.h"
#include "MockFailureReporterForTest.h"
TEST_GROUP(MockSupportTest)
{
MockExpectedCallsListForTest expectations;
MockFailureReporterInstaller failureReporterInstaller;
void teardown() _override
{
mock().checkExpectations();
CHECK_NO_MOCK_FAILURE();
MockFailureReporterForTest::clearReporter();
mock().clear();
}
};
TEST(MockSupportTest, setDataForUnsignedIntegerValues)
{
unsigned int expected_data = 7;
mock().setData("data", expected_data);
LONGS_EQUAL(expected_data, mock().getData("data").getUnsignedIntValue());
}
TEST(MockSupportTest, setDataForIntegerValues)
{
int expected_data = 10;
mock().setData("data", expected_data);
LONGS_EQUAL(expected_data, mock().getData("data").getIntValue());
}
TEST(MockSupportTest, setDataForBooleanValues)
{
bool expected_data = true;
mock().setData("data", expected_data);
CHECK_EQUAL(expected_data, mock().getData("data").getBoolValue());
}
TEST(MockSupportTest, hasDataBeenSet)
{
CHECK(!mock().hasData("data"));
mock().setData("data", 10);
CHECK(mock().hasData("data"));
}
TEST(MockSupportTest, dataCanBeChanged)
{
mock().setData("data", 10);
mock().setData("data", 15);
LONGS_EQUAL(15, mock().getData("data").getIntValue());
}
TEST(MockSupportTest, uninitializedData)
{
LONGS_EQUAL(0, mock().getData("nonexisting").getIntValue());
STRCMP_EQUAL("int", mock().getData("nonexisting").getType().asCharString());
}
TEST(MockSupportTest, setMultipleData)
{
mock().setData("data", 1);
mock().setData("data2", 10);
LONGS_EQUAL(1, mock().getData("data").getIntValue());
LONGS_EQUAL(10, mock().getData("data2").getIntValue());
}
TEST(MockSupportTest, setDataString)
{
mock().setData("data", "string");
STRCMP_EQUAL("string", mock().getData("data").getStringValue());
}
TEST(MockSupportTest, setDataDouble)
{
mock().setData("data", 1.0);
DOUBLES_EQUAL(1.0, mock().getData("data").getDoubleValue(), 0.05);
}
TEST(MockSupportTest, setDataPointer)
{
void * ptr = (void*) 0x001;
mock().setData("data", ptr);
POINTERS_EQUAL(ptr, mock().getData("data").getPointerValue());
}
TEST(MockSupportTest, setConstDataPointer)
{
const void * ptr = (const void*) 0x001;
mock().setData("data", ptr);
POINTERS_EQUAL(ptr, mock().getData("data").getConstPointerValue());
}
TEST(MockSupportTest, setDataFunctionPointer)
{
void (*ptr)() = (void(*)()) 0x001;
mock().setData("data", ptr);
FUNCTIONPOINTERS_EQUAL(ptr, mock().getData("data").getFunctionPointerValue());
}
TEST(MockSupportTest, setDataObject)
{
void * ptr = (void*) 0x001;
mock().setDataObject("data", "type", ptr);
POINTERS_EQUAL(ptr, mock().getData("data").getObjectPointer());
STRCMP_EQUAL("type", mock().getData("data").getType().asCharString());
}
TEST(MockSupportTest, setDataConstObject)
{
void * ptr = (void*) 0x011;
mock().setDataConstObject("data", "type", ptr);
POINTERS_EQUAL(ptr, mock().getData("data").getConstObjectPointer());
STRCMP_EQUAL("type", mock().getData("data").getType().asCharString());
}
TEST(MockSupportTest, tracing)
{
mock().tracing(true);
mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
mock("scope").actualCall("foo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
mock().checkExpectations();
STRCMP_CONTAINS("boo", mock().getTraceOutput());
STRCMP_CONTAINS("foo", mock().getTraceOutput());
}
TEST(MockSupportTest, tracingWorksHierarchically)
{
mock("scope").tracing(true);
mock().tracing(true);
mock().actualCall("boo");
mock("scope").actualCall("foo");
mock().checkExpectations();
STRCMP_CONTAINS("boo", mock().getTraceOutput());
STRCMP_CONTAINS("foo", mock().getTraceOutput());
}
TEST_GROUP(MockSupportTestWithFixture)
{
TestTestingFixture fixture;
void teardown() _override
{
mock().clear();
MockFailureReporterForTest::clearReporter();
}
};
static void CHECK_EXPECTED_MOCK_FAILURE_LOCATION_failedTestMethod_()
{
MockExpectedCallsList list;
MockUnexpectedCallHappenedFailure expectedFailure(UtestShell::getCurrent(), "unexpected", list);
mock().actualCall("boo");
CHECK_EXPECTED_MOCK_FAILURE_LOCATION(expectedFailure, "file", 1);
}
TEST(MockSupportTestWithFixture, CHECK_EXPECTED_MOCK_FAILURE_LOCATION_failed)
{
mock().setMockFailureStandardReporter(MockFailureReporterForTest::getReporter());
fixture.setTestFunction(CHECK_EXPECTED_MOCK_FAILURE_LOCATION_failedTestMethod_);
fixture.runAllTests();
fixture.assertPrintContains("MockFailures are different.");
fixture.assertPrintContains("Expected MockFailure:");
fixture.assertPrintContains("Mock Failure: Unexpected call to function: unexpected");
fixture.assertPrintContains("Actual MockFailure:");
fixture.assertPrintContains("Mock Failure: Unexpected call to function: boo");
}
static void CHECK_NO_MOCK_FAILURE_LOCATION_failedTestMethod_()
{
mock().actualCall("boo");
CHECK_NO_MOCK_FAILURE_LOCATION("file", 1);
}
TEST(MockSupportTestWithFixture, CHECK_NO_MOCK_FAILURE_LOCATION_failed)
{
mock().setMockFailureStandardReporter(MockFailureReporterForTest::getReporter());
fixture.setTestFunction(CHECK_NO_MOCK_FAILURE_LOCATION_failedTestMethod_);
fixture.runAllTests();
fixture.assertPrintContains("Unexpected mock failure:");
fixture.assertPrintContains("Mock Failure: Unexpected call to function: boo");
}
static bool cpputestHasCrashed;
static void crashMethod()
{
cpputestHasCrashed = true;
}
static void unexpectedCallTestFunction_(void)
{
mock().actualCall("unexpected");
} // LCOV_EXCL_LINE
#include "CppUTestExt/OrderedTest.h"
TEST(MockSupportTestWithFixture, shouldCrashOnFailure)
{
cpputestHasCrashed = false;
mock().crashOnFailure(true);
UtestShell::setCrashMethod(crashMethod);
fixture.setTestFunction(unexpectedCallTestFunction_);
fixture.runAllTests();
CHECK(cpputestHasCrashed);
mock().crashOnFailure(false);
UtestShell::resetCrashMethod();
}
TEST(MockSupportTestWithFixture, ShouldNotCrashOnFailureAfterCrashMethodWasReset)
{
cpputestHasCrashed = false;
UtestShell::setCrashMethod(crashMethod);
fixture.setTestFunction(unexpectedCallTestFunction_);
UtestShell::resetCrashMethod();
fixture.runAllTests();
fixture.assertPrintContains("Unexpected call to function: unexpected");
CHECK_FALSE(cpputestHasCrashed);
}
TEST(MockSupportTestWithFixture, shouldCrashOnFailureWithCppUTestSetting)
{
cpputestHasCrashed = false;
UtestShell::setCrashOnFail();
UtestShell::setCrashMethod(crashMethod);
fixture.setTestFunction(unexpectedCallTestFunction_);
fixture.runAllTests();
CHECK(cpputestHasCrashed);
UtestShell::restoreDefaultTestTerminator();
UtestShell::resetCrashMethod();
}
TEST(MockSupportTestWithFixture, failedMockShouldFailAgainWhenRepeated)
{
fixture.setTestFunction(unexpectedCallTestFunction_);
int repeatCount = 2;
while(repeatCount--)
{
fixture.runAllTests();
fixture.assertPrintContains("Unexpected call to function: unexpected");
fixture.assertPrintContains("Errors (1 failures, 1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out");
fixture.flushOutputAndResetResult();
}
}

View file

@ -0,0 +1,749 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestHarness_c.h"
#include "CppUTest/TestTestingFixture.h"
#include "CppUTestExt/MockSupport_c.h"
#include "MockSupport_cTestCFile.h"
#include "CppUTestExt/OrderedTest.h"
extern "C" {
static void dummy_function_for_mock_c_test()
{
}
static void dummy_function_for_mock_c_test_two()
{
}
}
TEST_GROUP(MockSupport_c)
{
void teardown() _override
{
mock_c()->clear();
}
};
TEST(MockSupport_c, OrderObserved)
{
mock_c()->strictOrder();
mock_c()->expectOneCall("foo1");
mock_c()->expectOneCall("foo2");
mock_c()->actualCall("foo1");
mock_c()->actualCall("foo2");
mock_c()->checkExpectations();
}
TEST(MockSupport_c, hasReturnValue)
{
mock_c()->expectOneCall("foo");
CHECK(mock_c()->actualCall("foo")->hasReturnValue() == 0);
CHECK(mock_c()->hasReturnValue() == 0);
mock_c()->expectOneCall("foo2")->andReturnIntValue(1);
CHECK(mock_c()->actualCall("foo2")->hasReturnValue() != 0);
CHECK(mock_c()->hasReturnValue() != 0);
}
TEST(MockSupport_c, expectAndActualOneCall)
{
mock_c()->expectOneCall("boo");
mock_c()->actualCall("boo");
mock_c()->checkExpectations();
}
TEST(MockSupport_c, expectAndActualThreeCalls)
{
mock_c()->expectNCalls(3, "boo");
mock_c()->actualCall("boo");
mock_c()->actualCall("boo");
mock_c()->actualCall("boo");
mock_c()->checkExpectations();
}
TEST(MockSupport_c, expectNoCall)
{
mock_c()->expectNoCall("foo");
mock_c()->expectOneCall("bar");
mock_c()->actualCall("bar");
mock_c()->checkExpectations();
}
TEST(MockSupport_c, expectAndActualParameters)
{
mock_c()->expectOneCall("boo")->withIntParameters("integer", 1)->withDoubleParameters("double", 1.0)->
withStringParameters("string", "string")->withPointerParameters("pointer", (void*) 1)->
withFunctionPointerParameters("functionPointer", dummy_function_for_mock_c_test);
mock_c()->actualCall("boo")->withIntParameters("integer", 1)->withDoubleParameters("double", 1.0)->
withStringParameters("string", "string")->withPointerParameters("pointer", (void*) 1)->
withFunctionPointerParameters("functionPointer", dummy_function_for_mock_c_test);
}
extern "C"{
static int typeNameIsEqual(const void* object1, const void* object2)
{
return object1 == object2;
}
static const char* typeNameValueToString(const void* PUNUSED(object))
{
return "valueToString";
}
static void typeCopy(void* dst, const void* src)
{
*(int*) dst = *(const int*) src;
}
}
TEST(MockSupport_c, expectAndActualParametersOnObject)
{
mock_c()->installComparator("typeName", typeNameIsEqual, typeNameValueToString);
mock_c()->expectOneCall("boo")->withParameterOfType("typeName", "name", (const void*) 1);
mock_c()->actualCall("boo")->withParameterOfType("typeName", "name", (const void*) 1);
mock_c()->checkExpectations();
mock_c()->removeAllComparatorsAndCopiers();
}
TEST(MockSupport_c, boolParameter)
{
mock_c()->expectOneCall("foo")->withBoolParameters("p", 1);
mock_c()->actualCall("foo")->withBoolParameters("p", 1);
}
TEST(MockSupport_c, unsignedIntParameter)
{
mock_c()->expectOneCall("foo")->withUnsignedIntParameters("p", 1);
mock_c()->actualCall("foo")->withUnsignedIntParameters("p", 1);
}
TEST(MockSupport_c, longIntParameter)
{
mock_c()->expectOneCall("foo")->withLongIntParameters("p", 1);
mock_c()->actualCall("foo")->withLongIntParameters("p", 1);
}
TEST(MockSupport_c, unsignedLongIntParameter)
{
mock_c()->expectOneCall("foo")->withUnsignedLongIntParameters("p", 1);
mock_c()->actualCall("foo")->withUnsignedLongIntParameters("p", 1);
}
TEST(MockSupport_c, doubleParameterWithTolerance)
{
mock_c( )->expectOneCall("foo")->withDoubleParametersAndTolerance("p", 2.0, 0.2);
mock_c( )->actualCall("foo")->withDoubleParameters("p", 1.9);
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockSupport_c, longLongIntParameter)
{
mock_c()->expectOneCall("foo")->withLongLongIntParameters("p", 1);
mock_c()->actualCall("foo")->withLongLongIntParameters("p", 1);
}
TEST(MockSupport_c, unsignedLongLongIntParameter)
{
mock_c()->expectOneCall("foo")->withUnsignedLongLongIntParameters("p", 1);
mock_c()->actualCall("foo")->withUnsignedLongLongIntParameters("p", 1);
}
#endif
TEST(MockSupport_c, memoryBufferParameter)
{
const unsigned char mem_buffer[] = { 1, 2, 3};
mock_c()->expectOneCall("foo")->withMemoryBufferParameter("out", mem_buffer, sizeof(mem_buffer));
mock_c()->actualCall("foo")->withMemoryBufferParameter("out", mem_buffer, sizeof(mem_buffer));
mock_c()->checkExpectations();
}
TEST(MockSupport_c, outputParameters)
{
int param = 1;
const int retval = 2;
mock_c()->expectOneCall("foo")->withOutputParameterReturning("out", &retval, sizeof(retval));
mock_c()->actualCall("foo")->withOutputParameter("out", &param);
mock_c()->checkExpectations();
LONGS_EQUAL(2, param);
LONGS_EQUAL(2, retval);
}
TEST(MockSupport_c, unmodifiedOutputParameter)
{
int param = 1;
mock_c()->expectOneCall("foo")->withUnmodifiedOutputParameter("out");
mock_c()->actualCall("foo")->withOutputParameter("out", &param);
mock_c()->checkExpectations();
LONGS_EQUAL(1, param);
}
TEST(MockSupport_c, outputParameters_differentType)
{
long param = 1;
const long retval = 2;
mock_c()->expectOneCall("foo")->withOutputParameterReturning("out", &retval, sizeof(retval));
mock_c()->actualCall("foo")->withOutputParameter("out", &param);
mock_c()->checkExpectations();
LONGS_EQUAL(2, param);
LONGS_EQUAL(2, retval);
}
TEST(MockSupport_c, outputParametersOfType)
{
int param = 1;
const int retval = 2;
mock_c()->installCopier("typeName", typeCopy);
mock_c()->expectOneCall("foo")->withOutputParameterOfTypeReturning("typeName", "out", &retval);
mock_c()->actualCall("foo")->withOutputParameterOfType("typeName", "out", &param);
LONGS_EQUAL(2, param);
LONGS_EQUAL(2, retval);
mock_c()->checkExpectations();
mock_c()->removeAllComparatorsAndCopiers();
}
TEST(MockSupport_c, ignoreOtherParameters)
{
mock_c()->expectOneCall("foo")->withIntParameters("int", 1)->ignoreOtherParameters();
mock_c()->actualCall("foo")->withIntParameters("int", 1)->withDoubleParameters("double", 0.01);
mock_c()->checkExpectations();
}
TEST(MockSupport_c, returnBoolValue)
{
int expected_value = 1;
mock_c()->expectOneCall("boo")->andReturnBoolValue(expected_value);
CHECK_EQUAL(expected_value, mock_c()->actualCall("boo")->boolReturnValue());
CHECK_EQUAL(expected_value, mock_c()->boolReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_BOOL, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnBoolValueOrDefaultShouldIgnoreTheDefault)
{
int defaultValue = 1;
int expectedValue = 0;
mock_c()->expectOneCall("foo")->andReturnBoolValue(expectedValue);
LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnBoolValueOrDefault(defaultValue));
LONGS_EQUAL(expectedValue, mock_c()->returnBoolValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnBoolValueOrDefaultShouldlUseTheDefaultValue)
{
int defaultValue = 1;
mock_c()->expectOneCall("foo");
LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnBoolValueOrDefault(defaultValue));
LONGS_EQUAL(defaultValue, mock_c()->returnBoolValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnIntValue)
{
int expected_value = -10;
mock_c()->expectOneCall("boo")->andReturnIntValue(expected_value);
LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->intReturnValue());
LONGS_EQUAL(expected_value, mock_c()->intReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_INTEGER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnIntValueOrDefaultShouldIgnoreTheDefault)
{
int defaultValue = -10;
int expectedValue = defaultValue - 1;
mock_c()->expectOneCall("foo")->andReturnIntValue(expectedValue);
LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnIntValueOrDefault(defaultValue));
LONGS_EQUAL(expectedValue, mock_c()->returnIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnIntValueOrDefaultShouldlUseTheDefaultValue)
{
int defaultValue = -10;
mock_c()->expectOneCall("foo");
LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnIntValueOrDefault(defaultValue));
LONGS_EQUAL(defaultValue, mock_c()->returnIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnUnsignedIntValue)
{
unsigned int expected_value = 7;
mock_c()->expectOneCall("boo")->andReturnUnsignedIntValue(expected_value);
LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->unsignedIntReturnValue());
LONGS_EQUAL(expected_value, mock_c()->unsignedIntReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_UNSIGNED_INTEGER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnUnsignedIntValueOrDefaultShouldIgnoreTheDefault)
{
unsigned int defaultValue = 10;
unsigned int expectedValue = defaultValue + 1;
mock_c()->expectOneCall("foo")->andReturnUnsignedIntValue(expectedValue);
LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnUnsignedIntValueOrDefault(defaultValue));
LONGS_EQUAL(expectedValue, mock_c()->returnUnsignedIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnUnsignedIntValueOrDefaultShouldlUseTheDefaultValue)
{
unsigned int defaultValue = 10;
mock_c()->expectOneCall("foo");
LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnUnsignedIntValueOrDefault(defaultValue));
LONGS_EQUAL(defaultValue, mock_c()->returnUnsignedIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnLongIntValue)
{
long int expected_value = -10L;
mock_c()->expectOneCall("boo")->andReturnLongIntValue(expected_value);
LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->longIntReturnValue());
LONGS_EQUAL(expected_value, mock_c()->longIntReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_LONG_INTEGER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnLongIntValueOrDefaultShouldIgnoreTheDefault)
{
long int defaultValue = -10L;
long int expectedValue = defaultValue - 1L;
mock_c()->expectOneCall("foo")->andReturnLongIntValue(expectedValue);
LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnLongIntValueOrDefault(defaultValue));
LONGS_EQUAL(expectedValue, mock_c()->returnLongIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnLongIntValueOrDefaultShouldlUseTheDefaultValue)
{
long int defaultValue = -10L;
mock_c()->expectOneCall("foo");
LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnLongIntValueOrDefault(defaultValue));
LONGS_EQUAL(defaultValue, mock_c()->returnLongIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnUnsignedLongIntValue)
{
unsigned long int expected_value = 10;
mock_c()->expectOneCall("boo")->andReturnUnsignedLongIntValue(expected_value);
LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->unsignedLongIntReturnValue());
LONGS_EQUAL(expected_value, mock_c()->unsignedLongIntReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_UNSIGNED_LONG_INTEGER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnUnsignedLongIntValueOrDefaultShouldIgnoreTheDefault)
{
unsigned long int defaultValue = 10L;
unsigned long int expectedValue = defaultValue + 1L;
mock_c()->expectOneCall("foo")->andReturnUnsignedLongIntValue(expectedValue);
LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnUnsignedLongIntValueOrDefault(defaultValue));
LONGS_EQUAL(expectedValue, mock_c()->returnUnsignedLongIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnUnsignedLongIntValueOrDefaultShouldlUseTheDefaultValue)
{
unsigned long int defaultValue = 10L;
mock_c()->expectOneCall("foo");
LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnUnsignedLongIntValueOrDefault(defaultValue));
LONGS_EQUAL(defaultValue, mock_c()->returnUnsignedLongIntValueOrDefault(defaultValue));
}
#ifdef CPPUTEST_USE_LONG_LONG
TEST(MockSupport_c, returnLongLongIntValue)
{
long long int expected_value = -10L;
mock_c()->expectOneCall("boo")->andReturnLongLongIntValue(expected_value);
LONGLONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->longLongIntReturnValue());
LONGLONGS_EQUAL(expected_value, mock_c()->longLongIntReturnValue());
LONGLONGS_EQUAL(MOCKVALUETYPE_LONG_LONG_INTEGER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnLongLongIntValueOrDefaultShouldIgnoreTheDefault)
{
long long int defaultValue = -10L;
long long int expectedValue = defaultValue - 1L;
mock_c()->expectOneCall("foo")->andReturnLongLongIntValue(expectedValue);
LONGLONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnLongLongIntValueOrDefault(defaultValue));
LONGLONGS_EQUAL(expectedValue, mock_c()->returnLongLongIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnLongLongIntValueOrDefaultShouldlUseTheDefaultValue)
{
long long int defaultValue = -10L;
mock_c()->expectOneCall("foo");
LONGLONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnLongLongIntValueOrDefault(defaultValue));
LONGLONGS_EQUAL(defaultValue, mock_c()->returnLongLongIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnUnsignedLongLongIntValue)
{
unsigned long long int expected_value = 10;
mock_c()->expectOneCall("boo")->andReturnUnsignedLongLongIntValue(expected_value);
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->unsignedLongLongIntReturnValue());
UNSIGNED_LONGLONGS_EQUAL(expected_value, mock_c()->unsignedLongLongIntReturnValue());
UNSIGNED_LONGLONGS_EQUAL(MOCKVALUETYPE_UNSIGNED_LONG_LONG_INTEGER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnUnsignedLongLongIntValueOrDefaultShouldIgnoreTheDefault)
{
unsigned long long int defaultValue = 10L;
unsigned long long int expectedValue = defaultValue + 1L;
mock_c()->expectOneCall("foo")->andReturnUnsignedLongLongIntValue(expectedValue);
UNSIGNED_LONGLONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnUnsignedLongLongIntValueOrDefault(defaultValue));
UNSIGNED_LONGLONGS_EQUAL(expectedValue, mock_c()->returnUnsignedLongLongIntValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnUnsignedLongLongIntValueOrDefaultShouldlUseTheDefaultValue)
{
unsigned long long int defaultValue = 10L;
mock_c()->expectOneCall("foo");
UNSIGNED_LONGLONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnUnsignedLongLongIntValueOrDefault(defaultValue));
UNSIGNED_LONGLONGS_EQUAL(defaultValue, mock_c()->returnUnsignedLongLongIntValueOrDefault(defaultValue));
}
#endif
TEST(MockSupport_c, returnStringValue)
{
mock_c()->expectOneCall("boo")->andReturnStringValue("hello world");
STRCMP_EQUAL("hello world", mock_c()->actualCall("boo")->stringReturnValue());
STRCMP_EQUAL("hello world", mock_c()->stringReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_STRING, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnStringValueOrDefaultShouldIgnoreTheDefault)
{
const char defaultValue[] = "bar";
const char expectedValue[] = "bla";
mock_c()->expectOneCall("foo")->andReturnStringValue(expectedValue);
STRCMP_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnStringValueOrDefault(defaultValue));
STRCMP_EQUAL(expectedValue, mock_c()->returnStringValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnStringValueOrDefaultShouldlUseTheDefaultValue)
{
const char defaultValue[] = "bar";
mock_c()->expectOneCall("foo");
STRCMP_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnStringValueOrDefault(defaultValue));
STRCMP_EQUAL(defaultValue, mock_c()->returnStringValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnDoubleValue)
{
mock_c()->expectOneCall("boo")->andReturnDoubleValue(1.0);
DOUBLES_EQUAL(1.0, mock_c()->actualCall("boo")->doubleReturnValue(), 0.005);
DOUBLES_EQUAL(1.0, mock_c()->doubleReturnValue(), 0.005);
LONGS_EQUAL(MOCKVALUETYPE_DOUBLE, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnDoubleValueOrDefaultShouldIgnoreTheDefault)
{
double defaultValue = 2.2;
double expectedValue = defaultValue + 0.1;
mock_c()->expectOneCall("foo")->andReturnDoubleValue(expectedValue);
DOUBLES_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnDoubleValueOrDefault(defaultValue), 0.005);
DOUBLES_EQUAL(expectedValue, mock_c()->returnDoubleValueOrDefault(defaultValue), 0.005);
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnDoubleValueOrDefaultShouldlUseTheDefaultValue)
{
double defaultValue = 2.2;
mock_c()->expectOneCall("foo");
DOUBLES_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnDoubleValueOrDefault(defaultValue), 0.005);
DOUBLES_EQUAL(defaultValue, mock_c()->returnDoubleValueOrDefault(defaultValue), 0.005);
}
TEST(MockSupport_c, returnPointerValue)
{
mock_c()->expectOneCall("boo")->andReturnPointerValue((void*) 10);
POINTERS_EQUAL((void*) 10, mock_c()->actualCall("boo")->pointerReturnValue());
POINTERS_EQUAL((void*) 10, mock_c()->pointerReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_POINTER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnPointerValueOrDefaultShouldIgnoreTheDefault)
{
void* defaultValue = (void*) 10;
void* expectedValue = (void*) 27;
mock_c()->expectOneCall("foo")->andReturnPointerValue(expectedValue);
POINTERS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnPointerValueOrDefault(defaultValue));
POINTERS_EQUAL(expectedValue, mock_c()->returnPointerValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnPointerValueOrDefaultShouldlUseTheDefaultValue)
{
void* defaultValue = (void*) 10;
mock_c()->expectOneCall("foo");
POINTERS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnPointerValueOrDefault(defaultValue));
POINTERS_EQUAL(defaultValue, mock_c()->returnPointerValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnConstPointerValue)
{
mock_c()->expectOneCall("boo")->andReturnConstPointerValue((const void*) 10);
POINTERS_EQUAL((const void*) 10, mock_c()->actualCall("boo")->constPointerReturnValue());
POINTERS_EQUAL((const void*) 10, mock_c()->constPointerReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_CONST_POINTER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnConstPointerValueOrDefaultShouldIgnoreTheDefault)
{
const void* defaultValue = (void*) 10;
const void* expectedValue = (void*) 27;
mock_c()->expectOneCall("foo")->andReturnConstPointerValue(expectedValue);
POINTERS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnConstPointerValueOrDefault(defaultValue));
POINTERS_EQUAL(expectedValue, mock_c()->returnConstPointerValueOrDefault(defaultValue));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnConstPointerValueOrDefaultShouldlUseTheDefaultValue)
{
const void* defaultValue = (void*) 10;
mock_c()->expectOneCall("foo");
POINTERS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnConstPointerValueOrDefault(defaultValue));
POINTERS_EQUAL(defaultValue, mock_c()->returnConstPointerValueOrDefault(defaultValue));
}
TEST(MockSupport_c, returnFunctionPointerValue)
{
mock_c()->expectOneCall("boo")->andReturnFunctionPointerValue(dummy_function_for_mock_c_test);
FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->actualCall("boo")->functionPointerReturnValue());
FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->functionPointerReturnValue());
LONGS_EQUAL(MOCKVALUETYPE_FUNCTIONPOINTER, mock_c()->returnValue().type);
}
TEST(MockSupport_c, whenReturnValueIsGivenReturnFunctionPointerValueOrDefaultShouldIgnoreTheDefault)
{
mock_c()->expectOneCall("foo")->andReturnFunctionPointerValue(dummy_function_for_mock_c_test);
FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->actualCall("foo")->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two));
FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two));
}
TEST(MockSupport_c, whenNoReturnValueIsGivenReturnFunctionPointerValueOrDefaultShouldlUseTheDefaultValue)
{
mock_c()->expectOneCall("foo");
FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test_two, mock_c()->actualCall("foo")->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two));
FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test_two, mock_c()->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two));
}
TEST(MockSupport_c, MockSupportWithScope)
{
mock_scope_c("scope")->expectOneCall("boo");
LONGS_EQUAL(0, mock_scope_c("other")->expectedCallsLeft());
LONGS_EQUAL(1, mock_scope_c("scope")->expectedCallsLeft());
mock_scope_c("scope")->actualCall("boo");
}
TEST(MockSupport_c, MockSupportSetBoolData)
{
mock_c()->setBoolData("boolean", 1);
CHECK_EQUAL(1, mock_c()->getData("boolean").value.boolValue);
}
TEST(MockSupport_c, MockSupportSetIntData)
{
mock_c()->setIntData("integer", 10);
LONGS_EQUAL(10, mock_c()->getData("integer").value.intValue);
}
TEST(MockSupport_c, MockSupportSetDoubleData)
{
mock_c()->setDoubleData("double", 1.0);
DOUBLES_EQUAL(1.00, mock_c()->getData("double").value.doubleValue, 0.05);
}
TEST(MockSupport_c, MockSupportSetStringData)
{
mock_c()->setStringData("string", "hello world");
STRCMP_EQUAL("hello world", mock_c()->getData("string").value.stringValue);
}
TEST(MockSupport_c, MockSupportSetPointerData)
{
mock_c()->setPointerData("pointer", (void*) 1);
POINTERS_EQUAL((void*) 1, mock_c()->getData("pointer").value.pointerValue);
}
TEST(MockSupport_c, MockSupportSetConstPointerData)
{
mock_c()->setConstPointerData("constPointer", (const void*) 1);
POINTERS_EQUAL((const void*) 1, mock_c()->getData("constPointer").value.constPointerValue);
}
TEST(MockSupport_c, MockSupportMemoryBufferData)
{
mock_c()->setDataObject("name", "const unsigned char*", (void *) 0xDEAD);
POINTERS_EQUAL(0xDEAD, mock_c()->getData("name").value.memoryBufferValue);
LONGS_EQUAL(MOCKVALUETYPE_MEMORYBUFFER, mock_c()->getData("name").type);
}
TEST(MockSupport_c, MockSupportSetFunctionPointerData)
{
mock_c()->setFunctionPointerData("functionPointer", dummy_function_for_mock_c_test);
FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->getData("functionPointer").value.functionPointerValue);
}
TEST(MockSupport_c, MockSupportSetDataObject)
{
mock_c()->setDataObject("name", "type", (void*) 1);
POINTERS_EQUAL((void*) 1, mock_c()->getData("name").value.objectValue);
}
TEST(MockSupport_c, MockSupportSetDataConstObject)
{
mock_c()->setDataConstObject("name", "type", (const void*) 5);
POINTERS_EQUAL((void*) 5, mock_c()->getData("name").value.constObjectValue);
}
TEST(MockSupport_c, WorksInCFile)
{
all_mock_support_c_calls();
}
static bool destructorWasCalled = false;
static void failedCallToMockC()
{
SetBooleanOnDestructorCall setOneDestructor(destructorWasCalled);
mock_c()->actualCall("Not a call");
} // LCOV_EXCL_LINE
// Silly wrapper because of a test that only fails in Visual C++ due to different
// destructor behaviors
#ifdef _MSC_VER
#define MSC_SWITCHED_TEST(testGroup, testName) IGNORE_TEST(testGroup, testName)
#else
#define MSC_SWITCHED_TEST(testGroup, testName) TEST(testGroup, testName)
#endif
MSC_SWITCHED_TEST(MockSupport_c, NoExceptionsAreThrownWhenAMock_cCallFailed)
{
TestTestingFixture fixture;
fixture.setTestFunction(failedCallToMockC);
fixture.runAllTests();
LONGS_EQUAL(1, fixture.getFailureCount());
// Odd behavior in Visual C++, destructor still gets called here
CHECK(!destructorWasCalled);
}
static bool cpputestHasCrashed;
static void crashMethod()
{
cpputestHasCrashed = true;
}
TEST_ORDERED(MockSupport_c, shouldCrashOnFailure, 21)
{
cpputestHasCrashed = false;
TestTestingFixture fixture;
UtestShell::setCrashMethod(crashMethod);
mock_c()->crashOnFailure(true);
fixture.setTestFunction(failedCallToMockC);
fixture.runAllTests();
CHECK(cpputestHasCrashed);
UtestShell::resetCrashMethod();
mock_c()->crashOnFailure(false);
}
TEST_ORDERED(MockSupport_c, nextTestShouldNotCrashOnFailure, 22)
{
cpputestHasCrashed = false;
TestTestingFixture fixture;
UtestShell::setCrashMethod(crashMethod);
fixture.setTestFunction(failedCallToMockC);
fixture.runAllTests();
CHECK_FALSE(cpputestHasCrashed);
UtestShell::resetCrashMethod();
}
static void failingCallToMockCWithParameterOfType_()
{
mock_c()->expectOneCall("bar")->withParameterOfType("typeName", "name", (const void*) 1);
mock_c()->actualCall("bar")->withParameterOfType("typeName", "name", (const void*) 2);
} // LCOV_EXCL_LINE
TEST(MockSupport_c, failureWithParameterOfTypeCoversValueToString)
{
TestTestingFixture fixture;
mock_c()->installComparator("typeName", typeNameIsEqual, typeNameValueToString);
fixture.setTestFunction(failingCallToMockCWithParameterOfType_);
fixture.runAllTests();
fixture.assertPrintContains("typeName name: <valueToString>");
mock_c()->removeAllComparatorsAndCopiers();
}
static void callToMockCWithOutputParameterOfType_()
{
int value1 = 7;
const int value2 = 9;
mock_c()->expectOneCall("bar")->withOutputParameterOfTypeReturning("intType", "bla", &value2);
mock_c()->actualCall("bar")->withOutputParameterOfType("intType", "bla", &value1);
LONGS_EQUAL(value1, value2);
}
TEST(MockSupport_c, successWithOutputParameterOfType)
{
TestTestingFixture fixture;
mock_c()->installCopier("intType", typeCopy);
fixture.setTestFunction(callToMockCWithOutputParameterOfType_);
fixture.runAllTests();
LONGS_EQUAL(2, fixture.getCheckCount());
LONGS_EQUAL(0, fixture.getFailureCount());
mock_c()->removeAllComparatorsAndCopiers();
}
static void failingCallToMockCWithMemoryBuffer_()
{
unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF };
unsigned char memBuffer2[] = { 0x12, 0x05, 0xFF };
mock_c()->expectOneCall("bar")->withMemoryBufferParameter("name", memBuffer1, sizeof(memBuffer1));
mock_c()->actualCall("bar")->withMemoryBufferParameter("name", memBuffer2, sizeof(memBuffer2));
} // LCOV_EXCL_LINE
TEST(MockSupport_c, expectOneMemBufferParameterAndValueFailsDueToContents)
{
TestTestingFixture fixture;
fixture.setTestFunction(failingCallToMockCWithMemoryBuffer_);
fixture.runAllTests();
fixture.assertPrintContains("Unexpected parameter value to parameter \"name\" "
"to function \"bar\": <Size = 3 | HexContents = 12 05 FF>");
}
TEST(MockSupport_c, ignoreOtherCalls)
{
mock_c()->expectOneCall("foo");
mock_c()->ignoreOtherCalls();
mock_c()->actualCall("foo");
mock_c()->actualCall("bar");
mock_c()->checkExpectations();
}

View file

@ -0,0 +1,221 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTestExt/MockSupport_c.h"
#include "MockSupport_cTestCFile.h"
static int typeNameIsEqual(const void* object1, const void* object2)
{
return object1 == object2;
}
static const char* typeNameValueToString(const void* object)
{
return (const char*) object;
}
void all_mock_support_c_calls(void)
{
mock_c()->strictOrder();
mock_c()->expectOneCall("boo");
mock_c()->expectNoCall("bla");
mock_c()->expectNCalls(1, "foo");
mock_c()->actualCall("boo");
mock_c()->actualCall("foo");
mock_c()->checkExpectations();
mock_c()->expectOneCall("boo")->withIntParameters("integer", 1)->
withBoolParameters("bool", 1)->
withUnsignedIntParameters("unsigned", 1)->
withLongIntParameters("long int", (long int) -1)->
withUnsignedLongIntParameters("unsigned long int", (unsigned long int) 1)->
#ifdef CPPUTEST_USE_LONG_LONG
withLongLongIntParameters("long long int", (long long int) -1)->
withUnsignedLongLongIntParameters("unsigned long long int", (unsigned long long int) 1)->
#endif
withDoubleParameters("double", 1.0)->
withDoubleParametersAndTolerance("doubleWithTolerance", 1.0, 1.0)->
withStringParameters("string", "string")->
withPointerParameters("pointer", (void*) 1)->
withConstPointerParameters("constpointer", (const void*) 1)->
withFunctionPointerParameters("functionpointer", (void(*)(void)) 1)->
withMemoryBufferParameter("name", (void*) 1, 0UL)->
ignoreOtherParameters();
mock_c()->actualCall("boo")->withIntParameters("integer", 1)->
withBoolParameters("bool", 1)->
withUnsignedIntParameters("unsigned", 1)->
withLongIntParameters("long int", (long int) -1)->
withUnsignedLongIntParameters("unsigned long int", (unsigned long int) 1)->
#ifdef CPPUTEST_USE_LONG_LONG
withLongLongIntParameters("long long int", (long long int) -1)->
withUnsignedLongLongIntParameters("unsigned long long int", (unsigned long long int) 1)->
#endif
withDoubleParameters("double", 1.0)->
withDoubleParameters("doubleWithTolerance", 0.0 )->
withStringParameters("string", "string")->
withPointerParameters("pointer", (void*) 1)->
withConstPointerParameters("constpointer", (const void*) 1)->
withFunctionPointerParameters("functionpointer", (void(*)(void)) 1)->
withMemoryBufferParameter("name", (void*) 1, 0UL)->
hasReturnValue();
mock_c()->disable();
mock_c()->expectOneCall("boo")->withParameterOfType("type", "name", (void*) 1)->
withOutputParameterReturning("name", (void*)1, 0UL)->
withOutputParameterOfTypeReturning("type", "name", (void*)1);
mock_c()->actualCall("boo")->withParameterOfType("type", "name", (void*) 1)->
withOutputParameter("name", (void*)1)->
withOutputParameterOfType("type", "name", (void*)1);
mock_c()->enable();
mock_c()->clear();
mock_c()->installComparator("typeName", typeNameIsEqual, typeNameValueToString);
mock_c()->expectOneCall("boo")->withParameterOfType("typeName", "name", (void*) 1);
mock_c()->actualCall("boo")->withParameterOfType("typeName", "name", (void*) 1);
mock_c()->clear();
mock_c()->removeAllComparatorsAndCopiers();
mock_c()->expectOneCall("boo")->andReturnBoolValue(1);
mock_c()->actualCall("boo")->boolReturnValue();
mock_c()->boolReturnValue();
mock_c()->expectOneCall("boo")->andReturnIntValue(-10);
mock_c()->actualCall("boo")->intReturnValue();
mock_c()->intReturnValue();
mock_c()->returnValue();
mock_c()->expectOneCall("boo2")->andReturnUnsignedIntValue(1);
mock_c()->actualCall("boo2")->unsignedIntReturnValue();
mock_c()->unsignedIntReturnValue();
mock_c()->expectOneCall("boo3")->andReturnLongIntValue(1);
mock_c()->actualCall("boo3")->longIntReturnValue();
mock_c()->longIntReturnValue();
mock_c()->expectOneCall("boo3")->andReturnUnsignedLongIntValue(1);
mock_c()->actualCall("boo3")->unsignedLongIntReturnValue();
mock_c()->unsignedLongIntReturnValue();
#ifdef CPPUTEST_USE_LONG_LONG
mock_c()->expectOneCall("mgrgrgr1")->andReturnLongLongIntValue(1);
mock_c()->actualCall("mgrgrgr1")->longLongIntReturnValue();
mock_c()->longLongIntReturnValue();
mock_c()->expectOneCall("mgrgrgr2")->andReturnUnsignedLongLongIntValue(1);
mock_c()->actualCall("mgrgrgr2")->unsignedLongLongIntReturnValue();
mock_c()->unsignedLongLongIntReturnValue();
#endif
mock_c()->expectOneCall("boo4")->andReturnDoubleValue(1.0);
mock_c()->actualCall("boo4")->doubleReturnValue();
mock_c()->doubleReturnValue();
mock_c()->expectOneCall("boo5")->andReturnStringValue("hello world");
mock_c()->actualCall("boo5")->stringReturnValue();
mock_c()->stringReturnValue();
mock_c()->expectOneCall("boo6")->andReturnPointerValue((void*) 10);
mock_c()->actualCall("boo6")->pointerReturnValue();
mock_c()->pointerReturnValue();
mock_c()->expectOneCall("boo7")->andReturnConstPointerValue((void*) 10);
mock_c()->actualCall("boo7")->constPointerReturnValue();
mock_c()->constPointerReturnValue();
mock_c()->expectOneCall("boo8")->andReturnFunctionPointerValue((void(*)(void)) 10);
mock_c()->actualCall("boo8")->functionPointerReturnValue();
mock_c()->functionPointerReturnValue();
mock_c()->setBoolData("bool", 1);
mock_c()->expectOneCall("bla")->withBoolParameters("bool", 1);
mock_c()->actualCall("bla")->withBoolParameters("bool", mock_c()->getData("bool").value.boolValue);
mock_c()->setIntData("int", 5);
mock_c()->expectOneCall("bla")->withIntParameters("int", 5);
mock_c()->actualCall("bla")->withIntParameters("int", mock_c()->getData("int").value.intValue);
mock_c()->setStringData("string", "lol");
mock_c()->expectOneCall("bla")->withStringParameters("str", "lol");
mock_c()->actualCall("bla")->withStringParameters("str", mock_c()->getData("string").value.stringValue);
mock_c()->setDoubleData("double", 0.001);
mock_c()->expectOneCall("bla")->withDoubleParameters("double", 0.001);
mock_c()->actualCall("bla")->withDoubleParameters("double", mock_c()->getData("double").value.doubleValue);
mock_c()->setPointerData("ptr", (void*)1);
mock_c()->expectOneCall("bla")->withPointerParameters("ptr", (void*)1);
mock_c()->actualCall("bla")->withPointerParameters("ptr", mock_c()->getData("ptr").value.pointerValue);
mock_c()->setConstPointerData("cptr", (const void*)1);
mock_c()->expectOneCall("bla")->withConstPointerParameters("cptr", (const void*)1);
mock_c()->actualCall("bla")->withConstPointerParameters("cptr", mock_c()->getData("ptr").value.constPointerValue);
mock_c()->setFunctionPointerData("ptr", (void(*)(void))1);
mock_c()->expectOneCall("bla")->withFunctionPointerParameters("ptr", (void(*)(void))1);
mock_c()->actualCall("bla")->withFunctionPointerParameters("ptr", mock_c()->getData("ptr").value.functionPointerValue);
mock_c()->clear();
mock_c()->hasReturnValue();
mock_c()->returnBoolValueOrDefault(1);
mock_c()->returnIntValueOrDefault(-1);
mock_c()->returnUnsignedIntValueOrDefault(1);
mock_c()->returnLongIntValueOrDefault(-1L);
mock_c()->returnUnsignedLongIntValueOrDefault(1L);
#ifdef CPPUTEST_USE_LONG_LONG
mock_c()->returnLongLongIntValueOrDefault(-1LL);
mock_c()->returnUnsignedLongLongIntValueOrDefault(1ULL);
#endif
mock_c()->returnStringValueOrDefault("");
mock_c()->returnDoubleValueOrDefault(0.01);
mock_c()->returnPointerValueOrDefault(0);
mock_c()->returnConstPointerValueOrDefault(0);
mock_c()->returnFunctionPointerValueOrDefault(0);
mock_c()->disable();
mock_c()->actualCall("disabled");
mock_c()->enable();
mock_c()->checkExpectations();
mock_c()->setIntData("bla1", -2);
mock_c()->setUnsignedIntData("bla2", 2);
mock_c()->setDoubleData("bla3", 0.035);
mock_c()->setStringData("bla4", "abc");
mock_c()->setPointerData("bla", (void*) 2);
mock_c()->setConstPointerData("bla", (const void*) 2);
mock_c()->setFunctionPointerData("bla", (void (*)(void)) 2);
mock_c()->setDataObject("bla", "type", (void*) 2);
mock_c()->getData("bla");
mock_scope_c("scope")->expectOneCall("boo");
mock_scope_c("other")->expectedCallsLeft();
mock_scope_c("scope")->expectedCallsLeft();
mock_scope_c("scope")->actualCall("boo");
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TESTMOCKSUPPORTC_CFILE_H
#define TESTMOCKSUPPORTC_CFILE_H
#ifdef __cplusplus
extern "C" {
#endif
extern void all_mock_support_c_calls(void);
#ifdef __cplusplus
}
#endif
#endif /* TESTMOCKSUPPORTC_CFILE_H */

View file

@ -0,0 +1,246 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTest/TestTestingFixture.h"
#include "CppUTestExt/OrderedTest.h"
#include "OrderedTestTest.h"
TEST_GROUP(TestOrderedTest)
{
TestTestingFixture* fixture;
OrderedTestShell orderedTest;
OrderedTestShell orderedTest2;
OrderedTestShell orderedTest3;
ExecFunctionTestShell normalTest;
ExecFunctionTestShell normalTest2;
ExecFunctionTestShell normalTest3;
OrderedTestShell* orderedTestCache;
void setup() _override
{
orderedTestCache = OrderedTestShell::getOrderedTestHead();
OrderedTestShell::setOrderedTestHead(NULLPTR);
fixture = new TestTestingFixture();
fixture->getRegistry()->unDoLastAddTest();
}
void teardown() _override
{
delete fixture;
OrderedTestShell::setOrderedTestHead(orderedTestCache);
}
void InstallOrderedTest(OrderedTestShell& test, int level)
{
OrderedTestInstaller(test, "testgroup", "testname", __FILE__, __LINE__, level);
}
void InstallNormalTest(UtestShell& test)
{
TestInstaller(test, "testgroup", "testname", __FILE__, __LINE__);
}
UtestShell* firstTest()
{
return fixture->getRegistry()->getFirstTest();
}
UtestShell* secondTest()
{
return firstTest()->getNext();
}
};
TEST(TestOrderedTest, TestInstallerSetsFields)
{
OrderedTestInstaller(orderedTest, "testgroup", "testname", "this.cpp", 10, 5);
STRCMP_EQUAL("testgroup", orderedTest.getGroup().asCharString());
STRCMP_EQUAL("testname", orderedTest.getName().asCharString());
STRCMP_EQUAL("this.cpp", orderedTest.getFile().asCharString());
LONGS_EQUAL(10, orderedTest.getLineNumber());
LONGS_EQUAL(5, orderedTest.getLevel());
}
TEST(TestOrderedTest, InstallOneText)
{
InstallOrderedTest(orderedTest, 5);
CHECK(firstTest() == &orderedTest);
}
TEST(TestOrderedTest, OrderedTestsAreLast)
{
InstallNormalTest(normalTest);
InstallOrderedTest(orderedTest, 5);
CHECK(firstTest() == &normalTest);
CHECK(secondTest() == &orderedTest);
}
TEST(TestOrderedTest, TwoTestsAddedInReverseOrder)
{
InstallOrderedTest(orderedTest, 5);
InstallOrderedTest(orderedTest2, 3);
CHECK(firstTest() == &orderedTest2);
CHECK(secondTest() == &orderedTest);
}
TEST(TestOrderedTest, TwoTestsAddedInOrder)
{
InstallOrderedTest(orderedTest2, 3);
InstallOrderedTest(orderedTest, 5);
CHECK(firstTest() == &orderedTest2);
CHECK(secondTest() == &orderedTest);
}
TEST(TestOrderedTest, MultipleOrderedTests)
{
InstallNormalTest(normalTest);
InstallOrderedTest(orderedTest2, 3);
InstallNormalTest(normalTest2);
InstallOrderedTest(orderedTest, 5);
InstallNormalTest(normalTest3);
InstallOrderedTest(orderedTest3, 7);
UtestShell * firstOrderedTest = firstTest()->getNext()->getNext()->getNext();
CHECK(firstOrderedTest == &orderedTest2);
CHECK(firstOrderedTest->getNext() == &orderedTest);
CHECK(firstOrderedTest->getNext()->getNext() == &orderedTest3);
}
TEST(TestOrderedTest, MultipleOrderedTests2)
{
InstallOrderedTest(orderedTest, 3);
InstallOrderedTest(orderedTest2, 1);
InstallOrderedTest(orderedTest3, 2);
CHECK(firstTest() == &orderedTest2);
CHECK(secondTest() == &orderedTest3);
CHECK(secondTest()->getNext() == &orderedTest);
}
class OrderedTestTestingFixture
{
public:
static void checkRun(int run) {
if(run != run_) {
run_ = run;
count_ = 0;
}
}
static int count(void) {
return count_++;
}
private:
static int run_;
static int count_;
};
int OrderedTestTestingFixture::run_ = 0;
int OrderedTestTestingFixture::count_ = 0;
TEST_GROUP(TestOrderedTestMacros)
{
void setup() _override
{
OrderedTestTestingFixture::checkRun(TestRegistry::getCurrentRegistry()->getCurrentRepetition());
}
};
TEST(TestOrderedTestMacros, NormalTest)
{
CHECK(OrderedTestTestingFixture::count() == 0);
}
TEST_ORDERED(TestOrderedTestMacros, Test2, 2)
{
CHECK(OrderedTestTestingFixture::count() == 2);
}
TEST_ORDERED(TestOrderedTestMacros, Test1, 1)
{
CHECK(OrderedTestTestingFixture::count() == 1);
}
TEST_ORDERED(TestOrderedTestMacros, Test4, 4)
{
CHECK(OrderedTestTestingFixture::count() == 4);
}
TEST_ORDERED(TestOrderedTestMacros, Test3, 3)
{
CHECK(OrderedTestTestingFixture::count() == 3);
}
// Test with same level
TEST_ORDERED(TestOrderedTestMacros, Test5_1, 5)
{
CHECK(OrderedTestTestingFixture::count() == 5);
}
TEST_ORDERED(TestOrderedTestMacros, Test6_1, 6)
{
CHECK(OrderedTestTestingFixture::count() == 7);
}
TEST_ORDERED(TestOrderedTestMacros, Test5_2, 5)
{
CHECK(OrderedTestTestingFixture::count() == 6);
}
TEST_ORDERED(TestOrderedTestMacros, Test6_2, 6)
{
CHECK(OrderedTestTestingFixture::count() == 8);
}
// Test C-Interface
TEST_ORDERED(TestOrderedTestMacros, Test10, 10)
{
CHECK(OrderedTestTestingFixture::count() == 12);
}
TEST_ORDERED(TestOrderedTestMacros, Test8, 8)
{
CHECK(OrderedTestTestingFixture::count() == 10);
}
// Export to be usable in OrderedTestTest_c.c
extern "C" {
int orderedTestFixtureCWrapper(void) {
return OrderedTestTestingFixture::count();
}
}
TEST_ORDERED_C_WRAPPER(TestOrderedTestMacros, Test11, 11)
TEST_ORDERED_C_WRAPPER(TestOrderedTestMacros, Test7, 7)
TEST_ORDERED_C_WRAPPER(TestOrderedTestMacros, Test9, 9)

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ORDEREDTESTTEST_H
#define ORDEREDTESTTEST_H
#ifdef __cplusplus
extern "C" {
#endif
extern int orderedTestFixtureCWrapper(void);
#ifdef __cplusplus
}
#endif
#endif /* ORDEREDTESTTEST_H */

View file

@ -0,0 +1,17 @@
#include "CppUTest/TestHarness_c.h"
#include "OrderedTestTest.h"
TEST_C(TestOrderedTestMacros, Test11)
{
CHECK_C(orderedTestFixtureCWrapper() == 13);
}
TEST_C(TestOrderedTestMacros, Test7)
{
CHECK_C(orderedTestFixtureCWrapper() == 9);
}
TEST_C(TestOrderedTestMacros, Test9)
{
CHECK_C(orderedTestFixtureCWrapper() == 11);
}