From 35b30af2ee3f561ab170f2746df540498413c81c Mon Sep 17 00:00:00 2001 From: Tomas Pecka <peckato1@fit.cvut.cz> Date: Sat, 23 Feb 2019 12:03:35 +0100 Subject: [PATCH] Unit tests: CPPUNIT -> Catch2: common --- .../test-src/common/CreateUniqueTest.cpp | 20 +-- .../test-src/common/CreateUniqueTest.h | 18 -- alib2common/test-src/core/ComponentsTest.cpp | 53 +++--- alib2common/test-src/core/ComponentsTest.h | 20 --- alib2common/test-src/main.cpp | 168 +----------------- alib2common/test-src/object/ObjectTest.cpp | 74 ++++---- alib2common/test-src/object/ObjectTest.h | 20 --- 7 files changed, 60 insertions(+), 313 deletions(-) delete mode 100644 alib2common/test-src/common/CreateUniqueTest.h delete mode 100644 alib2common/test-src/core/ComponentsTest.h delete mode 100644 alib2common/test-src/object/ObjectTest.h diff --git a/alib2common/test-src/common/CreateUniqueTest.cpp b/alib2common/test-src/common/CreateUniqueTest.cpp index 230b3d4688..3fc269231a 100644 --- a/alib2common/test-src/common/CreateUniqueTest.cpp +++ b/alib2common/test-src/common/CreateUniqueTest.cpp @@ -1,22 +1,14 @@ -#include "CreateUniqueTest.h" +#include <catch2/catch.hpp> #include <common/createUnique.hpp> -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( CreateUniqueTest, "common" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( CreateUniqueTest ); -void CreateUniqueTest::setUp ( ) { -} - -void CreateUniqueTest::tearDown ( ) { -} - -void CreateUniqueTest::testCreateUnique ( ) { +TEST_CASE ( "Create Unique", "[unit][common]" ) { ext::set < int > alphabet1 { 0, 1, 2 }; ext::set < int > alphabet2 { 3, 4, 5 }; - CPPUNIT_ASSERT ( common::createUnique ( 0, alphabet1, alphabet2 ) == 6 ); - CPPUNIT_ASSERT ( common::createUnique ( 0, alphabet1 ) == 3 ); - CPPUNIT_ASSERT ( common::createUnique ( 0, alphabet2 ) == 0 ); - CPPUNIT_ASSERT ( common::createUnique ( 0 ) == 0 ); + CHECK ( common::createUnique ( 0, alphabet1, alphabet2 ) == 6 ); + CHECK ( common::createUnique ( 0, alphabet1 ) == 3 ); + CHECK ( common::createUnique ( 0, alphabet2 ) == 0 ); + CHECK ( common::createUnique ( 0 ) == 0 ); } diff --git a/alib2common/test-src/common/CreateUniqueTest.h b/alib2common/test-src/common/CreateUniqueTest.h deleted file mode 100644 index 52fcc0d96f..0000000000 --- a/alib2common/test-src/common/CreateUniqueTest.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef CREATE_UNIQUE_TEST_H_ -#define CREATE_UNIQUE_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class CreateUniqueTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( CreateUniqueTest ); - CPPUNIT_TEST ( testCreateUnique ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testCreateUnique ( ); -}; - -#endif // CREATE_UNIQUE_TEST_H_ diff --git a/alib2common/test-src/core/ComponentsTest.cpp b/alib2common/test-src/core/ComponentsTest.cpp index cadcbe0ac7..34ac4c8708 100644 --- a/alib2common/test-src/core/ComponentsTest.cpp +++ b/alib2common/test-src/core/ComponentsTest.cpp @@ -1,12 +1,10 @@ -#include "ComponentsTest.h" +#include <catch2/catch.hpp> + #include <core/components.hpp> #include <alib/set> #include <alib/linear_set> -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ComponentsTest, "alphabet" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( ComponentsTest ); - struct GeneralAlphabet { }; @@ -108,39 +106,32 @@ public: } /* namespace core */ -void ComponentsTest::setUp ( ) { -} - -void ComponentsTest::tearDown ( ) { -} - -void ComponentsTest::testAdd ( ) { - A tmp ( "2" ); - - CPPUNIT_ASSERT_THROW ( tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ), exception::CommonException ); - tmp.accessComponent < GeneralAlphabet > ( ).add ( "1" ); - tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ); - - B tmp2 = B ( ); -} -void ComponentsTest::testRemove ( ) { +TEST_CASE ( "Compontents", "[unit][core]" ) { A tmp ( "2" ); - tmp.accessComponent < GeneralAlphabet > ( ).add ( "1" ); - tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ); - CPPUNIT_ASSERT_THROW ( tmp.accessComponent < GeneralAlphabet > ( ).remove ( "1" ), exception::CommonException ); - CPPUNIT_ASSERT_THROW ( tmp.accessComponent < GeneralAlphabet > ( ).remove ( ext::linear_set < std::string > { "1" } ), exception::CommonException ); - CPPUNIT_ASSERT_NO_THROW ( tmp.accessComponent < GeneralAlphabet > ( ).set ( ext::linear_set < std::string > { "1", "2", "aaa", "3" } ) ); - CPPUNIT_ASSERT_THROW ( tmp.accessComponent < GeneralAlphabet > ( ).set ( ext::linear_set < std::string > { } ), exception::CommonException ); + SECTION ( "Test Add" ) { + CHECK_THROWS_AS ( tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ), exception::CommonException ); + tmp.accessComponent < GeneralAlphabet > ( ).add ( "1" ); + tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ); - CPPUNIT_ASSERT ( tmp.accessComponent < GeneralAlphabet > ( ).get ( ).size ( ) == 4 ); + B tmp2 = B ( ); + } - CPPUNIT_ASSERT_NO_THROW ( tmp.accessComponent < NonlinearAlphabet > ( ).set ( ext::linear_set < std::string > { "1", "3" } ) ); - CPPUNIT_ASSERT_THROW ( tmp.accessComponent < NonlinearAlphabet > ( ).set ( ext::linear_set < std::string > { "1", "4" } ), exception::CommonException ); + SECTION ( "Test Remove" ) { + tmp.accessComponent < GeneralAlphabet > ( ).add ( "1" ); + tmp.accessComponent < NonlinearAlphabet > ( ).add ( "1" ); + REQUIRE_THROWS_AS ( tmp.accessComponent < GeneralAlphabet > ( ).remove ( "1" ), exception::CommonException ); + REQUIRE_THROWS_AS ( tmp.accessComponent < GeneralAlphabet > ( ).remove ( ext::linear_set < std::string > { "1" } ), exception::CommonException ); + REQUIRE_NOTHROW ( tmp.accessComponent < GeneralAlphabet > ( ).set ( ext::linear_set < std::string > { "1", "2", "aaa", "3" } ) ); + REQUIRE_THROWS_AS ( tmp.accessComponent < GeneralAlphabet > ( ).set ( ext::linear_set < std::string > { } ), exception::CommonException ); - tmp.accessComponent < NonlinearAlphabet > ( ).remove ( "1" ); - tmp.accessComponent < GeneralAlphabet > ( ).remove ( "1" ); + REQUIRE ( tmp.accessComponent < GeneralAlphabet > ( ).get ( ).size ( ) == 4 ); + REQUIRE_NOTHROW ( tmp.accessComponent < NonlinearAlphabet > ( ).set ( ext::linear_set < std::string > { "1", "3" } ) ); + REQUIRE_THROWS_AS ( tmp.accessComponent < NonlinearAlphabet > ( ).set ( ext::linear_set < std::string > { "1", "4" } ), exception::CommonException ); + REQUIRE_NOTHROW ( tmp.accessComponent < NonlinearAlphabet > ( ).remove ( "1" ) ); + REQUIRE_NOTHROW ( tmp.accessComponent < GeneralAlphabet > ( ).remove ( "1" ) ); + } } diff --git a/alib2common/test-src/core/ComponentsTest.h b/alib2common/test-src/core/ComponentsTest.h deleted file mode 100644 index eb19334635..0000000000 --- a/alib2common/test-src/core/ComponentsTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef ALPHABET_TEST_H_ -#define ALPHABET_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class ComponentsTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( ComponentsTest ); - CPPUNIT_TEST ( testAdd ); - CPPUNIT_TEST ( testRemove ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testAdd ( ); - void testRemove ( ); -}; - -#endif // ALPHABET_TEST_H_ diff --git a/alib2common/test-src/main.cpp b/alib2common/test-src/main.cpp index adb58324a1..4ed06df1f7 100644 --- a/alib2common/test-src/main.cpp +++ b/alib2common/test-src/main.cpp @@ -1,166 +1,2 @@ -#include <version.hpp> - -#include <tclap/CmdLine.h> - -#include <cppunit/CompilerOutputter.h> -#include <cppunit/extensions/TestFactoryRegistry.h> -#include <cppunit/ui/text/TestRunner.h> -#include <cppunit/TestResultCollector.h> -#include <cppunit/TestResult.h> -#include <cppunit/XmlOutputter.h> - -#include <cppunit/Test.h> -#include <cppunit/TestFailure.h> -#include <cppunit/portability/Stream.h> -#include <cppunit/TestListener.h> -#include <cppunit/SourceLine.h> -#include <cppunit/Exception.h> - -#include <exception/CommonException.h> - -CPPUNIT_NS_BEGIN - -class CPPUNIT_API TestProgressListener : public TestListener -{ -public: - TestProgressListener(); - - virtual ~TestProgressListener(); - - void startTest( Test *test ); - - void addFailure( const TestFailure &failure ); - - void endTest( Test *test ); - - int getResult() const; - - void printResults() const; - -private: - TestProgressListener( const TestProgressListener © ); - - void operator =( const TestProgressListener © ); - -private: - int m_Failures; - int m_Tests; - int m_Assertions; - bool m_lastTestFailed; -}; - -TestProgressListener::TestProgressListener() : m_Failures( 0 ), m_Tests(0), m_Assertions(0), m_lastTestFailed( false ) -{ -} - -TestProgressListener::~TestProgressListener() -{ -} - -void TestProgressListener::startTest( Test * test ) -{ - stdCOut() << test->getName() << ":" << "\n"; - stdCOut().flush(); - - m_lastTestFailed = false; - m_Tests++; -} - -void TestProgressListener::addFailure( const TestFailure &failure ) -{ - stdCOut() << (failure.isError() ? "error" : "assertion") << " : " << failure.failedTestName() << " : " << failure.sourceLine().lineNumber() << "\n"; - stdCOut() << "Exception " << failure.thrownException()->message().details(); - - m_lastTestFailed = true; - if(failure.isError()) m_Failures++; else m_Assertions++; -} - -void TestProgressListener::endTest( Test * test) -{ - stdCOut() << "Result (" << test->getName() << ")"; - stdCOut().flush(); - - if ( !m_lastTestFailed ) - stdCOut() << " : OK"; - else - stdCOut() << " : Fail"; - stdCOut() << "\n\n"; -} - -int TestProgressListener::getResult() const { - return m_Failures + m_Assertions; -} - -void TestProgressListener::printResults() const { - stdCOut() << "Overal result: Tests: " << m_Tests << " Assertions: " << m_Assertions << " Failures: " << m_Failures << "\n"; -} - -CPPUNIT_NS_END - -int main(int argc, char* argv[]) { - try { - TCLAP::CmdLine cmd("Main test binary.", ' ', ALIB_VERSION_INFO); - - TCLAP::MultiArg<std::string> testPathSegments("p", "path", "test path", false, "string" ); - cmd.add( testPathSegments ); - - cmd.parse(argc, argv); - - CppUnit::TestResult controller; - - CppUnit::TestResultCollector result; - controller.addListener( &result ); - - CppUnit::TestProgressListener progressListener; - controller.addListener( &progressListener ); - - CppUnit::Test *suite = NULL; - std::string testPath = ""; - if(testPathSegments.getValue().size() == 0) { - // Get the top level suite from the registry - suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); - } else if(testPathSegments.getValue().size() == 1) { - suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest(); - } else { - suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest(); - bool first = true; - for(const std::string& path : testPathSegments.getValue()) { - if(first) { - first = false; - continue; - } - testPath += path + "/"; - } - testPath.pop_back(); - } - - // Adds the test to the list of test to run - CppUnit::TextUi::TestRunner runner; - runner.addTest( suite ); - - // Change the default outputter to a compiler error format outputter - runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) ); - // Run the tests. - runner.run( controller, testPath ); - - progressListener.printResults(); - - std::ofstream xmlFileResults("CppUnitTestResults.xml"); - CppUnit::XmlOutputter xmlOut(&result, xmlFileResults); - xmlOut.write(); - - return progressListener.getResult(); - } catch(const exception::CommonException& exception) { - std::cerr << exception.getCause() << std::endl; - return 1; - } catch(const TCLAP::ArgException& exception) { - std::cerr << exception.error() << std::endl; - return 2; - } catch (const std::exception& exception) { - std::cerr << "Exception caught: " << exception.what() << std::endl; - return 3; - } catch(...) { - std::cerr << "Unknown exception caught." << std::endl; - return 127; - } -} +#define CATCH_CONFIG_MAIN +#include <catch2/catch.hpp> diff --git a/alib2common/test-src/object/ObjectTest.cpp b/alib2common/test-src/object/ObjectTest.cpp index 5b9c4af500..517d35082c 100644 --- a/alib2common/test-src/object/ObjectTest.cpp +++ b/alib2common/test-src/object/ObjectTest.cpp @@ -1,60 +1,46 @@ +#include <catch2/catch.hpp> + #include <alib/list> #include <alib/type_traits> #include <alib/set> -#include "ObjectTest.h" - #include "object/Object.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ObjectTest, "object" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( ObjectTest ); - -void ObjectTest::setUp ( ) { -} - -void ObjectTest::tearDown ( ) { -} - -void ObjectTest::testProperties ( ) { - CPPUNIT_ASSERT ( std::is_nothrow_move_constructible < object::Object >::value ); - CPPUNIT_ASSERT ( std::is_move_constructible < object::Object >::value && std::is_move_assignable < object::Object >::value ); - - object::Object tmp1 { object::AnyObject < unsigned > ( 1 ) }; - object::Object tmp2 { object::AnyObject < unsigned > ( 2 ) }; - - std::swap ( tmp1, tmp2 ); - - CPPUNIT_ASSERT ( tmp1 == object::Object ( object::AnyObject < unsigned > ( 2 ) ) ); - CPPUNIT_ASSERT ( tmp2 == object::Object ( object::AnyObject < unsigned > ( 1 ) ) ); - -} - -void ObjectTest::testConstruction ( ) { - object::Object tmp1 ( 1 ); - object::Object tmp2 ( ext::variant < int, std::string > ( 1 ) ); - object::Object tmp3 ( ext::variant < int, ext::variant < std::string, int > > ( ext::variant < std::string, int > ( 1 ) ) ); +TEST_CASE ( "Objects", "[unit][object]" ) { - CPPUNIT_ASSERT ( tmp1 == tmp2 ); - CPPUNIT_ASSERT ( tmp1 == tmp3 ); + SECTION ( "Test Properties" ) { + REQUIRE ( std::is_nothrow_move_constructible < object::Object >::value ); + REQUIRE ( ( std::is_move_constructible < object::Object >::value && std::is_move_assignable < object::Object >::value ) ); - object::Object tmp4 = object::Object ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ); - object::Object tmp5 ( ext::set < int > { } ); - object::Object tmp6 ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ); + object::Object tmp1 { object::AnyObject < unsigned > ( 1 ) }; + object::Object tmp2 { object::AnyObject < unsigned > ( 2 ) }; - std::cout << tmp6 << std::endl; + std::swap ( tmp1, tmp2 ); - CPPUNIT_ASSERT ( tmp4 == tmp5 ); - CPPUNIT_ASSERT ( tmp4 == tmp6 ); + CHECK ( tmp1 == object::Object ( object::AnyObject < unsigned > ( 2 ) ) ); + CHECK ( tmp2 == object::Object ( object::AnyObject < unsigned > ( 1 ) ) ); + } - tmp6 ++; - object::Object tmp7 ( object::AnyObject < ext::set < int > > ( ext::set < int > { }, 1 ) ); + SECTION ( "Test Construction" ) { + object::Object tmp1 ( 1 ); + object::Object tmp2 ( ext::variant < int, std::string > ( 1 ) ); + object::Object tmp3 ( ext::variant < int, ext::variant < std::string, int > > ( ext::variant < std::string, int > ( 1 ) ) ); + CHECK ( tmp1 == tmp2 ); + CHECK ( tmp1 == tmp3 ); - CPPUNIT_ASSERT ( tmp6 == tmp7 ); + object::Object tmp4 = object::Object ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ); + object::Object tmp5 ( ext::set < int > { } ); + object::Object tmp6 ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ); + CHECK ( tmp4 == tmp5 ); + CHECK ( tmp4 == tmp6 ); - object::Object test = object::Object ( object::AnyObject < object::Object > ( object::Object ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ) ) ); + tmp6 ++; + object::Object tmp7 ( object::AnyObject < ext::set < int > > ( ext::set < int > { }, 1 ) ); + CHECK ( tmp6 == tmp7 ); - std::cout << test << std::endl; - std::cout << tmp4 << std::endl; + object::Object test = object::Object ( object::AnyObject < object::Object > ( object::Object ( object::AnyObject < ext::set < int > > ( ext::set < int > { } ) ) ) ); - CPPUNIT_ASSERT ( test == tmp4 ); + CAPTURE ( test, tmp4 ); + CHECK ( test == tmp4 ); + } } diff --git a/alib2common/test-src/object/ObjectTest.h b/alib2common/test-src/object/ObjectTest.h deleted file mode 100644 index 31cb9efefd..0000000000 --- a/alib2common/test-src/object/ObjectTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef OBJECT_TEST_H_ -#define OBJECT_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class ObjectTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( ObjectTest ); - CPPUNIT_TEST ( testProperties ); - CPPUNIT_TEST ( testConstruction ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testProperties ( ); - void testConstruction ( ); -}; - -#endif // OBJECT_TEST_H_ -- GitLab