wpkg test coverage results

Coverage test results of the Windows Packager by Made to Order Software Corporation.

LCOV - code coverage report
Current view: top level - tests/unittests - unittest_main.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 73 60.3 %
Date: 2013-06-17 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*    unittest_main.cpp
       2             :  *    Copyright (C) 2013  Made to Order Software Corporation
       3             :  *
       4             :  *    This program is free software; you can redistribute it and/or modify
       5             :  *    it under the terms of the GNU General Public License as published by
       6             :  *    the Free Software Foundation; either version 2 of the License, or
       7             :  *    (at your option) any later version.
       8             :  *
       9             :  *    This program is distributed in the hope that it will be useful,
      10             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  *    GNU General Public License for more details.
      13             :  *
      14             :  *    You should have received a copy of the GNU General Public License along
      15             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      16             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      17             :  *
      18             :  *    Authors
      19             :  *    Alexis Wilke   alexis@m2osw.com
      20             :  */
      21             : 
      22             : #include <cppunit/BriefTestProgressListener.h>
      23             : #include <cppunit/CompilerOutputter.h>
      24             : #include <cppunit/extensions/TestFactoryRegistry.h>
      25             : #include <cppunit/TestRunner.h>
      26             : #include <cppunit/TestResult.h>
      27             : #include <cppunit/TestResultCollector.h>
      28             : #include "tools/license.h"
      29             : #include "libdebpackages/advgetopt.h"
      30             : #include "libdebpackages/debian_packages.h"
      31             : #include "time.h"
      32             : #ifdef HAVE_QT4
      33             : #include <qxcppunit/testrunner.h>
      34             : #include <QApplication>
      35             : #endif
      36             : 
      37             : 
      38             : namespace unittest
      39             : {
      40             : 
      41           1 : std::string   tmp_dir;
      42           1 : std::string   wpkg_tool;
      43             : 
      44             : }
      45             : 
      46             : 
      47             : // Recursive dumps the given Test heirarchy to cout
      48             : namespace
      49             : {
      50           0 : void dump(CPPUNIT_NS::Test *test, std::string indent)
      51             : {
      52           0 :     if(test)
      53             :     {
      54           0 :         std::cout << indent << test->getName() << std::endl;
      55             : 
      56             :         // recursive for children
      57           0 :         indent += "  ";
      58           0 :         int max(test->getChildTestCount());
      59           0 :         for(int i = 0; i < max; ++i)
      60             :         {
      61           0 :             dump(test->getChildTestAt(i), indent);
      62             :         }
      63             :     }
      64           0 : }
      65             : 
      66             : template<class R>
      67           1 : void add_tests(const advgetopt::getopt& opt, R& runner)
      68             : {
      69           1 :     CPPUNIT_NS::Test *root(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
      70           1 :     int max(opt.size("filename"));
      71           1 :     if(max == 0 || opt.is_defined("all"))
      72             :     {
      73           1 :         if(max != 0)
      74             :         {
      75           0 :             fprintf(stderr, "unittest: named tests on the command line will be ignored since --all was used.\n");
      76             :         }
      77           1 :         CPPUNIT_NS::Test *all_tests(root->findTest("All Tests"));
      78           1 :         if(all_tests == NULL)
      79             :         {
      80             :             // this should not happen because cppunit throws if they do not find
      81             :             // the test you specify to the findTest() function
      82           0 :             std::cerr << "error: no tests were found." << std::endl;
      83           0 :             exit(1);
      84             :         }
      85           1 :         runner.addTest(all_tests);
      86             :     }
      87             :     else
      88             :     {
      89           0 :         for(int i = 0; i < max; ++i)
      90             :         {
      91           0 :             std::string test_name(opt.get_string("filename", i));
      92           0 :             CPPUNIT_NS::Test *test(root->findTest(test_name));
      93           0 :             if(test == NULL)
      94             :             {
      95             :                 // this should not happen because cppunit throws if they do not find
      96             :                 // the test you specify to the findTest() function
      97           0 :                 std::cerr << "error: test \"" << test_name << "\" was not found." << std::endl;
      98           0 :                 exit(1);
      99             :             }
     100           0 :             runner.addTest(test);
     101             :         }
     102             :     }
     103           1 : }
     104             : }
     105             : 
     106           1 : int unittest_main(int argc, char *argv[])
     107             : {
     108             :     static const advgetopt::getopt::option options[] = {
     109             :         {
     110             :             '\0',
     111             :             0,
     112             :             NULL,
     113             :             NULL,
     114             :             "Usage: unittest [--opt] [test-name]",
     115             :             advgetopt::getopt::help_argument
     116             :         },
     117             :         {
     118             :             'a',
     119             :             0,
     120             :             "all",
     121             :             NULL,
     122             :             "run all the tests in the console (default)",
     123             :             advgetopt::getopt::no_argument
     124             :         },
     125             :         {
     126             :             'g',
     127             :             0,
     128             :             "gui",
     129             :             NULL,
     130             :             "start the GUI version if available",
     131             :             advgetopt::getopt::no_argument
     132             :         },
     133             :         {
     134             :             'h',
     135             :             0,
     136             :             "help",
     137             :             NULL,
     138             :             "print out this help screen",
     139             :             advgetopt::getopt::no_argument
     140             :         },
     141             :         {
     142             :             '\0',
     143             :             0,
     144             :             "license",
     145             :             NULL,
     146             :             "prints out the license of the tests",
     147             :             advgetopt::getopt::no_argument
     148             :         },
     149             :         {
     150             :             '\0',
     151             :             0,
     152             :             "licence",
     153             :             NULL,
     154             :             NULL, // hide this one from the help screen
     155             :             advgetopt::getopt::no_argument
     156             :         },
     157             :         {
     158             :             'l',
     159             :             0,
     160             :             "list",
     161             :             NULL,
     162             :             "list all the available tests",
     163             :             advgetopt::getopt::no_argument
     164             :         },
     165             :         {
     166             :             'S',
     167             :             0,
     168             :             "seed",
     169             :             NULL,
     170             :             "value to seed the randomizer",
     171             :             advgetopt::getopt::required_argument
     172             :         },
     173             :         {
     174             :             't',
     175             :             0,
     176             :             "tmp",
     177             :             NULL,
     178             :             "path to a temporary directory",
     179             :             advgetopt::getopt::required_argument
     180             :         },
     181             :         {
     182             :             'w',
     183             :             0,
     184             :             "wpkg",
     185             :             NULL,
     186             :             "path to the wpkg executable",
     187             :             advgetopt::getopt::required_argument
     188             :         },
     189             :         {
     190             :             'V',
     191             :             0,
     192             :             "version",
     193             :             NULL,
     194             :             "print out the wpkg project version these unit tests pertain to",
     195             :             advgetopt::getopt::no_argument
     196             :         },
     197             :         {
     198             :             '\0',
     199             :             0,
     200             :             "filename",
     201             :             NULL,
     202             :             NULL, // hidden argument in --help screen
     203             :             advgetopt::getopt::default_multiple_argument
     204             :         },
     205             :         {
     206             :             '\0',
     207             :             0,
     208             :             NULL,
     209             :             NULL,
     210             :             NULL,
     211             :             advgetopt::getopt::end_of_options
     212             :         }
     213             :     };
     214             : 
     215           1 :     std::vector<std::string> configuration_files;
     216           1 :     advgetopt::getopt opt(argc, argv, options, configuration_files, "UNITTEST_OPTIONS");
     217             : 
     218           1 :     if(opt.is_defined("help"))
     219             :     {
     220           0 :         opt.usage(advgetopt::getopt::no_error, "Usage: unittest [--opt] [test-name]");
     221             :         /*NOTREACHED*/
     222             :     }
     223             : 
     224           1 :     if(opt.is_defined("version"))
     225             :     {
     226           0 :         printf("%s\n", debian_packages_version_string());
     227           0 :         exit(1);
     228             :     }
     229             : 
     230           1 :     if(opt.is_defined("license") || opt.is_defined("licence"))
     231             :     {
     232           0 :         license::license();
     233           0 :         exit(1);
     234             :     }
     235             : 
     236           1 :     if(opt.is_defined("list"))
     237             :     {
     238           0 :         CPPUNIT_NS::Test *all = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
     239           0 :         dump(all, "");
     240           0 :         exit(1);
     241             :     }
     242             : 
     243             :     // by default we get a different seed each time; that really helps
     244             :     // in detecting errors! (I know, I wrote loads of tests before)
     245           1 :     unsigned int seed(static_cast<unsigned int>(time(NULL)));
     246           1 :     if(opt.is_defined("seed"))
     247             :     {
     248           0 :         seed = static_cast<unsigned int>(opt.get_long("seed"));
     249             :     }
     250           1 :     srand(seed);
     251           1 :     printf("wpkg:unittest: seed is %d\n", seed);
     252             : 
     253           1 :     if(opt.is_defined("tmp"))
     254             :     {
     255           1 :         unittest::tmp_dir = opt.get_string("tmp");
     256             :     }
     257           1 :     if(opt.is_defined("wpkg"))
     258             :     {
     259           1 :         unittest::wpkg_tool = opt.get_string("wpkg");
     260             :     }
     261             : 
     262           1 :     if(opt.is_defined("gui"))
     263             :     {
     264             : #ifdef HAVE_QT4
     265             :         QApplication app(argc, argv);
     266             :         QxCppUnit::TestRunner runner;
     267             :         //runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
     268             :         add_tests(opt, runner);
     269             :         runner.run();
     270             : #else
     271           0 :         fprintf(stderr, "error: no GUI compiled in this test, you cannot use the --gui option.\n");
     272           0 :         exit(1);
     273             : #endif
     274             :     }
     275             :     else
     276             :     {
     277             :         // Create the event manager and test controller
     278           1 :         CPPUNIT_NS::TestResult controller;
     279             : 
     280             :         // Add a listener that colllects test result
     281           1 :         CPPUNIT_NS::TestResultCollector result;
     282           1 :         controller.addListener(&result);        
     283             : 
     284             :         // Add a listener that print dots as test run.
     285           1 :         CPPUNIT_NS::BriefTestProgressListener progress;
     286           1 :         controller.addListener(&progress);      
     287             : 
     288           1 :         CPPUNIT_NS::TestRunner runner;
     289             : 
     290           1 :         add_tests(opt, runner);
     291             : 
     292           1 :         runner.run(controller);
     293             : 
     294             :         // Print test in a compiler compatible format.
     295           1 :         CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
     296           1 :         outputter.write(); 
     297             : 
     298           1 :         if(result.testFailuresTotal())
     299             :         {
     300           1 :             return 1;
     301           1 :         }
     302             :     }
     303             : 
     304           1 :     return 0;
     305             : }
     306             : 
     307             : 
     308           1 : int main(int argc, char *argv[])
     309             : {
     310           1 :     return unittest_main(argc, argv);
     311           3 : }
     312             : 
     313             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.9

The wpkg tool is an open source tool created by Made to Order Software Corporation.