[Libreoffice-commits] cppunit.git: 4 commits - examples/cppunittest include/cppunit NEWS src/cppunit

Markus Mohrhard markus.mohrhard at googlemail.com
Fri Dec 16 13:10:52 UTC 2016


 NEWS                                                 |   13 +++--
 examples/cppunittest/HelperMacrosTest.cpp            |   23 +++++++++
 examples/cppunittest/HelperMacrosTest.h              |    3 +
 include/cppunit/TestAssert.h                         |   31 -------------
 include/cppunit/TestCaller.h                         |   20 ++++++--
 include/cppunit/extensions/HelperMacros.h            |   11 ++++
 include/cppunit/extensions/TestNamer.h               |    7 ++
 include/cppunit/extensions/TestSuiteBuilderContext.h |   15 ++++++
 include/cppunit/tools/Makefile.am                    |    3 -
 include/cppunit/tools/StringHelper.h                 |   45 +++++++++++++++++++
 src/cppunit/TestNamer.cpp                            |    1 
 11 files changed, 133 insertions(+), 39 deletions(-)

New commits:
commit 079bdbdd2b0e23f30ee733b7592c9c5bc119bd1e
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Fri Dec 16 14:03:26 2016 +0100

    add changes for TestCaller and TEST_CASE_PARAMETERIZED into NEWS

diff --git a/NEWS b/NEWS
index 9044dbe..eee1a75 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,15 @@
   New in CppUnit 1.14.0:
   ---------------------
 
+  - TestCaller supports any callable that can be put into
+    std::function<void()>.
+
+  - CPPUNIT_TEST_PARAMETERIZED executes a test for any value in
+    an iteratable.
+
+  - Added new Assertion macros: ASSERT_LESS, ASSERT_GREATER,
+                    ASSERT_LESSEQUAL, ASSERT_GREATEREQUAL
+
 * Portability:
   - Always build with C++11.
 
@@ -17,10 +26,6 @@
 * Test Plug-in Runner: 
   - Fixed crash on Win64 in test runner (fdo#81433)
 
-  - Added new Assertion macros: ASSERT_LESS, ASSERT_GREATER,
-                    ASSERT_LESSEQUAL, ASSERT_GREATEREQUAL
-
-
   New in CppUnit 1.13.2:
   ---------------------
 
commit 4e529c6a6569d1f352e02af16e53aba7ae7bdc1a
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Fri Dec 16 13:53:51 2016 +0100

    implement parameterized tests
    
    This allows to execute the same test with different parameters and
    treats each execution as an own test. The change consists of two parts,
    the TestCaller can now handle any callable which also makes it easy to
    generate programatically more complex test cases as well as the new
    CPPUNIT_TEST_PARAMETERIZED macro. That macro takes the test name as well
    as an iteratable, e.g. std::initializer_list.
    
    An example for this usage is:
    
    class SimpleTest : public CppUnit::TestFixture
    {
    public:
        CPPUNIT_TEST_SUITE(SimpleTest);
        CPPUNIT_TEST_PARAMETERIZED(test, {1, 2, 3, 4});
        CPPUNIT_TEST_SUITE_END();
    
        void test(int i)
        {
            CPPUNIT_ASSERT(i < 5);
        }
    };
    
    which will execute test 4 times with the values 1 to 4.

diff --git a/examples/cppunittest/HelperMacrosTest.cpp b/examples/cppunittest/HelperMacrosTest.cpp
index 4e83b81..2cdd3a7 100644
--- a/examples/cppunittest/HelperMacrosTest.cpp
+++ b/examples/cppunittest/HelperMacrosTest.cpp
@@ -77,6 +77,19 @@ public:
   }
 };
 
