From 83d15d3cc3f2bef74c047e1a727d2927c5c8f81e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste CAYROU Date: Thu, 2 Aug 2012 10:11:03 +0200 Subject: [PATCH] Unit tests can now use nested try blocks --- include/GException.hpp | 6 ++++++ include/GTestSuite.hpp | 1 + src/test/GException_test.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/test/GTestSuite.cpp | 37 +++++++++++++++++++++++++++++-------- src/test/GTestSuites.cpp | 2 +- src/test/Makefile.am | 3 ++- 6 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 src/test/GException_test.cpp diff --git a/include/GException.hpp b/include/GException.hpp index a38293d..6f7b19b 100644 --- a/include/GException.hpp +++ b/include/GException.hpp @@ -660,6 +660,12 @@ public: std::string message = ""); }; + // Test exceptions + class test_nested_try_error : public GExceptionHandler { + public: + test_nested_try_error(std::string origin, std::string message = ""); + }; + }; #endif /* GEXCEPTION_HPP */ diff --git a/include/GTestSuite.hpp b/include/GTestSuite.hpp index bf4d5bf..94144e6 100644 --- a/include/GTestSuite.hpp +++ b/include/GTestSuite.hpp @@ -88,6 +88,7 @@ class GTestSuite{ // Private members private: std::vector m_tests; //!< Nomber of tests + std::vector m_stack_try; //!< Stack for nested try blocks int m_failures; //!< Nomber of failures int m_errors; //!< Nomber of errors std::string m_name; //!< Name of the test suite diff --git a/src/test/GException_test.cpp b/src/test/GException_test.cpp new file mode 100644 index 0000000..345a9e9 --- /dev/null +++ b/src/test/GException_test.cpp @@ -0,0 +1,47 @@ +/*************************************************************************** + * GException_test.cpp - Unit Test exception handlers * + * ----------------------------------------------------------------------- * + * copyright (C) 2012 by Jean-Baptiste Cayrou * + * ----------------------------------------------------------------------- * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + * * + ***************************************************************************/ +/** + * @file GException_test.cpp + * @brief Implement exceptions for unit test module + * @author Jean-Baptiste Cayrou + */ + +/* __ Includes ___________________________________________________________ */ +#ifdef HAVE_CONFIG_H +#include +#endif +#include "GException.hpp" +#include "GTools.hpp" + + +/***********************************************************************//** + * @brief Test nested try error + * @param[in] message Optional error message. + ***************************************************************************/ +GException::test_nested_try_error::test_nested_try_error(std::string origin, std::string message) +{ + // Set origin and message + m_origin = origin; + m_message = "Nested try error ("+message+")"; + + // Return + return; +} diff --git a/src/test/GTestSuite.cpp b/src/test/GTestSuite.cpp index 8a75ef5..aafc26a 100644 --- a/src/test/GTestSuite.cpp +++ b/src/test/GTestSuite.cpp @@ -34,6 +34,8 @@ /* __ Method name definitions ____________________________________________ */ #define G_OP_ACCESS "GTestSuite::operator[](int&)" +#define G_TRY_SUCCESS "GTestSuite::test_try_success()" +#define G_TRY_FAILURE "GTestSuite::test_try_failure(std::string&,std::string&)" /* __ Macros _____________________________________________________________ */ @@ -299,7 +301,7 @@ void GTestSuite::test_try(const std::string& name) GTestCase* testcase = new GTestCase(GTestCase::ERROR_TEST,name); //Add test case to container - m_tests.push_back(testcase); + m_stack_try.push_back(testcase); // Return return; @@ -326,6 +328,16 @@ void GTestSuite::test_try(const std::string& name) * ***************************************************************************/ void GTestSuite::test_try_success(){ + // If the stack is empty + if(m_stack_try.size()==0) + throw GException::test_nested_try_error(G_TRY_SUCCESS, "Error : test_try_success() without test_try()"); + + // Add the test in the container + m_tests.push_back(m_stack_try.back()); + + // Delete the test case of the stack + m_stack_try.pop_back(); + // The try block test is ok m_log<print_result(); @@ -358,21 +370,30 @@ void GTestSuite::test_try_success(){ * ***************************************************************************/ void GTestSuite::test_try_failure(const std::string& message, const std::string& type){ + // If the stack is empty + if(m_stack_try.size()==0) + throw GException::test_nested_try_error(G_TRY_FAILURE,"test_try_success() call without test_try()"); + // Test is not ok - m_tests.back()->m_passed=false; - + m_stack_try.back()->m_passed=false; + // Increment the number of errors m_errors++; - + // Set a message - m_tests.back()->message(message); - + m_stack_try.back()->message(message); + // Set a type of message - m_tests.back()->message_type(type); + m_stack_try.back()->message_type(type); + + // Add the test in the container + m_tests.push_back(m_stack_try.back()); + // Delete the test case of the stack + m_stack_try.pop_back(); + // Show the result ( ".","F" or, "E") m_log<print_result(); - //Return return; } diff --git a/src/test/GTestSuites.cpp b/src/test/GTestSuites.cpp index 517d308..807e7bc 100644 --- a/src/test/GTestSuites.cpp +++ b/src/test/GTestSuites.cpp @@ -367,7 +367,7 @@ void GTestSuites::write(GXml& xml) element_testsuite->attribute("skipped",""); // not used element_testsuite->attribute("tests",str(testsuite->tests())); element_testsuite->attribute("time","0"); // not used - element_testsuite->attribute("timestamp",str(testsuite->timestamp())); + element_testsuite->attribute("timestamp",str(testsuite->timestamp())); //FIXME timestamp does not work //Loop over all test cases contains in the test suite for (int j=0; jtests(); ++j) diff --git a/src/test/Makefile.am b/src/test/Makefile.am index 7eff7d5..8c8f627 100644 --- a/src/test/Makefile.am +++ b/src/test/Makefile.am @@ -8,7 +8,8 @@ INCLUDES = -I$(top_srcdir)/include \ # Define sources for this directory sources = GTestCase.cpp \ GTestSuite.cpp \ - GTestSuites.cpp + GTestSuites.cpp \ + GException_test.cpp # Build libtool library noinst_LTLIBRARIES = libtest.la -- libgit2 0.21.4