Commit 83d15d3cc3f2bef74c047e1a727d2927c5c8f81e
1 parent
60dc7bc1
Unit tests can now use nested try blocks
Showing
6 changed files
with
86 additions
and
10 deletions
include/GException.hpp
| ... | ... | @@ -660,6 +660,12 @@ public: |
| 660 | 660 | std::string message = ""); |
| 661 | 661 | }; |
| 662 | 662 | |
| 663 | + // Test exceptions | |
| 664 | + class test_nested_try_error : public GExceptionHandler { | |
| 665 | + public: | |
| 666 | + test_nested_try_error(std::string origin, std::string message = ""); | |
| 667 | + }; | |
| 668 | + | |
| 663 | 669 | }; |
| 664 | 670 | |
| 665 | 671 | #endif /* GEXCEPTION_HPP */ | ... | ... |
include/GTestSuite.hpp
| ... | ... | @@ -88,6 +88,7 @@ class GTestSuite{ |
| 88 | 88 | // Private members |
| 89 | 89 | private: |
| 90 | 90 | std::vector<GTestCase*> m_tests; //!< Nomber of tests |
| 91 | + std::vector<GTestCase*> m_stack_try; //!< Stack for nested try blocks | |
| 91 | 92 | int m_failures; //!< Nomber of failures |
| 92 | 93 | int m_errors; //!< Nomber of errors |
| 93 | 94 | std::string m_name; //!< Name of the test suite | ... | ... |
src/test/GException_test.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * GException_test.cpp - Unit Test exception handlers * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2012 by Jean-Baptiste Cayrou * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file GException_test.cpp | |
| 23 | + * @brief Implement exceptions for unit test module | |
| 24 | + * @author Jean-Baptiste Cayrou | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#ifdef HAVE_CONFIG_H | |
| 29 | +#include <config.h> | |
| 30 | +#endif | |
| 31 | +#include "GException.hpp" | |
| 32 | +#include "GTools.hpp" | |
| 33 | + | |
| 34 | + | |
| 35 | +/***********************************************************************//** | |
| 36 | + * @brief Test nested try error | |
| 37 | + * @param[in] message Optional error message. | |
| 38 | + ***************************************************************************/ | |
| 39 | +GException::test_nested_try_error::test_nested_try_error(std::string origin, std::string message) | |
| 40 | +{ | |
| 41 | + // Set origin and message | |
| 42 | + m_origin = origin; | |
| 43 | + m_message = "Nested try error ("+message+")"; | |
| 44 | + | |
| 45 | + // Return | |
| 46 | + return; | |
| 47 | +} | ... | ... |
src/test/GTestSuite.cpp
| ... | ... | @@ -34,6 +34,8 @@ |
| 34 | 34 | |
| 35 | 35 | /* __ Method name definitions ____________________________________________ */ |
| 36 | 36 | #define G_OP_ACCESS "GTestSuite::operator[](int&)" |
| 37 | +#define G_TRY_SUCCESS "GTestSuite::test_try_success()" | |
| 38 | +#define G_TRY_FAILURE "GTestSuite::test_try_failure(std::string&,std::string&)" | |
| 37 | 39 | |
| 38 | 40 | /* __ Macros _____________________________________________________________ */ |
| 39 | 41 | |
| ... | ... | @@ -299,7 +301,7 @@ void GTestSuite::test_try(const std::string& name) |
| 299 | 301 | GTestCase* testcase = new GTestCase(GTestCase::ERROR_TEST,name); |
| 300 | 302 | |
| 301 | 303 | //Add test case to container |
| 302 | - m_tests.push_back(testcase); | |
| 304 | + m_stack_try.push_back(testcase); | |
| 303 | 305 | |
| 304 | 306 | // Return |
| 305 | 307 | return; |
| ... | ... | @@ -326,6 +328,16 @@ void GTestSuite::test_try(const std::string& name) |
| 326 | 328 | * |
| 327 | 329 | ***************************************************************************/ |
| 328 | 330 | void GTestSuite::test_try_success(){ |
| 331 | + // If the stack is empty | |
| 332 | + if(m_stack_try.size()==0) | |
| 333 | + throw GException::test_nested_try_error(G_TRY_SUCCESS, "Error : test_try_success() without test_try()"); | |
| 334 | + | |
| 335 | + // Add the test in the container | |
| 336 | + m_tests.push_back(m_stack_try.back()); | |
| 337 | + | |
| 338 | + // Delete the test case of the stack | |
| 339 | + m_stack_try.pop_back(); | |
| 340 | + | |
| 329 | 341 | // The try block test is ok |
| 330 | 342 | m_log<<m_tests.back()->print_result(); |
| 331 | 343 | |
| ... | ... | @@ -358,21 +370,30 @@ void GTestSuite::test_try_success(){ |
| 358 | 370 | * |
| 359 | 371 | ***************************************************************************/ |
| 360 | 372 | void GTestSuite::test_try_failure(const std::string& message, const std::string& type){ |
| 373 | + // If the stack is empty | |
| 374 | + if(m_stack_try.size()==0) | |
| 375 | + throw GException::test_nested_try_error(G_TRY_FAILURE,"test_try_success() call without test_try()"); | |
| 376 | + | |
| 361 | 377 | // Test is not ok |
| 362 | - m_tests.back()->m_passed=false; | |
| 363 | - | |
| 378 | + m_stack_try.back()->m_passed=false; | |
| 379 | + | |
| 364 | 380 | // Increment the number of errors |
| 365 | 381 | m_errors++; |
| 366 | - | |
| 382 | + | |
| 367 | 383 | // Set a message |
| 368 | - m_tests.back()->message(message); | |
| 369 | - | |
| 384 | + m_stack_try.back()->message(message); | |
| 385 | + | |
| 370 | 386 | // Set a type of message |
| 371 | - m_tests.back()->message_type(type); | |
| 387 | + m_stack_try.back()->message_type(type); | |
| 388 | + | |
| 389 | + // Add the test in the container | |
| 390 | + m_tests.push_back(m_stack_try.back()); | |
| 372 | 391 | |
| 392 | + // Delete the test case of the stack | |
| 393 | + m_stack_try.pop_back(); | |
| 394 | + | |
| 373 | 395 | // Show the result ( ".","F" or, "E") |
| 374 | 396 | m_log<<m_tests.back()->print_result(); |
| 375 | - | |
| 376 | 397 | //Return |
| 377 | 398 | return; |
| 378 | 399 | } | ... | ... |
src/test/GTestSuites.cpp
| ... | ... | @@ -367,7 +367,7 @@ void GTestSuites::write(GXml& xml) |
| 367 | 367 | element_testsuite->attribute("skipped",""); // not used |
| 368 | 368 | element_testsuite->attribute("tests",str(testsuite->tests())); |
| 369 | 369 | element_testsuite->attribute("time","0"); // not used |
| 370 | - element_testsuite->attribute("timestamp",str(testsuite->timestamp())); | |
| 370 | + element_testsuite->attribute("timestamp",str(testsuite->timestamp())); //FIXME timestamp does not work | |
| 371 | 371 | |
| 372 | 372 | //Loop over all test cases contains in the test suite |
| 373 | 373 | for (int j=0; j<testsuite->tests(); ++j) | ... | ... |
src/test/Makefile.am
| ... | ... | @@ -8,7 +8,8 @@ INCLUDES = -I$(top_srcdir)/include \ |
| 8 | 8 | # Define sources for this directory |
| 9 | 9 | sources = GTestCase.cpp \ |
| 10 | 10 | GTestSuite.cpp \ |
| 11 | - GTestSuites.cpp | |
| 11 | + GTestSuites.cpp \ | |
| 12 | + GException_test.cpp | |
| 12 | 13 | |
| 13 | 14 | # Build libtool library |
| 14 | 15 | noinst_LTLIBRARIES = libtest.la | ... | ... |