setuptest.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # @file setuptest.py
00004 # @brief CppUnit test environment setup script
00005 # @date $Date: 2008-02-29 04:50:54 $
00006 # @author Norkai Ando <n-ando@aist.go.jp>
00007 #
00008 # Copyright (C) 2006
00009 #     Noriaki Ando
00010 #     Task-intelligence Research Group,
00011 #     Intelligent Systems Research Institute,
00012 #     National Institute of
00013 #         Advanced Industrial Science and Technology (AIST), Japan
00014 #     All rights reserved.
00015 #
00016 # $Id: setuptest.py 775 2008-07-28 16:14:45Z n-ando $
00017 #
00018 # [usage]
00019 # setuptest.py [class_name]
00020 #
00021 # 1. make test class file and Makefile.am
00022 # > setuptest.py
00023 # " Hoge/Makefile.am " was generated.
00024 # " Hoge/HogeTests.cpp " was generated.
00025 #
00026 # 2. add Makefile entry into the configure.ac
00027 # > vi configure.ac
00028 #------------------------------------------------------------
00029 # AC_OUTPUT([Makefile
00030 #            _test_dir_/Hoge/Makefile <- add this entry
00031 #           ])
00032 #------------------------------------------------------------
00033 # 2.5 add dubsir entry to parent dir's Makefile.am
00034 #
00035 # 3. autoreconf and configure again
00036 # autoreconf generate Makefile.in
00037 # configure generate Makefile
00038 #
00039 # 4. buid test
00040 # > cd  _test_dir_/Hoge
00041 # > make
00042 # > ./HogeTests
00043 #
00044 # done
00045 
00046 import sys
00047 import os
00048 import yat
00049 
00050 makefile_am = """# -*- Makefile -*-
00051 #------------------------------------------------------------
00052 # @file   Makefile.am
00053 # @brief  Makefile.am for [class_name] unit test
00054 # @date   [dollar]Date[dollar]
00055 
00056 # @author Noriaki Ando <n-ando@aist.go.jp>
00057 #
00058 # [dollar]Id[dollar]
00059 
00060 #
00061 #------------------------------------------------------------
00062 
00063 #
00064 # [dollar]Log[dollar]
00065 
00066 #
00067 
00068 
00069 AUTOMAKE_OPTIONS = 1.9
00070 
00071 AM_CPPFLAGS= -I.                  \\
00072         -I$(includedir)           \\
00073         -I$(top_builddir)
00074 
00075 AM_LDFLAGS= -L.                   \\
00076         -L$(top_builddir)
00077 
00078 
00079 noinst_PROGRAMS = [class_name]Tests
00080 
00081 [class_name]Tests_SOURCES = ../TestRunner.cpp [class_name]Tests.cpp
00082 [class_name]Tests_LDFLAGS = -L$(libdir)
00083 [class_name]Tests_LDADD   = -lcppunit
00084 
00085 TEST_SRC = [dollar]([class_name]Tests_SOURCES)
00086 TEST_H   = 
00087 
00088 # all
00089 all: do-test
00090 
00091 # do tests
00092 do-test:
00093         ./[class_name]Tests
00094 
00095 # clean-up
00096 clean-local:
00097         rm -f *.o *.Po *.gch *.la
00098         rm -f *~ *core
00099         rm -f *.xml
00100         rm -f Makefile.old
00101         rm -f *.vcproj
00102         rm -rf Release Debug
00103 
00104 #------------------------------------------------------------
00105 # vcproj file build rules
00106 #------------------------------------------------------------
00107 win32_builddir = .
00108 
00109 vcproj: [class_name]_vc8.vcproj [class_name]_vc9.vcproj
00110 
00111 [class_name]_vc8.vcproj:
00112         $(top_builddir)/build/vcprojtool.py vcproj \\
00113                 --projectname [class_name]Test \\
00114                 --type EXE \\
00115                 --vcversion "8.00" \\
00116                 --version $(COIL_VERSION) \\
00117                 --out $(win32_builddir)/[class_name]_vc8.vcproj \\
00118                 --yaml ../coil_test.vcproj.yaml \\
00119                 --source $(TEST_SRC)
00120 #               --header $(TEST_H)
00121         qkc -sm $(win32_builddir)/[class_name]_vc8.vcproj
00122 
00123 [class_name]_vc9.vcproj:
00124         $(top_builddir)/build/vcprojtool.py vcproj \\
00125                 --projectname [class_name]Test \\
00126                 --type EXE \\
00127                 --vcversion "9.00" \\
00128                 --version $(COIL_VERSION) \\
00129                 --out $(win32_builddir)/[class_name]_vc9.vcproj \\
00130                 --yaml ../coil_test.vcproj.yaml \\
00131                 --source $(TEST_SRC)
00132 #               --header $(TEST_H)
00133         qkc -sm $(win32_builddir)/[class_name]_vc9.vcproj
00134 
00135 
00136 """
00137 
00138 test_cpp = """// -*- C++ -*-
00139 /*!
00140  * @file   [class_name]Tests.cpp
00141  * @brief  [class_name] test class
00142  * @date   [dollar]Date[dollar]
00143  
00144  * @author Noriaki Ando <n-ando@aist.go.jp>
00145  *
00146  * [dollar]Id[dollar]
00147  
00148  *
00149  */
00150 
00151 /*
00152  * [dollar]Log[dollar]
00153 
00154  *
00155  */
00156 
00157 #ifndef [class_name]_cpp
00158 #define [class_name]_cpp
00159 
00160 #include <cppunit/ui/text/TestRunner.h>
00161 #include <cppunit/TextOutputter.h>
00162 #include <cppunit/extensions/TestFactoryRegistry.h>
00163 #include <cppunit/extensions/HelperMacros.h>
00164 #include <cppunit/TestAssert.h>
00165 
00166 /*!
00167  * @class [class_name]Tests class
00168  * @brief [class_name] test
00169  */
00170 namespace [class_name]
00171 
00172 {
00173   class [class_name]Tests
00174    : public CppUnit::TestFixture
00175   {
00176     CPPUNIT_TEST_SUITE([class_name]Tests);
00177     CPPUNIT_TEST(test_case0);
00178     CPPUNIT_TEST_SUITE_END();
00179   
00180   private:
00181   
00182   public:
00183   
00184     /*!
00185      * @brief Constructor
00186      */
00187     [class_name]Tests()
00188     {
00189     }
00190     
00191     /*!
00192      * @brief Destructor
00193      */
00194     ~[class_name]Tests()
00195     {
00196     }
00197   
00198     /*!
00199      * @brief Test initialization
00200      */
00201     virtual void setUp()
00202     {
00203     }
00204     
00205     /*!
00206      * @brief Test finalization
00207      */
00208     virtual void tearDown()
00209     { 
00210     }
00211   
00212     /* test case */
00213     void test_case0()
00214     {
00215     }
00216   };
00217 }; // namespace [class_name]
00218 
00219 
00220 /*
00221  * Register test suite
00222  */
00223 CPPUNIT_TEST_SUITE_REGISTRATION([class_name]::[class_name]Tests);
00224 
00225 #ifdef LOCAL_MAIN
00226 int main(int argc, char* argv[])
00227 {
00228     CppUnit::TextUi::TestRunner runner;
00229     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00230     CppUnit::Outputter* outputter = 
00231       new CppUnit::TextOutputter(&runner.result(), std::cout);
00232     runner.setOutputter(outputter);
00233     bool retcode = runner.run();
00234     return !retcode;
00235 }
00236 #endif // MAIN
00237 #endif // [class_name]_cpp
00238 """
00239 
00240 
00241 class test_dict:
00242     def __init__(self, classname):
00243         self.data = {}
00244         self.data["dollar"] = "$"
00245         self.data["class_name"] = classname
00246         self.data["makefile"]   = classname + "/Makefile.am"
00247         self.data["testcpp"]    = classname + "/" + classname + "Tests.cpp"
00248         return
00249     def get_dict(self):
00250         return self.data
00251     
00252 
00253 class test_gen:
00254     def __init__(self, data):
00255         self.data = data
00256         return
00257     
00258     def gen(self, fname, temp_txt, data):
00259         f = file(fname, "w")
00260         t = yat.Template(temp_txt)
00261         #t.parse(temp_txt)
00262         text=t.generate(data)
00263         f.write(text)
00264         f.close()
00265         print "\"", fname, "\"" " was generated."
00266         return
00267 
00268     def gen_all(self):
00269         self.write_makefile()
00270         self.write_testcpp()
00271         return
00272 
00273     def write_makefile(self):
00274         self.gen(self.data["makefile"], makefile_am, self.data)
00275         return
00276 
00277     def write_testcpp(self):
00278         self.gen(self.data["testcpp"], test_cpp, self.data)
00279         return
00280 
00281 
00282 if len(sys.argv) < 2:
00283     sys.exit(1)
00284 
00285 class_name = sys.argv[1]
00286 try:
00287     os.mkdir(class_name, 0755)
00288 except:
00289     print "Directory \"" + class_name + "\" already exists."
00290     sys.exit(1)
00291 
00292 data = test_dict(class_name)
00293 gen  = test_gen(data.get_dict())
00294 gen.gen_all()


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:39