+class ParameterizedTestFixture : public CPPUNIT_NS::TestFixture
+{
+    CPPUNIT_TEST_SUITE(ParameterizedTestFixture);
+    CPPUNIT_TEST_PARAMETERIZED(testMethod, {1, 2, 3, 4});
+    CPPUNIT_TEST_SUITE_END();
+
+public:
+
+    void testMethod(int /*val*/)
+    {
+    }
+};
+
 
 #undef TEST_ADD_N_MOCK
 #define TEST_ADD_N_MOCK( totalCount )                                              \
@@ -225,3 +238,13 @@ HelperMacrosTest::testAddTest()
   suite->run( m_result );
   m_testListener->verify();
 }
+
+void
+HelperMacrosTest::testParameterizedTests()
+{
+  std::unique_ptr<CPPUNIT_NS::TestSuite> suite( ParameterizedTestFixture::suite() );
+  m_testListener->setExpectedStartTestCall(4);
+  m_testListener->setExpectedAddFailureCall( 0 );
+  suite->run(m_result);
+  m_testListener->verify();
+}
diff --git a/examples/cppunittest/HelperMacrosTest.h b/examples/cppunittest/HelperMacrosTest.h
index 7a09797..966b125 100644
--- a/examples/cppunittest/HelperMacrosTest.h
+++ b/examples/cppunittest/HelperMacrosTest.h
@@ -16,6 +16,7 @@ class HelperMacrosTest : public CPPUNIT_NS::TestFixture
   CPPUNIT_TEST( testExceptionNotCaught );
   CPPUNIT_TEST( testCustomTests );
   CPPUNIT_TEST( testAddTest );
+  CPPUNIT_TEST( testParameterizedTests );
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -39,6 +40,8 @@ public:
   void testCustomTests();
   void testAddTest();
 
+  void testParameterizedTests();
+
 private:
   HelperMacrosTest( const HelperMacrosTest &copy );
   void operator =( const HelperMacrosTest &copy );
diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h
index 3dfab2d..4c30319 100644
--- a/include/cppunit/extensions/HelperMacros.h
+++ b/include/cppunit/extensions/HelperMacros.h
@@ -301,6 +301,17 @@ public:									       \
                   &TestFixtureType::testMethod,           \
                   context.makeFixture() ) ) )
 
+#define CPPUNIT_TEST_PARAMETERIZED( testMethod, ... )    \
+    for (auto& i : __VA_ARGS__)                                  \
+    {                                                           \
+        TestFixtureType* fixture = context.makeFixture();       \
+        CPPUNIT_TEST_SUITE_ADD_TEST(                            \
+        ( new CPPUNIT_NS::TestCaller<TestFixtureType>(          \
+                    context.getTestNameFor(#testMethod, i),     \
+                    std::bind(&TestFixtureType::testMethod, fixture, i),          \
+                    fixture)));                                  \
+    }
+
 /*! \brief Add a test which fail if the specified exception is not caught.
  *
  * Example:
diff --git a/include/cppunit/extensions/TestNamer.h b/include/cppunit/extensions/TestNamer.h
index 3907569..0c8fb31 100644
--- a/include/cppunit/extensions/TestNamer.h
+++ b/include/cppunit/extensions/TestNamer.h
@@ -3,6 +3,7 @@
 
 #include <cppunit/Portability.h>
 #include <string>
+#include <cppunit/tools/StringHelper.h>
 
 #include <typeinfo>
 
@@ -63,6 +64,12 @@ public:
    */
   virtual std::string getTestNameFor( const std::string &testMethodName ) const;
 
+  template<typename E>
+  std::string getTestNameFor( const std::string& testMethodName, const E& val) const
+  {
+      return getTestNameFor(testMethodName) + " with parameter: " + CPPUNIT_NS::StringHelper::toString(val);
+  }
+
 protected:
   std::string m_fixtureName;
 };
diff --git a/include/cppunit/extensions/TestSuiteBuilderContext.h b/include/cppunit/extensions/TestSuiteBuilderContext.h
index b62eeb8..72bfa70 100644
--- a/include/cppunit/extensions/TestSuiteBuilderContext.h
+++ b/include/cppunit/extensions/TestSuiteBuilderContext.h
@@ -62,6 +62,21 @@ public:
    */
   std::string getTestNameFor( const std::string &testMethodName ) const;
 
