summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Mohrhard <markus.mohrhard@googlemail.com>2015-11-08 07:31:41 -0800
committerMarkus Mohrhard <markus.mohrhard@googlemail.com>2015-11-08 07:31:41 -0800
commitbff09f0ff2795d98b75c5579b1e50254eecf01a4 (patch)
treef1d8db3f70ef6bb7427ce8956fa263b11a37c3cd
parent292026e36ce5b9ebdad86e99304be2ded8c199d8 (diff)
downloadcppunit-feature/parameterized.tar.gz
add parameterized test callersfeature/parameterized
-rw-r--r--examples/cppunittest/Makefile.am6
-rw-r--r--examples/cppunittest/TestParameterizedTestCaller.cpp49
-rw-r--r--examples/cppunittest/TestParameterizedTestCaller.h23
-rw-r--r--include/cppunit/Makefile.am5
-rw-r--r--include/cppunit/ParameterizedTestCaller.h212
-rw-r--r--src/cppunit/Makefile.am6
-rw-r--r--src/cppunit/TestParameterizedRunner.cpp1
7 files changed, 302 insertions, 0 deletions
diff --git a/examples/cppunittest/Makefile.am b/examples/cppunittest/Makefile.am
index 4b93173..16bd162 100644
--- a/examples/cppunittest/Makefile.am
+++ b/examples/cppunittest/Makefile.am
@@ -75,6 +75,12 @@ cppunittestmain_SOURCES = \
XmlUniformiserTest.h \
XmlUniformiserTest.cpp
+if WITH_OPTIONAL_FEATURES
+cppunittestmain_SOURCES += \
+ TestParameterizedTestCaller.cpp \
+ TestParameterizedTestCaller.h
+endif
+
cppunittestmain_LDADD= \
$(top_builddir)/src/cppunit/libcppunit.la \
$(LIBADD_DL)
diff --git a/examples/cppunittest/TestParameterizedTestCaller.cpp b/examples/cppunittest/TestParameterizedTestCaller.cpp
new file mode 100644
index 0000000..a8e83b0
--- /dev/null
+++ b/examples/cppunittest/TestParameterizedTestCaller.cpp
@@ -0,0 +1,49 @@
+#include "HelperSuite.h"
+#include "TestParameterizedTestCaller.h"
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <cppunit/ParameterizedTestCaller.h>
+
+#include <iostream>
+
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TestParameterizedTestCaller,
+ helperSuiteName() );
+
+namespace {
+
+class SimpleTest : public CPPUNIT_NS::TestFixture
+{
+public:
+
+ void test(const int& i);
+};
+
+void SimpleTest::test(const int& i)
+{
+ std::cout << i;
+}
+
+}
+
+TestParameterizedTestCaller::TestParameterizedTestCaller()
+{
+}
+
+
+TestParameterizedTestCaller::~TestParameterizedTestCaller()
+{
+}
+
+void
+TestParameterizedTestCaller::test1()
+{
+ {
+ CPPUNIT_NS::ParameterizedTestCaller<SimpleTest, int> caller( "test1",
+ &SimpleTest::test,
+ { 1, 2, 3} );
+ CPPUNIT_NS::TestResult m_result;
+ caller.run( &m_result );
+
+ } // Force destruction of the test caller.
+}
diff --git a/examples/cppunittest/TestParameterizedTestCaller.h b/examples/cppunittest/TestParameterizedTestCaller.h
new file mode 100644
index 0000000..21cd1f0
--- /dev/null
+++ b/examples/cppunittest/TestParameterizedTestCaller.h
@@ -0,0 +1,23 @@
+#ifndef TESTCALLERTEST_H
+#define TESTCALLERTEST_H
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/TestCase.h>
+#include <cppunit/TestResult.h>
+#include <cppunit/TestSuite.h>
+
+class TestParameterizedTestCaller : public CPPUNIT_NS::TestFixture
+{
+ CPPUNIT_TEST_SUITE( TestParameterizedTestCaller );
+ CPPUNIT_TEST( test1 );
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ TestParameterizedTestCaller();
+ ~TestParameterizedTestCaller();
+
+ void test1();
+
+};
+
+#endif // TESTCALLERTEST_H
diff --git a/include/cppunit/Makefile.am b/include/cppunit/Makefile.am
index 733dcc9..1d9929a 100644
--- a/include/cppunit/Makefile.am
+++ b/include/cppunit/Makefile.am
@@ -38,5 +38,10 @@ libcppunitinclude_HEADERS = \
XmlOutputter.h \
XmlOutputterHook.h
+if WITH_OPTIONAL_FEATURES
+libcppunitinclude_HEADERS += \
+ ParameterizedTestCaller.h
+endif
+
dist-hook:
rm -f $(distdir)/config-auto.h
diff --git a/include/cppunit/ParameterizedTestCaller.h b/include/cppunit/ParameterizedTestCaller.h
new file mode 100644
index 0000000..55a79b7
--- /dev/null
+++ b/include/cppunit/ParameterizedTestCaller.h
@@ -0,0 +1,212 @@
+#ifndef CPPUNIT_PARAMETERIZEDTESTCALLER_H // -*- C++ -*-
+#define CPPUNIT_PARAMETERIZEDTESTCALLER_H
+
+#include <cppunit/Exception.h>
+#include <cppunit/TestCase.h>
+
+
+#if CPPUNIT_USE_TYPEINFO_NAME
+# include <cppunit/extensions/TypeInfoHelper.h>
+#endif
+
+
+CPPUNIT_NS_BEGIN
+
+#if 0
+/*! \brief Marker class indicating that no exception is expected by TestCaller.
+ * This class is an implementation detail. You should never use this class directly.
+ */
+class CPPUNIT_API NoExceptionExpected
+{
+private:
+ //! Prevent class instantiation.
+ NoExceptionExpected();
+};
+
+
+/*! \brief (Implementation) Traits used by TestCaller to expect an exception.
+ *
+ * This class is an implementation detail. You should never use this class directly.
+ */
+template<class ExceptionType>
+struct ExpectedExceptionTraits
+{
+ static void expectedException()
+ {
+#if CPPUNIT_USE_TYPEINFO_NAME
+ throw Exception( Message(
+ "expected exception not thrown",
+ "Expected exception type: " +
+ TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
+#else
+ throw Exception( "expected exception not thrown" );
+#endif
+ }
+};
+
+
+/*! \brief (Implementation) Traits specialization used by TestCaller to
+ * expect no exception.
+ *
+ * This class is an implementation detail. You should never use this class directly.
+ */
+template<>
+struct ExpectedExceptionTraits<NoExceptionExpected>
+{
+ static void expectedException()
+ {
+ }
+};
+
+
+#endif
+
+//*** FIXME: rework this when class Fixture is implemented. ***//
+
+
+/*! \brief Generate a test case from a fixture method.
+ * \ingroup WritingTestFixture
+ *
+ * A test caller provides access to a test case method
+ * on a test fixture class. Test callers are useful when
+ * you want to run an individual test or add it to a
+ * suite.
+ * Test Callers invoke only one Test (i.e. test method) on one
+ * Fixture of a TestFixture.
+ *
+ * Here is an example:
+ * \code
+ * class MathTest : public CppUnit::TestFixture {
+ * ...
+ * public:
+ * void setUp();
+ * void tearDown();
+ *
+ * void testAdd();
+ * void testSubtract();
+ * };
+ *
+ * CppUnit::Test *MathTest::suite() {
+ * CppUnit::TestSuite *suite = new CppUnit::TestSuite;
+ *
+ * suite->addTest( new CppUnit::TestCaller<MathTest>( "testAdd", testAdd ) );
+ * return suite;
+ * }
+ * \endcode
+ *
+ * You can use a TestCaller to bind any test method on a TestFixture
+ * class, as long as it accepts void and returns void.
+ *
+ * \see TestCase
+ */
+
+template <class Fixture, typename ParameterType>
+class ParameterizedTestCaller : public TestCase
+{
+ typedef void (Fixture::*TestMethod)(const ParameterType&);
+
+public:
+ /*!
+ * Constructor for ParameterizedTestCaller. This constructor builds a new Fixture
+ * instance owned by the ParameterizedTestCaller.
+ * \param name name of this ParameterizedTestCaller
+ * \param test the method this ParameterizedTestCaller calls in runTest()
+ */
+ ParameterizedTestCaller( std::string name, TestMethod test, const std::initializer_list<ParameterType>& parameter_list ) :
+ TestCase( name ),
+ m_ownFixture( true ),
+ m_fixture( new Fixture() ),
+ m_test( test ),
+ m_parameter_list(parameter_list)
+ {
+ }
+
+ /*!
+ * Constructor for ParameterizedTestCaller.
+ * This constructor does not create a new Fixture instance but accepts
+ * an existing one as parameter. The ParameterizedTestCaller will not own the
+ * Fixture object.
+ * \param name name of this ParameterizedTestCaller
+ * \param test the method this ParameterizedTestCaller calls in runTest()
+ * \param fixture the Fixture to invoke the test method on.
+ */
+ ParameterizedTestCaller(std::string name, TestMethod test, Fixture& fixture,
+ const std::initializer_list<ParameterType>& parameter_list) :
+ TestCase( name ),
+ m_ownFixture( false ),
+ m_fixture( &fixture ),
+ m_test( test ),
+ m_parameter_list(parameter_list)
+ {
+ }
+
+ /*!
+ * Constructor for ParameterizedTestCaller.
+ * This constructor does not create a new Fixture instance but accepts
+ * an existing one as parameter. The ParameterizedTestCaller will own the
+ * Fixture object and delete it in its destructor.
+ * \param name name of this ParameterizedTestCaller
+ * \param test the method this ParameterizedTestCaller calls in runTest()
+ * \param fixture the Fixture to invoke the test method on.
+ */
+ ParameterizedTestCaller(std::string name, TestMethod test, Fixture* fixture,
+ const std::initializer_list<ParameterType>& parameter_list) :
+ TestCase( name ),
+ m_ownFixture( true ),
+ m_fixture( fixture ),
+ m_test( test ),
+ m_parameter_list(parameter_list)
+ {
+ }
+
+ ~ParameterizedTestCaller()
+ {
+ if (m_ownFixture)
+ delete m_fixture;
+ }
+
+ void runTest()
+ {
+ for (auto itr = m_parameter_list.begin(), itrEnd = m_parameter_list.end();
+ itr != itrEnd; ++itr)
+ {
+ (m_fixture->*m_test)(*itr);
+ }
+ }
+
+ void setUp()
+ {
+ m_fixture->setUp ();
+ }
+
+ void tearDown()
+ {
+ m_fixture->tearDown ();
+ }
+
+ std::string toString() const
+ {
+ return "ParameterizedTestCaller " + getName();
+ }
+
+ int countTestCases() const
+ {
+ return m_parameter_list.size();
+ }
+
+private:
+ ParameterizedTestCaller( const ParameterizedTestCaller &other );
+ ParameterizedTestCaller &operator =( const ParameterizedTestCaller &other );
+
+private:
+ bool m_ownFixture;
+ Fixture *m_fixture;
+ TestMethod m_test;
+ std::initializer_list<ParameterType> m_parameter_list;
+};
+
+
+
+CPPUNIT_NS_END
+
+#endif // CPPUNIT_TESTCALLER_H
diff --git a/src/cppunit/Makefile.am b/src/cppunit/Makefile.am
index 15cc24d..3bdcec7 100644
--- a/src/cppunit/Makefile.am
+++ b/src/cppunit/Makefile.am
@@ -61,6 +61,12 @@ libcppunit_la_SOURCES = \
XmlOutputterHook.cpp \
Win32DynamicLibraryManager.cpp
+if WITH_OPTIONAL_FEATURES
+
+libcppunit_la_SOURCES += \
+ TestParameterizedRunner.cpp
+endif
+
libcppunit_la_LDFLAGS= \
-no-undefined -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \
-release $(LT_RELEASE) $(LIBADD_DL)
diff --git a/src/cppunit/TestParameterizedRunner.cpp b/src/cppunit/TestParameterizedRunner.cpp
new file mode 100644
index 0000000..163b4d3
--- /dev/null
+++ b/src/cppunit/TestParameterizedRunner.cpp
@@ -0,0 +1 @@
+