LCOV - code coverage report
Current view: top level - tests - test_as2js_main.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 50 78 64.1 %
Date: 2014-11-22 Functions: 5 7 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* test_as2js_main.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "test_as2js_main.h"
      37             : #include    "license.h"
      38             : 
      39             : #include    "as2js/as2js.h"
      40             : 
      41             : #include    <advgetopt/advgetopt.h>
      42             : 
      43             : #include    <cppunit/BriefTestProgressListener.h>
      44             : #include    <cppunit/CompilerOutputter.h>
      45             : #include    <cppunit/extensions/TestFactoryRegistry.h>
      46             : #include    <cppunit/TestRunner.h>
      47             : #include    <cppunit/TestResult.h>
      48             : #include    <cppunit/TestResultCollector.h>
      49             : 
      50             : //#include "time.h"
      51             : #include    <unistd.h>
      52             : 
      53             : #ifdef HAVE_QT4
      54             : #include    <qxcppunit/testrunner.h>
      55             : #include    <QApplication>
      56             : #endif
      57             : 
      58             : 
      59             : namespace as2js_test
      60             : {
      61             : 
      62           4 : std::string     g_tmp_dir;
      63           4 : std::string     g_as2js_compiler;
      64             : bool            g_gui = false;
      65             : bool            g_run_stdout_destructive = false;
      66             : bool            g_save_parser_tests = false;
      67             : 
      68             : }
      69             : 
      70             : 
      71             : // Recursive dumps the given Test heirarchy to cout
      72             : namespace
      73             : {
      74           0 : void dump(CPPUNIT_NS::Test *test, std::string indent)
      75             : {
      76           0 :     if(test)
      77             :     {
      78           0 :         std::cout << indent << test->getName() << std::endl;
      79             : 
      80             :         // recursive for children
      81           0 :         indent += "  ";
      82           0 :         int max(test->getChildTestCount());
      83           0 :         for(int i = 0; i < max; ++i)
      84             :         {
      85           0 :             dump(test->getChildTestAt(i), indent);
      86             :         }
      87             :     }
      88           0 : }
      89             : 
      90             : template<class R>
      91           3 : void add_tests(const advgetopt::getopt& opt, R& runner)
      92             : {
      93           3 :     CPPUNIT_NS::Test *root(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
      94           3 :     int max(opt.size("filename"));
      95           3 :     if(max == 0 || opt.is_defined("all"))
      96             :     {
      97           1 :         if(max != 0)
      98             :         {
      99           0 :             fprintf(stderr, "unittest: named tests on the command line will be ignored since --all was used.\n");
     100             :         }
     101           1 :         CPPUNIT_NS::Test *all_tests(root->findTest("All Tests"));
     102           1 :         if(all_tests == nullptr)
     103             :         {
     104             :             // this should not happen because cppunit throws if they do not find
     105             :             // the test you specify to the findTest() function
     106           0 :             std::cerr << "error: no tests were found." << std::endl;
     107           0 :             exit(1);
     108             :         }
     109           1 :         runner.addTest(all_tests);
     110             :     }
     111             :     else
     112             :     {
     113           4 :         for(int i = 0; i < max; ++i)
     114             :         {
     115           2 :             std::string test_name(opt.get_string("filename", i));
     116           2 :             CPPUNIT_NS::Test *test(root->findTest(test_name));
     117           2 :             if(test == nullptr)
     118             :             {
     119             :                 // this should not happen because cppunit throws if they do not find
     120             :                 // the test you specify to the findTest() function
     121           0 :                 std::cerr << "error: test \"" << test_name << "\" was not found." << std::endl;
     122           0 :                 exit(1);
     123             :             }
     124           2 :             runner.addTest(test);
     125             :         }
     126             :     }
     127           3 : }
     128             : }
     129             : 
     130           4 : int unittest_main(int argc, char *argv[])
     131             : {
     132             :     static const advgetopt::getopt::option options[] = {
     133             :         {
     134             :             '\0',
     135             :             0,
     136             :             nullptr,
     137             :             nullptr,
     138             :             "Usage: %p [--opt] [test-name]",
     139             :             advgetopt::getopt::help_argument
     140             :         },
     141             :         {
     142             :             '\0',
     143             :             0,
     144             :             nullptr,
     145             :             nullptr,
     146             :             "with --opt being one or more of the following:",
     147             :             advgetopt::getopt::help_argument
     148             :         },
     149             :         {
     150             :             'a',
     151             :             0,
     152             :             "all",
     153             :             nullptr,
     154             :             "run all the tests in the console (default)",
     155             :             advgetopt::getopt::no_argument
     156             :         },
     157             :         {
     158             :             '\0',
     159             :             0,
     160             :             "destructive",
     161             :             nullptr,
     162             :             "also run the stdout destructive test (otherwise skip the test so we do not lose stdout)",
     163             :             advgetopt::getopt::no_argument
     164             :         },
     165             :         {
     166             :             'g',
     167             :             0,
     168             :             "gui",
     169             :             nullptr,
     170             : #ifdef HAVE_QT4
     171             :             "start the GUI version if available",
     172             : #else
     173             :             "GUI version not available; this option will fail",
     174             : #endif
     175             :             advgetopt::getopt::no_argument
     176             :         },
     177             :         {
     178             :             'h',
     179             :             0,
     180             :             "help",
     181             :             nullptr,
     182             :             "print out this help screen",
     183             :             advgetopt::getopt::no_argument
     184             :         },
     185             :         {
     186             :             '\0',
     187             :             0,
     188             :             "license",
     189             :             nullptr,
     190             :             "prints out the license of the tests",
     191             :             advgetopt::getopt::no_argument
     192             :         },
     193             :         {
     194             :             '\0',
     195             :             0,
     196             :             "licence",
     197             :             nullptr,
     198             :             nullptr, // hide this one from the help screen
     199             :             advgetopt::getopt::no_argument
     200             :         },
     201             :         {
     202             :             'l',
     203             :             0,
     204             :             "list",
     205             :             nullptr,
     206             :             "list all the available tests",
     207             :             advgetopt::getopt::no_argument
     208             :         },
     209             :         {
     210             :             'S',
     211             :             0,
     212             :             "seed",
     213             :             nullptr,
     214             :             "value to seed the randomizer",
     215             :             advgetopt::getopt::required_argument
     216             :         },
     217             :         {
     218             :             '\0',
     219             :             0,
     220             :             "save-parser-tests",
     221             :             nullptr,
     222             :             "save the JSON used to test the parser",
     223             :             advgetopt::getopt::no_argument
     224             :         },
     225             :         {
     226             :             't',
     227             :             0,
     228             :             "tmp",
     229             :             nullptr,
     230             :             "path to a temporary directory",
     231             :             advgetopt::getopt::required_argument
     232             :         },
     233             :         {
     234             :             '\0',
     235             :             0,
     236             :             "as2js",
     237             :             nullptr,
     238             :             "path to the as2js executable",
     239             :             advgetopt::getopt::required_argument
     240             :         },
     241             :         {
     242             :             'V',
     243             :             0,
     244             :             "version",
     245             :             nullptr,
     246             :             "print out the as2js project version these unit tests pertain to",
     247             :             advgetopt::getopt::no_argument
     248             :         },
     249             :         {
     250             :             '\0',
     251             :             0,
     252             :             "filename",
     253             :             nullptr,
     254             :             nullptr, // hidden argument in --help screen
     255             :             advgetopt::getopt::default_multiple_argument
     256             :         },
     257             :         {
     258             :             '\0',
     259             :             0,
     260             :             nullptr,
     261             :             nullptr,
     262             :             nullptr,
     263             :             advgetopt::getopt::end_of_options
     264             :         }
     265             :     };
     266             : 
     267           4 :     std::vector<std::string> configuration_files;
     268           7 :     advgetopt::getopt opt(argc, argv, options, configuration_files, "UNITTEST_OPTIONS");
     269             : 
     270           4 :     if(opt.is_defined("help"))
     271             :     {
     272           0 :         opt.usage(advgetopt::getopt::no_error, "Usage: test_as2js [--opt] [test-name]");
     273             :         /*NOTREACHED*/
     274             :     }
     275             : 
     276           4 :     if(opt.is_defined("version"))
     277             :     {
     278           1 :         std::cout << AS2JS_VERSION << std::endl;
     279           1 :         exit(1);
     280             :     }
     281             : 
     282           3 :     if(opt.is_defined("license") || opt.is_defined("licence"))
     283             :     {
     284           0 :         as2js_tools::license::license();
     285           0 :         exit(1);
     286             :     }
     287             : 
     288           3 :     if(opt.is_defined("list"))
     289             :     {
     290           0 :         CPPUNIT_NS::Test *all = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
     291           0 :         dump(all, "");
     292           0 :         exit(1);
     293             :     }
     294           3 :     as2js_test::g_run_stdout_destructive = opt.is_defined("destructive");
     295             : 
     296           3 :     as2js_test::g_save_parser_tests = opt.is_defined("save-parser-tests");
     297             : 
     298             :     // by default we get a different seed each time; that really helps
     299             :     // in detecting errors! (I know, I wrote loads of tests before)
     300           3 :     unsigned int seed(static_cast<unsigned int>(time(nullptr)));
     301           3 :     if(opt.is_defined("seed"))
     302             :     {
     303           0 :         seed = static_cast<unsigned int>(opt.get_long("seed"));
     304             :     }
     305           3 :     srand(seed);
     306           3 :     std::cout << opt.get_program_name() << "[" << getpid() << "]" << ": version " << AS2JS_VERSION << ", seed is " << seed << std::endl;
     307             : 
     308           3 :     if(opt.is_defined("tmp"))
     309             :     {
     310           0 :         as2js_test::g_tmp_dir = opt.get_string("tmp");
     311             :     }
     312           3 :     if(opt.is_defined("as2js"))
     313             :     {
     314           0 :         as2js_test::g_as2js_compiler = opt.get_string("as2js");
     315             :     }
     316             : 
     317           3 :     if(opt.is_defined("gui"))
     318             :     {
     319             : #ifdef HAVE_QT4
     320           0 :         as2js_test::g_gui = true;
     321           0 :         QApplication app(argc, argv);
     322           0 :         QxCppUnit::TestRunner runner;
     323           0 :         add_tests(opt, runner);
     324           0 :         runner.run();
     325             : #else
     326             :         std::cerr << "error: no GUI compiled in this test, you cannot use the --gui option.\n";
     327             :         exit(1);
     328             : #endif
     329             :     }
     330             :     else
     331             :     {
     332             :         // Create the event manager and test controller
     333           3 :         CPPUNIT_NS::TestResult controller;
     334             : 
     335             :         // Add a listener that colllects test result
     336           6 :         CPPUNIT_NS::TestResultCollector result;
     337           3 :         controller.addListener(&result);        
     338             : 
     339             :         // Add a listener that print dots as test run.
     340           6 :         CPPUNIT_NS::BriefTestProgressListener progress;
     341           3 :         controller.addListener(&progress);      
     342             : 
     343           6 :         CPPUNIT_NS::TestRunner runner;
     344             : 
     345           3 :         add_tests(opt, runner);
     346             : 
     347           3 :         runner.run(controller);
     348             : 
     349             :         // Print test in a compiler compatible format.
     350           6 :         CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
     351           3 :         outputter.write(); 
     352             : 
     353           3 :         if(result.testFailuresTotal())
     354             :         {
     355           0 :             return 1;
     356           3 :         }
     357             :     }
     358             : 
     359           6 :     return 0;
     360             : }
     361             : 
     362             : 
     363           4 : int main(int argc, char *argv[])
     364             : {
     365           4 :     return unittest_main(argc, argv);
     366          12 : }
     367             : 
     368             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10