+  /*! \brief Returns the name of the test for the specified method with the corresponding parameter.
+   *
+   * \param testMethodName Name (including a parameter) of the method that implements a test.
+   * \return A string that is the concatenation of the test fixture name
+   *         (returned by getFixtureName()), \a testMethodName,
+   *         separated using '::' and the parameter. This provides a fairly unique name for a given
+   *         test. The parameter must be convertable to std::string through operator<<
+   *         or a specialization of CPPUNIT_NS::StringHelper::toString needs to exist.
+   */
+  template<typename T>
+  std::string getTestNameFor( const std::string &testMethodName, const T& value ) const
+  {
+      return m_namer.getTestNameFor(testMethodName, value);
+  }
+
   /*! \brief Adds property pair.
    * \param key   PropertyKey string to add.
    * \param value PropertyValue string to add.
diff --git a/src/cppunit/TestNamer.cpp b/src/cppunit/TestNamer.cpp
index 0cdf258..1eb6c5f 100644
--- a/src/cppunit/TestNamer.cpp
+++ b/src/cppunit/TestNamer.cpp
@@ -1,5 +1,6 @@
 #include <cppunit/extensions/TestNamer.h>
 #include <cppunit/extensions/TypeInfoHelper.h>
+#include <cppunit/tools/StringHelper.h>
 #include <string>
 
 CPPUNIT_NS_BEGIN
commit 923e2a837d515eb0d33792aba8bbb839f0012067
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Fri Dec 16 13:50:27 2016 +0100

    use std::function for the test method in TestCaller
    
    This allows us to pass in any callable e.g. results of std::bind.

diff --git a/include/cppunit/TestCaller.h b/include/cppunit/TestCaller.h
index bbb9002..fbf3902 100644
--- a/include/cppunit/TestCaller.h
+++ b/include/cppunit/TestCaller.h
@@ -4,6 +4,8 @@
 #include <cppunit/Exception.h>
 #include <cppunit/TestCase.h>
 
+#include <functional>
+
 
 #if defined(CPPUNIT_USE_TYPEINFO_NAME)
 #  include <cppunit/extensions/TypeInfoHelper.h>
@@ -116,7 +118,7 @@ public:
 	    TestCase( name ), 
 	    m_ownFixture( true ),
 	    m_fixture( new Fixture() ),
-	    m_test( test )
+	    m_test_function( std::bind(test, m_fixture) )
   {
   }
 
@@ -133,7 +135,7 @@ public:
 	    TestCase( name ), 
 	    m_ownFixture( false ),
 	    m_fixture( &fixture ),
-	    m_test( test )
+	    m_test_function( std::bind(test, &fixture) )
   {
   }
     
@@ -150,9 +152,17 @@ public:
 	    TestCase( name ), 
 	    m_ownFixture( true ),
 	    m_fixture( fixture ),
-	    m_test( test )
+	    m_test_function( std::bind(test, fixture) )
   {
   }
+
+  TestCaller(std::string name, std::function<void()> test_function, Fixture* fixture):
+      TestCase(name),
+      m_ownFixture(true),
+      m_fixture(fixture),
+      m_test_function(test_function)
+    {
+    }
     
   ~TestCaller() 
   {
@@ -162,7 +172,7 @@ public:
 
   void runTest()
   { 
-      (m_fixture->*m_test)();
+      m_test_function();
   }  
 
   void setUp()
@@ -187,7 +197,7 @@ private:
 private:
   bool m_ownFixture;
   Fixture *m_fixture;
-  TestMethod m_test;
+  std::function<void()> m_test_function;
 };
 
 
commit 25ccf3dd39c283c54313d8d26374e493e0e5f1a6
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date:   Fri Dec 16 13:37:29 2016 +0100

    extract the code to turn a variable into a string

diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h
index 69c105e..bb29ac0 100644
--- a/include/cppunit/TestAssert.h
+++ b/include/cppunit/TestAssert.h
@@ -5,10 +5,9 @@
 #include <cppunit/Exception.h>
 #include <cppunit/Asserter.h>
 #include <cppunit/portability/Stream.h>
-#include <type_traits>
+#include <cppunit/tools/StringHelper.h>
 #include <stdio.h>
 #include <float.h> // For struct assertion_traits<double>
-#include <type_traits>
 
 // Work around "passing 'T' chooses 'int' over 'unsigned int'" warnings when T
 // is an enum type:
@@ -19,32 +18,6 @@
 
 CPPUNIT_NS_BEGIN
 
-namespace impl {
-
-// work around to handle C++11 enum class correctly. We need an own conversion to std::string
-// as there is no implicit coversion to int for enum class.
-
-template<typename T>
-typename std::enable_if<!std::is_enum<T>::value, std::string>::type toStringImpl(const T& x)
-{
-    OStringStream ost;
-    ost << x;
-
-    return ost.str();
-}
-
-template<typename T>
-typename std::enable_if<std::is_enum<T>::value, std::string>::type toStringImpl(const T& x)
-{
-    OStringStream ost;
-    ost << static_cast<typename std::underlying_type<T>::type>(x);
-
-    return ost.str();
-}
-
-
-}
-
 /*! \brief Traits used by CPPUNIT_ASSERT* macros.
  *
  * Here is an example of specialising these traits: 
@@ -98,7 +71,7 @@ struct assertion_traits
 
     static std::string toString( const T& x )
     {
-        return impl::toStringImpl(x);
+        return CPPUNIT_NS::StringHelper::toString(x);
     }
 };
 
diff --git a/include/cppunit/tools/Makefile.am b/include/cppunit/tools/Makefile.am
index 8ce0808..79f35b6 100644
--- a/include/cppunit/tools/Makefile.am
+++ b/include/cppunit/tools/Makefile.am
@@ -3,5 +3,6 @@ libcppunitincludedir = $(includedir)/cppunit/tools
 libcppunitinclude_HEADERS = \
 	Algorithm.h		\
 	StringTools.h \
+	StringHelper.h \
 	XmlElement.h \
-	XmlDocument.h
\ No newline at end of file
+	XmlDocument.h
diff --git a/include/cppunit/tools/StringHelper.h b/include/cppunit/tools/StringHelper.h
new file mode 100644
index 0000000..3301045
--- /dev/null
+++ b/include/cppunit/tools/StringHelper.h
@@ -0,0 +1,45 @@
+#ifndef CPPUNIT_TOOLS_STRINGHELPER_H
+#define CPPUNIT_TOOLS_STRINGHELPER_H
+
+#include <cppunit/Portability.h>
+#include <cppunit/portability/Stream.h>
+#include <string>
+#include <type_traits>
+
+
+CPPUNIT_NS_BEGIN
+
+
+/*! \brief Methods for converting values to strings. Replaces CPPUNIT_NS::StringTools::toString
+ */
+namespace StringHelper
+{
+
+// work around to handle C++11 enum class correctly. We need an own conversion to std::string
+// as there is no implicit coversion to int for enum class.
+
+template<typename T>
+typename std::enable_if<!std::is_enum<T>::value, std::string>::type toString(const T& x)
+{
+    OStringStream ost;
+    ost << x;
+
+    return ost.str();
+}
+
+template<typename T>
+typename std::enable_if<std::is_enum<T>::value, std::string>::type toString(const T& x)
+{
+    OStringStream ost;
+    ost << static_cast<typename std::underlying_type<T>::type>(x);
+
+    return ost.str();
+}
+
+}
+
+
+CPPUNIT_NS_END
+
+#endif  // CPPUNIT_TOOLS_STRINGHELPER_H
+


More information about the Libreoffice-commits mailing list