diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2011-07-04 22:38:03 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2011-07-04 22:38:03 +0000 |
commit | dadc34655c3ab961b0b0b94a10eaaba710f0b5e8 (patch) | |
tree | 99e72842fe687baea16376a147619b6048d7e441 /developer-doc/phb/test-examples.docbook | |
download | kmymoney-dadc34655c3ab961b0b0b94a10eaaba710f0b5e8.tar.gz kmymoney-dadc34655c3ab961b0b0b94a10eaaba710f0b5e8.zip |
Added kmymoney
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kmymoney@1239792 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'developer-doc/phb/test-examples.docbook')
-rw-r--r-- | developer-doc/phb/test-examples.docbook | 231 |
1 files changed, 231 insertions, 0 deletions
diff --git a/developer-doc/phb/test-examples.docbook b/developer-doc/phb/test-examples.docbook new file mode 100644 index 0000000..43f9d7f --- /dev/null +++ b/developer-doc/phb/test-examples.docbook @@ -0,0 +1,231 @@ +<appendix id="test-examples"> +<title>Unit Test Examples</title> +<para> +This appendix contains an example of a +<link linkend="test-header-example">unit test header file listing</link> and a +<link linkend="test-source-example">unit test source file listing</link> as well +as a +<link linkend="test-container-example">unit test container source file listing</link>. +</para> + +<section id="test-header-example"> +<title>Unit Test Header File Example</title> +<screen> + +/*************************************************************************** + mymoneyexceptiontest.h + ------------------- + copyright : (C) 2002 by Thomas Baumgart + email : ipwizard@users.sourceforge.net + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef __MYMONEYEXCEPTIONTEST_H__ +#define __MYMONEYEXCEPTIONTEST_H__ + +#include <cppunit/extensions/HelperMacros.h> + +#define private public +#include "mymoneyutils.h" +#include "mymoneyexception.h" +#undef private + +class MyMoneyExceptionTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(MyMoneyExceptionTest); + CPPUNIT_TEST(testDefaultConstructor); + CPPUNIT_TEST(testConstructor); + CPPUNIT_TEST_SUITE_END(); + +protected: +public: + MyMoneyExceptionTest(); + + + void setUp(); + + void tearDown(); + + void testDefaultConstructor(); + + void testConstructor(); + +}; +#endif +</screen> +</section> + +<section id="test-source-example"> +<title>Unit Test Source File Example</title> + +<screen> + +/*************************************************************************** + mymoneyexceptiontest.cpp + ------------------- + copyright : (C) 2002 by Thomas Baumgart + email : ipwizard@users.sourceforge.net + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "mymoneyexceptiontest.h" + +MyMoneyExceptionTest::MyMoneyExceptionTest() +{ +} + + +void MyMoneyExceptionTest::setUp() +{ +} + +void MyMoneyExceptionTest::tearDown() +{ +} + +void MyMoneyExceptionTest::testDefaultConstructor() +{ + MyMoneyException *e = new MYMONEYEXCEPTION("Message"); + CPPUNIT_ASSERT(e->what() == "Message"); + CPPUNIT_ASSERT(e->line() == __LINE__-2); + CPPUNIT_ASSERT(e->file() == __FILE__); + delete e; +} + +void MyMoneyExceptionTest::testConstructor() +{ + MyMoneyException *e = new MyMoneyException("New message", + "Joe's file", 1234); + CPPUNIT_ASSERT(e->what() == "New message"); + CPPUNIT_ASSERT(e->line() == 1234); + CPPUNIT_ASSERT(e->file() == "Joe's file"); + delete e; +} + +</screen> + +</section> + +<section id="test-container-example"> +<title>Unit Test Container Source File Example</title> +<para> +This test environment also contains a reference to a memory usage checker +which can safely be ignored. It is also contained in the &app; environment +and is a great help if looking for memory leaks. Also notice the usage of +the C++ preprocessor directive <emphasis>#ifdef HAVE_LIBCPPUNIT</emphasis> +to avoid compile errors when CPPUNIT is not installed. +</para> +<para> +Another specialty that is not required by CPPUNIT is the specific +TestProgressListener. It is used here to print the name of the fixture that +is currently running. Since this method is called upon start of each +test case, some logic is necessary to print the name only once. +</para> +<screen> + + +/*************************************************************************** + autotest.cpp + ------------------- + copyright : (C) 2002 by Thomas Baumgart + email : ipwizard@users.sourceforge.net + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "config.h" + +#include <iostream> + +#ifdef HAVE_LIBCPPUNIT + +#include "cppunit/TextTestRunner.h" +#include "cppunit/TextTestResult.h" +#include "cppunit/TestSuite.h" +#include "cppunit/extensions/HelperMacros.h" + +#include "mymoneyexceptiontest.h" + +#include "cppunit/TextTestProgressListener.h" + +class MyProgressListener : public CppUnit::TextTestProgressListener +{ + void startTest(CppUnit::Test *test) { + QString name = test->getName().c_str(); + name = name.mid(2); // cut off first 2 chars + name = name.left(name.find('.')); + if(m_name != name) { + if(m_name != "") + cout << endl; + cout << "Running: " << name << endl; + m_name = name; + } + } +private: + QString m_name; +}; + +#endif + + +int +main(int argc, char** argv) +{ +#ifdef HAVE_LIBCPPUNIT + +#ifdef _CHECK_MEMORY + _CheckMemory_Init(0); +#endif + + CPPUNIT_TEST_SUITE_REGISTRATION(MyMoneyExceptionTest); + + CppUnit::TestFactoryRegistry &registry = + CppUnit::TestFactoryRegistry::getRegistry(); + CppUnit::Test *suite = registry.makeTest(); + + CppUnit::TextTestRunner* runner = new CppUnit::TextTestRunner(); + runner->addTest(suite); + + MyProgressListener progress; + runner->eventManager().addListener(&progress); + runner->run(); + + delete runner; + +#ifdef _CHECK_MEMORY + chkmem.CheckMemoryLeak( true ); + _CheckMemory_End(); +#endif // _CHECK_MEMORY + +#else + std::cout << "libcppunit not installed. no automatic tests available." + << std::endl; +#endif // HAVE_LIBCPPUNIT + return 0; +} +</screen> + +</section> +</appendix> |