tests_main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #define CATCH_CONFIG_RUNNER
  8. #include <catch.hpp>
  9. #include <reporters/catch_reporter_compact.hpp>
  10. #include <QFile>
  11. int (*TestForkedMethod)()/* = nullptr*/;
  12. namespace base {
  13. namespace assertion {
  14. // For Assert() / Expects() / Ensures() / Unexpected() to work.
  15. void log(const char *message, const char *file, int line) {
  16. std::cout << message << " (" << file << ":" << line << ")" << std::endl;
  17. }
  18. } // namespace assertion
  19. } // namespace base
  20. namespace Catch {
  21. struct MinimalReporter : CompactReporter {
  22. MinimalReporter( ReporterConfig const& _config )
  23. : CompactReporter( _config )
  24. {}
  25. virtual void testRunEnded( TestRunStats const& _testRunStats ) {
  26. printTotals( _testRunStats.totals );
  27. }
  28. private:
  29. // Colour, message variants:
  30. // - white: No tests ran.
  31. // - red: Failed [both/all] N test cases, failed [both/all] M assertions.
  32. // - white: Passed [both/all] N test cases (no assertions).
  33. // - red: Failed N tests cases, failed M assertions.
  34. // - green: Passed [both/all] N tests cases with M assertions.
  35. std::string bothOrAll( std::size_t count ) const {
  36. return count == 1 ? std::string() : count == 2 ? "both " : "all " ;
  37. }
  38. void printTotals( const Totals& totals ) const {
  39. if( totals.testCases.total() == 0 ) {
  40. }
  41. else if( totals.testCases.failed == totals.testCases.total() ) {
  42. Colour colour( Colour::ResultError );
  43. const std::string qualify_assertions_failed =
  44. totals.assertions.failed == totals.assertions.total() ?
  45. bothOrAll( totals.assertions.failed ) : std::string();
  46. stream <<
  47. "Failed " << bothOrAll( totals.testCases.failed )
  48. << pluralise( totals.testCases.failed, "test case" ) << ", "
  49. "failed " << qualify_assertions_failed <<
  50. pluralise( totals.assertions.failed, "assertion" ) << '.';
  51. }
  52. else if( totals.assertions.total() == 0 ) {
  53. stream <<
  54. "Passed " << bothOrAll( totals.testCases.total() )
  55. << pluralise( totals.testCases.total(), "test case" )
  56. << " (no assertions).";
  57. }
  58. else if( totals.assertions.failed ) {
  59. Colour colour( Colour::ResultError );
  60. stream <<
  61. "Failed " << pluralise( totals.testCases.failed, "test case" ) << ", "
  62. "failed " << pluralise( totals.assertions.failed, "assertion" ) << '.';
  63. }
  64. else {
  65. }
  66. }
  67. };
  68. INTERNAL_CATCH_REGISTER_REPORTER( "minimal", MinimalReporter )
  69. } // namespace Catch
  70. int main(int argc, const char *argv[]) {
  71. auto touchFile = QString();
  72. for (auto i = 0; i != argc; ++i) {
  73. if (argv[i] == QString("--touch") && i + 1 != argc) {
  74. touchFile = QFile::decodeName(argv[++i]);
  75. } else if (argv[i] == QString("--forked") && TestForkedMethod) {
  76. return TestForkedMethod();
  77. }
  78. }
  79. const char *catch_argv[] = {
  80. argv[0],
  81. touchFile.isEmpty() ? "-b" : "-r",
  82. touchFile.isEmpty() ? "-b" : "minimal" };
  83. constexpr auto catch_argc = sizeof(catch_argv) / sizeof(catch_argv[0]);
  84. auto result = Catch::Session().run(catch_argc, catch_argv);
  85. if (result == 0 && !touchFile.isEmpty()) {
  86. QFile(touchFile).open(QIODevice::WriteOnly);
  87. }
  88. return (result < 0xff ? result : 0xff);
  89. }