| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2007 by Lukas Lalinsky |
| 3 | email : [email protected] |
| 4 | ***************************************************************************/ |
| 5 | |
| 6 | /*************************************************************************** |
| 7 | * This library is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU Lesser General Public License version * |
| 9 | * 2.1 as published by the Free Software Foundation. * |
| 10 | * * |
| 11 | * This library is distributed in the hope that it will be useful, but * |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | * Lesser General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Lesser General Public * |
| 17 | * License along with this library; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
| 19 | * 02110-1301 USA * |
| 20 | * * |
| 21 | * Alternatively, this file is available under the Mozilla Public * |
| 22 | * License Version 1.1. You may obtain a copy of the License at * |
| 23 | * http://www.mozilla.org/MPL/ * |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include <cstdlib> |
| 27 | #include <cstring> |
| 28 | #include <fstream> |
| 29 | #include <stdexcept> |
| 30 | #include <cppunit/TestResult.h> |
| 31 | #include <cppunit/TestResultCollector.h> |
| 32 | #include <cppunit/TestRunner.h> |
| 33 | #include <cppunit/extensions/TestFactoryRegistry.h> |
| 34 | #include <cppunit/BriefTestProgressListener.h> |
| 35 | #include <cppunit/CompilerOutputter.h> |
| 36 | #include <cppunit/XmlOutputter.h> |
| 37 | |
| 38 | int main(int argc, char* argv[]) |
| 39 | { |
| 40 | std::string testPath = argc > 1 ? std::string(argv[1]) : ""; |
| 41 | |
| 42 | // Create the event manager and test controller |
| 43 | CppUnit::TestResult controller; |
| 44 | |
| 45 | // Add a listener that collects test result |
| 46 | CppUnit::TestResultCollector result; |
| 47 | controller.addListener(&result); |
| 48 | |
| 49 | // Add a listener that print dots as test run. |
| 50 | CppUnit::BriefTestProgressListener progress; |
| 51 | controller.addListener(&progress); |
| 52 | |
| 53 | // Add the top suite to the test runner |
| 54 | CppUnit::TestRunner runner; |
| 55 | runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); |
| 56 | |
| 57 | try { |
| 58 | std::cout << "Running " << testPath; |
| 59 | runner.run(controller, testPath); |
| 60 | |
| 61 | std::cerr << '\n'; |
| 62 | |
| 63 | // Print test in a compiler compatible format. |
| 64 | CppUnit::CompilerOutputter outputter(&result, std::cerr); |
| 65 | outputter.write(); |
| 66 | |
| 67 | #if defined(_MSC_VER) && _MSC_VER > 1500 |
| 68 | char *xml = NULL; |
| 69 | ::_dupenv_s(&xml, NULL, "CPPUNIT_XML"); |
| 70 | #else |
| 71 | char *xml = ::getenv("CPPUNIT_XML"); |
| 72 | #endif |
| 73 | if(xml && !::strcmp(xml, "1")) { |
| 74 | std::ofstream xmlfileout("cpptestresults.xml"); |
| 75 | CppUnit::XmlOutputter xmlout(&result, xmlfileout); |
| 76 | xmlout.write(); |
| 77 | } |
| 78 | #if defined(_MSC_VER) && _MSC_VER > 1500 |
| 79 | ::free(xml); |
| 80 | #endif |
| 81 | } |
| 82 | catch(std::invalid_argument &e){ |
| 83 | std::cerr << '\n' |
| 84 | << "ERROR: " << e.what() |
| 85 | << '\n'; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | return result.wasSuccessful() ? 0 : 1; |
| 90 | } |