Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
separate formal CppUTest tests and example show-case tests for MixIns
  • Loading branch information
the-real-orca committed Jun 15, 2015
commit 26924624add1ee083d91b1cc2d9c44cc500c1966
1 change: 0 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ CppUTestTests_SOURCES = \
tests/MemoryLeakDetectorTest.cpp \
tests/MemoryLeakOperatorOverloadsTest.cpp \
tests/MemoryLeakWarningTest.cpp \
tests/MixInApplyTest.cpp \
tests/MixInTest.cpp \
tests/PluginTest.cpp \
tests/PreprocessorTest.cpp \
Expand Down
3 changes: 3 additions & 0 deletions examples/AllTests/AllTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
<ClCompile Include="CircularBufferTest.cpp" />
<ClCompile Include="EventDispatcherTest.cpp" />
<ClCompile Include="HelloTest.cpp" />
<ClCompile Include="MixInApplyTest.cpp" />
<ClCompile Include="MixInTest.cpp" />
<ClCompile Include="MockDocumentationTest.cpp" />
<ClCompile Include="PrinterTest.cpp" />
</ItemGroup>
Expand All @@ -160,6 +162,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="AllTests.h" />
<ClInclude Include="MixInTest.h" />
<ClInclude Include="MockPrinter.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
77 changes: 77 additions & 0 deletions examples/AllTests/MixInApplyTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

#include "CppUTest/TestHarness.h"
#include "MixInTest.h"

/*
each implementation of the SUT interface should have:
- a function className() returning the name of the class
- a function foo(int val) returning an integer value greater than the given parameter val
*/
class ImplA : public FooInterface
{
public:

// implementing the FooInterface
const char* className() { return "ImplA"; }
int foo(int val) { return val + 1; }

// implementation specific
bool bar() { return true; }
};

class ImplB : public FooInterface
{
public:
ImplB() { dummyVariable = 1; }

// implementing the FooInterface
const char* className() { return "ImplB"; }
int foo(int val) { return val + 10; }

// implementation specific
int dummyVariable;
};

//
// tests for ImplA implementation of the FooInterface
//
TEST_GROUP(ImplATestGroup)
{
ImplA obj;
};

// add common tests of DemoMixInGroup to ImplATestGroup with the name prefix: MixInImplA
MIXIN_APPLY(ImplATestGroup, DemoMixInGroup, MixInImplA)
{
params.sut = &obj;
params.expectedName = "ImplA";
}

// implementation specific tests
TEST(ImplATestGroup, implementationSpecificTest)
{
CHECK( obj.bar() );
}


//
// tests for ImplA implementation of the FooInterface
//
TEST_GROUP(ImplBTestGroup)
{
ImplB obj;
};

// add common tests of DemoMixInGroup to ImplBTestGroup with the name prefix: MixInImplB
MIXIN_APPLY(ImplBTestGroup, DemoMixInGroup, MixInImplB)
{
params.sut = &obj;
params.expectedName = "ImplB";
}

// implementation specific tests
TEST(ImplBTestGroup, justADummyTest)
{
LONGS_EQUAL( 1, obj.dummyVariable );
}

53 changes: 53 additions & 0 deletions examples/AllTests/MixInTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Stephan Veigl
* 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 "MixInTest.h"


MIXIN_GROUP(DemoMixInGroup)
{
void setup()
{
CHECK(params.sut);
}
void teardown() {}
};


MIXIN_TEST(DemoMixInGroup, classNameCheck)
{
const char* className = params.sut->className();
STRCMP_EQUAL( params.expectedName, className );
}


MIXIN_TEST(DemoMixInGroup, fooFunctionTest)
{
CHECK( 1 < params.sut->foo(1) );
}

13 changes: 9 additions & 4 deletions tests/MixInTest.h → examples/AllTests/MixInTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@

#include "CppUTest/TestHarness.h"

// system under test interface
class SUT
/*
each implementation of the SUT interface should have:
- a function className() returning the name of the class
- a function foo() returning an integer value greater 1
*/
class FooInterface
{
public:
virtual const char* className() = 0;
virtual ~SUT() {}
virtual int foo(int) = 0;
virtual ~FooInterface() {}
};

MIXIN_PARAMS(DemoMixInGroup) // MIXIN_GROUP name
{
SUT* obj;
FooInterface* sut;
char const* expectedName;
};
2 changes: 0 additions & 2 deletions tests/AllTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
<ClCompile Include="MemoryLeakDetectorTest.cpp" />
<ClCompile Include="MemoryLeakOperatorOverloadsTest.cpp" />
<ClCompile Include="MemoryLeakWarningTest.cpp" />
<ClCompile Include="MixInApplyTest.cpp" />
<ClCompile Include="MixInTest.cpp" />
<ClCompile Include="PluginTest.cpp" />
<ClCompile Include="PreprocessorTest.cpp" />
Expand Down Expand Up @@ -187,7 +186,6 @@
<ClInclude Include="AllTests.h" />
<ClInclude Include="CppUTestExt\MockFailureTest.h" />
<ClInclude Include="CppUTestExt\MockSupport_cTestCFile.h" />
<ClInclude Include="MixInTest.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
37 changes: 0 additions & 37 deletions tests/MixInApplyTest.cpp

This file was deleted.

50 changes: 42 additions & 8 deletions tests/MixInTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,55 @@
*/

#include "CppUTest/TestHarness.h"
#include "MixInTest.h"


MIXIN_GROUP(DemoMixInGroup)
// system under test interface
class SUT
{
void setup()
public:
const char* name() { return "SystemUnderTest"; }
};

MIXIN_PARAMS(DemoMixInGroup)
{
SUT* obj;
char const* expectedName;
bool initializedByMixInSetup;
};

MIXIN_GROUP(DemoMixInGroup) {
void setup()
{
CHECK(params.obj);
params.initializedByMixInSetup = true;
init = true;
}
void teardown() {}

bool init;
};
TEST_GROUP(MixInTestGroup) {};

MIXIN_APPLY(MixInTestGroup, DemoMixInGroup, ImplA_test)
{
SUT sut;
params.obj = &sut;
params.expectedName = "SystemUnderTest";
params.initializedByMixInSetup = false;
}

MIXIN_TEST(DemoMixInGroup, name)
MIXIN_TEST(DemoMixInGroup, paramsSet)
{
const char* className = params.obj->className();
STRCMP_EQUAL( params.expectedName, className );
CHECK(params.obj);
CHECK(params.expectedName);
}

MIXIN_TEST(DemoMixInGroup, paramsObjectSubFunction)
{
STRCMP_EQUAL( params.expectedName, params.obj->name() );
}

MIXIN_TEST(DemoMixInGroup, mixInSetupWasCalled)
{
CHECK(init);
CHECK(params.initializedByMixInSetup);
}